// JavaScript Document

var loading = true;
var isConnected = false;

function addFavoris() {
	if ( navigator.appName != 'Microsoft Internet Explorer' )
		window.sidebar.addPanel("Alphonce Production","http://www.alfonce-production.com",""); 
	else
		window.external.AddFavorite("http://www.alfonce-production.com","Alphonce Production"); 
}

function langue_switcher(lang) {
	var cible = document.location.href;
	var nameFile =cible.substring(cible.lastIndexOf( "/" )+1 ,cible.length);
	nameFile = nameFile.substring(0, nameFile.lastIndexOf( "." ));
	if(nameFile == "") nameFile = "index.php";
	location.href = "./include/php/switch_langue.php?lang=" + lang + "&cible=" + nameFile;
}

function panierManager(id, act, qte, cat) {
	$("#cartlistContener").load("./interface/panier.php", {'id' : id, 'act':act,'qte':qte, 'cat':cat }, 
	  function(){ 
	  	$("#divBoxBasketStatut").html( $("#counterArt").val() );
		if( $("#counterArt").val() > 0) 
		  $("#divBoxPanier div.divBoxMenuBarIcon").removeClass("iconCartEmpty").addClass("iconCartNoEmpty");
		else
		  $("#divBoxPanier div.divBoxMenuBarIcon").removeClass("iconCartNoEmpty").addClass("iconCartEmpty");	  
	  
	//  $('#divBoxBasket').show(0, function(){$('#cartlistContener').jScrollPane()}).hide();  
	$('#divBoxBasket').show(10, function(){$('#cartlistContener').jScrollPane(); $('#divBoxBasket').hide()}); 
	 });	
}


function refreshInfoAuth(isAuth) {
	isConnected =isAuth; 
	if( isAuth ) {
		if( $("#divBoxAccount div.divBoxMenuBarIcon").hasClass("iconIdentNok") ) $("#divBoxAccount div.divBoxMenuBarIcon").removeClass("iconIdentNok");
		$("#divBoxAccount div.divBoxMenuBarIcon").addClass("iconIdentOk");
		$("#divBoxIdentify").hide(200);
		$("#spanTxtIdentNok").hide(200);
		$("#spanTxtIdentOk").show(200);
	}
	else {
		if ( $("#divBoxAccount div.divBoxMenuBarIcon").hasClass("iconIdentOk") )  $("#divBoxAccount div.divBoxMenuBarIcon").removeClass("iconIdentOk")
		$("#divBoxAccount div.divBoxMenuBarIcon").addClass("iconIdentNok");
		$("#spanTxtIdentOk").hide(200);
		$("#spanTxtIdentNok").show(200);	
	}
}

/********************************/
/*		GESTION DU MENU 		*/
/********************************/
var currentSubMenu = "";
var currentTimer = "";
function disableSubMenu() {
	if(currentSubMenu != "") {
		var obj = $("#"+currentSubMenu);
		if (obj) { obj.slideUp('normal') ; currentSubMenu = ""; }
	}
}



function closePopup() {
	$("#popupcontent div.content").fadeOut(300, function(){ 
		$("#popupcontent div.content").html("");
		$("#popup").fadeOut(600);
	});
}

function refreshIsConnect() {
	$.ajax({
	   url: "interface/session.php",
	   success: function(msg){

	   }});
		
}

function launchDetailPart(id){
	$("#popupcontent div.content").load("composant/partition/partition-detail.php", {'id' : id }, function(){
			$("#popup").fadeIn(600);
			$.getScript("composant/partition/partition-detail.js", function(){	});								   
	});		
}

function launchNewsInscr(){
	$("#popupcontent div.content").load("composant/client/client.php", {"act" : "inl"}, function(){
			$("#popupcontent div.content").css("display", "block");
			$("#popup").fadeIn(600);
			$.getScript("composant/client/client.js", function(){	});								   
	});		
}

function launchLostPwd(){
	$("#popupcontent div.content").load("composant/client/demande.php", '', function(){
			$("#popupcontent div.content").css("display", "block");
			$("#popup").fadeIn(600);
			$.getScript("composant/client/demande.js", function(){	});								   
	});		
}

function launchEspaceClient(numMenu){
	$("#popupcontent div.content").load("composant/client/client.php", {"menu" : numMenu, "act" : "cli", "id":"self" }, function(){
			$("#popupcontent div.content").css("display", "block");
			$("#popup").fadeIn(600);
			$.getScript("composant/client/client.js", function(){	});								   
	});		
}

function launchCreateClient(numMenu){
	document.location.href = "./client.php";
	/*
	$("#popupcontent div.content").load("composant/client/client.php", {"menu" : numMenu, "act" : "cli", "id":"new" }, function(){
			$("#popupcontent div.content").css("display", "block");
			$("#popup").fadeIn(600);
			$.getScript("composant/client/client.js", function(){	});								   
	});		*/
}

function launchInfoPaiementSecure() {
	$("#popupcontent div.content").load("composant/paiement_securise/paiement_securise.php", '', function(){
			$("#popupcontent div.content").css("display", "block");
			$("#popup").fadeIn(600);
			$.getScript("composant/paiement_securise/paiement_securise.js", function(){	});								   
	});	
}

function refreshPanier() {
	$("#divBoxBasket div.content").load("./interface/panier.php", {}, function(){
		$("#divBoxBasketStatut").html( $("#counterArt").val() ); 
		var tblCounter = $("#counterArt").val().split(" ");
		var counter = tblCounter[0];
		 (counter == 0 || isNaN(counter) ) ?   $("#goToPanier").hide() : $("#goToPanier").show();
		$('#divBoxBasket').show(10, function(){$('#cartlistContener').jScrollPane(); $('#divBoxBasket').hide()});  		 
	});		
}


function disconnect(){
	$.get("interface/auth.php", { act: "disconnect" }, function(){ refreshInfoAuth(false); } );	
}

$(function(){		   
	$(".down").hover(
		function(){ 
			var subUl = $(this).parent("a").parent("li").find("div.sub");
			subUl.slideDown('normal') ;
			currentSubMenu = subUl.attr("id");
		},
		function(){ currentTimer = setTimeout( "disableSubMenu()", 200); }
	);
	$(".sub").hover( function(){ clearTimeout( currentTimer);}, function() { currentTimer = setTimeout( "disableSubMenu()", 200); }	);   


	$("#spanTxtIdentOk div.home").click(function(){ launchEspaceClient(0) });
	$("#spanTxtIdentOk div.adresse").click(function(){ launchEspaceClient(1) });
	$("#spanTxtIdentOk div.newsletter").click(function(){ launchEspaceClient(2) });
	$("#spanTxtIdentOk div.commande").click(function(){ launchEspaceClient(4) });
	$("#spanTxtIdentOk div.promotion").click(function(){ launchEspaceClient(5) });
	$("#spanTxtIdentOk div.logout").click(function(){ disconnect();  });
	
	$("#cmdNewCli").click( function(){ launchCreateClient() });

	$('#divBoxIdentify').bind('keypress', function(e) { if(e.keyCode==13) $('#divButtonValideIdent').click(); });
	$('#divBoxPanier').click( function(){  document.location.href="./panier.php" ; } );
});




/********************************/
/*	GESTION MENUS FLOTTANT 		*/
/********************************/
var saisiIdentity = false;
var timerAccount = -1;
var timerPanel = -1;
function disabledFloatMenuAcces() { $("#divBoxIdentify").fadeOut(500, function(){ $("#divBoxFloatMenu").height(50)}) };
function disabledFloatMenuBasket() { $("#divBoxBasket").fadeOut(500 , function(){ $("#divBoxFloatMenu").height(50)}) };

var numDoc = 0;

$(function(){
		
	/****************************************************************************	
	 * Clic sur +d'infos du paiement sécurisé
	/****************************************************************************/
	$("#linkInfoPaiement").click( function(){ launchInfoPaiementSecure(); });
	/****************************************************************************/

	
	/****************************************************************************
	 * Gestion de la barre d'icones [facebook, favoris]
	/****************************************************************************/
	$("#divIconFB").hover( 
		function(){ $(this).removeClass("divIconFB").addClass("divIconFBHover"); }, 
		function(){ $(this).removeClass("divIconFBHover").addClass("divIconFB");
	});
	
	$("#divIconAF").hover( 
		function(){ $(this).removeClass("divIconAF").addClass("divIconAFHover"); }, 
		function(){ $(this).removeClass("divIconAFHover").addClass("divIconAF");
	});	
	$("#divIconAF").click( function(){ addFavoris(); });
	//$("#divIconFB").click( function(){  window.open("http://fr-fr.facebook.com/pages/Clermont-Ferrand-France/Alfonce-Production/158240254208255");});
	$("#divIconFB").click( function(){  window.open("http://www.facebook.com/alfoncep");});
	/****************************************************************************/
	
	
	/****************************************************************************
	 * Gestion des évenements sur le panier
	/****************************************************************************/		   
	$("#divBoxPanier").mouseover(function(){ 
		clearTimeout(timerPanel);	
		$("#divBoxFloatMenu").height(120);
		$("#divBoxBasket").fadeIn(500);	
		$(this).addClass("divBoxMenuBarBkg");
	});
	$("#divBoxPanier").mouseout(function(){  timerPanel = setTimeout("disabledFloatMenuBasket()", 800); });
	$("#divBoxBasket").mouseover(function(){ clearTimeout(timerPanel);});
	$("#divBoxBasket").mouseout(function(){  timerPanel = setTimeout("disabledFloatMenuBasket()", 800); });	
	/****************************************************************************/
	
	
	/****************************************************************************
	 * Gestion des évenements sur le formulaire d'authentification
	/****************************************************************************/	
	$("#divBoxAccount").mouseover(function(){
		if(!isConnected) {
			clearTimeout(timerAccount);	
			 $("#divBoxFloatMenu").height(120);
			$("#divBoxIdentify").fadeIn(500);	
			$(this).addClass("divBoxMenuBarBkg");
		}
	});
	
	$("#divButtonValideIdent").click( function(){
		var statut = true;
		if(( $("#inputLogin").val() != "") && ( $("#inputPwd").val() != "") ) {
			$("#sha1").val(	hex_sha1(hex_sha1($("#inputPwd").val()).toString() + $("#seed").val() ));
			$("#inputPwd").val('');
			$.ajax({
			   type: "POST",
			   url: "interface/auth.php",
			   data: 'login='+ $("#inputLogin").val() +'&pwd=' + $("#sha1").val() + "&seed=" + $("#seed").val() ,
			   success: function(msg){refreshInfoAuth((msg == 'OK') )  }});
		}
	});	
	$("#divBoxAccount").mouseout(function(){ if(!saisiIdentity) timerAccount = setTimeout("disabledFloatMenuAcces()", 800); });
	$("#divBoxIdentify").mouseover(function(){clearTimeout(timerAccount);	});
	$("#divBoxIdentify").mouseout(function(){ if(!saisiIdentity) timerAccount = setTimeout("disabledFloatMenuAcces()", 800); });
	
	$("#frmIdentify .valide").hover( function() { $(this).removeClass("validOff").addClass("validOn") }, function() { $(this).removeClass("validOn").addClass("validOff") }  );
	$("#frmIdentify .rowBtn").hover( function() { $(this).removeClass("rowBtnOff").addClass("rowBtnOn") }, function() { $(this).removeClass("rowBtnOn").addClass("rowBtnOff") }  );	
	$("#frmIdentify input").hover( function(){ $(this).css("border", "1px solid #ccc")}, function(){$(this).css("border", "1px solid #999")} );
	$("#frmIdentify input").focus( function(){ $(this).css("background-color", "#666").css("border", "1px #999 solid"); saisiIdentity=true; } );
	$("#frmIdentify input").blur( function(){ $(this).css("background-color", "#333").css("border", "1px #999 solid"); saisiIdentity=false; } );	
	$(".divBoxValide").hover( function(){ $(this).removeClass("divBoxValide").addClass("divBoxValideOver");}, function(){ $(this).removeClass("divBoxValideOver").addClass("divBoxValide"); });
	$("#spanTxtIdentOk div").hover( function(){ $(this).fadeTo(200, 1); },  function(){ $(this).fadeTo(200, 0.7); });
	$("#cmdLostPwd").click( function(){ launchLostPwd();  });
	
	/****************************************************************************/
	
	
	/****************************************************************************
	 * Gestion de la recherche rapide
	/****************************************************************************/		
	$("#divBoxValideRapidSearch").click( function(){
		if( $("#inputRapidSearch").val() != "" ) document.location.href= "./recherche.php?rs="+$("#inputRapidSearch").val();	
	});
	$("#frmSearch input").hover( function(){ $(this).css("border", "1px solid #ccc")}, function(){$(this).css("border", "1px solid #999")} );
	$("#frmSearch input").focus( function(){ $(this).css("background-color", "#666").css("border", "1px #999 solid") } );
	$("#frmSearch input").blur( function(){ $(this).css("background-color", "#333").css("border", "1px #999 solid") } );
	/****************************************************************************/


	/****************************************************************************
	 * Gestion du selecteur de langue
	/****************************************************************************/	
	$("#divBoxMenuLang .en").hover( function(){ $(this).removeClass("en").addClass("enActive"); }, function() { $(this).removeClass("enActive").addClass("en") } );
	$("#divBoxMenuLang .fr").hover( function(){ $(this).removeClass("fr").addClass("frActive"); }, function() { $(this).removeClass("frActive").addClass("fr") } );
	$("#divBoxMenuLang div").click( function(){ langue_switcher( $(this).attr("id") ); });
	
	/****************************************************************************/
	

	/****************************************************************************
	 * Gestion de l'inscription a la newsletter
	/****************************************************************************/
	$("#inscriptionNewsLink").click( function(){launchNewsInscr();} );
	/****************************************************************************/	

	$(".btnValide").hover( function() { $(this).removeClass("btnValideOff").addClass("btnValideOn") }, function() { $(this).removeClass("btnValideOn").addClass("btnValideOff") }  );
	$(".panelLeftDetailSubMenu").hover( function(){ $(this).animate({paddingLeft:12}, 250 ); }, function(){ $(this).animate({paddingLeft:7}, 200 );} );
	$("#spanTxtIdentOk div.home").click( function(){  } );

	$("#goToPanier").hover( 
		function(){ $(this).css("border-bottom", "1px solid #FF0").css("color", "#FF0");},
		function(){ $(this).css("border-bottom", "transparent").css("color", "#fff"); }							 
	);

	$("#goToPanier").click( function(){ document.location.href = "./panier.php"; });

	$(".linkCatalogue").mouseover( 
		function(){ 
			var $this = $(this);
			var img = $this.find("img").get(0); 
			
			$(".classimgzoom").each( function(){
				$(this).fadeOut(100, function(){ $(this).remove(); }); //.animate( {width:0,height:0, opacity: 0 },  { duration: 400, queue: false }, "swing", function(){} );											 										  
			});
			
			
			
			
			var offset = $this.offset();
			var zoom = 1.4;
						
			var $imgSrc = $this.find("img");
			var $img = $imgSrc.clone(true).appendTo("body");
			var wImg = img.clientWidth;
			var hImg = img.clientHeight;
			var wImgFinal = $img.width();
			var hImgFinal = $img.height();
			var newTop = offset.top - ((hImgFinal-hImg)/2)-50;
			var newLeft =  offset.left - ((wImgFinal-wImg)/2);
			$img.attr("src", $imgSrc.attr("src") ).attr("id", "imgzoom_"+(numDoc++)).attr("class", "classimgzoom");
			$img.css({"position":"absolute", "width" : wImg, "height" : hImg, "z-index":(1000+numDoc), "cursor":"pointer"});
			$img.css({"top":offset.top, "left" : (offset.left), "display" : "none", "background-color":"#fff"});
			$img.click( function(){ window.open( $this.attr("href")); });
			
			 $img.mouseout(function(){
									 
				$img.animate( {width:0,height:0, opacity: 0 },  { duration: 400, queue: false }, "swing", function(){} );											 
			});
			
			$img.animate( {width:wImgFinal,height:hImgFinal,top:newTop, left:newLeft,  opacity: 1 },  { duration: 600, queue: false }, "linear", function(){} );
			
		}
	);

});


$(document).ready(function(){
/*
	$("#divBoxBasket div.content").load("./interface/panier.php", {}, function(){
		$("#divBoxBasketStatut").html( $("#counterArt").val() ); 
		$('#divBoxBasket').show(10, function(){$('#cartlistContener').jScrollPane(); $('#divBoxBasket').hide()});  		 
	});		
	*/
	refreshPanier();
	$("#divBoxFade").fadeOut(1000, function(){loading = false;});
	
	isConnected = ($("#spanTxtIdentNok").hasClass('hidden'));
	refreshInfoAuth(isConnected);
	// Raffraichissement du logo d'identification
// 	setInterval("refreshIsConnect()", 10000);
});

