$(document).ready(function () {
	
	$("#div_loader_page").hide("puff");
	/*$("#div_opaque").hide("puff");*/
	$('#div_opaque').css('height','0%');
	$('#div_opaque').css('width','0%');
	$("#fen_inf").hide("drop");
	$("#toolbar").hide("puff");
	
	$(".checkboxes").checkbox();
	
//Fonction de défilement des zooms
	$(".vignette_zoom").everyTime("8s",function () {
		var id = $(this).attr("id");
		var array_name = "tab_"+id.substr(14,id.length);
		try
		{
			var random = Math.floor(Math.random()*(eval(array_name).length));
			var zooms_infos = eval(array_name)[random].split(':separateur:');
			$(this).children("div").children("a").children("ins").children("img").attr('src',zooms_infos[0]);
			$(this).children("div").children("a").attr("href",eval(array_name)[random]);
		}
		catch(error)
		{
			$(this).children("div").children("a").attr("disabled","disabled");
			$(this).children("div").children("a").children("ins").empty();
			$(this).children("div").children("a").children("ins").append('<span class="no_zoom_lbl">'+zoom_translation+'</span>');
		}
	},0,true);
	
	
//recheche avancée
	$("#advanced_search").accordion({
		collapsible : true,
		active : false,
		icons : {
			header: "ui-icon-circle-plus",
			headerSelected: "ui-icon-circle-close"
		}
	});
	
	$(".div_pictos_meteo").click(function () {
		if($(this).children("span").children("input").is(":checked"))
		{
			$(this).children("span").children("input").attr("checked","");
			$(this).children("span").children("span").removeClass("ui-icon-check");
			$(this).children("span").children("span").addClass("ui-icon-empty");
		}
		else
		{
			$(this).children("span").children("input").attr("checked","checked");
			$(this).children("span").children("span").removeClass("ui-icon-empty");
			$(this).children("span").children("span").addClass("ui-icon-check");
		}
	});
	
	$(".div_pictos_sort").click(function () {
		$(".div_pictos_sort").children("span").children("span").removeClass("ui-icon-bullet");
		$(".div_pictos_sort").children("span").children("span").addClass("ui-icon-empty");
		$(this).children("span").children("input").attr("checked",true);
		$(this).children("span").children("span").removeClass("ui-icon-empty");
		$(this).children("span").children("span").addClass("ui-icon-bullet");
	});
	
	
//initialisation du slider de fen_inf
	var max = $('#content_fen_inf').width()-$('.panoramique').width();
	$("#slider").slider({
		max : max,
		min : 0,
		orientation : 'horizontal',
		slide : function() {
			$('.panoramique').css("left",parseInt($("#slider").slider('option', 'value'))*(-1)+"px");
		}
	});
	$("#slider").hide();
	$("#slider").children("a").css("background-image","url(images/bg_slider.png)");
	
	
//gestion du formulaire de contact (fenetre modale, puis validation)
	var buttons = {};
	
	buttons[send_translation] = function(){
		if($("#form_buy").valid())
		{
			$("#form_buy").submit();
			//$(this).dialog('close');
		}
	}
	
	buttons[cancel_translation] = function(){
		$(this).dialog('close');
	}
	
	$("#div_form_buy").dialog({
		bgiframe: true,
		autoOpen: false,
		modal: true,
		width: 500,
		height: 550,
		resizable: false,
		buttons: buttons
	});
	
	$.validator.addMethod(
		"regex",
		function(value, element, regexp) {
			var check = false;
			var re = new RegExp(regexp);
			return this.optional(element) || re.test(value);
		},
		"Vérifiez votre champ svp."
	);
	
	var validator = $("#form_buy").validate({
		rules: {
			last_name: "required",
			first_name: "required",
			phone: {
				required: true,
				regex: "^[+0-9][0-9 .]{0,20}$"
			},
			email: {
				required: true,
				email: true
			},
			message: "required"
		},
		messages: {
			last_name: "",
			first_name: "",
			phone: "",
			email: "",
			message: ""
		}
	});
	
	
//gestion du clic sur les vignettes
	$(".lien_pano").click(function() {
		
		var variables_href = $(this).attr("href").split(':separateur:');
		var idFTPCAM = $(this).attr("id").replace("idFTPCAM_","");
		
		/*$("#div_opaque").show("puff");*/
		$('#div_opaque').css('height','100%');
		$('#div_opaque').css('width','100%');
		
		$("#slider").slider('option', 'value',0);
		
		//vidage de la fenetre
		$("#date_fen_inf").empty();
		$("#content_fen_inf").empty();
		$("#load_fen_inf").empty();
		$("#btn_zoom").empty();
		
		//remplissage de la fenetre
		$("#load_fen_inf").append('<img src="images/load.gif" alt="loading" id="loading" />');
		$("#content_fen_inf").append('<img src="'+variables_href[0]+'" alt="'+$(this).attr("alt")+'" id="pano_'+$(this).attr("alt")+'" class="panoramique" />');
		$("#date_fen_inf").append('<img src="http://www.trinum.com/ibox/ftpcam/'+idFTPCAM+'_date.jpg" alt="'+idFTPCAM+'" id="date_'+idFTPCAM+'" />');
		$("#btn_zoom").append('<ins><div id="div_zoom"></div></ins>');
		$("#btn_lien_interface").attr('href',variables_href[1]);
		$("#btn_zoom").attr('href',variables_href[0].replace('&','&amp;').replace('&','&amp;'));
		
		//le panoramique exploite 65% de la hauteur de page affichable
		//le jour où IE sera un vrai browser (impossible) ou qu'il aura disparu (google mange microsoft), vous pourrez enlever le if
		if(navigator.appName.search("xplorer") == -1)
		{
			$('.panoramique').css('max-height',(window.innerHeight*0.65));
		}
		
		//quand le panoramique est chargé on vire le loader, on regle le slider et on affiche le slider et la toolbar
		$("#pano_"+$(this).attr("alt")).load(function() {
			$(document).ready(function () {
				$("#load_fen_inf").empty();
				
				var view_width = parseInt($("#content_fen_inf").width());
				var width_r = parseInt($(".panoramique").width());
				var width_o = parseInt(variables_href[3]);
				var offset_o = parseInt(variables_href[2]);
				var offset_r = (offset_o*width_r)/width_o;
				var margin = width_r-(view_width+offset_o);
				
				max = $('.panoramique').width()-$('#content_fen_inf').width();
				$('#slider').slider('option', 'max', max);
				
				if(margin>0 && margin<(width_r-view_width))
				{
					$('.panoramique').css("left",parseInt(margin)*(-1)+"px");
					$('#slider').slider('option', 'value', margin);
				}
				
				$("#slider").show("puff");
				$("#toolbar").show("puff");
			});
		});
		
		max = $('.panoramique').width()-$('#content_fen_inf').width();
		$('#slider').slider('option', 'max', max);
		
		$("#fen_inf").show("drop");
		
		//on rend le pano draggable
		$('.panoramique').draggable({
			cursor : 'e-resize',
			axis : 'x',
			stop : function() {
				var div_width = $('#content_fen_inf').width();
				var img_width = $('.panoramique').width();
				var img_x = $('.panoramique').position().left;
				if(img_x>5)
				{
					$('.panoramique').css("left","0px");
				}
				else if(img_x<(div_width-img_width))
				{
					$('.panoramique').css("left",(div_width-img_width)+"px");
					
				}
			},
			drag : function() {
				var value = parseInt($('.panoramique').css("left"))*(-1);
				$("#slider").slider('option', 'value', value)
			}
		});
		
		return false;
	});
	
	$(".lien_zoom").click(function() {
		
		//si ya pas de zooms on fait rien
		if($(this).attr("disabled")!="disabled")
		{
			/*$("#div_opaque").show("puff");*/
			$('#div_opaque').css('height','100%');
			$('#div_opaque').css('width','100%');
			
			$("#slider").hide();
			
			//vidage de la fenetre
			$("#date_fen_inf").empty();
			$("#content_fen_inf").empty();
			$("#load_fen_inf").empty();
			
			if($(this).children("ins").children("img").attr('src')!="images/loader_zoom.gif")
			{
				//remplissage de la fenetre
				var zoom_infos = $(this).attr('href').split(':separateur:');
				$("#load_fen_inf").append('<img src="images/load.gif" alt="loading" id="loading" />');
				$("#content_fen_inf").append('<img src="'+zoom_infos[0]+'" alt="'+$(this).attr("title")+'" id="zoom_'+$(this).attr("title")+'" class="zoom" />');
				
				//le zoom exploite 65% de la hauteur de page affichable
				//le jour où IE sera un vrai browser (impossible) ou qu'il aura disparu (google mange microsoft), vous pourrez enlever le if
				if(navigator.appName.search("xplorer") == -1)
				{
					$('.zoom').css('max-height',(window.innerHeight*0.65));
				}
				
				//quand la vue est chargée on vire le loader
				$("#zoom_"+$(this).attr("title")).load(function() {
					$("#load_fen_inf").empty();
					$("#load_fen_inf").append(zoom_infos[1]+' - '+zoom_infos[2]);
				});
			}
			else
			{
				$("#load_fen_inf").empty();
				$("#content_fen_inf").append(zoom_translation);
			}
			
			$("#fen_inf").show('drop');
		}
		
		return false;
	});
	
	
//changement de langues
	$(".flags").click(function() {
		if($(this).attr('alt')=="french")
		{
			$("#lang_hidden").attr("value","0");
		}
		else if($(this).attr('alt')=="english")
		{
			$("#lang_hidden").attr("value","1");
		}
		$("#form_hidden").submit();
	});
	
	
//suppression de criteres
	$("#crit_beau").click(function() {
		$("#beau_hidden").attr("value","0");
		$("#form_hidden").submit();
	});
	
	$("#crit_mitige_ete").click(function() {
		$("#mitige_ete_hidden").attr("value","0");
		$("#form_hidden").submit();
	});
	
	$("#crit_pluie").click(function() {
		$("#pluie_hidden").attr("value","0");
		$("#form_hidden").submit();
	});
	
	$("#crit_orage").click(function() {
		$("#orage_hidden").attr("value","0");
		$("#form_hidden").submit();
	});
	
	$("#crit_neige").click(function() {
		$("#neige_hidden").attr("value","0");
		$("#form_hidden").submit();
	});
	
	$("#crit_mitige_hiver").click(function() {
		$("#mitige_hiver_hidden").attr("value","0");
		$("#form_hidden").submit();
	});
	
	$("#div_crit_search").click(function() {
		$("#search_hidden").attr("value","%");
		$("#form_hidden").submit();
	});
	
	$("#div_crit_search_massif").click(function() {
		$("#search_massif_hidden").attr("value","%");
		$("#form_hidden").submit();
	});
	
	$("#box_crit_sort_time").click(function() {
		$("#sort_hidden").attr("value","timeinv");
		$("#form_hidden").submit();
	});
	
	$("#box_crit_sort_timeinv").click(function() {
		$("#sort_hidden").attr("value","time");
		$("#form_hidden").submit();
	});
	
	$("#box_crit_sort_alphanum").click(function() {
		$("#sort_hidden").attr("value","alphanuminv");
		$("#form_hidden").submit();
	});
	
	$("#box_crit_sort_alphanuminv").click(function() {
		$("#sort_hidden").attr("value","alphanum");
		$("#form_hidden").submit();
	});
	
	
//gestion du changement de l'ajout de la class hover sur les bouttons
	$(".button").bind("mouseenter mouseleave", function(e){
        $(this).toggleClass("ui-state-hover");
    });
	
	
//gestion du select_cam
	$("#select_cam").change(function() {
		$("#search_hidden").attr("value",$("#select_cam").attr("value"));
		
		//remise au valeur par défaut
		$("#page_hidden").attr("value",1);
		$("#search_massif_hidden").attr("value",'%');
		$("#beau_hidden").attr("value",0);
		$("#mitige_ete_hidden").attr("value",0);
		$("#pluie_hidden").attr("value",0);
		$("#orage_hidden").attr("value",0);
		$("#neige_hidden").attr("value",0);
		$("#mitige_hiver_hidden").attr("value",0);
		
		$("#form_hidden").submit();
	});
	
	
//event clic sur les boutons
	$("#btn_close_fen_inf").click(function() {
		$("#slider").hide("puff");
		$("#toolbar").hide("puff");
		$("#fen_inf").hide('slide');
		
		
		/*$("#div_opaque").hide("puff");*/
		$('#div_opaque').css('height','0%');
		$('#div_opaque').css('width','0%');
		
		//vidage de la fenetre
		$("#date_fen_inf").empty();
		$("#content_fen_inf").empty();
		$("#load_fen_inf").empty();
		
		return false;
	});
	
	$(".btn_search_submit").click(function() {
		$("#form_search_bar").submit();
	});
	
	$("#div_buy").click(function() {
		$("#div_form_buy").dialog("open");
	});
	
	$(".target_blank").attr("target","_blank");
	
	$("#easteregg").dblclick(function() {
		alert("Je t'aime ma saussette :)");
	});
	
});

