/* SCROLL PAGE */
function scrollpage(hash)
{   
	// active scroll 
	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");  
    
	// si pas animation
	if(!no_scroll)
	{  
		// trouve l'index de l'element                  
		var scroll = $(window).scrollTop();
		var index = $("a[href='#"+hash+"']").index(".menu_element a");
		var scroll_to_height = (index * body_height);
		if(hash!='entreprise')
		scroll_to_height += 70;
        		
		// scroll le body  
		if(!(scroll==0 && hash == 'entreprise'))   
		$("body").scrollTo(scroll_to_height,4000,{axis:'y', easing: "easeInOutCubic", onAfter: clear_anim });    
		else
		clear_anim();
	}
    
	// init
	init_page(hash);
	
	// desactive scroll
	if(anim)
	no_scroll = true;

	return true; 
}


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

/* FUNCTION LOAD ETAPE FABRICATION */
function load_etape_fabrication(value)
{   
	$(".fabrication_element").removeClass("selected");
	$(".fabrication_element[load="+value+"]").addClass("selected");
	$("#fabrication_right").load('/manager.php',{ action: "load_etape_fabrication", etape: value});   
}   


/* FUNCTION LOAD ETAPE PHOTO */
function load_etape_photo(value)
{                                    
	var max = $(".etape_photo_img").length;  
	if(max)
	{  
	var selected = $("#etape_photo .selected").index();   	
	if(selected!=value)
	{   
	$("#etape_photo .selected").hide().removeClass("selected"); 
	$("#etape_photo img").eq(value).fadeIn(1000).addClass("selected");
	}
	$("#etape_photo_next,#etape_photo_prev").show();      
	
	if(value==(max-1))	 
	$("#etape_photo_next").hide();
	if(value==0)  
	$("#etape_photo_prev").hide();    
	}
}                


/* FUNCTION LOAD INGREDIENT */
function load_ingredient(element)
{   

	$(".ingredient_element").filter(".selected").removeClass("selected");
	element.addClass("selected");

	var ingredient_content = $(".ingredient_element").filter(".selected").find(".ingredient_element_content").text(); 
	if(!ingredient_content)
	var ingredient_content = $(".ingredient_element").eq(0).addClass("selected").find(".ingredient_element_content").text();
	$("#ingredient_content").fadeOut(100,function() { 
		$("#ingredient_content").text(ingredient_content) }).delay(2).fadeIn(150); 
}
    

/* FUNCTION LOAD PRODUIT */
function load_produit(value)
{                         
	var index = $(".produits_element[load="+value+"]").index();
	var max = $(".produits_element").length;	         
	$(".produits_element").removeClass("selected");        
	$(".produits_category").removeClass("cat_selected"); 
	$(".produits_element[load="+value+"]").addClass("selected");      
	if($(".produits_element[load="+value+"]").hasClass("in_cat"))  
	{          
	$(".produits_element[load="+value+"]").parents(".produits_category").addClass("cat_selected");   
	}
	$(".produits_category").removeClass("open");
	$(".produits_category_container").hide();
 
	
	if($("#produit_biere").is(":visible"))
	{
	$("#produit_biere").fadeOut("slow", function() {
		 load_produit_img(value); 
	});           
	}   
	else
    load_produit_img(value);   
}   

/* FUNCTION LOAD PRODUIT IMG */
function load_produit_img(value)
{
	$("#produits_info").load('/manager.php',{
	     action: "load_produit", produit: value}, 
				function() { 
					if (jQuery.browser.msie)
					$("#produit_biere").show();
					else                                                 
					$("#produit_biere").fadeIn("slow"); 
					$("#produits_random").load('/manager.php',{
					     action: "load_random_produit", produit: value });
					}
					);	
}


/* FUNCTION LOAD DAY NAV */
function load_nouvelle(value)
{     
	$(".load_day").removeClass("selected");
	$(".load_day[load="+value+"]").addClass("selected");
	
	$("#nouvelle_container").load('/manager.php',{
	     action: "load_nouvelle", nouvelle: value});
}   


/* FUNCTION LOAD NOUVELLE */
function load_day_nav(value)
{   
   	$(".nouvelle_month_element").removeClass("selected");
	$(".nouvelle_month_element[load="+value+"]").addClass("selected");
	
	$("#nouvelles_day_nav").load('/manager.php',{
	     action: "load_day_nav", month: value}); 
}

/* FUNCTION LOAD GOOGLE MAP */
function load_google_map(lat,lng)
{
	var latlng = new google.maps.LatLng(lat,lng);
	var myOptions = {  
		scrollwheel: false,
		zoom: 12,
		center: latlng,   
		disableDefaultUI: true,      
	    zoomControl: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};    
	var map = new google.maps.Map(document.getElementById("contact_google_map"),
	myOptions);     
	var marker = new google.maps.Marker({
		position: latlng
	});
	marker.setMap(map);
}   


/* FUNCTION SHOW TOOLTIP */
function show_tooltip(element,value)
{   
	var position = element.position(); 
	$("#social_tooltip").html(value);     
	var height = $("#social_tooptip").height(); 
	$("#social_tooltip").css("left",(position.left+82)); 
	$("#social_tooltip").css("top","15px").show();     
}


/* FUNCTION SCROLLPANE */
function scrollpane(id)
{
	$('#'+id).jScrollPane({ verticalDragMinHeight: 12, verticalDragMaxHeight: 12 });
}       


/* FUNCTION INIT PAGE */
function init_page(hash)
{   
	// page jamais init
	if($("#body_"+hash).data("init") != 1)
	{
	
	if(hash=='fabrication')
	{
		load_ingredient($(".ingredient_element").eq(0));  
		load_etape_fabrication(1);	
	}  
	
	if(hash=='nous_joindre')
	{
	  	load_google_map(lat,lng);    
	} 
	
	if(hash=='produits')
	{
	 	load_produit($(".produits_element").first().attr("load"));     
	}
	
	if(hash=='nouvelles')
	{                
		if($(".nouvelle_month_element").eq(0).length)
	 	load_day_nav($(".nouvelle_month_element").eq(0).attr("load"));     
	}
	
    $("#body_"+hash).data("init",1);
	}
}


$(document).ready(function() {  

	/* FONCTION ET VALEUR */        
	clear_anim();                              
	body_height = $(".body_element").eq(0).height();


	/* COMMUNICATION */
	if(msg)
	alert(msg);

   	$("#lang_en").click(function() {
	alert("English version not yet available");  
	return false;
	});      
	
	
	/* HASH */
	$(window).bind( 'hashchange', function(){ 
		scrollpage(location.hash);     
	});
	if(!location.hash && default_hash)
	location.hash = default_hash;
	if(location.hash) 
	scrollpage(location.hash);   


	/* SCROLL */ 
	$(window).scroll(function() {   
		if(anim == false )
		{
			var hash = false;    
			var scroll = $(window).scrollTop(); 

			if(!scroll)   
			index = 0;
			else
			index = Math.floor((scroll+500)/body_height); 	
			hash = $(".menu_element a").eq(index).attr("href").substring(1);     
			if(hash!=location.hash)
			{
				no_scroll = true;	    
				location.hash = hash;   
			}  
		}
	});

	/* MENU */     
	$(".menu_element a").click(function() {
		no_scroll = false;
	}); 
             

    /* NOUVELLES */
    $(".nouvelle_month_element:not('.selected')").live('click', function() {     
		load_day_nav($(this).attr("load"));
	});
    
	$(".load_day:not('.selected')").live('click', function() {     
		load_nouvelle($(this).attr("load"));
	});     
	
	// serial scroll
	if($("#nouvelles_month_up_down").length)
	{
	$('#nouvelles_month_nav_content').serialScroll({
		items:'.nouvelle_month_element',     
		cycle: false,
		duration: 200,
		axis:'y',
		prev:'#nouvelles_month_up',
		next:'#nouvelles_month_down'
	});  
	}
	
	/* FABRICATION */

	// fabrication element 
	$(".fabrication_element:not('.selected'), #etape_nav").live('click', function() { 
		load_etape_fabrication($(this).attr("load"));
	});   

	// ingredient
	$(".ingredient_element:not('.selected')").live('mouseover', function() {       
		load_ingredient($(this));
	});    
	
	// navigation photo
	$("#etape_photo_prev").live('click', function(event) {
		if($("#etape_photo .selected").prev("img").length)
		load_etape_photo($("#etape_photo .selected").prev("img").index());
		else
		load_etape_photo(0);
	});
   	$("#etape_photo_next").live('click', function(event) {           
		if($("#etape_photo .selected").next("img").length)
		load_etape_photo($("#etape_photo .selected").next("img").index());
		else
		load_etape_photo($("#etape_photo .selected").last("img").index());
	});
    
	/* PRODUITS */    
	$(".produits_category").mouseover(function() {
		$(this).addClass("cat_open").find(".produits_category_container").show();
	});
	$(".produits_category").mouseleave(function() {
		$(this).removeClass("cat_open").find(".produits_category_container").hide();
	});   
   	$(".produits_element:not('.selected'), .produit_random_element").live('click', function() {   
		load_produit($(this).attr("load"));
	});          
	$("#produit_nav_prev").click(function() {        
		var prev = $("#produits_list .selected").prev().attr("load");
		if(prev)
		load_produit(prev);    
	});
	$("#produit_nav_next").click(function() {
		var next = $("#produits_list .selected").next().attr("load");
		if(next)
		load_produit(next); 
	});
	
	/* NOUS JOINDRE */    

	// prepare input
	$("#contact_form .form_input, #contact_form textarea").each(function(index) {
		$(this).data("default",$(this).val());
	});   

	// input focus in
	$("#contact_form .form_input, #contact_form textarea").focusin(function() {
		if($(this).data("default") == $(this).val())
		$(this).val("");
	});  

	// input focus out
	$("#contact_form .form_input, #contact_form textarea").focusout(function() {
		if(!$(this).val())
		$(this).val($(this).data("default"));
	});     

	// form submit        
	$(".form_error").click(function() {
	   $(".form_error").hide();
	});
	$("#contact_form_submit").click(function() {  
		contact_form_error = false;
		$("#contact_form .form_input, #contact_form textarea").each(function(index) {
			if($(this).hasClass("required") && $(this).data("default") == $(this).val()) 
			{
			contact_form_error = true;   
			$(this).prev(".form_error").show();
			}
		}); 

		if(contact_form_error) 
		$("#contact_status").addClass("send_error").removeClass("send_success").show().html(msg_not_sent).delay(4000).fadeOut("slow");
		else 
		{
			$.ajax({
				type: "POST",
				url: '/manager.php',
				data: $("#contact_form").serialize(),
				success: function(msg){  
				  $("#contact_status").addClass("send_success").removeClass("send_error").show().html(msg).delay(2000).fadeOut("slow"); 
				  $("#contact_form .form_input, #contact_form textarea").each(function(index) {
				    $(this).val($(this).data("default"));
				  });; 
				}
			});
		}
	});  
	
	
	// tooltip 
	$("#contact_social a").mouseenter(function() {
		show_tooltip($(this),$(this).attr("title"));
	});  
	$("#contact_social a").mouseleave(function() {
		$("#social_tooltip").hide();
	});
   
 
	/* COLORBOX */
	$("a[rel='colorbox']").colorbox({ opacity: 0.8, rel: "nofollow", height: 468, width: 708, scrolling: false });  
	$("#colorbox_close").live('click', function(event) {
		$.colorbox.close();
	});    
	$(document).bind('cbox_complete', function(){
	});
     

    /* SCROLL BAR */   
	scrollpane("entreprise_sidebar");
	scrollpane("maitre_brasseur_sidebar");                  
});
