/*
	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!
*/

var bandeauGauche = null;
var bandeauDroite = null;

var numDiv = null;
var listeDivGFI = null;

if (without_css == false) {
  $(document).ready(function() {
    bandeauGauche = new divDance();
    bandeauGauche.init(menu_gauche_selected, "#bandeau_gauche");
    bandeauDroite = new divDance();
    bandeauDroite.init(menu_droite_selected, "#bandeau_droite");
    
    // récupération de tous les liens utilisant le GFI
    if($('a[@rel=gfi]')!=null){
      listeDivGFI = $('a[@rel=gfi]').map(function () { $(this).click( function(){
        showGFI($(this).attr("href"));
        return false;
        });
      });
    }
  });
}

function divDance() {
	
		this.openMenu = null;
		this.previousMenu = null;
		
		
		this.init = function(rub, conteneur){
		
			var name = this;

			this.animated = false;
		
			this.elements =  conteneur + " div div.content";
			
			
			// récupération de toutes les rubriques
			
			if($(this.elements)!=null){
			
				this.listRubrique = $(this.elements).map(function () { return this;});
			
			}else{
			
				this.listRubrique = new Array();
			
			}
			
			// Par défaut on ferme toutes les rubriques, hormis la première
			for(index = 0; index < this.listRubrique.length; index++){
				//alert("index --> "+index+", rub --> "+rub);
				if(index != rub && rub != "undefined"){
					$(this.listRubrique[index]).css("display","none");	
				}
				else{
					//alert(this.listRubrique[index]);
					this.openMenu = this.listRubrique[index];	
					//alert($(this.openMenu).attr('id'));
					//alert($(this.openMenu).prev().attr("class"));
					if($(this.openMenu).prev().attr("class").search("last") > 0){
																											
						var source = $(this.openMenu).prev().find("img:first").attr("src").substr(0,$(this.openMenu).prev().find("img:first").attr("src").lastIndexOf("."));
						//alert($(this.openMenu).parent().find("img").attr("src"));
						source += "_hover.jpg";
						$(this.openMenu).prev().find("img:first").attr("src", source);
						$(this.openMenu).prev().find("img:first").attr("height", 210);
						$(this.openMenu).prev().find("img:first").attr("height", 42);
					}
				}
				
				$(this.listRubrique[index]).parent().find("a.move").click(
																						function(){
																							numDiv = $(this).next().attr('id');
																							var appellant = this;
																							setTimeout(function(){name.click($(appellant).next().attr('id'), appellant);}, 500);
																						},
																						
																						function(){}
																					);
			}
	}
		
	this.click = function(divNum, caller){
		if(divNum == numDiv){
			if($(caller).attr('class').search("last") > 0  && this.animated == false && $(this.openMenu).attr('id') != $(caller).next().attr('id')){
			  var objetJQ = $(caller).find("img:first");
			  var source = objetJQ.attr("src").substr(0,objetJQ.attr("src").lastIndexOf("."));
			  source += "_hover.jpg";
			  
			  objetJQ.attr("src", source);
			  objetJQ.attr("width", 210);
			  objetJQ.attr("height", 42);
		  	}
		  	this.dancing($(caller).next(), this);	
		}
	}
	
	
	 this.dancing = function(caller, objet)  {
		// the callback variable is set to do the animation only when the slideDown is finish so no animation are on the same time
		//alert("caller -> " + $(caller).attr('id') + ", this.openMenu --> " + $(this.openMenu).attr('id') + ', objet.openMenu --> ' + $(objet.openMenu).attr('id'));
		//alert($(this.openMenu).attr('id'));
		if($(this.openMenu).attr('id') != $(caller).attr('id') && objet.animated == false){
			if(this.openMenu != null){
				$(this.openMenu).slideToggle("slow", function(){objet.animated = false;});
				objet.animated = true;
			}
			
			this.previousMenu = this.openMenu;
			this.openMenu = caller;
			
			$(this.openMenu).slideToggle("slow", function(){ 
																					   					
																										objet.animated = false; 
																					   					//alert($(objet.previousMenu).attr('id') + "delos");
																										//alert($(objet.previousMenu).prev().attr("class"));
																										if($(objet.previousMenu).prev().attr("class") != undefined){
																											if($(objet.previousMenu).prev().attr("class").search("last") > 0  ){
																												
																												var objetJQ = $(objet.previousMenu).prev().find("img:first");
																												//alert(objetJQ.attr("src"));
																												var source = objetJQ.attr("src").substr(0,objetJQ.attr("src").lastIndexOf("_hover.jpg"));
																												source += ".jpg";
																												//alert(source);
																												objetJQ.attr("src", source);
																												objetJQ.attr("width", 210);
																												objetJQ.attr("height", 52);
																											}
														  												}
																									
																									});
			objet.animated = true;
		}
	}
	
}

/*divDance = {
	
	divDance : function(rub, conteneur)  {
		
		this.openMenu = null;
		
		var elements =  conteneur + " li div.content";
		
		// récupération de toutes les rubriques
		var listRubrique = $(elements).map(function () { return this;});
		
		// Par défaut on ferme toutes les rubriques, hormis la première
		for(index = 0; index < listRubrique.length; index++){
			//alert("index --> "+index+", rub --> "+rub);
			if(index != rub && rub != "undefined"){
				$(listRubrique[index]).css("display","none");	
			}
			else{
				this.openMenu = listRubrique[index];	
			}
			
			$(listRubrique[index]).parent().find("a.move").hover(
																					function(){
																						this.dancing(this);
																					},
																					
																					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
		//alert($(this.openMenu).parent().find('div.content').attr("id") + ", " + $(caller).parent().find('div.content').attr("id"));
		if($(this.openMenu).parent().find('div.content').attr("id") != $(caller).parent().find('div.content').attr("id")){
			if(this.openMenu != null){
				$(this.openMenu).parent().find('div.content').slideToggle("slow");	
			}
			
			this.openMenu = caller;
			
			$(this.openMenu).parent().find('div.content').slideToggle("slow");
		}
	}
	
}*/


/*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%'
		});
	}
}


/*
 * Fonction permettant d'ouvrir une div
 * param nom_div id de la div
 */
function menuDiv(nom_div)
{

  var menu = document.getElementById(nom_div);

  if(menu.style.display == ''){
	
    menu.style.display='none';
    
  }else{

    menu.style.display='';	
	
  }
}

/*
 * Fonction permettant de changer le background d'un élément
 * param nom_element id de l'élément
 * param couleur_initiale code de la couleur initiale
 * param couleur_finale code de la couleur finale
 */
function changeFond(nom_element,couleur_initiale,couleur_finale)
{
  
  if( $('#'+nom_element).css('background-color').replace(new RegExp(" ","g"),"") == couleur_initiale){
    
    $('#'+nom_element).css({'background-color':couleur_finale});
    
  }else{
    
    $('#'+nom_element).css({'background-color':couleur_initiale});
    
  }
  
}
