$(document).ready(function () {

	
	/******************************************************************
	*
	* ACCUEIL
	*
	*******************************************************************/
	if($("#formAccueil #localite").val() == ''){
		$("#formAccueil #localite").val("Saisissez le nom de votre ville ou de votre village");
	}
	$("#formAccueil #localite").click(function () { 
		
	    if($(this).val() == "Saisissez le nom de votre ville ou de votre village"){
			$(this).val(''); 
		}
	    
	});
	$("#formAccueil #localite").autocomplete("./recherche/autocompletelocalite/");
	// LOCALITE AJAX COMPLETION
	$("input#rechercheRapideCarte").live("keyup", function(){
			
			var search = $("input#rechercheRapideCarte").val();
			
			$.getJSON("recherche/ajaxlocalite/search/"+search, function(data){
				$("#rechercheCarte #listeLocalites .liste").empty().append(data.content);
			});
	
	});
	
	
	/******************************************************************
	*
	* COL RIGHT
	*
	*******************************************************************/
	// IDENTIFICATION
	$("#colright #email_identification").val("Votre email");
	$("#colright #password_identification").val("Votre mot de passe");
	$("#colright #email_identification").click(function () { 
		
	    if($(this).val() == "Votre email"){$(this).val('');}
	    
	});
	$("#colright #password_identification").click(function () { 
		
	    if($(this).val() == "Votre mot de passe"){$(this).val('');}
	    
	});
	$("#colright #formMembreIdentification").submit(function() {
		
		s = $(this).serialize();

		 $.post('./membre/identification', s, function(data){
			 $("#colright #identification").empty().append(data);
		}, "html");
		
		return false;
		
	});
	// INSCRIPTION  MEMBRE
	$("#formMembreInscriptionBoxe #email_inscription_membre").val("Votre email");
	$("#formMembreInscriptionBoxe #email_inscription_membre").click(function () { 
		
	    if($(this).val() == "Votre email"){$(this).val('');}
	    
	});
	// INSCRIPTION NEWSLETTER
	$("#formNewsletterInscription #email_inscription_newsletter").val("Votre email");
	$("#formNewsletterInscription #email_inscription_newsletter").click(function () { 
		
	    if($(this).val() == "Votre email"){$(this).val('');}
	    
	});
	
	/******************************************************************
	*
	* RECHERCHE AVANCEE
	*
	*******************************************************************/
	// LISTE DES PAYS
	if($("#listeGeo .liste").length > 0){
		
		$("div#ajaxload").show();
		$.getJSON("recherche/ajaxpays/nbCols/3", function(data){
			$("#listeGeo .liste").empty().append(data.content);
			$("div#ajaxload").hide();
		});
		
	}
	// PAYS AJAX COMPLETION
	$("input#rechercheRapidepays").live("keyup", function(){
			
			var search = $("input#rechercheRapidepays").val();
			
			$("div#ajaxload").show();
			$.getJSON("recherche/ajaxpays/nbCols/3/search/"+search, function(data){
				$("#listeGeo .liste").empty().append(data.content);
				$("div#ajaxload").hide();
			});
	
	});
	// DEPARTEMENT AJAX COMPLETION
	$("input#rechercheRapidedepartement").live("keyup", function(){
			
			var search = $("input#rechercheRapidedepartement").val();
			
			$("div#ajaxload").show();
			$.getJSON("recherche/ajaxdepartement/nbCols/3/search/"+search, function(data){
				$("#listeGeo .liste").empty().append(data.content);
				$("div#ajaxload").hide();
			});
	
	});
	// LOCALITE AJAX COMPLETION
	$("input#rechercheRapidelocalite").live("keyup", function(){
			
			var search = $("input#rechercheRapidelocalite").val();
			
			$("div#ajaxload").show();
			$.getJSON("recherche/ajaxlocalite/nbCols/3/search/"+search, function(data){
				$("#listeGeo .liste").empty().append(data.content);
				$("div#ajaxload").hide();
			});
	
	});
	// LISTE DES DEPARTEMENTS, DES LOCALITES
	$("#ongletsGeo a").live('click',function() {
		
		$("#ongletsGeo a").removeClass('on');
		$(this).addClass('on');
		var selection = $(this).attr("href");
		$("div.rechercheRapide input").attr("id","rechercheRapide"+selection);
		$("div.rechercheRapide input").val("");
			
		$("div#ajaxload").show();
		$.getJSON("recherche/ajax"+selection+"/nbCols/3", function(data){
			
			$("#listeGeo .liste").empty().append(data.content);
			$("div#ajaxload").hide();
		});
		
		return false;
	});
	// NB CARTES
	$("#listeGeo .liste a").live('click',function() {
		
		var id = $(this).attr("href");
		var localite = $(this).attr("title");
		var type = $(this).attr("rel");
		
		if(type == 'pays' && id == '1' ){
			
			$("#ongletsGeo #o_departement").show();
			alert("Sélectionnez un département ou une localité.");
			
		}else{
			
			// masquage de l'onglet département
			if(type == 'pays'){
				$("#ongletsGeo #o_departement").hide();
			}
			
			$("div#ajaxload").show();
			// recherche
			$.getJSON("recherche/ajaxnbcartes/"+type+"/"+id, function(data){
				
				$("div#nbCarte #selection").empty().append(localite);
				$("div#nbCarte #nb").empty().append(data.nbCartes);
				if(data.nbCartes == '0'){
					$("#btnAfficher").css('display','none');
					$("#btnRecInfructueuse").css('display','inline');
				}else{
					$("#btnAfficher").css('display','inline');
					$("#btnAfficher").attr("href",'catalogue/pellicule/');
					$("#btnRecInfructueuse").css('display','none');
				}
				$("div#ajaxload").hide();
			});
		
		}
		
		return false;
	});

	/******************************************************************
	*
	* RECHERCHE RAPIDE
	*
	*******************************************************************/
	// AUTCOMPLETION LOCALITE
	$("#formRechercheRapide #localite").autocomplete("./recherche/autocompletelocalite/");
	// ON MASQUE ET ON VIDE LA LISTE DES LEGENDES LORSQUE LE CONTENU DE LOCALITE EST CHANGE
	$("#formRechercheRapide #localite").live("keyup", function(){
		
		$("#formRechercheRapide #divLegendeslist").hide();
		$("#formRechercheRapide #legendeslist").empty();
	});
	// ON LANCE A NOUVEAU LA RECHERCHE LORSQUE UNE LEGENDE DE LA LISTE EST CHOISIE
	$("#formRechercheRapide #legendeslist").change(function(){
		rechercheRapide("formRechercheRapide",$("#formRechercheRapide #localite").val());
	});
	// REQUETE DE RECHERCHE RAPIDE
	function rechercheRapide(form,selection){
		
		s = $("#"+form).serialize();
		
		$("div#ajaxload").show();
		$.post("./recherche/ajaxnbcartes/rapide/1", s, function(data){
				
				$("div#nbCarte #selection").empty().append(selection);
				$("div#nbCarte #nb").empty().append(data.nbCartes);
				
				if(data.nbCartes == '0'){
					$("#btnAfficher").css('display','none');
					$("#btnRecInfructueuse").css('display','inline');
				}else{
					$("#btnAfficher").css('display','inline');
					$("#btnRecInfructueuse").css('display','none');
				}
				$("div#ajaxload").hide();
				
		}, "json");	
		
	}
	// SELECTION D'UNE LOCALITE DANS LA LISTE
	function selectLocalite(){
		
		$("#rechercheRapide a.selectlocalite").click(function(){
			
			$("#rechercheRapide div.success").remove();
			lo_ref_localite = $(this).attr("href");
			$("#localite").val(lo_ref_localite);
			rechercheRapide("formRechercheRapide",lo_ref_localite);
			return false;
		});
		
	}
	// RECHERCHE DES LEGENDES
	function rechercheLegende(refLocaliteVal){
		
		$("div#ajaxload").show();
		$.post("./recherche/ajaxlegende", {reflocalite:refLocaliteVal}, function(data){
		
			if(data.legendes){
				
				$("#legendeslist").empty().append(data.legendes);
				$("#divLegendeslist").show();
				
			}else{
				$("#divLegendeslist").hide();
			}
			$("div#ajaxload").hide();
			
		}, "json");
		
	}
	// 
	$("#legendeslist").click(function(){
		
		if($(this).val() != ''){
			$("#legende").val("");
		}
	});
	// NB CARTES
	$("#formRechercheRapide").submit(function() {
		
		// recherche d'une reference (prioritaire sur la localite)
		if($("#reference").val() != ''){
			
			rechercheRapide("formRechercheRapide",$("#reference").val());
		
		}else if($("#localite").val() != ''){
			
			// recherche d'une localite
			localiteVal = $("#localite").val();
			
			$("div#ajaxload").show();
			// recherche du lo_ref_localite
			$.post("./recherche/ajaxreflocalite", {localite:localiteVal}, function(data){
				
				// une localite
				if(data.lo_ref_localite){
					
					$("#localite").val(data.lo_ref_localite);
					rechercheRapide("formRechercheRapide",data.lo_ref_localite);
					rechercheLegende(data.lo_ref_localite);
					$("div#ajaxload").hide();
					
					if(data.msg_hameau != null){
						alert(data.msg_hameau);
					}
					
				}else if(data.localites_choice) {
					
					// plusieurs localite
					$("#rechercheRapide div.success").remove();
					$("#rechercheRapide").prepend(data.localites_choice);
					selectLocalite();
					$("div#ajaxload").hide();
					
				}else{
					// aucune localite
					alert('Aucune localité ne correspond à votre recherche. Utilisez la recherche avancée.');
				}
				
			}, "json");
			
			
		}else{
			
			// aucune recherche
			alert("Veuillez indiquer une localité ou une référence.");
		}
		
		return false;
		
	});
	
	/******************************************************************
	*
	* RECHERCHE THEMATIQUE
	*
	*******************************************************************/
	// LISTE DES THEMES
	if($("#listeThemes .liste").length > 0){
		
		$("div#ajaxload").show();
		$.getJSON("recherche/ajaxthemes/", function(data){
			$("#listeThemes .liste").empty().append(data.content);
			$("div#ajaxload").hide();
		});
	}
	// THEMES AJAX COMPLETION
	$("input#rechercheRapidetheme").live("keyup", function(){
			
			var search = $("input#rechercheRapidetheme").val();
			
			$("div#ajaxload").show();
			$.getJSON("recherche/ajaxthemes/search/"+search, function(data){
				$("#listeThemes .liste").empty().append(data.content);
				$("div#ajaxload").hide();
			});
	
	});
	// LISTE DES PAYS
	if($("#listeFiltre .liste").length > 0){
		
		$("div#ajaxload").show();
		$.getJSON("recherche/ajaxpays/nbCols/3", function(data){
			$("#listeFiltre .liste").empty().append(data.content);
			$("div#ajaxload").hide();
		});
		
	}
	// LISTE DES DEPARTEMENTS, DES PAYS
	$("#ongletsFiltres a").live('click',function() {
		
		$("#ongletsFiltres a").removeClass('on');
		$(this).addClass('on');
		var selection = $(this).attr("href");

		if (selection == 'departement'){
			
			$("div#ajaxload").show();
			$.getJSON("recherche/ajaxdepartement/nbCols/2", function(data){
				$("#listeFiltre .liste").empty().append(data.content);
				$("div#ajaxload").hide();
			});
			
		}else{
			
			$("div#ajaxload").show();
			$.getJSON("recherche/ajaxpays/nbCols/2", function(data){
				$("#listeFiltre .liste").empty().append(data.content);
				$("div#ajaxload").hide();
			});
		}
		
		return false;
	});
	// NB CARTES
	$("#listeThemes .liste a").live('click',function() {
		
		var id = $(this).attr("href");
		var theme = $(this).attr("title");

		$("div#ajaxload").show();
		$.getJSON("recherche/ajaxnbcartes/theme/"+id, function(data){
			
			$("div#nbCarte #selection").empty().append(theme);
			$("div#nbCarte #nb").empty().append(data.nbCartes);
			if(data.nbCartes == '0'){
				$("#btnAfficher").css('display','none');
				$("#btnRecInfructueuse").css('display','inline');
			}else{
				$("#btnAfficher").css('display','inline');
				$("#btnRecInfructueuse").css('display','none');
			}
			$("div#ajaxload").hide();

		});
		
		return false;
	});
	// NB CARTES AVEC FILTRES
	$("#listeFiltre .liste a").live('click',function() {
		
		var id = $(this).attr("href");
		var name = $(this).attr("title");
		var type = $(this).attr("rel");

		$("div#ajaxload").show();
		$.getJSON("recherche/ajaxnbcartes/"+type+"theme/"+id+"/theme/0", function(data){
			
			var theme = $("div#nbCarte #selection").html();
			var values = theme.split("<br>");
			$("div#nbCarte #selection").empty().append(values[0]+"<br />"+name);
			$("div#nbCarte #nb").empty().append(data.nbCartes);
			if(data.nbCartes == '0'){
				$("#btnAfficher").css('display','none');
				$("#btnRecInfructueuse").css('display','inline');
			}else{
				$("#btnAfficher").css('display','inline');
				$("#btnRecInfructueuse").css('display','none');
			}
			$("div#ajaxload").hide();

		});
		
		return false;
	});
	
	/**********************************************************
	 * 
	 * RECHERCHE PAR CARTE
	 * 
	 **********************************************************/
	// NB CARTES
	$("#listeLocalites .liste a").live('click',function() {
		
		var id = $(this).attr("href");
		var localite = $(this).attr("title");

		$("div#ajaxload").show();
		$.getJSON("recherche/ajaxnbcartes/localite/"+id, function(data){
			
			$("div#nbCarte #selection").empty().append(localite);
			$("div#nbCarte #nb").empty().append(data.nbCartes);
			if(data.nbCartes == '0'){
				$("#btnAfficher").css('display','none');
				$("#btnRecInfructueuse").css('display','inline');
			}else{
				$("#btnAfficher").css('display','inline');
				$("#btnAfficher").attr("href",'catalogue/pellicule/');
				$("#btnRecInfructueuse").css('display','none');
			}
			$("div#ajaxload").hide();
		});

		return false;
	});
	
	/**********************************************************
	 * 
	 * PELLICULE
	 * 
	 **********************************************************/
	// MINIATURES : PRECEDENT, SUIVANT
	$(".navigationCartes a").live('click',function() {

		var order = $(this).attr("href");
		
		$("div#ajaxload").show();
		$.getJSON("catalogue/ajaxpellicule/order/"+order, function(data){
			$("#pellicule").empty().append(data.miniatures);
			// current nb carte
			$("#currentNbCarte").empty().append(data.current_nb_carte+"/"+data.count_cartes+" photos");
			$("div#ajaxload").hide();
		});
		
		return false;
		
	});
	
	// AFFICHAGE DYNAMIQUE PHOTO GRANDE TAILLE
	$("#pellicule a.miniature").live('click',function() {

		var id =  $(this).attr("href");
		
		$("div#ajaxload").show();
		$.getJSON("catalogue/ajaxmaxcarte/id/"+id, function(data){
			
			$("#carteGrandeTailleP").empty().append('<img '+data.size+' src="'+data.url+'"/>');
			$("div#ajaxload").hide();
		});
		
		$("#ongletsAjouter a").attr("href", id);
		return false;
		
	}, "html");
	
	/**********************************************************
	 * 
	 * DIAPORAMA
	 * 
	 **********************************************************/
	
	// CARTE PRECEDENTE, SUIVANTE
	$(".navigationCartesG a.navigation").live('click',function() {

		var order = $(this).attr("href");
		diaporamaCarte(order, false);
		return false;
		
	});
	
	// DEFILEMENT AUTOMATIQUE
	$(".navigationCartesG a#automaticButton").click(function(){
		
		if(!go){
			diaporamaCarte('next', true);
			diaporamaCommand('go', "Défilement automatique activé.<br /> Pour le désactiver : cliquez sur le bouton 'Défilement automatique'");
		}else{
			diaporamaCommand('stop', "Défilement automatique désactivé.");
		}
		return false;
		
	});
	
	/**********************************************************
	 * 
	 * MINIATURES
	 * 
	 **********************************************************/
	// PAGE PRECEDENTE, SUIVANTE
	$("#catalogueMiniatures #navigationCartes a").live('click',function() {

		var order = $(this).attr("href");
		
		$("div#ajaxload").show();
		$.getJSON("catalogue/ajaxminiatures/order/"+order, function(data){
			$("#miniatures").empty().append(data.miniatures);
			// current nb carte
			$("#currentNbCarte").empty().append(data.current_nb_carte+"/"+data.count_cartes+" photos");
			
			$("div#ajaxload").hide();
		});
		
		return false;
		
	});
	
	/**********************************************************
	 * 
	 * CATALOGUE COMMON
	 * 
	 **********************************************************/
	// AJOUTER AUX FAVORIS
	$("a.addfavori").live('click',function(){
		
		var id =  $(this).attr("href");
		
		$.getJSON("favoris/ajouter/id/"+id, function(data){
			
			if(data.success){
				alert("Cette carte a été ajoutée à vos favoris.");
			}else{
				if(data.error == 'no_membre'){
					alert("Vous devez être membre pour enregistrer vos cartes favorites.\nMerci de vous identifier ou de vous inscrire.");
				}else{
					alert("Carte inconnue.");
				}
			}

		});
		
		return false;
		
	});
	// SUPPRIMER DES FAVORIS
	$("a.delfavori").live('click',function(){
		
		var id =  $(this).attr("href");
		
		$.getJSON("favoris/supprimer/id/"+id, function(data){
			
			if(data.success){
				alert("Cette carte a été supprimée de vos favoris.");
			}
		});
		
		return false;
		
	});
	// AJOUTER AU PANIER
	$("a.addpanier").live('click',function(){
		
		var id =  $(this).attr("href");
		
		tb_show("Ajouter au panier",'./catalogue/fiche/carte/'+id+'?KeepThis=true&TB_iframe=true&height=450&width=950');
		
		return false;
		
	});
	// PIVOTER LA PHOTO
	$("#ongletsAjouter a.rotation").click( function(){
		
		var id =  $(this).attr("href");
		var rotation =  $(this).attr("rel");
		
		$("div#ajaxload").show();
		$.get("catalogue/rotatecarte/id/"+id+"/rotation/"+rotation, function(data){
			
				if($("#carteGrandeTaille").length > 0){
					var gt = "#carteGrandeTaille";
				}else{
					var gt = "#carteGrandeTailleP";
				}
			
				$(gt).empty().append(data);
				$("div#ajaxload").hide();

		}, "html");
		
		//location.reload();
		
		return false;
		
	});
	
	/************************************************
	 * PANIER
	 ************************************************/
	// modifier
	$("table.panierArticles input.qte").blur(function(){
			
		// modification de l'article
		$("div#ajaxload").show();
		$.getJSON("panier/modifier/article_id/"+$(this).attr('id')+"/qte/"+$(this).val(), function(data){
			
			if(data.success){
				$("div#ajaxload").hide();
				alert("La quantité d'un article a été modifiée. Votre panier va être réactualisé.");
				location.reload();
			}
		});
	});
	
	// supprimer
	$("table.panierArticles td.supprimer a").click(function(){
		
		if (confirm("Confirmez-vous la suppression de cet article ?")) {
			
			// suppression de l'article
			$("div#ajaxload").show();
			$.get($(this).attr('href'), function(datas){
				$("div#ajaxload").hide();
				location.reload();
			}, "json");
			
		}
		
		return false;
	});
	
	// afficher la carte
	$("table.panierArticles td.firstpa a").live('click',function() {

		var id =  $(this).attr("href");
		
		tb_show("Ajouter au panier",'./catalogue/fiche/carte/'+id+'?KeepThis=true&TB_iframe=true&height=450&width=950');
		
		return false;
		
	});
	
	
	/************************************************
	 * COMMANDE
	 ************************************************/
	// adresse de livraison = adresse de facturation
	$("#formPanierAdresseLivraison #adresse_membre").click( function(){
			
		$("div#ajaxload").show();
		var form = '#formPanierAdresseLivraison';
		
		$(form+" #forme_juridique").val("");
		$(form+" #raison_sociale").val("");
		$(form+" #societe").val("");
		$(form+" #civilite").val("");
		$(form+" #nom").val("");
		$(form+" #prenom").val("");
		$(form+" #adresse").val("");
		$(form+" #code_postal").val("");
		$(form+" #ville").val("");
		$(form+" #telephone").val("");
		$("div#ajaxload").hide();
		
	});
	
	
	/************************************************
	 * TEXTE DEFILANT
	 ************************************************/
	$("div#slideText marquee").marquee('pointer').mouseover(function () {
		  $(this).trigger('stop');
		}).mouseout(function () {
		  $(this).trigger('start');
		}).mousemove(function (event) {
		  if ($(this).data('drag') == true) {
		    this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
		  }
		}).mousedown(function (event) {
		  $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
		}).mouseup(function () {
		  $(this).data('drag', false);
	});

});
// LISTE DES LOCALITES : RECHERCHE RAPIDE AVEC CARTE
function getLocalites(type, id){
	
	$("div#ajaxload").show();
	$.getJSON("recherche/ajaxlocalites/"+type+"/"+id, function(data){
		$("#listeLocalites .liste").empty().append(data.content);
		$("div#ajaxload").hide();
	});
};
var go;
var diaporamaCommand = function(action, message){
	if(action == 'stop'){
		if(go){
			clearTimeout(go);
			go = false;
		}else{
			message = null;
		}
	}
	$("#AfficheCartes #message").empty().append(message);
};
var diaporamaCarte = function(order, automatic){
	
	if(automatic){
		go = setTimeout("diaporamaCarte('next', true)", 3500);
	}else{
		diaporamaCommand('stop', "Défilement automatique désactivé.");
	}
	
	$("div#ajaxload").show();
	$.getJSON("catalogue/ajaxdiaporama/order/"+order, function(data){
		$("#carteGrandeTaille").empty().append('<img src="'+data.url+'" '+data.size+' />');
		
		if(data.prec_button){
			$("#cartePrecedente").empty().append('<a href="prec" class="navigation"><img border="0" alt="Carte précédente" src="images/content/carte_precedente_g.gif"/></a>');
			$("#navigationBottom span#precButton").empty().append('<a href="prec" class="navigation"><img border="0" alt="Carte précédente" src="images/content/btn_carte_precedente.jpg"/></a>');
		}else{
			$("#cartePrecedente").empty();
			$("#navigationBottom span#precButton").empty();
		}
		if(data.next_button){
			$("#carteSuivante").empty().append('<a href="next" class="navigation"><img border="0" alt="Carte suivante" src="images/content/carte_suivante_g.gif"/></a>');
			$("#navigationBottom span#nextButton").empty().append('<a href="next" class="navigation"><img border="0" alt="Carte suivante" src="images/content/btn_carte_suivante.jpg"/></a>');
			$(".navigationCartesG a#automaticButton:hidden").show();
		}else{
			$("#carteSuivante").empty();
			$("#navigationBottom span#nextButton").empty();
			
			diaporamaCommand('stop', "Fin du diaporama.<br />Défilement automatique désactivé.");
			$(".navigationCartesG a#automaticButton").hide();
		}
		
		// current nb carte
		$("#currentNbCarte").empty().append(data.current_nb_carte+"/"+data.count_cartes+" photos");
		
		$("#ongletsAjouter a").attr("href", data.id);
		$("div#ajaxload").hide();
	});
};