/* SCROLL PAGE */
function scrollpage(hash)
{   
	// active animation 
	if(!no_scroll)
	anim = true;
	
	// detecte si hash est legal
	if(!$("a[href='"+hash+"']").length) 
	hash = '#'+default_hash+'';   
	hash = hash.substring(1);

	// enleve selected pour menu
	$(".menu .selected").removeClass("selected"); 

	// ajoute selected au menu choisi    
	$("a[href='#"+hash+"']").parent().addClass("selected");  
	
	// scroll le body  
	if(!no_scroll)  
	$("body").scrollTo("#body_"+hash+"",1000,{axis:'y', onAfter: clear_anim });   
	
	// exception     
	global_function(); 
	if(hash=='equipe')
	show_equipe(0);   
	if(hash=='portfolio') 
	show_portfolio();    
	if(hash=='services') 
	show_services();
	
	// arrete ou demarre le showcase accueil
	var show_trigger = (hash=='accueil')? "start":"stop";
	$("#accueil_showcase").trigger( show_trigger );
	
	return true;
}


/* FUNCTION CLEAR ANIM */
function clear_anim()
{
	anim = false; 
	no_scroll = false;   
	return true;
}     


/* FUNCTION GLOBAL FUNCTION */
function global_function()
{  
   $(".portfolio_item").clearQueue();
   $(".portfolio_item").hide();       
   $(".service_td").hide();   
   hide_portfolio_item();    
}   


/* FUNCTION SHOW EQUIPE */
function show_equipe(index)
{   
	$(".equipe_box").hide();
	$(".equipe_photo img").each(function(index) {
		$(this).attr("src", $(this).attr("src").split("_over.").join("."));
	});
	$(".equipe_box").eq(index).show();
	$(".equipe_photo img").eq(index).attr("src", $(".equipe_photo img").eq(index).attr("src").split(".").join("_over.")); 
}     


/* FUNCTION SHOW SERVICE */
function show_services()
{
}


/* FUNCTION SHOW PORTFOLIO */
function show_portfolio()
{
	$(".portfolio_item").each(function(index) {   
		$(this).delay((200 * (index+1))+800).fadeIn("slow");      
	});
	
	if(show_item)  
	{        
	show_portfolio_item($(".portfolio_item[key='"+show_item+"']"));     
	show_item = false;       
	}
}


/* FUNCTION SHOW PORTFOLIO ITEM */
function show_portfolio_item(element)
{   
	if(!element.attr("key") || $("#portfolio_box").data("key")==element.attr("key"))
	{      
	hide_portfolio_item(); 
	return true;
	}     
	portfolio_index = $(".portfolio_item").index(element);    
	portfolio_total = $(".portfolio_item").length;
	$("#portfolio_box_prev").show();
	$("#portfolio_box_next").show();    
	
	if(portfolio_index==0)   
	$("#portfolio_box_prev").hide();  
	if(portfolio_index==(portfolio_total-1)) 
	$("#portfolio_box_next").hide();   
	
	$("#portfolio_box").data("key",element.attr("key"));
	$("#portfolio_box").slideDown("slow"); 
	$(".portfolio_item").removeClass("selected");   
	$(".portfolio_item").not($(this)).find("img").hide();
	$("#portfolio_box_inner").load('/page/portfolio.php',{ action: "show_portfolio_item", key: element.attr("key")});
	element.addClass("selected");  
	
	if(element.find("img").is(":hidden"))
	element.find("img").show();      
}


/* FUNCTION HIDE PORTFOLIO ITEM */
function hide_portfolio_item()
{ 
	if($("#video_player").length)
	{
	jwplayer(0).remove();  
	$("#video_player").remove();
	} 
	$(".portfolio_item").removeClass("selected"); 
	$("#portfolio_box").data("key","");
	$("#portfolio_box").slideUp("slow");      
	portfolio_index = 0;     
} 


/* FUNCTION CHANGE PORTFOLIO PAGE */
function change_portfolio_page(key)
{
	$("#portfolio_items").load('/page/portfolio.php',{
	 action: "change_portfolio_page", key: key} );	
}  


/* FUNCTION UPDATE IMAGE BAR */
function update_image_bar()
{
	var total = $(".image_portfolio").length;
	var position = $(".image_portfolio").index($(".image_portfolio:visible"));   
	position = (position>0)? position:0; 
	$(".image_bar").find("span:nth-child(1)").text(position+1);    
	$(".image_bar").find("span:nth-child(2)").text(total);
}


/* JW MEDIA PLAYER */    
function create_player(target,width,height,file)
{   
	jwplayer(target).setup({
	file: file, 
	height: height, 
	width: width,
	autostart: true, 
	controlbar: "over", 
	skin: "/script/stormtrooper/stormtrooper.xml",
	players: [ { type: "html5" }, { type: "flash", src: "/script/player.swf" } ],
	events: { 
		onComplete: function(event) { hide_portfolio_item(); }
		 } 
	});
}  
function stop_player(target)
{  
   if($('#vid_player').length)
   jwplayer().stop(); 
}


$(document).ready(function() { 
    
	/* VALEUR */
	anim = false;
	timeout = false;
	cancelmouseout = false;    
	no_scroll = false;
    index = false;  
	portfolio_index = 0;
    portfolio_total = false;
    show_item = false;

	/* HASH */
	$(window).bind( 'hashchange', function(){ 
		scrollpage(location.hash);     
	});
	if(!location.hash && start_hash)
	location.hash = start_hash;
	if(location.hash) 
	scrollpage(location.hash);   
	else
	scrollpage(default_hash);    
	
	
	/* CURSOR */     
	$(".menu span a").click(function() {
		no_scroll = false;
	});
	$(".service_button").click(function() {
	   no_scroll = false;     
	   location.hash = '#services';
	});
 
	
	/* SCROLL */ 
	$(window).scroll(function() {   
		if(anim == false )
		{
		var hash = false;    
		var scroll = $(window).scrollTop(); 
		var height = $(".body_element").height();
		
		if(!scroll)   
		index = 0;
		else
		index = Math.floor((scroll+500)/height); 	
		hash = $(".menu span a").eq(index).attr("href").substring(1);     
		if(hash!=location.hash)
		{
		no_scroll = true;	    
		location.hash = hash;   
		}  
		}
	});
	
	
	/* ACCUEIL SHOWCASE */
	$('#accueil_showcase').serialScroll({
		items:'.accueil_showcase_element',
		duration:800,
		force:true,
		axis:'x',
		interval:5000
	}); 
	
	
	/* EQUIPE */
	$(".equipe_photo").mouseover(function() {
		show_equipe($(".equipe_photo").index(this));
	});          
	

	/* PORTFOLIO */    
	$(".portfolio_item").live('mouseover', function(event) {
	  $(this).find("img").fadeIn("slow");  
	});   
	$(".portfolio_item").live('mouseleave', function(event) {   
	  if(!$(this).hasClass("selected"))
	  $(this).find("img").fadeOut("slow");  
	});
	$(".portfolio_item").live('click', function() {
		show_portfolio_item($(this));
	}); 
	$("#portfolio_box_close").click(function() {
		hide_portfolio_item();
	});      
	$("#portfolio_box_prev").click(function() {
		show_portfolio_item($(".portfolio_item").eq(portfolio_index-1));
	}); 
	$("#portfolio_box_next").click(function() {
		show_portfolio_item($(".portfolio_item").eq(portfolio_index+1));
	});  
	$(".change_portfolio_page").live('click', function(event) {
		change_portfolio_page($(this).attr("key"));
	}); 
	$(".portfolio_link").click(function() {  
		no_scroll = false; 
		location.hash = '#portfolio';    
		show_item = 'portfolio';
	}); 
	$(".image_portfolio").live('click', function(event) {   
		if($(".image_portfolio").length > 1)
		{
		$(this).hide();
		if($(this).next(".image_portfolio").length)   
		$(this).next(".image_portfolio").show();
		else
		$(".image_portfolio").eq(0).show();
		update_image_bar();     
		}
	});
	
	
	/* COMMUNICATION */
	if(msg)
	alert(msg);      
	
});
