/*
	APPLE.COM HOVER MENU Style
	
	Copyright: Projet Bleu and Cedric Dugas 
	(Change, use this code for whatever you need)
	
	A small program using jquery to do a hover style menu
	
	2 small bug, the A tags are moving a little from their emplacement.
	This is cause by the fact that they are all on relative position, it is a
	 small bug considering that doing this animation with an absolute position
	  would be alot more difficult and longer.
	Also, the animation can fliker a bit, you need to be very careful with your
	 css, as jquery have a tendancy to not be thatstable with advanced positioning 
	 and animation.
	
	Enjoy!
*/

$(document).ready(function() {	
	divDance.init()
});


divDance = {
	
	init : function()  {
		divDance.callback = false
		
		$(".listDance a.move").hover(function () {
			
			// The 2 if are for delaying the animation in case you rollover too fast
			if ($(this).parent().find(".imgContent").css("height") > "194px" ){
		
				if(divDance.callback == false) {
					divDance.dancing(this)
				}else{
					caller = this
					setTimeout("divDance.dancing(caller)", 800);
				}
		  	}	
	      }, 
	      function () {});
	      // the hover function do not work if you don't have a rollout function...
	},
	
	dancing : function(caller,startAnim)  {
		// the callback variable is set to do the animation only when the slideDown is finish so no animation are on the same time
		if (divDance.callback == false){
			
			$(".listDance a.move").not($(caller).parent().find("a.move")).addClass("hover")
			$(caller).removeClass("hover")	 
				divDance.callback = true
				//animation is inbricked in callback method
				$(".listDance .imgContent").not($(caller).parent().find(".imgContent")).slideUp(600, function () {
		
					$(caller).parent().find(".imgContent").slideDown(600, function () {
						divDance.callback = false
					});  
				});  
		}
	}
	
}





openDiv = {
	init : function(caller)  {
	$(caller).next().css('display','none')	
	},
	
	toggle : function(caller)  {
		
		if($(caller).next().css('display') == 'none' ) {
			$(caller).next().slideDown("slow")
			$(caller).addClass("openDiv")
		}else{
			$(caller).next().slideUp("slow")
			$(caller).removeClass("openDiv")
		}	
	},
	
	chambreToggle : function(caller) {
		if($(caller).next().css('display') == 'none' ) {
			$(caller).next().slideDown("slow", function() {
				var targetOffset = ($(caller).offset().top -40)
				$("html,body").animate({scrollTop:targetOffset}, 'slow')
			})
			$(caller).addClass("openDiv")
		}else{
			$(caller).next().slideUp("slow")
			$(caller).removeClass("openDiv")
		}	
	}
}





overlay = {
	open : function(pageToOpen){
		$("select").css("visibility","hidden")
		
		overlay.displayLoader();
		
		overlay.buildOverlay();
		
		$('div.overlayContainer').load(pageToOpen,function(){
				
			$('div.overlayContainer').animate({opacity:'show'},'fast');
			
			var xScroll, yScroll;
		       if (self.pageYOffset) {
		           yScroll = self.pageYOffset;
		           xScroll = self.pageXOffset;
		       } else if (document.documentElement && document.documentElement.scrollTop){     // Explorer 6 Strict
		           yScroll = document.documentElement.scrollTop;
		           xScroll = document.documentElement.scrollLeft;
		       } else if (document.body) {// all other Explorers
		           yScroll = document.body.scrollTop;
		           xScroll = document.body.scrollLeft;    
		       }

			if($.browser.msie){
				browserHeight = document.documentElement.clientHeight;
			}else{
				browserHeight = window.innerHeight;
			}
			
			planifOffset = $("#planif").offset().top
			
		
			if(browserHeight >= $('div.overlayContainer').height()){
				$('div.overlayContainer').css({
					'top' : "100px",
					'left' : "50%",
					'margin' : '0px 0 0 -' + $('div.overlayContainer').width()/2 + "px"
				});
				$('html,body').animate({scrollTop:100},'slow');
			}else{
				$('html,body').animate({scrollTop:100},'slow');
				$('div.overlayContainer').css({
					'top' : "100px",
					'left' : "50%",
					'margin' : '0 0 0 -' + $('div.overlayContainer').width()/2 + "px"
				});
			}

			$('#bigAjaxLoader').remove();
		});
	},
	close : function(){
		$('div.overlayContainer').fadeOut('fast',function(){
			$(this).remove();
			$('div.overlay').fadeOut('fast',function(){
				$(this).remove();
				$("select").css("visibility","visible")
			});
		});
	},
	buildOverlay : function(){

		// Build the background overlay div
		backgroundDiv = document.createElement('div');
			
		$(backgroundDiv).addClass('overlay');
		$(backgroundDiv).css({ 'height':$('#container').height() });
		
		$(backgroundDiv).bind('click',function(){
			overlay.close();
		});
		
		// Build the content overlay divs
		overlayContainer = document.createElement('div')
		$(overlayContainer).addClass('overlayContainer');
		
		
		$('body').append(backgroundDiv);
		// $(overlayContentContainer).append(overlayContent);
		// $(overlayContainer).append(overlayContentContainer);
		$('body').append(overlayContainer);
		
		// Then fade it in
		$('div.overlay').css('opacity',0);
		$('div.overlay').show();
		$('body').append(ajaxLoader);
		$('div.overlay').animate({opacity: 0.60}, 300, "");
		
		overlayContent = document.createElement('div')
		$(overlayContent).addClass('overlayContent');
		
		$(overlayContainer).append(overlayContent);
		
	},
	displayLoader : function(){
		ajaxLoader = document.createElement('img')
		$(ajaxLoader).attr({
			'id':'bigAjaxLoader',
			'src':'img/ajax-loader.gif'
		});
		$(ajaxLoader).css({
			'position':'absolute',
			'top':'50%',
			'left':'50%'
		});
	}
}