var prehome = {
	bkgdWidth : null, 
	bkgdHeight : null, 
	minWidth : 1024, 
	maxWidth : 1280,
	maxBkgdWidth : 1280,
	minHeight : 576, 
	maxHeight : 720,
	maxBkgdHeight : 720,
	//rapportMin : null,
	rapportMax : null,
	backgroundResize : function(){
		var docWidth = document.body.offsetWidth || document.documentElement.offsetWidth;
		var docHeight = window.innerHeight || document.documentElement.clientHeight;
		
		// Dimensionnement
		if(docWidth >= this.maxBkgdWidth && docHeight >= this.maxBkgdHeight){
			this.bkgdHeight = this.maxBkgdHeight;
			this.bkgdWidth = this.maxBkgdWidth;
		}else if(docWidth <= this.minWidth || docHeight <= this.minHeight){
			this.bkgdHeight = this.minHeight;
			this.bkgdWidth = this.minWidth;
		}else{
			var tmpHeight = Math.min(Math.max(this.minHeight,docHeight),this.maxBkgdHeight);
			var tmpWidthFromHeight =  Math.min(Math.max(this.minWidth,Math.floor(tmpHeight/this.rapportMax)),this.maxBkgdWidth);
			
			if(tmpWidthFromHeight == this.minWidth){
				this.bkgdHeight = this.minHeight;
				this.bkgdWidth = this.minWidth;
			}else if(tmpWidthFromHeight>docWidth){
				var tmpWidth = Math.min(Math.max(this.minWidth,docWidth),this.maxBkgdWidth);
				
				if(tmpWidth == this.minWidth){
					this.bkgdHeight = this.minHeight;
					this.bkgdWidth = this.minWidth;
				}else{
					var tmpHeightFromWidth =  Math.min(Math.max(this.minHeight,Math.floor(tmpWidth*this.rapportMax)),this.maxBkgdHeight);
					this.bkgdHeight = tmpHeightFromWidth;
					this.bkgdWidth = tmpWidth;
				}
			}else{
				this.bkgdHeight = tmpHeight;
				this.bkgdWidth = tmpWidthFromHeight;
			}
		}
		
		$('#background_prehome').css('height',this.bkgdHeight+"px");
		$('#background_prehome').css('width',this.bkgdWidth+"px");
		
		// Positionnement
		if(docHeight<=this.minHeight) {
			$('#background_prehome').css('top',"0px");
			$('#background_prehome').css('marginTop',"0px");
		} else {
			$('#background_prehome').css('top',"50%");
			$('#background_prehome').css('marginTop',"-"+Math.ceil(this.bkgdHeight/2)+"px");
		}
		if(docWidth<=this.minWidth) {
			$('#background_prehome').css('left',"0px");
			$('#background_prehome').css('marginLeft',"0px");
		} else {
			$('#background_prehome').css('left',"50%");
			$('#background_prehome').css('marginLeft',"-"+Math.ceil(this.bkgdWidth/2)+"px");
		}	
	},
	centreActu : function(){
		var sizeActu = 0;
		$('#mainmenu').children('.rubrique').each(function(){
			sizeActu += $(this).outerWidth();
		});
		var margeRestante = $('#mainmenu').width() - sizeActu;
		//var marge = ((margeRestante/2)/$('#mainmenu').width())*100;
		
		$('#mainmenu').children('.firstrubrique').css('marginLeft',(margeRestante/2)+'px');
		$('#mainmenu').children('.lastrubrique').css('marginRight',((margeRestante/2)-30)+'px');
		//$('.ie6 #mainmenu').children('.firstrubrique').css('marginLeft',((margeRestante/4)-5)+'px');
		//$('.ie6 #mainmenu').children('.lastrubrique').css('marginRight',((margeRestante/4)-2)+'px');
	},
	init : function() {
		this.rapportMax = this.maxHeight / this.maxWidth;

		this.backgroundResize();
		this.centreActu();
		this.addSlide();
		
		$('#inlineMenu').css('display',"none").css('visibility',"visible");
        if (!document.getElementsByTagName("html")[0].className.match(new RegExp('(\\s|^)nocufon(\\s|$)'))) {
              Cufon.replace('#mainmenu h2', { fontFamily: 'Times New Roman' , hover: false });
              Cufon.replace('#mainmenu h3', { fontFamily: 'ArialR' , hover: false });
        }
        //$('#inlineMenu').css('visibility',"hidden").css('display',"block");
		
		$('#enterBtn, .bkgd_img').click(function(){
			$('#background_prehome').data('manualAction', true);
			$('#enterBtn').hide();
			$('#inlineMenu').css('display',"none").css('visibility',"visible").fadeIn("slow", function(){$('#closeBtn').show();});
			$(window).trigger('resize');
			return false;
		});
		
		$('#closeBtn').click(function(){
			$('#inlineMenu').fadeOut("slow",function(){
				$(this).css('visibility',"hidden").css('display',"block");
				$('#closeBtn').hide();
				$('#enterBtn').show();
			});
			$('.indispo').hide();
			return false;
		});
		
		$('.IndispoBtn').click(function(){
			$('.indispo').fadeOut();
			return false;
		 });
		
		$('.link a').hover(
			function(){
				$(this).siblings("span").addClass('active');
			},
			function(){
				$(this).siblings("span").removeClass('active');
			}
		);
		 

		var  t = window.setTimeout(function() { $('.indispo').fadeOut();	}, 9000);

		$('.rubrique ul li .indisponible').click(function(){
			$('.indispo').show('normal', function(){
				clearTimeout(t);  
				t=window.setTimeout(function() {
				$('.indispo').fadeOut();
			}, 9000)
			});
			return false;
		});
		
		$('#background_prehome').data('manualAction', false);
		
		if ($('#background_prehome').is('.opened')) {
			$('#enterBtn').trigger('click');
		} else {
			setTimeout(
				function() {
					if (!$('#background_prehome').data('manualAction')) {
						$('#enterBtn').trigger('click');
					}
				}, 
				8000			
			);
		}
	},
	addSlide : function() {
		
		// check nombre li pour ajout fleche show/hide
		$(".rubrique ul").each(function(){
			var $this = $(this).parent();
			var n = $(this).children().size();
			liHeight = $(this).children().height(); // calcul de la hauteur des LI (fix pour tous les navigateurs)
			var arrow = '<div class="showList"></div>';
			if (n > 3) { // si plus de 3 liens, on ajoute la flèche
				$this.append(arrow);
				$(this).css('height', liHeight*3); // on calcul la taille des UL en fonction des LI
			}
		});
		// show/hide des li suplémentaires
		$('.showList').click(function() {
			var status = $(this).attr('class'); // check si déplié ou non
			var ulCible = "#"+$(this).parent().attr('id')+" ul"; // récupération de l'id du UL
			var nbLi = $(ulCible).children().size()-3; // nombre de li dans le ul moins les 3 de départ
			if (status == 'showList') { // si pas de 'arrowUp', on déplie
				$(this).addClass('arrowUp');
				$(ulCible).animate({height:$(ulCible).height()+liHeight*nbLi+"px"}, 300, 'linear'); // on déplie de liHeight*le nombre de li cachés
			}
			else {
				// retour à la position de départ
				$(this).removeClass('arrowUp');
				$(ulCible).animate({height:liHeight*3}, 300, 'linear');
			}
		});		
	}
}

prehome.init();

$(window).resize(function() {
	prehome.backgroundResize();
	prehome.centreActu();
});
