// JavaScript Document


$(window).resize(function(){
	if ($('#slider li').size()>1) {
		centerSlider("#slider");
	}
	equalize();
	if($("body").hasClass("blog")){
		adaptoblog();
	}
});

window.onorientationchange = function(){
	if($("body").hasClass("blog")){
		adaptoblog();
	}
	
	setTimeout(function(){
		centerSlider("#slider");
		equalize();
		},1000);
}

window.onload=function(){
	if($("body").hasClass("services") && $("body").hasClass("plouf")){
		$(".lcontentpush").each(function(){
			var size = $(this).prev().children("span").outerWidth(true);
			$(this).css("margin-left",size);
		});
	}
	
	if($("body").hasClass("blog")){
		adaptoblog();
	}
}

$(document).ready(function(){	
	
	//used in contact (rub13)
	$("html").removeClass("nojs");
	
	
	if ($('#slider li').size()>1) {
		var wid;
		if($(window).width()>screen.width){
			wid = $(window).width();
		}else{
			wid = screen.width;
		}
		//$('#home #slider .sliderElement').css({"width":screen.width,"display":"block"});
		$('#home #slider .sliderElement').css({"width":wid,"display":"block"});
		//alert($(window).width());
		$('#next #slider .sliderElement').css({"width":$("#sliderWrapper").outerWidth(),"display":"block"});
		//alert($("#sliderWrapper").outerWidth());
		$("#slider li a").attr("tabindex", "-1");
	} else {
		$('#slider .sliderElement').css({"display":"block"});
	}
 
 
 
  $('#slideleft button').click(function() {
    var $lefty = $(this).next();
    $lefty.animate({
      left: parseInt($lefty.css('left'),10) == 0 ?
        -$lefty.outerWidth() :
        0
    });
  });
	// imac reference rotator
	$(function() {
		//ie8
		$('#rotator li').css("opacity",0);
		
		setInterval( "slideSwitch()", 5000 );
	});

	$("#navigation li").not(".current").find("a").hover(	
		function(){ 
			$(this).parent().animate({
				top: '-5',
				opacity:'1'
			  }, 100, function() {
					// Animation complete.
		  			}
			)
		},function(){ 
			$(this).parent().animate({
				top:'0',
				opacity:'0.9'
			  }, 100, function() {
				// Animation complete.
			 });
		});

	
	$("header nav ul").lavaLamp({ fx: "backout", speed: 600 });
	$("header nav ul").addClass("lava");

	// initiate slider on oneweb4all offer
    // doubleSlider()
		$("#doubleSlider").doubleSlider({
			auto: true, 
			pause: 4000,
			continuous: true
		});
	
	if ($('#slider li').size()>1) {
		$("#slider").easySlider({
			auto: true, 
			pause: 4000,
			numeric: true,
			continuous: true
		});
		/* hide prev next controls */
			var timeout = 0;
			$(function(){
				$('#slider, #prevBtn, #nextBtn').mouseover(function(){
					clearTimeout(timeout);
					// ie can't render the fade properly
					if(!($("body").hasClass("ie"))){
						$('#prevBtn, #nextBtn').fadeIn();
					}
				});
				$('#slider').mouseout(function(){
					timeout = setTimeout(function(){
					if(!($("body").hasClass("ie"))){
						$('#prevBtn, #nextBtn').fadeOut();
					}
					}, 1000);
				});
			});
	}
	equalize();

	/* 	*/
	$("#fixedBlock").hover(
		function(){
			$(this).fadeTo(300,1);
		},function(){
			$(this).fadeTo(300,.66);
	});
	
	//NB: le formulaire de contact est géré dans contactForm.js
	$("#fixedBlock li.scroll").hover(
		function(){
			$(this).stop().animate({"margin-left":"-190px"},300);
			$("body.ie #fixedBlock").stop().animate({"padding-left":"250px"},300);
		},function(){
			$(this).stop().animate({"margin-left":"-30px"},300);
			$("body.ie #fixedBlock").stop().animate({"padding-left":"60px"},300);
		}
	);
	

	/* page services/formation : catalogue de formations */
	$(".descriptionlist aside").hide().each(function(){
		if(!$("body").hasClass("ie")){
			var contenu = $(this).parent().not("aside").html();
			$(this).prepend("<h3>"+contenu+"</h3>");
		}
	});
		
	$(".descriptionlist li").bind("click",function(){
		if($(this).children("aside").hasClass("open")){
			$(this).children("aside").removeClass("open").hide(350);
		}else{
			$(".descriptionlist li aside.open").hide(350).removeClass("open");
			$(this).children("aside").show(350).addClass("open");
		}			
	});	
	
	
	
	
	
	//Penser à trouver un moyen de comprimer ces trois fonctions en une seule	
	
	function refElH(el){
		var pt = parseInt(el.css("padding-top"));
		var pb = parseInt(el.css("padding-bottom"));
		return el.height()+pt+pb;
	}
	
	$(".refElement p").each(function(){
		//make sure the texts go down as much as needed lààà
		$(this).css("bottom",-(refElH($(this))));
	});
	
	var speed = 200;
	
	$(".refElement").hover(
		function(){
			var h = refElH($(this).children("p"));
			$(this).children("p").stop().height(h).animate({"bottom":"0"},speed);
			$(this).children(".imgWrapper").children("a").stop().animate({"top":"-"+h},speed);
		},function(){
			var h = refElH($(this).children("p"));
			$(this).children("p").stop().height(h).animate({"bottom":"-"+h},speed);
			$(this).children(".imgWrapper").children("a").stop().animate({"top":"0"},speed);
		}
	);	
	
	
	
	function refBElH(el){
		var pt = parseInt(el.css("padding-top"));
		var pb = parseInt(el.css("padding-bottom"));
		return el.height()+pt+pb+20;
	}
	
	var speed = 200;
	$(".illustrationContainer").hover(
		function(){
			var h = $(this).children("p.schlunkText").height();
			$(this).children("img").stop().animate({"top":h},speed);
		},function(){
			$(this).children("img").stop().animate({"top":"0"},speed);
		}
	);	
	
  //smoothscroll
  $('p.top a').click(function(e) {
		if($.browser.safari){ bodyelem = $("body") } else{ bodyelem = $("html") } bodyelem.animate({scrollTop:0},300);
  });

	//Shorten blog description in order for it not to screw up the footer layout
	$("#pageFooter nav ul li:nth-child(2) p:last-child").html($("#pageFooter nav ul li:nth-child(2) p:last-child").html().replace("Un cocktail de nouvelles fraîches&nbsp;!","Des nouvelles fraîches&nbsp;!"));

	//Closing of contact form messages
	$(".closebtn").live("click",function(){
		$(this).parent().fadeOut(300);
	});
	
	
	
	
	
	if($("#curtain")){
		$curtainopen = false;  
		$(".rope").click(function(){  
			$(this).blur();  
			if($curtainopen == false){   
				$(this).stop().animate({top: '-150px' }, {queue:false, duration:350, easing:'easeOutBounce'});  
				$(".leftcurtain").stop().animate({width:'60px'}, 2000 );  
				$(".rightcurtain").stop().animate({width:'60px'},2000 );  
				$curtainopen = true;  
			}else{  
				$(this).stop().animate({top: '-190px' }, {queue:false, duration:350, easing:'easeOutBounce'});  
				$(".leftcurtain").stop().animate({width:'55%'}, 2000 );  
				$(".rightcurtain").stop().animate({width:'56%'}, 2000 );  
				$curtainopen = false;  
			}  
			return false;  
		});	
	}
	
	$("span.hoverh").attr("style","vertical-align:top").parent().hover(function(){
		$(this).children("span").children("img").attr("src","IMG/png/visu360h.png");
	},function(){
		$(this).children("span").children("img").attr("src","IMG/png/visu360.png");
	});
});

