$(document).ready(function(){
	
	
	var ArrRefSelected = new Array();
	var initCombo = 0;
	var stateDisplay = false;
	var displayinfobulle = 0;
	var ArrType= new Array("PR","CM","VE","ME","CG","DI1","DI2","GR","D7","BO","BO1","BO2","MO","EN","CL","NA","MD1","MD2","MD3","MD4","SO");
	var ArrRefSelected = new Array(21);
	var typeSelected = "";
	
	$.extend(ArrType);
	$.extend(typeSelected);
	$.extend(ArrRefSelected);
	$.extend(dataPartner);
	$.extend({
		WarningBox:function(msg){
			$("#messageBox").show().css({
				top: $(window).scrollTop() + (($(window).height()/2)-($("#messageBox").width()/2))  ,
				left: (($("body").innerWidth()/2) - ($("#messageBox").width())/2)	
			});
			$("#messageBox div.content").html(msg);
		},
		SetTypeSelecting:function(typeSelecting) {
			return typeSelected = typeSelecting;
		},
		GetTypeSelecting:function() {
			return typeSelected;
		},
		PutPriceTotal:function ()
		{
			var Data1 =new Array();
			var Data2 =new Array();
			var Data3 =new Array();
			var powerSupply;
			var powerGraphicCard;
			var PxRefSelected=0;
			
			
			for (j=0; j<ArrType.length; j++){
				Data1[j] = $("#Values"+ArrType[j]).val();
				
				if (Data1[j]!="") {
					Data2[j]=(Data1[j]).split("&");
					for (k=0; k<Data2[j].length; k++) {
						Data3[k]=(Data2[j][k]).split('=');
						if (Data3[k][0]==ArrType[j]+"[price]") {
							PxRefSelected = PxRefSelected + parseInt(Data3[k][1]);
						}
						if (Data3[k][0]==ArrType[j]+"[ref]") {
							//alert(j+" "+Data3[k][1]);	
							ArrRefSelected[j]=Data3[k][1]; ///Pour la recup le lien avec toutes les refs selectionn&eacute;
						}
						
						
					}
					
				}
			}
			if ($.GetTypeSelecting()!="" && dataPartner[0]!="") {
				
				$("#"+$.GetTypeSelecting()+" div.Linebox").after("<div class='pxTotalForType' id=\"pxTotalFor"+$.GetTypeSelecting()+"\" >sous total: "+$.formatPrice(PxRefSelected)+"</div>");
				$("#pxTotalFor"+$.GetTypeSelecting()).animate({ opacity: 1 },3000).fadeOut(3000);
			}
			$.setAllRefSelected(ArrRefSelected);
			$("#pxtotal").val($.formatPrice(PxRefSelected));
			
		},
		MoreInfoAboutSelectedRef:function (pType, ref) {
			
			
			if (ref!="null"+pType) { 
				$("#"+pType+" div.choiceTitle").append("<div class='moreInfos' id='Infos"+pType+"'>Plus d\'infos sur la r&eacute;f s&eacute;lectionn&eacute;e</div>");
				if ( $.browser.msie && parseInt($.browser.version) < 8)
				$("#"+pType+" div#Infos"+pType).css({marginTop:2});
				
				
				$("#"+pType+" div#Infos"+pType).click( function() {
					
					
					
					$.backgroundLayer(true);
					$("#ContentDataArt").show();
					$("#ContentDataArt").css({
						top: $(window).scrollTop() + 100 ,
						left: (($("body").innerWidth()/2) - ($("#ContentDataArt").width())/2)	
					});
					$("#ContentDataArt").html("<div id='loadingFiche'></div>");
					
					$.ajax(
					{ 
						type: "POST",
						url: 'ajxcfgo/a-fiche.php',
						data: "ptype="+pType+"&ref=" + ref, ////param&egrave;tres
						dataType: "text",
						error: function(retour)
						{
							alert("Erreur lors de la récupération des données");
							$.backgroundLayer(false);
							$("#ContentDataArt").hide();
						},
						success: function(html)
						{
							
							$("#ContentDataArt").html(html);
							$("#ContentDataArt").draggable({handle: "div.title"});
							
							$("#WinFicheArt div.close, #backgroundLayer").click(function() {
								$.backgroundLayer(false);
								$("#ContentDataArt").hide();
								
							});
							
							
						}
					});
					
				});	
			}else{
				$("#"+pType+" div#Infos"+idType).addClass("h");
			} 
		},
		formatPrice:function (price) {
			price = (price/100).toFixed(2)+" \u20AC";
			return (price);	 
		},
		backgroundLayer:function(show){
			if(show)
			{
				$("#backgroundLayer").show();
				$("#backgroundLayer").width($(document).width());
				$("#backgroundLayer").height($(document).height());
				
			}
			else $("#backgroundLayer").hide();
		},
		setAllRefSelected:function(ArrRef){
			ArrRefSelected = ArrRef;
			return ArrRefSelected;
		},
		setCookieInfosBulle:function() {
			var exp=new Date(); 
			var MyCookie = new Array();
			exp.setTime (exp.getTime() + (24*60*60*1000) ); 
			
			document.cookie="infobulleconfigo=ok; expires="+exp.toGMTString()+"; domain=.topachat.com; path=/"; 
			
		},
		getCookieInfoBulle:function() {
			if ( $("#ckhelp").val()!="ok" ) {
				$.setCookieInfosBulle();
				$("#LayerHelpSort").fadeIn(2000);
				$("#LayerHelpSort").css({
					top:  $("#PR div.LineBox").position().top-190+'px',
					left: $("#PR div.LineBox").position().left+30
				});
			}
		},
		saveConfig:function() {
			var urlConfig="";
			var item="";
			//$("#BO").hasClass('s');
			
			if (ArrRefSelected[9]!="" && $("#BO").hasClass('s')===false) ArrRefSelected[9]="";
			if (ArrRefSelected[10]!="" && $("#BO").hasClass('s')===true) ArrRefSelected[10]="";
			if (ArrRefSelected[11]!="" && $("#BO").hasClass('s')===true) ArrRefSelected[11]="";
			
			$.each(ArrRefSelected, function(k,v) {
				if (v==undefined || v=="") { 
					item=""; 
				} else { 
					
					item="z"+k+"="+v+"&"; 
				}
				urlConfig=urlConfig+item;
			
				
			});	
						
			urlConfig = urlConfig.substr(0, (urlConfig.length-1));
			var Now=new Date();
			var MaDate;
			//if($.browser.webkit) MaDate = Now.toUTCString(); //Now.toDateString()+" "
			//else MaDate = Now.toLocaleString();
			
			MaDate = Now.getDate()+" "+(Now.getMonth()+1)+" "+Now.getFullYear()+" "+Now.getHours()+":"+Now.getMinutes()+":"+Now.getSeconds();
			MaDate = MaDate.replace(/\s/g,"_");
			var DateExpiration = new Date();
			DateExpiration.setMonth(DateExpiration.getMonth()+6);
			
			document.cookie ="Configomatic-("+MaDate+")="+urlConfig+"; expires="+DateExpiration+";path=/";
			
			$.backgroundLayer(true);
			$("#messageBox").show();
			$("#messageBox").css({
				top: $(window).scrollTop() + (($(window).height()/2)-($("#messageBox").width()/2))  ,
				left: (($("body").innerWidth()/2) - ($("#messageBox").width())/2)	
			});
			$("#messageBox div.content").html("Configuration du<br>"+MaDate+"<br><b>sauvegard&eacute;e !</b>");
			
			$("#BoxUserConfig").html("Chargement des configurations pr&eacute;sentes sur cette machine...");
			$.post('ajxcfgo/a-configbackup.php', {'partnerId': $("#partnerId").val() }, function(html){
				$("#BoxUserConfig").html(html);
			}, 'html');	
			
		}
	});
	$(document).click(function(evt){ 
		if (
			!$(evt.target).hasClass('LineBox') && 
			!$(evt.target).hasClass('Selector') && 
			stateDisplay
		) 
		{ 
			$(".LineBox").removeClass('arrowUp');
			$.each( $(".Selector"), function(k,divObj) {
				if ( $(divObj).css('display')=='block') 
				$(divObj).removeClass('s');	
			});
		}
	});
	
	$("#LayerHelpSort div.close").click( function () {
		$("#LayerHelpSort").fadeOut(2000);	
	});
	$("#messageBox div.close").click( function () {
		$.backgroundLayer(false);
		$("#messageBox").hide();	
	});
	$("div.LineBox").click( function () {
		
		
		$("#nbLigne").val('');
		id_type=$(this).closest(".type").attr("id");
		id_selector = $("#"+id_type+" .Selector");
		$.SetTypeSelecting(id_type);
		if ($.browser.msie)  $("#"+id_type+" .Selector").css({width: $(this).width()+12 }); 
		
		nbRefSelected=0;
		$.each(ArrRefSelected, function (k,v) {
			if (v) nbRefSelected++;
		});
		
		if (id_type=="PR" && displayinfobulle==0) {
				displayinfobulle=1;
				$.getCookieInfoBulle();
				//$.setCookieInfosBulle();
		}
		if (id_type=="PR" && nbRefSelected>1 && $("#PartTwo").css('display')=='block' ) {
			if (!confirm("changer le processeur, modifie la compatibilité de la carte mère\n...Si vous confirmez, vous perdrez la configuration en cours")) 
			return false;
			else {
				$.backgroundLayer(true);
				if(dataPartner[0]!="") {
					window.location='?partnerId='+ $("#partnerId").val();
				} else {
					window.location='?';	
				}
				return false;
			}
		}
		
		else if ((id_type=="PR" || id_type=="CM") && $("#PartTwo").css('display')=='block' && (nbRefSelected>2 || parseInt( $("#nbComboOpened").val() )>2) )
		{	
			if (!confirm("changer la carte mère, modifie la compatibilité; de l'ensemble des composants\n...Si vous confirmez, vous perdrez la configuration en cours")) 
			return false;
			else {
				$.backgroundLayer(true);
				if(dataPartner[0]!="") {
					window.location='?partnerId='+ $("#partnerId").val();
				} else {
					window.location='?';	
				}
				return false;
			}
		} 
		if (id_type=="PR") $("#CM").show();
		if (id_type=="CM" && $("#ValuesPR").val()=="" && initCombo==0) {
			
			$.backgroundLayer(true);
			$("#messageBox").show();
			$("#messageBox").css({
				top: $(window).scrollTop() + (($(window).height()/2)-($("#messageBox").width()/2)),
				left: (($("body").innerWidth()/2) - ($("#messageBox").width())/2)	
			});
			$("#messageBox div.content").html("Il faut d\'abord choisir le processeur");
			return false;	
		}
		$("#"+id_type+" .Selector").toggleClass('s');
				
		stateDisplay = $("#"+id_type+" .Selector").hasClass('s');
		if (stateDisplay) {
			$.each( $(".Selector"), function(i, n){
				idBlocOther = $(n).closest(".type").attr("id");
				if (id_type!=idBlocOther) { 
					$("#"+idBlocOther+" div.LineBox").removeClass('arrowUp');
					$("#"+idBlocOther+" .Selector").removeClass('s');
				}
			});
			$("#"+id_type+" div.LineBox").addClass('arrowUp');
			
			if ( id_selector.html() !="" && initCombo==0 && $("#ac"+id_type).val()=='1') {
			
			} else {
			
				id_selector.html("<div id='loading'><div></div></div>");
				$.ajax(
				{ 
					type: "POST",
					url: "ajxcfgo/get_products.php",
					data: "type="+id_type+"&"+$("#ValuesPR").val() +"&ValuesCM="+ $("#ValuesCM").val()+"&ValuesCG="+ $("#ValuesCG").val()+"&ref=", ////param&egrave;tres
					dataType: "html",
					error: function(retour)
					{
						alert("Erreur lors de la récupération des données");
					},
					success: function(html)
					{
						
						id_selector.css({background:"#FFF"});
						id_selector.html(html);
						
						if ($("#nbComboOpened").val()=="") $("#nbComboOpened").val(0);
						$("#nbComboOpened").val( parseInt($("#nbComboOpened").val()) + 1 );
					
						
						if ( $("#nbLigne").val()>=1 ) $("#ac"+id_type).val('1');
						else $("#ac"+id_type).val('');
						//return initCombo;
						//alert( $("#ac"+id_type).val() +" = "+id_type+" nblign :" +$("#nbLigne").val());
						
					}
				});
			}
				
		} else {
			
			$("#"+id_type+" div.LineBox").removeClass('arrowUp');
		}
		return stateDisplay;
	});
	$("#type-PCbox").click( function () {
		$("#WattBOX,#WattALIM").val('');
		$("#acBO,#acBO1,#acBO2").val('');
		if ($("#BO").hasClass('s')) {
			$("#BO .moreInfos").hide();
			$(this).html("Cliquez ici pour choisir un boîtier avec alimentation");
			$("#ValuesBO").val('');
			$("#BO div.LineBox").html(' -- Cliquez ici pour choisir votre boîtier PC (alimentation incluse)-- ');
			$("#BO").fadeOut("slow").addClass('h').removeClass('s');
			$("#BO1,#BO2").fadeIn("slow").addClass('s').removeClass('h');
			
			$.PutPriceTotal();
		} else {
			
			$(this).html("Cliquez ici pour choisir un boîtier et une alimentation s&eacute;par&eacute;ment");
			$("#ValuesBO1,#ValuesBO2").val('');
			$("#BO1 div.LineBox").html(' -- Cliquez ici pour choisir votre Boitier nu -- ');
			$("#BO2 div.LineBox").html(' -- Cliquez ici pour choisir votre Alimentation -- ');
			$("#BO").fadeIn("slow").addClass('s').removeClass('h');
			$("#BO1,#BO2").fadeOut("slow").addClass('h').removeClass('s');
			$("#BO1 .moreInfos, #BO2 .moreInfos").hide();
			$.PutPriceTotal();
		}
		
	});
	$("#btnAddBasket img").click(function() {
		$.saveConfig();
		
		var urlBasket="";
		var item="";
		if (ArrRefSelected[9]!="" && $("#BO").hasClass('s')===false) ArrRefSelected[9]="";
		if (ArrRefSelected[10]!="" && $("#BO").hasClass('s')===true) ArrRefSelected[10]="";
		if (ArrRefSelected[11]!="" && $("#BO").hasClass('s')===true) ArrRefSelected[11]="";
		
		$.each(ArrRefSelected, function(k,v) {
			if (v==undefined) item=""; 
			else item=v+",";
			urlBasket=urlBasket+item;
		});	
		if (dataPartner[1]!="") {
			
			urlBasket = "http://ad.zanox.com/ppc/?"+dataPartner[1]+"&ULP=[[http://www.topachat.com/pages/landing_zanox.php?origine=aff&ref="+urlBasket.substr(0, (urlBasket.length-1))+"]]";
			window.open(urlBasket, '_blank');	
			//alert(urlBasket);
			return false;
		}else if(dataPartner[0]!="") {
			
			urlBasket = "http://panier.topachat.com/info/basket/purchase_tac.cfm?ref=" + urlBasket.substr(0, (urlBasket.length-1));
			window.open(urlBasket, '_blank');	
			return false;
		}else {
			urlBasket = "http://panier.topachat.com/info/basket/purchase_tac.cfm?ref=" + urlBasket.substr(0, (urlBasket.length-1));
			window.location=urlBasket;
			return false;
		}
	});
	$("#btnSaveConfig").click( function () {
		$.saveConfig();
		
	});
	$("#btnPrintConfig").click( function () {
		var urlPrint="";
		var item="";
		$.each(ArrRefSelected, function(k,v) {
			if (v==undefined || v=="") item=""; 
			else item="z"+k+"="+v+"&";
			urlPrint=urlPrint+item;
		});
		urlPrint = "/pages/popup/configo_print.php?"+urlPrint.substr(0, (urlPrint.length-1));
		window.open(urlPrint,'imprimerConfig','width=650,height=919');
	});
	
});
