// JavaScript CATALOG class
var Catalog = {
	URL_ADMIN : '../modules/catalog/admin.php',
	//------------------------------------------------------------------------------//
	// ADMINISTRATION																//
	//------------------------------------------------------------------------------//
	//---- Liste des catégories
	listCategories : function(){
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data:{
				action: 'listCategories'
			},
			success: function(msg){
				$('#workspace').html(msg);
			}
		});
		return false;
	},
	//---- Edition de la fiche Catégorie
	editCategory : function(IdCategory, IdParentcategory){
		!IdParentcategory ? IdParentcategory=0 : false;
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data:{
				action: 'editCategory',
				id_category: IdCategory,
				id_parent_category: IdParentcategory
			},
			success: function(msg){
				$('#workspace').html(msg);
			}
		});
		return false;
	},
	//---- Suppression d'une catégorie
	deleteCategory : function(IdCategory){
		messagebox('ouinon', {
			titre: 'Suppression',
			message: 'Souhaitez-vous supprimer la catégorie sélectionnée ?'
		}, function(r){
			if (r){
				$.ajax({
					type: 'post',
					url: Catalog.URL_ADMIN,
					data: {
						action: 'deleteCategory',
						id_category: IdCategory
					},
					success: function(msg){
						messagebox('La catégorie a été supprimée.');
						Catalog.listCategories();
					}
				});
			}
		});
	},
	//---- Enregistrement d'une categorie
	saveCategory : function(IdCategory){
		var Datas = $('#CatForm').serialize();
		var Valid = false;
		for( i=0; i<LANGS.length; i++ ){
			var Lang 	 = LANGS[i];
			var IdLang 	 = Lang[0];
			var CodeLang = Lang[1];
			if ( $('#' + CodeLang + '_name').val()!='' ) Valid=true;
		}
		if ( !Valid ) {
			messagebox('alert', 'Le nom de la catégorie est obligatoire');
			return false;
		}
		$.ajax({
			type: 'post',
			url: Catalog.URL_ADMIN,
			data: {
				action: 'saveCategory',
				datas: Datas,
				id_category: IdCategory
			},
			success: function(msg){
				messagebox('Catégorie enregistrée avec succès');
				Catalog.listCategories();
			}
		});
		return false;
	},
	//---- Liste des produits
	listProducts : function(Niveau, IdCategory){
		!Niveau ? Niveau = 1 : false;
		!IdCategory ? IdCategory = 0 : false;
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data:{
				action: 'listProductsNiv' + Niveau,
				id_category : IdCategory
			},
			success: function(msg){
				$('#workspace').html(msg);
				Custom.init();
			}
		});
		return false;
	},
	//---- Fiche produit
	editProduct : function(IdProduct, IdCategory){
		!IdCategory ? IdCategory=0 : false;
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data:{
				action: 'productSheet',
				id_product: IdProduct,
				id_product_category : IdCategory
			},
			success: function(msg){
				$('#workspace').html(msg);
				Custom.init();
			}
		});
		return false;
	},
	//---- Enregistrement d'un produit
	saveProduct : function(IdProduct){
		if ( $('#name').val()=='' ){
			Message = 'Le champ "Libellé est obligatoire';
			messagebox('alert', Message);
			return false;
		}
		var Descriptions = new Array;
		var Supplements_1 = new Array;
		var Supplements_2 = new Array;
		var Supplements_3 = new Array;
		var Supplements_4 = new Array;
		for (i=0; i<LANGS.length; i++){
			if ( Global.getParam('catalog_html_desc')=='1' ){
				oEditor  = FCKeditorAPI.GetInstance(LANGS[i][1] + "_description");
				Descriptions[i] = LANGS[i][1] + '_description=' + oEditor.GetXHTML();
				if ( Global.getParam('catalog_supplements')=='1' ){
					oEditor1  = FCKeditorAPI.GetInstance(LANGS[i][1] + "_supplement_1");
					Supplements_1[i] = LANGS[i][1] + '_supplement_1=' + oEditor1.GetXHTML();
					oEditor2  = FCKeditorAPI.GetInstance(LANGS[i][1] + "_supplement_2");
					Supplements_2[i] = LANGS[i][1] + '_supplement_2=' + oEditor2.GetXHTML();
					oEditor3  = FCKeditorAPI.GetInstance(LANGS[i][1] + "_supplement_3");
					Supplements_3[i] = LANGS[i][1] + '_supplement_3=' + oEditor3.GetXHTML();
					oEditor4  = FCKeditorAPI.GetInstance(LANGS[i][1] + "_supplement_4");
					Supplements_4[i] = LANGS[i][1] + '_supplement_4=' + oEditor4.GetXHTML();
				}
			} else {
				Descriptions[i] = LANGS[i][1] + '_description=' + $('#' + LANGS[i][1] + '_description').val();
			}
		}
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data:{
				action: 'saveProduct',
				id_product: IdProduct,
				datas: $('#productForm').serialize(),
				descriptions: Descriptions.join('||'),
				supplements_1: Supplements_1.join('||'),
				supplements_2: Supplements_2.join('||'),
				supplements_3: Supplements_3.join('||'),
				supplements_4: Supplements_4.join('||')
			},
			success: function(msg){
				messagebox('Produit enregistré.');
				Catalog.listProducts();
			}
		});
		return false;
	},
	//---- Suppression d'un produit
	deleteProduct : function(IdProduct){
		messagebox('ouinon', {
			titre: 'Suppression',
			message: 'Souhaitez-vous supprimer le produit sélectionné ?'
		}, function(r){
			if (r){
				$.ajax({
					type: 'post',
					url: Catalog.URL_ADMIN,
					data: {
						action: 'deleteProduct',
						id_product: IdProduct
					},
					success: function(msg){
						Catalog.listProducts();
					}
				});
			}
		});
	},
	//---- Liste des Bundles (Multi-produits)
	listBundles : function(){
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data: {
				action: 'listBundles'
			},
			success: function(msg){
				$('#workspace').html(msg);
			}
		});
		return false;
	},
	//---- Fiche Bundle
	bundleSheet : function(IdBundle){
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data: {
				action: 'bundleSheet',
				id_bundle: IdBundle
			},
			success: function(msg){
				$('#workspace').html(msg);
			}
		});
		return false;
	},
	//---- Après Upload de l'image de fond
	affichImageFond : function(IdBundle, Empty){
		!Empty ? Empty=false : false;
		if ( !Empty ){
			$("#image_fond_text").html("<span class=\'A12 vert\'>Image envoyé avec succès.</span>");
			$("#image_fond_img").html('<img src="'+URL_THEME_DIR+'delete.png" alt="Delete" class="hand" title="Supprimer l\'image" onclick="Catalog.deleteBundleImgFd('+IdBundle+')" />');
		}
	},
	//---- Suppression image de fond d'un bundle
	deleteBundleImgFd : function(IdBundle){
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data: {
				action: 'deleteBundleImgFd',
				id_bundle: IdBundle
			},
			success: function(msg){
				$('#image_fond_text').html('<span class="A12 gris">Pas d\'image</span>');
				$('#image_fond_img').html('<img src="'+URL_THEME_DIR+'add.png" alt="Add" class="hand" title="Ajouter une image de fond" onclick="return Global.uploadImage(\'files/products_bundle/\', \''+IdBundle+'_fd\', 2250, 1250, \'AffichImageFond('+IdBundle+')\')" />');
			}
		});
		return false;
	},
	//---- Après Upload de l'image de fond
	affichImageTeen : function(IdBundle, Empty){
		!Empty ? Empty=false : false;
		if ( !Empty ){
			$("#image_teen_text").html("<span class=\'A12 vert\'>Image envoyé avec succès.</span>");
			$("#image_teen_img").html('<img src="'+URL_THEME_DIR+'delete.png" alt="Delete" class="hand" title="Supprimer l\'image" onclick="Catalog.deleteBundleImgTn('+IdBundle+')" />');
		}
	},
	//---- Suppression de la miniature d'un bundle
	deleteBundleImgTn : function(IdBundle){
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data: {
				action: 'deleteBundleImgTn',
				id_bundle: IdBundle
			},
			success: function(msg){
				$('#image_teen_text').html('<span class="A12 gris">Pas d\'image</span>');
				$('#image_teen_img').html('<img src="'+URL_THEME_DIR+'add.png" alt="Add" class="hand" title="Ajouter une miniature" onclick="return Global.uploadImage(\'files/products_bundle/\', \''+IdBundle+'_fd\', 2250, 1250, \'AffichImageTeen('+IdBundle+')\')" />');
			}
		});
		return false;
	},
	//---- Enregistrement d'un bundle
	saveBundle : function(IdBundle){
		if ( $('#name').val()=='' ){
			messagebox('alert', 'Le libellé est obligatoire.');
			return false;
		}
		var Datas = new Array;
		Datas[Datas.length] = 'name=' + $('#name').val();
		Datas[Datas.length] = 'id_category=' + $('#id_category').val();
		for(i=0; i<LANGS.length; i++){
			Datas[Datas.length] = LANGS[i][1] + '_title=' + $('#'+LANGS[i][1] + '_title').val();
			Datas[Datas.length] = LANGS[i][1] + '_description=' + $('#'+LANGS[i][1] + '_description').val();
		}
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data: {
				action: 'saveBundle',
				id_bundle: IdBundle,
				datas: Datas.join('||')
			},
			success: function(msg){
				Catalog.listBundles();
			}
		});
	},
	//---- Suppression d'un bundle
	deleteBundle : function(IdBundle){
		messagebox('ouinon', {
			titre: 'Suppression',
			message: 'Souhaitez-vous supprimer le bundle sélectionné ?'
		}, function(r){
			if (r){
				$.ajax({
					type: 'post',
					url: Catalog.URL_ADMIN,
					data: {
						action: 'deleteBundle',
						id_bundle: IdBundle
					},
					success: function(msg){
						Catalog.listBundles();
					}
				});
			}
		});
	},
	//---- Ajouter un produit à un bundle
	addBundleProduct : function(IdBundle){
		if ( $('#lstProducts').val()==0 ){
			messagebox('alert', 'Veuillez sélectionner un produit dans la liste');
			return false;
		}
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data: {
				action: 'addBundleProduct',
				id_bundle: IdBundle,
				id_product: $('#lstProducts').val()
			},
			success: function(msg){
				Catalog.lstBundleProducts(IdBundle);
			}
		});
		return false;
	},
	//---- Supprimer un produit à un bundle
	deleteBundleProduct : function(IdBundle, Id){
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data: {
				action: 'deleteBundleProduct',
				id: Id
			},
			success: function(msg){
				Catalog.lstBundleProducts(IdBundle);
			}
		});
		return false;
	},
	//---- Liste des articles d'un bundle
	lstBundleProducts : function(IdBundle){
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data: {
				action: 'lstBundleProducts',
				id_bundle: IdBundle
			},
			beforeSend: Global.Wait('lstBundleProducts'),
			success: function(msg){
				$('#lstBundleProducts').html(msg);
			}
		});
	},
	//---- Positionne un article sur une image de fond Bundle
	positionneProduct : function(Id){
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data: {
				action: 'positionneProduct',
				id: Id
			},
			success: function(msg){
				$('#inWindow').html(msg);
			}
		});
		return false;
	},
	//---- Enregistrement de la position de la puce
	savePucePosition : function(Id){
		$.ajax({
			type: 'post',
			url: this.URL_ADMIN,
			data: {
				action: 'savePucePosition',
				id: Id,
				posX: $('#posX').val(),
				posY: $('#posY').val()
			},
			success: function(msg){
				Global.closeWindow();
			}
		});
		return false;
	}
}
//------------------------------------------------------------------------------//
// BUNDLES																		//
//------------------------------------------------------------------------------//
var Bundle = {
	BundleEnCours : null,
	Products: new Array(),
	positionnePuces: function(){
		var Details;
		$('.bundle_puce').hide();
		for(i=0; i<Bundle.Products.length; i++){
			Details = Bundle.Products[i];
			$('#puce_' + Details[0]).css({
				'left' : (560 / Details[1])+'px',
				'top': (560 / Details[2])+'px'
			}).fadeIn();
		}
	},
	positionneProductsName : function(){
		$(".product_name").hide();
		window.setTimeout(function(){
			$(".product_name").each( function(){
				var Id = $(this).attr("id").split('_');
				Id = Id[1];
				var marginLeft = parseInt($("#puce_"+Id).css("left").replace('px',''))+35;
				var marginTop = parseInt($("#puce_"+Id).css("top").replace('px',''));
				$(this).css({
					"left" : marginLeft + 'px',
					"top" : marginTop + 'px'
				});
			});
		},500);
	},
	closeProductWindow : function(){
		$('div.CACHE').hide();
		$("#CADRE_PRODUCT").hide();
		$('.bundle_puce').fadeIn();
	},
	showProductName : function(Id, Appear){
		this.positionneProductsName();
		Appear ? $("#name_"+Id).fadeIn("fast") : $("#name_"+Id).fadeOut("fast");
	},
	openProductDetails : function(IdProduct){
		$.ajax({
			type: "post",
			url: "modules/catalog/bundle.php",
			data: {
				action: "openProductDetails",
				id_product: IdProduct
			},
			beforeSend: Global.Wait('CADRE_PRODUCT div.main'),
			success: function(msg){
				if ( !$.browser.msie || ($.browser.msie && $.browser.version >=8 ) )  $('div.CACHE').show();
				$("#CADRE_PRODUCT div.main").html(msg);
				$("#CADRE_PRODUCT div.main div.title").hide();
				$("#CADRE_PRODUCT").show();
				$('.bundle_puce').fadeOut();
			}
		});
	},
	loadLastProduct : function(){
		var Products = Bundle.Products;
		var nbrProducts = Products.length;
		var LastNum;
		for(i=0; i<nbrProducts; i++){
			Products[i][0]==$("#id_product").val() ? LastNum = i-1 : false;
		}
		LastNum==-1 ? Bundle.openProductDetails(Products[Products.length-1][0]) : Bundle.openProductDetails(Products[LastNum][0]);
	},
	loadNextProduct : function(){
		var Products = Bundle.Products;
		var nbrProducts = Products.length;
		var NextNum;
		for(i=0; i<nbrProducts; i++){
			Products[i][0]==$("#id_product").val() ? NextNum = i+1 : false;
		}
		NextNum>=Products.length ? Bundle.openProductDetails(Products[0][0]) : Bundle.openProductDetails(Products[NextNum][0]);
	},
	showMainProductTitle : function(){
		$("#CADRE_PRODUCT div.main div.title").animate({ width : "show" }, "fast");
		return false;
	},
	hideMainProductTitle : function(){
		$("#CADRE_PRODUCT div.main div.title").animate({ width : "hide" }, "fast");
		return false;
	},
	//---- Charge un Bundle
	openBundle : function(IdBundle, navPos){
		$.ajax({
			type: "post",
			url: "modules/catalog/bundle.php",
			data: {
				id: IdBundle,
				position: $('#BUNDLE_NAV ul').css('marginTop')
			},
			success: function(msg){
				$('#workspace').html(msg);
			}
		});
		return false;
	},
	//---- Déplacement des vignettes vers le haut
	moveUp: function(){
		var Max = $('#BUNDLE_NAV ul').height()-$('#BUNDLE_NAV').height()-5;
		Max = Max * (-1);
		$('#BUNDLE_NAV ul').animate({
			'marginTop': Max + 'px'
		}, 2000);
	},
	//---- Déplacement des vignettes vers le bas
	moveDown: function(){
		$('#BUNDLE_NAV ul').animate({
			'marginTop': '0px'
		}, 2000);
	},
	stopAnimate: function(){
		$('#BUNDLE_NAV ul').stop();
	}
}
