/*
	Gestoin du flip up des colonnes
*/

var pvLibrary = {};

/*
	Filtres
*/

pvLibrary.filter =
{
	advancedSearch : function()
	{
		$("form#filterForm .advancedSearch").show();
		
		// on effase le résumé
		$("#filterEncart .resume").html("");
		
		// on change la classe du père
		$("#filterEncart").toggleClass("open", true);
		$("#rechercheSimple").toggle(true);
		$("#rechercheAvance").toggle(false);
	},
	
	simpleSearch : function()
	{
		$("form#filterForm .advancedSearch").hide();
		
		// on affiche le résumé
		var resume = "";
		$("#filterForm").find("div.classements").find("input[type=checkbox][checked]").each
		(
			function()
			{
				resume += (resume.length > 0 ? ", " : "") + $(this).parent().next("label").html();
			}
		);
		$("#filterForm").find("div.sousEncart").not("div.classements").find("input[type=checkbox][checked]").each
		(
			function()
			{
				resume += (resume.length > 0 ? ", " : "") + $(this).next("label").html();
			}
		);
		
		//$("#filterEncart .resume").html(resume);
		
		// on change la classe du père
		$("#filterEncart").toggleClass("open", false);
		$("#rechercheSimple").toggle(false);
		$("#rechercheAvance").toggle(true);
	},

	selectAll : function(name)
	{
		$("form#filterForm input[name=" + name + "]").attr("checked", "checked");
	},
	
	unselectAll : function(name)
	{
		$("form#filterForm input[name=" + name + "]").attr("checked", "");
	},
	
	inverseSelection : function(name)
	{
		$("form#filterForm input[name=" + name + "]").each
		(
			function()
			{
				this.checked = !this.checked;
			}
		);
	},

	submit : function()
	{
		$("form#filterForm").submit();
	},
	
	goToPage : function(page)
	{
		$("form#filterForm input[name=page]").val(page);
		this.submit();
	},
	
	selectAllClassements : function(famille)
	{
		$("form#filterForm input[name=classement].famille-" + famille).attr("checked", "checked");
	},
	
	unselectAllClassements : function(famille)
	{
		$("form#filterForm input[name=classement].famille-" + famille).attr("checked", "");
	},
	
	inverseSelectionClassements : function(famille)
	{
		$("form#filterForm input[name=classement].famille-" + famille).each
		(
			function()
			{
				this.checked = !this.checked;
			}
		);
	}
};

/*
	Colonnes
*/


pvLibrary.columns = 
{
	// called in the context of checkbox
	onCheckBoxChanged : function()
	{
		pvLibrary.columns.toggle(this.checked, this.value);
	},
	
	toggle : function(show, name)
	{
		var display = $.browser.msie ? "block" : "table-cell";
		$("table.result td." + name).css("display", show ? display : "none");
	},
	
	toggleAll : function(show)
	{
		$("div.encart.show input[name=show][value!=all]").attr("checked", show ? "checked" : "").each(this.onCheckBoxChanged);
	}
};


$(document).ready
(
	function()
	{
		/*
			Gestion des colonnes a afficher
		*/
	
		$("div.encart.show input[name=show]").each
		(
			function()
			{
				$(this).click(pvLibrary.columns.onCheckBoxChanged).filter("[value!=all]").each(pvLibrary.columns.onCheckBoxChanged);
			}
		);
		
		/*
			Gestion des filtres
		*/
		
		$("#rechercheAvance a").click
		(
			function()
			{
				pvLibrary.filter.advancedSearch();
			}
		);
		
		$("#rechercheSimple a").click
		(
			function()
			{
				pvLibrary.filter.simpleSearch();
			}
		)
		.click();
	}
);

/*
	Overlay
*/

pvLibrary.overlay =
{
    open : function(miroitier, page)
    {
    	this.show();
    
    	$("#overlayContent").html("Chargement ...");
    	$("#overlayContent").load
		(
		    "overlay", {miroitier:miroitier, page:page}
		);
    },

    close : function()
    {
		this.hide();
    },
    
    submit : function()
    {
    	this.hide();
    	$("#overlayContent form").submit();
		location.reload(); 
    },

    show  : function()
    {
		if ( $("#overlayBackground").is(":visible") )
		{
		    return;
		}
	
		var b = $("body");
		var d = $("html");
	
		// on enregistre tout d'abord l'état du body et du html
		this.bodyCSS	    = this._saveCSS(b);
		this.documentScroll = d.scrollTop();
		this.documentCSS    = this._saveCSS(d);
	
		// on initialise le body et le html
		d.add(b).css
		({
		    margin	: 0,
		    padding	: 0,
		    width	: "100%",
		    height	: "100%"
		});
	
		$("#overlayBackground").css("opacity", 1).add("#overlayContainer").fadeIn(500);
		this._center();
    },

    hide : function()
    {
		if ( !$("#overlayBackground").is(":visible") )
		{
		    return;
		}
	
		this._restoreCSS($("body"), this.bodyCSS);
		this._restoreCSS($("html"), this.documentCSS);
	
		$("#overlayBackground").css("opacity", 1).add("#overlayContainer").fadeOut(500);
    },

    _center : function()
    {
		if ( !$("#overlayBackground").is(":visible") )
		{
		    return;
		}
	
		$("#overlay").css("margin-top", Math.max(10, $("html").height()/2 - $("#overlay").outerHeight()/2 + $("html").scrollTop()));
    },
    
    _adjustHeight : function()
    {
		$("#overlayContent").css("visibility", "hidden");
	
		var _this = this;	
	
		$("#overlayContent").animate
		(
		    {
				height : $("#overlayContent").get(0).scrollHeight		    
		    },

		    {
				step : function()
				{
					_this._center();
				},
				
				complete : function()
				{
				    $("#overlayContent").css("visibility", "visible").css("overflow", "auto");
				    $("#overlayBackground").css("opacity", 0.9);
				}
		    }
		);
    },

    _saveCSS : function(e)
    {
		var p = {};
		p.width		= e.css("width");
		p.height	= e.css("height");
		p.margin	= e.css("margin");
		//p.padding	= e.css("padding");
		return p;
    },

    _restoreCSS : function(e, c)
    {
		e.css(c);
    }
};

$.ajaxSetup({cache:false});
