$(document).ready(function() {
	$('a#pwdforget').click(function(){
		$('#UI_login_form').hide();
		$('#UI_pwdforget').show();
	});
	
	$('a#linkProduct').click(function(){
		var link = $(this).attr('href');
		tb_show("", link +"?keepThis=true&TB_iframe=true&height=520&width=400", "");
		$('#TB_closeWindowButton').html('');
		$('#TB_closeWindowButton').prepend('<img>').find(':first-child').attr('src','http://www.1jour1vin.com/fr/img/generique/popup_close2.gif').attr('title','Fermer');		
		return false;
	});
	
	$('form#form_login').submit(function(e){
		e.preventDefault();
		
		var mail = $("#mail").attr('value');
		var pwd = $("#pwd").attr('value');
		var WebUserId = $("#IdentificationWebUserId").attr('value');
		var IdPartenaire = $("#IdentificationIdPartenaire").attr('value');
		var Origine = $("#IdentificationOrigine").attr('value');
		
		$.ajax({
			type: "POST",
			timeout: 7000,
			data: {mail: mail, pwd: pwd, WebUserId: WebUserId, IdPartenaire: IdPartenaire, Origine: Origine},
			url: '/fr/identifications/login',
			success: function(result) {
				if(result == "false") {
					addWindow("Attention !","Veuillez corriger les erreurs"); 
					setWindow();
				}
				else if(result == "mail") {
					addWindow("Attention !","Veuillez corriger votre email"); 
					setWindow();
				}
				else if(result == "mail_error") {
					addWindow("Attention !","Email inexistant dans la base"); 
					setWindow();
				}
				else if(result == "pwd_error") {
					addWindow("Attention !","Mot de passe incorrect"); 
					setWindow();
				}
				else if(result == "true") {
					$('form#form_login').submit();
				}			
			}
		})
	});
	
	$('button#submit_forget').click(function(){
		 $('#form_forget').ajaxSubmit({
			 success: function(responseText, responseCode) {
				 if (responseText == 'mail') {
					 addWindow("Attention !","Veuillez saisir une adresse email valide"); 
					 setWindow();
				 }
				 else if (responseText == 'false') {
					 addWindow("Attention !","Veuillez saisir votre email"); 
					 setWindow();
				 }
				 else if (responseText == 'mail_error') {
					 addWindow("Attention !","L\'envoi de vos identifiants a &eacute;chou&eacute; ; veuillez v&eacute;rifier votre email et recommencer."); 
					 setWindow();
				 }
				 else {
					 $('#pwdforget').parent().hide();
					 $('#form_forget').hide();
					 $('#UI_login_form').show();
					 setTimeout(function(){$('#messages').html(responseText).fadeIn();},1000);
				 }			 
			 } 
		 })
		 return false;
	 });
	 
	 
	var NumberofDIV = $("div#autres_vins > div.ligne_autre_vin").length;
	for (i=0; i <= NumberofDIV; i++) {
		$("div#ligne_"+i).mouseover(function() {
			 $(this).addClass('ligne_autre_vin_hover');
			 $(this).removeClass('ligne_autre_vin_out');
		});
		$("div#ligne_"+i).mouseout(function() {
			 $(this).removeClass('ligne_autre_vin_hover');
			 $(this).addClass('ligne_autre_vin_out');
		});
	}
	
	
	// on masque les produits
	$(".liste_produits").css({ display: "none"});
	//$('div#liste_0').show();

	 $('a.toggle').click(function(){		
		 
		 // get the src attribute of the image
		 var toggleImage = $('img', this).attr('src'); 	  
		 
		 // toggle the image src  attribute, then return selector to
	    $(this).find('img').attr('src', toggleImage == 'http://www.1jour1vin.com/fr/img/generique/icon_close.png' ? 'http://www.1jour1vin.com/fr/img/generique/icon_open.png' : 'http://www.1jour1vin.com/fr/img/generique/icon_close.png').end()  
		$(this).find('img').attr('src', toggleImage == '/fr/img/generique/icon_close.png' ? '/fr/img/generique/icon_open.png' : '/fr/img/generique/icon_close.png').end()  
		
		//$(this).find('img').attr('src', toggleImage == 'http://localhost/1jour1vin/img/generique/icon_close.png' ? 'http://localhost/1jour1vin/img/generique/icon_open.png' : 'http://localhost/1jour1vin/img/generique/icon_close.png').end()  
		//$(this).find('img').attr('src', toggleImage == '/1jour1vin/img/generique/icon_close.png' ? '/1jour1vin/img/generique/icon_open.png' : '/1jour1vin/img/generique/icon_close.png').end()  
		   
	    var Link = $(this).attr('id');	    
	    var number = Link.split('operation_');
	    $('div#liste_'+number[1]).prev(".waitBloc").show();
	    
	    var URL = $.jqURL.url();
	    
	    if(CheckURL2(URL, 'gripbyregion') == true) {
	    	elem = 'opgripbyregion';
	    	$('div#liste_'+number[1]).toggle();
	    }
	    
	    else {
		    if (CheckURL2(URL, 'over') == true) {
		    	elem = 'opmember';
		    }
	
		    else if(CheckURL2(URL, 'grip') == true) {
		    	elem = 'opgrip';
		    }
		    else {
		    	elem = 'op';
		    }
		    
		    $.ajax({
				type: "GET",
				url: "http://www.1jour1vin.com/fr/vente-vin-achat-vin/" + elem + "/" + number[1],
				//url: "http://localhost/1jour1vin/vente-vin-achat-vin/" + elem + "/" + number[1],
				success: function(msg){
					$('.waitBloc').hide();
					$('div#liste_'+number[1]).html(msg);
					//$('div#liste_'+number[1]).show();
					$('div#liste_'+number[1]).toggle();
				}
			})
		}
	 });	
	 
	 
	 $('a.toggle2').click(function(){		
		 
		// var Descriptif = $(this).parent().parent().next().next('.liste_produits');

		// get the src attribute of the image
		 var toggleImage = $('img' , $(this).parent('h2').parent('div.icon > a.toggle')).attr('src');
		 
		 // toggle the image src  attribute, then return selector to
		 $(this).parent('h2').parent('div.icon > a.toggle').find('img').attr('src', toggleImage == 'http://www.1jour1vin.com/fr/img/generique/icon_close.png' ? 'http://www.1jour1vin.com/fr/img/generique/icon_open.png' : 'http://www.1jour1vin.com/fr/img/generique/icon_close.png').end()
		 $(this).parent('h2').parent('div.icon > a.toggle').find('img').attr('src', toggleImage == '/fr/img/generique/icon_close.png' ? '/fr/img/generique/icon_open.png' : '/fr/img/generique/icon_close.png').end()
		 
		 //$(this).parent('h2').parent('div.icon > a.toggle').find('img').attr('src', toggleImage == 'http://localhost/1jour1vin/img/generique/icon_close.png' ? 'http://localhost/1jour1vin/img/generique/icon_open.png' : 'http://localhost/1jour1vin/img/generique/icon_close.png').end()  
		 //$(this).parent('h2').parent('div.icon > a.toggle').find('img').attr('src', toggleImage == '/1jour1vin/img/generique/icon_close.png' ? '/1jour1vin/img/generique/icon_open.png' : '/1jour1vin/img/generique/icon_close.png').end()  
		    			

	    var Link = $(this).attr('id');	    
	    var number = Link.split('operation2_');
	    $('div.liste_produits').hide();
	    $('div#liste_'+number[1]).prev(".waitBloc").show();
	    
	    if (toggleImage == 'http://www.1jour1vin.com/fr/img/generique/icon_close.png' || toggleImage =='/fr/img/generique/icon_close.png'){
			$('.waitBloc').hide();
			$('div#liste_'+number[1]).hide();
		}
		
		else if (toggleImage == 'http://www.1jour1vin.com/fr/img/generique/icon_open.png' || toggleImage =='/fr/img/generique/icon_open.png'){
			var URL = $.jqURL.url();
		    if(CheckURL2(URL, 'gripbyregion') == true) {
	    		$('div#liste_'+number[1]).toggle();
	    	}
		    else {
			    if (CheckURL2(URL, 'over') == true) {
			    	elem = 'opmember';
			    }
		
			    else if(CheckURL2(URL, 'grip') == true) {
			    	elem = 'opgrip';
			    }
			    else {
			    	elem = 'op';
			    }
			    $.ajax({
					type: "GET",
					url: "http://www.1jour1vin.com/fr/vente-vin-achat-vin/" + elem + "/" + number[1],
					//url: "http://localhost/1jour1vin/vente-vin-achat-vin/" + elem + "/" + number[1],
					success: function(msg){
						$('.waitBloc').hide();
						$('div#liste_'+number[1]).html(msg);
						$('div#liste_'+number[1]).show();
						
					}
				})
			}
		}

	 });
	 
	 
	 // pour page regions
	 $('a.toggle3').click(function(){		
		 // get the src attribute of the image
		 var toggleImage = $('img', this).attr('src'); 	    
		 // toggle the image src  attribute, then return selector to
	    $(this).find('img').attr('src', toggleImage == 'http://www.1jour1vin.com/fr/img/generique/icon_close.png' ? 'http://www.1jour1vin.com/fr/img/generique/icon_open.png' : 'http://www.1jour1vin.com/fr/img/generique/icon_close.png').end()
	    $(this).find('img').attr('src', toggleImage == '/fr/img/generique/icon_close.png' ? '/fr/img/generique/icon_open.png' : '/fr/img/generique/icon_close.png').end()
	    
	    //$(this).find('img').attr('src', toggleImage == 'http://localhost/1jour1vin/img/generique/icon_close.png' ? 'http://localhost/1jour1vin/img/generique/icon_open.png' : 'http://localhost/1jour1vin/img/generique/icon_close.png').end()  
		//$(this).find('img').attr('src', toggleImage == '/1jour1vin/img/generique/icon_close.png' ? '/1jour1vin/img/generique/icon_open.png' : '/1jour1vin/img/generique/icon_close.png').end()  
		   
		    
	    var Link = $(this).attr('id');	    
	    var number = Link.split('operation_');
	    $('div#liste_'+number[1]).fadeToggle();
	    //return false;
	 });	
	 
	 
	 $('a.toggle4').click(function(){		
		 // get the src attribute of the image
		 var toggleImage = $('img' , $(this).parent('h2').parent('div.icon > a.toggle3')).attr('src');
		 // toggle the image src  attribute, then return selector to
		 $(this).parent('h2').parent('div.icon > a.toggle3').find('img').attr('src', toggleImage == 'http://www.1jour1vin.com/fr/img/generique/icon_close.png' ? 'http://www.1jour1vin.com/fr/img/generique/icon_open.png' : 'http://www.1jour1vin.com/fr/img/generique/icon_close.png').end()
		 $(this).parent('h2').parent('div.icon > a.toggle3').find('img').attr('src', toggleImage == '/fr/img/generique/icon_close.png' ? '/fr/img/generique/icon_open.png' : '/fr/img/generique/icon_close.png').end()
		 
		 //$(this).parent('h2').parent('div.icon > a.toggle3').find('img').attr('src', toggleImage == 'http://localhost/1jour1vin/img/generique/icon_close.png' ? 'http://localhost/1jour1vin/img/generique/icon_open.png' : 'http://localhost/1jour1vin/img/generique/icon_close.png').end()  
		 //$(this).parent('h2').parent('div.icon > a.toggle3').find('img').attr('src', toggleImage == '/1jour1vin/img/generique/icon_close.png' ? '/1jour1vin/img/generique/icon_open.png' : '/1jour1vin/img/generique/icon_close.png').end()  
		     
		    
	    var Link = $(this).attr('id');	    
	    var number = Link.split('operation2_');
	    $('div#liste_'+number[1]).fadeToggle();
	    //return false;
	 });
	 
	 

	 $('a.close1').click(function(){
		
		 /*// get the src attribute of the image
		var toggleImage = $('img' , $(this).parent('.close').parent('.ligne1').parent('.liste_produits').parent('.operation > div.icon > a.toggle')).attr('src');
		// toggle the image src  attribute, then return selector to
		$(this).parent('.close').parent('.ligne1').parent('.liste_produits').parent('.operation > div.icon > a.toggle').find('img').attr('src', toggleImage == '../img/generique/icon_close.png' ? '../img/generique/icon_open.png' : '../img/generique/icon_close.png').end()  
		 */
		var link = $(this).attr('id');			   
		var number = link.split('close1_');
		$('div#liste_'+number[1]).hide();
		return false;
	 });
	 
	 
	 
	 $('a.close2').click(function(){
		/*
		 // get the src attribute of the image
		var toggleImage = $('img' , $(this).parent('.close').parent('.ligne_fin').parent('.liste_produits').parent('.operation > div.icon > a.toggle')).attr('src');
		// toggle the image src  attribute, then return selector to
		$(this).parent('.close').parent('.ligne_fin').parent('.liste_produits').parent('.operation > div.icon > a.toggle').find('img').attr('src', toggleImage == '../img/generique/icon_close.png' ? '../img/generique/icon_open.png' : '../img/generique/icon_close.png').end()  
		*/
		 
		var link = $(this).attr('id');			   
		var number = link.split('close2_');
		$('div#liste_'+number[1]).hide();
		return false;
	 });
	 
	 
	
});

jQuery.fn.fadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle'}, speed, easing, callback);
}; 

var defile;// l'element a deplacer
var psinit = 200; // position horizontale de depart
var pscrnt = psinit;       

function texteDefile() {
   if (!defile) defile = document.getElementById('defile');
   if (defile) 
   {
      if(pscrnt < ( - 1200))
	  	{
         pscrnt = psinit;
        } 
		else 
		{
         pscrnt += -1; // pixel par deplacement
      	}
      defile.style.left = pscrnt+"px";
   }
}
var timer = setInterval("texteDefile()",10); // delai de deplacement 

//----------------------------
function CheckURL(url, pattern) {
	var exp=new RegExp(pattern,"g");
	if ( exp.test(url) ) { return true; }
	else { return false;	}
}

function CheckURL2(url, pattern) {
 	//var recherche = new RegExp();
 	//var recherche = /(pattern)/g;
 	var recherche = new RegExp(pattern,'g');
 	var resultat = recherche.test(url);
 	if ( resultat == false ) { return false; }
	else { return true;	}
	
}

function addWindow(title,texte){
    if($("#UI_window").length > 0){
        $("#UI_window").remove();
    }
    $("#footer").after('<div id="UI_window" class="PNG"><div class="window"><div class="top"></div><div class="middle"><h2>'+title+'</h2><span class="close"><a href="javascript:removeWindow();"><img src="img/generique/popup_close.png" alt="fermer" /></a></span><div class="area">'+texte+'<div style="text-align:center;"><button class="customButton" type="submit" onclick="javascript:removeWindow();"><div><span class="">OK</span></div></button></div></div></div><div class="bottom"></div></div></div>');
    $("#UI_window .window").hide();
}

function removeWindow(){
    $("#UI_window").remove();
}

function setWindow(){
	var newX = $(window).width()/2 - $("#UI_window .window").width()/2;
	var newY = $(window).height()/2 - $("#UI_window .window").height()/2;
	$("#UI_window .window").css({left: newX, top: newY});
	$("#UI_window").width($(document).width());
	$("#UI_window").height($(document).height());
	$("#UI_window .window").show();
}
function setWindowPopupPanier(){
	var newX = $(window).width()/2 - $("#UI_popup_panier .window").width()/2;
	var newY = $(window).height()/2 - $("#UI_popup_panier .window").height()/2;
	$("#UI_popup_panier .window").css({left: newX, top: newY});
	$("#UI_popup_panier").width($(document).width());
	$("#UI_popup_panier").height($(document).height());
	$("#UI_popup_panier .window").show();
}
