var elt_panneau = $("#panneau_gauche");
var str_fixed = "#menu_navigLiensRevue__DISABLE";

//-------------------------------------------------------------------
//                          DEPLIABLES
//-------------------------------------------------------------------
var arr_depliable = [ "menu_Compte", "menu_recherche" ];

// On charge l'état de dépliement des menus dépliable des cookies

for( i = 0; i < arr_depliable.length; i++ )
{
	// Récupère l'elt pliable
	var elt_plieur = $("#" + arr_depliable[i] + " .js_plieur" );
	if (elt_plieur.length <= 0)
		continue;
	
	// On assigne un callback sur click
	elt_plieur.click( js_pliable__click );
	
	// On déplie ceux qui doivent l'être
	var val = Cookie__Get( "pliable_state_" + arr_depliable[i] );
	if (val == "1")
	{
	    var elt_pliable = elt_plieur.parent().find(".js_pliable");
	    if (elt_pliable.length > 0)
	    {
	        js_pliable__hide(elt_pliable, 0);
	    }
	}
}

function js_pliable__hide( elt_pliable, anim_delay )
{
	elt_pliable.stop(); // stop animation
	if( anim_delay > 0 )
		elt_pliable.slideUp( anim_delay, Update_Elt_Fixed_Init_Offset );
	else
		elt_pliable.slideUp( anim_delay, function(){});
}

// En cas de click sur un pliable persistant du menu
function js_pliable__click()
{
	var elt_plieur = $(this);
	
	var elt_pliable = elt_plieur.parent().find( ".js_pliable" )
	if (elt_pliable.length <= 0)
		return;
	
	if( elt_pliable.css( "display" ) == "none" )
	{
		elt_pliable.stop(); // stop animation
		elt_pliable.slideDown( 400, Update_Elt_Fixed_Init_Offset );
		
		var cookie_name = "pliable_state_" + elt_plieur.parent().attr('id');
		Cookie__Remove(cookie_name);
	}
	else
	{
		js_pliable__hide( elt_pliable, 400 );
		var cookie_name = "pliable_state_" + elt_plieur.parent().attr('id');
		Cookie__Add( cookie_name, "1" );
	}
}

//-------------------------------------------------------------------
//                          FIXED
//-------------------------------------------------------------------
function Update_Elt_Fixed_Init_Offset(elt_fixed) 
{
	elt_fixed.attr( "init_offset", elt_fixed.offset().top );
}

var elt_fixed = $(str_fixed);
if( elt_fixed.size() > 0 )
{
	Update_Elt_Fixed_Init_Offset(elt_fixed);
	elt_fixed.attr( "init_margin", elt_fixed.css("margin-top") );

	$(window).scroll( elt_fixed__motion );
	$(window).resize( elt_fixed__motion );
}

function elt_fixed__motion( )
{
	var min_margin = 40;
	
	var elt_fixed = $(str_fixed);
	elt_fixed.stop(); // stop animation
	
	var init_offset = parseInt( elt_fixed.attr( "init_offset" ) );
	var init_margin = parseInt( elt_fixed.attr( "init_margin" ) );
	
	var curr_offset = $(window).scrollTop();
	var new_margin = curr_offset + min_margin - init_offset;
	if( (new_margin + init_offset - curr_offset ) < min_margin )
		new_margin = (curr_offset + min_margin) - init_offset;
	if( new_margin < init_margin )
		new_margin = init_margin;
	elt_fixed.animate( {"margin-top" : new_margin }, 500, function(){} );
}

