/*JS*/
$(document).ready(function() {		
		
	if($('#showAllResid').length) {
		showAllResid();
	}
	
	if($('#moteurResaHome').length) {
	$('#moteurResaHome fieldset select').sSelect({ddMaxHeight: '155px'});	
	$("#moteurResaHome input.datepicker").datepicker({
			showOn: 'both',
			buttonImage: 'images/common/btn_datepicker.png'	,
			changeMonth: true,
			changeYear: true,			
			minDate: 0,			
			buttonText: 'Choisissez votre date de départ'		
		});
		showMoreCrit();	
		//initCarouselOffre();	
		
		if($('#lastSeen').length) {
			initCarouselLastSeen();
		}
	}
	
	if($('#moteurResaCommon').length) {
	$('#moteurResaCommon fieldset select').sSelect({ddMaxHeight: '155px'});	
	$("#moteurResaCommon input.datepicker").datepicker({
			showOn: 'both',
			buttonImage: 'images/common/btn_datepicker.png'	,
			changeMonth: true,
			changeYear: true,			
			minDate: 0,			
			buttonText: 'Choisissez votre date de départ'		
		});
		showMoreCrit();		
	}
	
	if($('div.filterResults').length) {
		$('.filterResults select').sSelect({ddMaxHeight: '155px'});
	}
	
	
	
	if($('#navFicheResidence').length) {
		ajaxTabLoad();
	}
	
	if($('#navFicheStation').length) {
		ajaxTabLoadStation();
	}
	
	if($('#cycleHeader').length) {
		initCycleHeader();
	}
	
	if($('#carouselBonPlans').length) {
		initCarouselBonPlans();
	}
	
	if($('#ficheDestination').length) {
		initCarouselNotToMiss();
	}
	
	if($('#carouselCorners').length) {
		initCarouselCorners();
	}
	
	if($('#contact').length) {
		$('#contact fieldset select').sSelect({ddMaxHeight: '155px'});	
		$('#contact .textMsg label').click(function() {
			$('#message').slideDown();
		});
	}
	
	/*GESTION POPIN*/	
	$('.popinLink_ajax').colorbox({opacity:"0.7" , width:"650px" , close:"FERMER" , scrolling:false , scalePhotos:false });
		
	$('.popinLinkVideo_ajax').colorbox({opacity:"0.7" , width:"742px" , close:"FERMER" , scrolling:false , scalePhotos:false });
	
	$('.popinLinkCorners_ajax').colorbox({opacity:"0.7" , iframe:true, width:"780px" , height:"570px" , close:"FERMER" , scrolling:false , scalePhotos:false });
		
	$('.popinLink_inline').click(function() {
		var popinToShow = $(this).attr('href');			
		$.fn.colorbox({opacity:"0.7" , close:"FERMER" ,width:"650px" , scrolling:false , scalePhotos:false ,inline:true, href:popinToShow});
		return false;
	});
		
/*only functions for IE 6*/
	if ($.browser.msie && $.browser.version <= 6 ) {
			try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {} //caching CSS images				
			navIE6();		
		}		
/*end IE6*/	

});

function navIE6() {
	$('#nav > li').hover(function() {
		$(this).addClass('hovered');
	},function() {
		$(this).removeClass('hovered');
	});
}

function showAllResid() {
	$('#showAllResid').click(function() {
		$('#allResid').slideDown();
	});
	
	$('#closeAllResid').click(function() {
		$('#allResid').slideUp();
	});
}

function showMoreCrit() {
	$('#moreCritBtn').click(function() {
		$('#moreCrit').slideDown();
		return false;
	});

	$('#closeMoreCrit').click(function() {
		$('#moreCrit').slideUp();
	});
}

function initCarouselOffre() {
	 $("#carouselOffre .carouselContainer").jCarouselLite({
		visible: 1 ,
		start: 0 , 
		speed: 500 ,
		btnGo:
		[".#carouselOffre .sliderOffre0", "#carouselOffre .sliderOffre1",".#carouselOffre .sliderOffre2", "#carouselOffre .sliderOffre3"]	
    });	
 
 $carouselTrigger = $("#carouselOffre .trigger_carouselOffre li span");
	
	$carouselTrigger.eq(0).parent('li').addClass('active');
	$carouselTrigger.click(function() {		
		$carouselTrigger.parent('li').removeClass('active');
		$(this).parent('li').addClass('active');
	});
	
} 

function initCarouselBonPlans() {
	var nbBonPlans = $('#carouselBonPlans .carouselContainer li').length;	
	if(nbBonPlans > 4) {	
		 $("#carouselBonPlans .carouselContainer").jCarouselLite({
			btnNext: "#carouselBonPlans .next",
			btnPrev: "#carouselBonPlans .prev",
			visible: 4 ,
			start: 0 , 
			speed: 500 		
		});		
	} else {
		$("#carouselBonPlans .prev , #carouselBonPlans .next").css('visibility','hidden');
	}
	
	 $("#carouselBonPlans .carouselContainer li:not(.active)").hover(function() {
		$(this).stop().animate({opacity: 1});
	 },function() {
		$(this).stop().animate({opacity: 0.3});
	 });
}

function initCarouselLastSeen() {
	if($('#lastSeen li').length > 5) {
	 $("#lastSeen .lastSeenCarousel").jCarouselLite({
		btnNext: "#lastSeen .next",
        btnPrev: "#lastSeen .prev",
		visible: 5 ,
		start: 0 , 
		speed: 500 		
    });	
	} else {
		$("#lastSeen .next, #lastSeen .prev").css('visibility','hidden');
	}
}

function initCarouselCorners() {
	$nbItem = parseInt($('#carouselCorners input').val());
	var nbBonPlans = $('#carouselCorners .carouselContainer li').length;	
	if(nbBonPlans > 5) {	
		 $("#carouselCorners .carouselContainer").jCarouselLite({
			btnNext: "#carouselCorners .next",
			btnPrev: "#carouselCorners .prev",
			visible: 5 ,
			start: $nbItem , 
			speed: 500 		
		});		
	} else {
		$("#carouselCorners .prev , #carouselCorners .next").css('visibility','hidden');
	}
	
	 $("#carouselCorners .carouselContainer li:not(.active)").hover(function() {
		$(this).stop().animate({opacity: 1});
	 },function() {
		$(this).stop().animate({opacity: 0.3});
	 });
}



function ajaxTabLoad() {		
	$triggerMainTabs = $('#navFicheResidence > li > a[target!="_blank"]');	
	$triggerMainTabs.click(function() {
		
		//ALA : patch pour desactiver l'appel ajax, 
    //dans ce cas le clique sur lien va directement la valeur de href 
	
	if($(this).parent('li').hasClass('active')) {		
		return false;
	} else {		
	
    if ($(this).attr('class') == 'disable_ajax')
		{       
       return;     
    }
    
    var ajaxUrl = $(this).attr('href');		
		$.ajax({
		   type: "POST",
		   url: ajaxUrl,		  
		   beforeSend: function() {
			$('#ajaxTabContent').html('<p class="ajaxLoading"><img src="images/common/loading.gif" alt="please wait" /></p>');
			},
		   success: function(msg){
			$('#ajaxTabContent').html(msg) ;				
			$('a.popinLink_ajax').colorbox({opacity:"0.7" , width:"650px" , close:"FERMER" , scrolling:false , scalePhotos:false });	
		   }
		 });		 
		$triggerMainTabs.parent('li').removeClass('active');	
		$(this).parent('li').addClass('active');
		 return false;		
		}	
	});	
}

function ajaxTabLoadStation() {		
	$triggerMainTabs = $('#navFicheStation > li > a');	
	$triggerMainTabs.click(function() {
		
		if($(this).parent('li').hasClass('active')) {			
			return false;
		} else {		
		
		var ajaxUrl = $(this).attr('href');		
		$.ajax({
		   type: "POST",
		   url: ajaxUrl,		  
		   beforeSend: function() {
			$('#ajaxTabContent').html('<p class="ajaxLoading"><img src="images/common/loading.gif" alt="please wait" /></p>');
			},
		   success: function(msg){			
			$('#ajaxTabContent').html(msg) ;				
			$('a.popinLink_ajax').colorbox({opacity:"0.7" , width:"650px" , close:"FERMER" , scrolling:false , scalePhotos:false });				
		   }
		 });		 
		$triggerMainTabs.parent('li').removeClass('active');	
		$(this).parent('li').addClass('active');
		 return false;		
		}	
	});	
}

function sliderAppart() {
	$triggerAppartSlider = $('#appartementSlider > li');
	$triggerAppartSlider.eq(0).addClass('openedAppart').css('height','auto');
	$triggerAppartSlider.click(function() {
		var myAppartHeight = $(this).children('div.appartDesc').height();
		if (myAppartHeight < 165) {
			myAppartHeight = 175 ;
		}			
		
		if(!($(this).hasClass('openedAppart'))) {		
			$triggerAppartSlider.removeClass('openedAppart').animate({height : 20});			
			$(this).addClass('openedAppart').animate({height : myAppartHeight});	
		}
	});
}

/* French initialisation for the jQuery UI date picker plugin. */
jQuery(function($){
	$.datepicker.regional['fr'] = {		
		closeText: 'Fermer',
		prevText: '<<',
		nextText: '>>',
		currentText: 'Courant',
		monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin',
		'Juillet','Août','Septembre','Octobre','Novembre','Décembre'],
		monthNamesShort: ['Jan','Fév','Mar','Avr','Mai','Jun',
		'Jul','Aoû','Sep','Oct','Nov','Déc'],
		dayNames: ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'],
		dayNamesShort: ['Dim','Lun','Mar','Mer','Jeu','Ven','Sam'],
		dayNamesMin: ['Di','Lu','Ma','Me','Je','Ve','Sa'],
		dateFormat: 'dd/mm/yy', firstDay: 1,
		isRTL: false};
	$.datepicker.setDefaults($.datepicker.regional['fr']);
});

function pleaseWait() {
	var viewportHeight = $(document).height();			
	$('#pleaseWaitLayer').css('height',viewportHeight).show();
	$('#pleaseWaitLoader').show();	
}

function killPleaseWait() {
	$('#pleaseWaitLayer , #pleaseWaitLoader').hide();	
}

function initCarouselNotToMiss() {
	$("#notToMiss .notToMissCarousel").jCarouselLite({
		//btnNext: "#lastSeen .next",
		//btnPrev: "#lastSeen .prev",
		visible: 1 ,
		start: 0 , 
		auto:5000,
		speed: 500 ,	
		beforeStart: function() {
			if($('#notToMiss .externalControl button.active').next('button').length) {
				$('#notToMiss .externalControl button.active').removeClass('active').next().addClass('active');
			} else {
				$('#notToMiss .externalControl button.active').removeClass('active')
				$('#notToMiss .externalControl button').eq(0).addClass('active')
			}								
		},	
		 btnGo:
		["#notToMiss .externalControl .1", "#notToMiss .externalControl .2",
		"#notToMiss .externalControl .3", "#notToMiss .externalControl .4"]	
	});	

	$('#notToMiss .externalControl button').click(function() {
		$('#notToMiss .externalControl button').removeClass('active');
		$(this).addClass('active');
	});
}	
