// JavaScript Document

	var mouse_is_inside = false;
	var mouse_is_inside2 = false;
	var mouse_is_inside3 = false;
	var count_image;
	var index_image = 0;

	$(function(){
			   
		// Devises
		$('.entete_devise').toggle(
			function(){$('.entete_liste').slideDown()},
			function(){$('.entete_liste').slideUp()}
		)
		
		$('.entete_liste').hover(
			function(){mouse_is_inside3=true;},function(){mouse_is_inside3=false;}
		);
		
		$('.entete_liste li').click(
			function(){
				var id_devise = $(this).attr('value');
				$('.entete_liste').slideUp();
				$.ajax({
					type: "POST", 
					url: "ajax2/setDevise.php",
					data: "id_devise="+id_devise, 
					success: function(retour){
						if( retour != '' ){
							location.reload();
						}
					}
				});
			}
		)
		
		
		// win_contact
		$('a.modalcont').fancybox({
			'width'				: 734,
			'height'			: 590,
			'autoScale'     	: false,
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'type'				: 'iframe'
		});
		
		// win_send
		$('a.modalsend').fancybox({
			'width'				: 460,
			'height'			: 420,
			'autoScale'     	: false,
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'type'				: 'iframe'
		});
		
		// Listes de recherche
		$('.listeClic').click(
			function(){
				$('.listeHide').hide();
				$(this).next('.listeHide').show();
			}
		)
		
		$('.droite').click(
			function(){
				$(this).parents('.listeHide').eq(0).hide();
				return false;
			}
		)
			
		$('.gauche').click(
			function(){
				var listeHide = $(this).parents('.listeHide').eq(0);
				var index = $('.listeHide').index(listeHide);
				listeHide.find('input').attr('checked','');
				listeHide.prev('div').html(listeHide.find('span').html());
				initListe(index);
				return false;
			}
		)
		
		// Checkbox des listes
		$('.listeHide input').live('click', function(){
			
			var listeHide = $(this).parents('.listeHide').eq(0);
			var index = $('.listeHide').index(listeHide);
			var tab = new Array();
			var tab_id = new Array();

			listeHide.find('input:checked').each(
				function(i){
					tab[i] = $(this).next('label').text();
					tab_id[i] = $(this).val();
				}
			)
				
			if( tab.length > 0 ){
				
				var chaine = tab.join(',');
				if( chaine.length > 30 ) chaine = chaine.substr(0,30) + '...';
			
				listeHide.prev().html(chaine);
				
				// AJAX
				if( index == 0 || index == 1 ){
			
					$.ajax({
						type: "POST", 
						url: "ajax2/getDest.php",
						data: "ids="+tab_id.join(',')+"&index="+index, 
						success: function(retour){
							if( retour != '' ){
								initListe(index);
								$('.listeHide').eq(index+1).find('.listeBody').html(retour);
								$('.listeHide').eq(index+1).parents('.listeGroupe').eq(0).show();
							}
						},
						beforeSend: function() {},
						complete: function() {} 
					});
				
				}
				
			}
			else{
				listeHide.prev('div').html(listeHide.find('span').html());
				initListe(index);
			}
				
		})
		
		// Le calendrier
		$('#txtStartDate, #txtEndDate').datepicker({
			minDate: new Date(),
			//beforeShow: customRange,
			onSelect: selectRange,
			dateFormat: "dd/mm/yy",
			firstDay: 1, 
			changeFirstDay: false
		});
		
		// Autocomplete villas
		$('.villaInput').keyup(
			function(){
			
				if( $(this).val().length >= 3 ){
			
					$.ajax({
						type: "POST", 
						url: "ajax2/getVillas.php",
						data: "villa="+$(this).val(), 
						success: function(retour){
							if( retour != '' ){
								$('.villaDiv').html(retour).show('slow');
							}
							else{
								$('.villaDiv').html('').hide();
							}
						}
					});
					
				}
				else $('.villaDiv').html('').hide();
			}
		)
		
		// Diapo image
		count_image = $('.intro img').length;
		setInterval(fade,6000);
		
		// Menu des onglets
		$('.tabs li a').click(
			function(){
				var index = $('.tabs li a').index($(this));
				$('.tabs li a').removeClass('sel').blur();
				$(this).addClass('sel');
				$('.tabs .tab').hide().eq(index).show();
			}
		)
		
		// Newsletter
		$('.subscribe').click(
			function(){
				var id = $(this).attr('id');
				var news;
				
				if( id == 'news1' ) news = 1;
				else news = 0;
				
				$.ajax({ 
					type: "POST", 
					url: "ajax2/news.php",
					data: 'clmail='+$('#clmail').val()+'&news='+news,
					success: function(retour){
						alert(retour);
					}
				
				})
			}
		)
		
		// Blur des divs
		$('.listeHide').hover(
			function(){mouse_is_inside=true;},function(){mouse_is_inside=false;}
		);
			
		$('.villaDiv').hover(
			function(){mouse_is_inside2=true;},function(){mouse_is_inside2=false;}
		);
			
		$('body').mouseup(
			function(){
				if( !mouse_is_inside ) $('.listeHide').hide();
				if( !mouse_is_inside2 ) $('.villaDiv').hide();
				if( !mouse_is_inside3 ) $('.entete_liste').slideUp();
			}
		);
		
		// Filter les villas
		$('.refine_categorie li[class!=disabled]').live('click', function(){
																		  
			var refine_cat = $(this).parents('.refine_categorie').eq(0);
			var index_cat;
			var valeur;
			var classe;
			var tab_val = new Array();
			var tab_chaine = new Array();
			var chaine_val;
			var nombre;
			
			if( $(this).hasClass('checked') ){
				$(this).removeClass('checked');
			}
			else{
				if( $(this).hasClass('one') ) refine_cat.find('li').removeClass('checked');
				$(this).addClass('checked');
			}
			
			// Réinitialiser
			$('.villa').removeClass('hide');
			$('.refine_categorie li').removeClass('disabled');
			
			// Cacher les villas
			$('.refine_categorie .checked').each(
				function(i){
					refine_cat = $(this).parents('.refine_categorie').eq(0);
					index_cat = $('.refine_categorie').index(refine_cat);
					valeur = $(this).attr('value');
					$('.spans').find('span:eq('+index_cat+'):not(:contains(cdt'+valeur+'cdt))').parents('.villa').addClass('hide');
				}
			)
			
			// Recalculer le total
			/*$('.villa:not(.hide)').each(
				function(){
					index_span = 0;
					$(this).find('.spans span').each(
						function(){
							
							var text_span = $(this).html();
							var tab_span = text_span.split('-');
							var valeur_span;
							var iSpan;
							for( iSpan = 0 ; iSpan < tab_span.length ; iSpan++ ){
								valeur = tab_span[iSpan].replace(/cdt/g,'');
								var nombre = parseInt($('.refine_categorie').eq(index_span).find('li[value='+valeur+'] span').html());
								$('.refine_categorie').eq(index_span).find('li[value='+valeur+'] span').html(nombre+1);
							}
							
							index_span++;
							
						}
					)	
				}
			)*/
			$('.refine_categorie li').each(
				function(i){	
					refine_cat = $(this).parents('.refine_categorie').eq(0);
					index_cat = $('.refine_categorie').index(refine_cat);
					valeur = $(this).attr('value');
					nombre = $('.villa[class$=villa]').find('.spans span:eq('+index_cat+'):contains(cdt'+valeur+'cdt)').size();
					if( nombre == 0 ) classe = 'disabled';
					else classe = '';
					$(this).find('span').html(nombre).parent().addClass(classe);
				}
			)
			
			// Afficher le total des villas
			$('.nb_villas span').text($('.villa[class$=villa]').length);
			
			// Afficher les photos
			$('.villa[class$=villa] .villa_img img').lazyload({effect:'fadeIn'});
			
			//alert(tab_val.length);
			
			// Ajax
			$('.refine_categorie').each(
				function(i){
					
					index_cat = $('.refine_categorie').index($(this));
					tab_val[index_cat] = new Array();
					
					$(this).find('.checked').each(
						function(i){
							valeur = $(this).attr('value');
							tab_val[index_cat][i] = valeur;
						}
					)
					
				}
			)
			for( iVal = 0 ; iVal < tab_val.length ; iVal++ ) tab_chaine[iVal] = 'params[]='+tab_val[iVal].join(',');
			$.ajax({
				type: "POST", 
				url: "ajax2/params.php",
				data: tab_chaine.join('&'), 
				success: function(retour){}
			});
			
		})
		
		// Verificateur de formulaire
		$('.form_verif').submit(
			function(){
			
				var form_verif = $(this);
				var isEmpty = false;
				
				if( form_verif.find('.require[value=]').size() > 0 ){
				
					$.scrollTo(form_verif.find('.require[value=]').eq(0).parents('.ligne'),500);
				
					form_verif.find('.require[value=]')
					.animate({ backgroundColor: '#FEEF83' }, 150)
					.animate({ backgroundColor: 'white' }, 150)
					.animate({ backgroundColor: '#FEEF83' }, 150)
					.animate({ backgroundColor: 'white' }, 150)
					.animate({ backgroundColor: '#FEEF83' }, 150)
					.animate({ backgroundColor: 'white' }, 150)
					.animate({ backgroundColor: '#FEEF83' }, 150)
					.animate({ backgroundColor: 'white' }, 150)
					.animate({ backgroundColor: '#FEEF83' }, 150)
					.animate({ backgroundColor: 'white' }, 150);
					
					return false;
					
				}
				else return true;
				
			}
		)
		
		// page win_contact et win_send
		$('.aback').click(
			function(){
				$('#contact_suc, #contact_msg').hide();
				$('#contact_frm').show();
			}
		)
		
		
	})
	
	// Verification format email
	function isEMailAddr(mail) {
		
			var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
			if( mail.match(re) ){
				return true;
			}
			else{
				return false;
			}
		
		}
	
	// Initialer une liste
	function initListe(index){
		$('.listeGroupe:gt('+index+')').each(
			function(){
				var self = $(this);
				var span = self.find('span').html();
				self.hide().find('.listeClic').html(span).find('.listeBody').html('');
			}
		)
	}
	
	// Configuration du calendrier
	function customRange(input){
		
		var msg_arrival = $('#txtStartDate').next('span').text();
		var msg_departure = $('#txtEndDate').next('span').text();
	 
		var min = new Date(); //Set this to your absolute minimum date
		var dateMin = min;
		var dateMax = null;
		var dayRange = 1000; // Set this to the range of days you want to restrict to
	
		if( input.id == "txtStartDate" ){
		
			if ( $("#txtEndDate").datepicker("getDate") != null && $("#txtEndDate").val() != msg_departure ){
			
				dateMax = $("#txtEndDate").datepicker("getDate");
				dateMin = $("#txtEndDate").datepicker("getDate");
				dateMin.setDate(dateMin.getDate() - dayRange);
				if (dateMin < min){dateMin = min;}
				
			 }
			 else{
				//dateMax = new Date(); //Set this to your absolute maximum date
			 }
			 
		}
		else if( input.id == "txtEndDate" ){
		
			//dateMax = new Date(); //Set this to your absolute maximum date
			if ($("#txtStartDate").datepicker("getDate") != null && $("#txtEndDate").val() != msg_arrival ){
			
				dateMin = $("#txtStartDate").datepicker("getDate");
				var rangeMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + dayRange);

				if(rangeMax < dateMax){dateMax = rangeMax;}
			}
		}
			
		return {
			minDate: dateMin, 
			maxDate: dateMax
		}; 
	
	}
	
	function selectRange(dateText, inst){
		
		var msg_arrival = $('#txtStartDate').next('span').text();
		var msg_departure = $('#txtEndDate').next('span').text();
		
		dateStart = $("#txtStartDate").datepicker("getDate").getTime();
		dateEnd = $("#txtEndDate").datepicker("getDate").getTime();
		
		if( dateStart > dateEnd && $(this).attr('id') == "txtStartDate" ) $("#txtEndDate").val(msg_departure);
		else if( dateEnd < dateStart && $(this).attr('id') == "txtEndDate" ) $("#txtStartDate").val(msg_arrival);
		
		/*
	 
		var min = new Date(); //Set this to your absolute minimum date
		var dateMin = min;
		var dateMax = null;
		var dayRange = 1000; // Set this to the range of days you want to restrict to
	
		if( input.id == "txtStartDate" ){
		
			if ( $("#txtEndDate").datepicker("getDate") != null && $("#txtEndDate").val() != msg_departure ){
			
				dateMax = $("#txtEndDate").datepicker("getDate");
				dateMin = $("#txtEndDate").datepicker("getDate");
				dateMin.setDate(dateMin.getDate() - dayRange);
				if (dateMin < min){dateMin = min;}
				
			 }
			 else{
				//dateMax = new Date(); //Set this to your absolute maximum date
			 }
			 
		}
		else if( input.id == "txtEndDate" ){
		
			//dateMax = new Date(); //Set this to your absolute maximum date
			if ($("#txtStartDate").datepicker("getDate") != null && $("#txtEndDate").val() != msg_arrival ){
			
				dateMin = $("#txtStartDate").datepicker("getDate");
				var rangeMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + dayRange);

				if(rangeMax < dateMax){dateMax = rangeMax;}
			}
		}
			
		return {
			minDate: dateMin, 
			maxDate: dateMax
		}; */
	
	
	}
	
	// Animer les images
	function fade(){
		if( count_image > 1 ){
			$('.intro img').eq(index_image).fadeOut(3000);
			$('.intro .intro_titre').eq(index_image).fadeOut(3000);
			if( index_image == count_image - 1 ) index_image = 0;
			else index_image++;
			$('.intro img').eq(index_image).fadeIn(3000);
			$('.intro .intro_titre').eq(index_image).fadeIn(3000);
		}
	}
	
	// Cherche un element dans un tableau
	function in_array(needle,collection,strict){
    
		if(strict == null) strict = false;
		
		var i = collection.length-1;
		
		if (i >= 0) {
			
			do {
				if (collection[i] == needle) {
					
					if(strict && typeof(collection[i]) != typeof(needle)) {
						continue;
					}
					
					return true;
				}
			} while (i--);
		}
		
		return false;
	}
	
	function number_format(number,decimals,dec_point,thousands_sep) {
		// Strip all characters but numerical ones.
		number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
		var n = !isFinite(+number) ? 0 : +number,
			prec = !isFinite(+decimals) ? 2 : Math.abs(decimals),        sep = (typeof thousands_sep === 'undefined') ? ' ' : thousands_sep,
			dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
			s = '',
			toFixedFix = function (n, prec) {
				var k = Math.pow(10, prec);            return '' + Math.round(n * k) / k;
			};
		// Fix for IE parseFloat(0.55).toFixed(0) = 0;
		s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
		if (s[0].length > 3) {        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
		}
		if ((s[1] || '').length < prec) {
			s[1] = s[1] || '';
			s[1] += new Array(prec - s[1].length + 1).join('0');    }
		return s.join(dec);
	}
	
	function dump(obj){
			var out = '';
			for (var i in obj) {
				out += i + ": " + obj[i] + "\n";
			}
		
			alert(out);
		
			// or, if you wanted to avoid alerts...
		
			/*var pre = document.createElement('pre');
			pre.innerHTML = out;
			document.body.appendChild(pre)*/
		}
			
	
