/*JS*/
$(document).ready(function() {		
	nav();
	$('#searchText .text').labelValue();
	
	if($('#quickAccess').length) {
		quickAccess();
	}		

	if($('#homeContent').length) {		
		videoTour();
		
		if($('#lastSeen').length) {
			initCarouselLastSeen();
		}
	}
	
	if($('#quickFAQ').length) {
		quickFAQ();
	}	
	
	if($('#content ul.faq').length) {
		faq();			
	}	
	
	if($('#content .blocSlider').length) {
		blocSlider();			
	}	
	
	if($('#nav_ficheResid').length) {
		navFicheResid();
		$('#tabContainer ul.tab_sidebar a.popinLink_img').colorbox({ opacity:"0.7" , close:"FERMER" , scrolling:false , scalePhotos:false });					
	}
	
	if($('#content fieldset.contactForm').length) {
		contactForm();		
	}
		
	if($('#showcase').length) {
		showcase();
	}
	
	if($('#filterBy').length) {
		$('#filterBy select').sSelect({ddMaxHeight: '155px'});	
	}	
		
	/* AJOUT BBO : popin plan d'acces des resultats d'acces rapides */
	$('#content ul.results div.situationMap a.situationMap_link').colorbox({ opacity:"0.7" , close:"FERMER" , scrolling:false , scalePhotos:false });
	/*popin pour la sidebar vignette (edito_sidebar) sur toutes les pages sauf residence */
	$('#content ul.tab_sidebar a.popinLink_img').colorbox({ opacity:"0.7" , close:"FERMER" , scrolling:false , scalePhotos:false });	

	$('#tooltipDevise').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		extraClass: "toolTip_services",
		left: -39,
		top: 20
	});	

	
	$('.popinLink_inline').colorbox({ opacity:"0.7" , width:"650px" , close:"FERMER" , scrolling:false , inline: true , scalePhotos:false, href: $(this).attr('href') });

	
	/*only functions for IE 6*/
		if ($.browser.msie && $.browser.version <= 6 ) {
				try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {} //caching CSS images				
				navIE();
			}		
	/*end IE6*/	
});

function quickAccess() {
	$('#quickAccess > li a').click(function() {
		var quickLinksBloc = $(this).attr('href').split('#quickAccess_');
		
		if($(this).parent('li').hasClass('active')) {
			$('#quickAccess_'+quickLinksBloc[1]).slideUp();			
			$('#quickAccess > li').removeClass('active');
		} else {				
			var quickAccessHeight = $('#quickAccessLinks').height();
					
			if(quickAccessHeight == 0) {				
				$('#quickAccess_'+quickLinksBloc[1]).slideDown();
				$(this).parent('li').addClass('active');
			} else {				
				$('#quickAccess > li').removeClass('active');
				$(this).parent('li').addClass('active');	
								
				$('#quickAccessLinks').height(quickAccessHeight);				
				$('#quickAccessLinks ul').hide();
				$('#quickAccess_'+quickLinksBloc[1]).show();
				var newHeight = $('#quickAccess_'+quickLinksBloc[1]).outerHeight();	
				$('#quickAccessLinks').animate({height:newHeight} , function() {
					$('#quickAccessLinks').height('auto');	
				});													
			}			
		}		
		return false;	
	});
	
	$('#quickAccessLinks ul > li').hover(function() {
		$(this).find('img').stop().animate({opacity:0.6}, 300);
	}, function() {
		$(this).find('img').stop().animate({opacity:1} , 300);
	});	
}

function navFicheResid() {

	$('#nav_ficheResid > li a').click(function() {	
		var tabToShow = $(this).attr('rel');
		
		if(tabToShow) {
		
			if(!($('#'+tabToShow+' *').length)) { //AJAX load
										
				$('#nav_ficheResid > li').removeClass('active');
				$(this).parent('li').addClass('active');	
				
				var ajaxUrl = $(this).attr('href');
				$.ajax({
				   type: "POST",
				   url: ajaxUrl,		  
				   beforeSend: function() {
						var tabHeight = $('#tabContainer').height();	
						$('#tabContainer').height(tabHeight);				
						$('#tabContainer div.tabContent').hide();				
						$('#'+tabToShow).show().html('<img src="images/common/loading.gif" alt="please wait" class="loader" />');							
					},
				   success: function(msg){	   					
						$('#'+tabToShow).html(msg);	
						
						var newTabHeight = $('#'+tabToShow).outerHeight();	
						$('#tabContainer').animate({height:newTabHeight} , function() {
							$('#tabContainer').height('auto');	
						});										
					}
				 });				
				return false;
			} 
			else { // Show / hide
			
				if($(this).parent('li').hasClass('active')) {
					return false ;
				} else {				
					var tabHeight = $('#tabContainer').height();						
									
					$('#nav_ficheResid > li').removeClass('active');
					$(this).parent('li').addClass('active');	
									
					$('#tabContainer').height(tabHeight);				
					$('#tabContainer div.tabContent').hide();
					$('#'+tabToShow).show();
					var newTabHeight = $('#'+tabToShow).outerHeight();	
					$('#tabContainer').animate({height:newTabHeight} , function() {
						$('#tabContainer').height('auto');	
					});							
				}		
				return false;	
			}
		} else {
			return true;
		}
	});
}

function nav() {
	$('#nav > li').hover(function() {
		if($(this).children('ul').length || $(this).children('div').length) {
			$(this).children('a').css('cursor','default');
		}
	}, function() {});
	
	/*$('#nav > li').each(function() {
		$(this).find('li').eq(0).addClass('firstItem');
	});*/
}

function navIE() {
	$('#nav > li').eq(0).addClass('first');

	$('#nav > li').hover(function() {
		$(this).addClass('hoverIE');
	},function() {
		$(this).removeClass('hoverIE');
	});
}

function quickFAQ() {
	$faqTrigger = $('#quickFAQ li h4'); 
	$faqContent = $('#quickFAQ li p');
	
	$faqTrigger.eq(0).addClass('active'); 
	$faqContent.eq(0).show(); 
	
	$faqTrigger.click(function() {
		$faqContent.slideUp();
		$faqTrigger.removeClass('active');
		$(this).next('p:hidden').slideDown(function() {
			$(this).prev('h4').addClass('active');
		});
	});
}

function faq() {
	$faqTrigger = $('#content ul.faq li h2 , #content ul.titledFaq li h3'); 
	$faqContent = $('#content ul.faq li .faqTxt');
	
	$faqTrigger.eq(0).addClass('active'); 
	$faqContent.eq(0).show(); 
	
	$faqTrigger.click(function() {
		$faqContent.slideUp();
		$faqTrigger.removeClass('active');
		$(this).next('div:hidden').slideDown(function() {
			$(this).prev().addClass('active');
		});
	});	
}

function blocSlider() {	
	$('#content .blocSlider h2.active').next().show();
		
	$('#content .blocSlider h2').click(function() {		
		//$(this).css('color','red');
		$(this).toggleClass('active').next('div').slideToggle();		
	});
		
	/* fonctionement en accordeon (comme FAQ)
	
	$blocSliderTrigger = $('#content .blocSlider h2'); 
	$blocSliderContent = $('#content .blocSlider .blocSliderTxt');
	$('#content .blocSlider h2.active').next().show();
			
	$blocSliderTrigger.click(function() {
		$blocSliderContent.slideUp();
		$blocSliderTrigger.removeClass('active');
		$(this).next('div:hidden').slideDown(function() {
			$(this).prev('h2').addClass('active');
		});
	});	*/
}

function videoTour() {
	$videoTrigger = $('#videoTour li h4'); 
	$videoImg = $('#videoTour li img');
	
	$videoTrigger.eq(0).addClass('active'); 
	$videoImg.eq(0).show(); 
	
	$videoTrigger.click(function() {
		$videoImg.hide();
		$videoTrigger.removeClass('active');
		$(this).addClass('active').next('a').children('img').show();
	});
	
	// popin en display:none dans la page (video_home.tpl)
	$('#videoTour li > a').colorbox({ opacity:"0.7" , width:"655px" , close:"FERMER" , scrolling:false , inline: true , scalePhotos:false, href: $(this).attr('href') });
}

function expandMapHome(xOffset , yOffset ) {
	if($('#quickAccessLinks ul:visible').length) {
		$('#quickAccess > li').removeClass('active');
		$('#quickAccessLinks ul:visible').slideUp('normal',function() {			
			$.scrollTo($('#wrapper'), 800 , {
				offset:120 ,
				onAfter: function() {
					createContainer(xOffset , yOffset);
				}
			});
		});		
	} else {
		$.scrollTo($('#wrapper'), 800 , {
			offset:120 ,
			onAfter: function() { 
				createContainer(xOffset , yOffset);			
			}
		});		
	}		
}

function createContainer(xOffset , yOffset) {
	$('#wrapper').css('position','relative').prepend( '<div id="expandMap"><div id="expandMapFlash">  </div></div>' );	
	createExpandMapFlash(xOffset , yOffset);
}

function createExpandMapFlash(xOffset , yOffset) {		
	var flashvars = {
	 edit:0,
	 big:1,
	 mapx:xOffset,
	 mapy:yOffset
	};
	var params = {
	 wmode: "transparent"
	};							
	var attributes = {};
	swfobject.embedSWF("flash/loader.swf", "expandMapFlash", "880", "702", "10", false, flashvars, params, attributes);
}

function shrinkMapHome() {	
	$('#expandMap').remove();	
}

function contactForm() {
	$('#content fieldset.contactForm p.msg_add a').toggle(function() {
		$(this).next('textarea').show();
	},function() {
		$(this).next('textarea').hide();
	});
	
	$('#content fieldset.contactForm  p.msg_to a').toggle(function() {
		$('#content fieldset.contactForm  p.msg_to .moreTxt').css('display','block');
		return false;
	}, function() {
		$('#content fieldset.contactForm  p.msg_to .moreTxt').css('display','none');
		return false;
	});

	$('#content fieldset.contactForm select').sSelect({ddMaxHeight: '155px'});

	$("#content fieldset.contactForm .datepicker").datepicker({
		showOn: 'both',
		buttonImage: 'images/common/btn_datepicker.png'	,
		changeMonth: true,
		changeYear: true,			
		minDate: 0,			
		buttonText: 'Choisissez une date'		
	});
	datepickerFR();
	
	$('#content fieldset.contactForm .text, #content fieldset.contactForm  textarea').labelValue();
}

function showcase() {
	if($('#showcase li').length > 6) {
		 $("#showcase .showcase_carousel").jCarouselLite({
			btnNext: "#showcase .next",
			btnPrev: "#showcase .prev" ,
			visible: 6 ,	
			scroll: 3 ,	
			speed: 700
			//circular: false	
		});			
	} else {
		$('#showcase .next , #showcase .prev').css('visibility','hidden'); 
	}
}

function initCarouselLastSeen() {
	if($('#lastSeen li').length > 4) {
	 $("#lastSeen .lastSeenCarousel").jCarouselLite({
		btnNext: "#lastSeen .next",
        btnPrev: "#lastSeen .prev",
		visible: 4 ,
		start: 0 , 
		speed: 500 		
    });	
	} else {
		$("#lastSeen .next, #lastSeen .prev").css('visibility','hidden');
	}
}

/* labelValue function */
$.fn.labelValue = function() {
	return this.focus(function() {	
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;			
		}
	});
};

/* French initialisation for the jQuery UI date picker plugin. */
function datepickerFR() {
	$.datepicker.regional['fr'] = {		
		closeText: 'Fermer',
		prevText: '<<',
		nextText: '>>',
		currentText: 'Courant',
		monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin',
		'Juillet','Aout','Septembre','Octobre','Novembre','Décembre'],
		monthNamesShort: ['Jan','Fev','Mar','Avr','Mai','Jun',
		'Jul','Aout','Sep','Oct','Nov','Dec'],
		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']);
}

		



