// JavaScript Document
var hash;
hash = document.location.hash;
var discover_timer;

jQuery.fn.exists = function(){return jQuery(this).length>0;}

/* CUFON FONT REPLACEMENT */


$(document).ready(function(){
	/* MENU HOVER */	
	$("#content").css("height", $("#content").height() + 5);
	
	$("#menu a.menu_element").mouseover(function(){
		$(this).addClass("hover");
	});
	$("#menu a.menu_element").mouseout(function(){
		$(this).removeClass("hover");
	});
	
	/* MORE INFO */
	
	//Hide the "more info" boxes if javascript is activated
	$(".more").hide();
	$("#content").css("height", "auto");

	$(".more-button").click(function(){
		$(this).toggleClass("less");
		
		if ($(this).hasClass("less")) {
			$(this).text("Moins d'info");
		} else {
			$(this).text("Plus d'info");
		}
		
		$("#content").css("height", "auto");
		$(this).nextAll(".more").first().slideToggle($(this).nextAll(".more").first().height() * 2, function(){
			$("#content").css("height", $("#content").height() + 5);	
		});
		
		return false;
	});
	
	
	$(window).hashchange(function(){
		hash = document.location.hash;
		if($(hash).exists() && !$(hash).find(".more-button").hasClass("less")) {
			$(hash).find(".more-button").trigger("click");
		}
	})
	$(window).hashchange();
	
	$("#forfaits a.forfait").click(function(){
		$(window).hashchange();
	});
	
	$('#nouvelles-list').css('overflow', 'hidden');
	
	$("#more-news").click(function(e){
		e.preventDefault();
		
		$('#nouvelles-list').scrollTo($('#nouvelles-list').scrollTop() + 84,800, function() {
	
		});
		
		if(!$("#less-news").is(':visible') ){
			$("#less-news").css({'visibility' : 'visible'});
			$("#less-news").fadeIn("slow");	
		}
			
		if($('#nouvelles-list').scrollTop() + 84 >= $('#nouvelles-list ul').height() - $('#nouvelles-list').height()){
			$("#more-news").fadeOut("slow");		
		}			
		
	});
	
	$("#less-news").click(function(e){
		e.preventDefault();
		var scrollDestination = $('#nouvelles-list').scrollTop() - 84;
		if (scrollDestination < 0) {
			scrollDestination = 0;
		}
		$('#nouvelles-list').scrollTo(scrollDestination,800);
		
		if(!$("#more-news").is(':visible') ){
			$("#more-news").fadeIn("slow");		
		}
		
		
		if($('#nouvelles-list').scrollTop() - 84 <= 0 ) {
			$("#less-news").fadeOut("slow");	
		}
		/*
		if(position_actu-1 >= 1){
			position_actu--;
			$('#nouvelles-list').scrollTo("#actu"+position_actu,800);
		}
		if(position_actu == 1){
			$("#less-news").fadeOut("slow");	
		}
		if(position_actu == max_actu -1){	
			$("#more-news").fadeIn("slow");		
		}
		*/
	});
	
	// "Découvrez nos réalisations" box
	$("#decouvrez div.thumbnails_wrapper img").load(function() {
		$(this).fadeIn(300, function() {
			discover_timer = clearTimeout();
			discover_timer = setTimeout("play_next_medium_realisation()", 5000);	
		})
	});

});



function play_next_medium_realisation() {
	$("#decouvrez div.thumbnails_wrapper img").fadeOut(300, function() {
		$(this).attr("src", $("#decouvrez div.thumbnails_wrapper input:first").val());
		$("#decouvrez div.thumbnails_wrapper").append($("#decouvrez div.thumbnails_wrapper input:first"));
	});
}

//var position_actu = 0;
$(window).load(function(){
	//$('#nouvelles-list').scrollTo("#actu"+position_actu,800);
	discover_timer = clearTimeout();
	discover_timer = setTimeout("play_next_medium_realisation()", 5000);
});
