vanilla.namespace("campagnepublique.util");

/*
    Overlay
*/

campagnepublique.util.overlay =
{
    openURL : function(url, parameters, callback)
    {
	this.show();
	var self = this;

	$("#overlayContent").load
	(
	    url, parameters, 
	    function()
	    {
		self._bindClose();

		// on appelle la callback
		if ( callback )
		{
		    callback($("#overlayContent"));
		}

		$("#overlayContent").css("visibility", "hidden");
		self._adjustSize();
	    }
	);
    },

    open : function(content, callback)
    {
	this.show();

	$("#overlayContent").empty().append($(content));

	this._bindClose();
	if ( callback )
	{
	    callback($("#overlayContent"));
	}

	$("#overlayContent").css("visibility", "hidden");
	this._adjustSize();
    },

    close : function()
    {
	this.hide();
    },

    show  : function()
    {
	var self = this;

	var b = $("body");
	var d = $("html");
	var h = $("head");

	if ( $("#overlayBackground").length <= 0 )
	{
	    $.create("div", {id:"overlayBackground"}).hide().appendTo(b)
	    .click
	    (
		function()
		{
		    self.close();
		}
	    );


	    $.create
	    (
	        "div", {id:"overlayContainer"},
	        $.create
	        (
	            "div", {id:"overlay"},
	            $.create("div", {id:"overlayContent"})
	        )
	    )
	    .hide().appendTo(b).click
	    (
		function()
		{
		    self.close();
		}
	    );
	}
	else if ( $("#overlayBackground").is(":visible") )
	{
	    return;
	}

	if ( vanilla.isBoggyIE )
	{
	    $("#overlayBackground, #overlayContainer").width(b.width()).height(b.height());
	}

	$("#overlayBackground").css("opacity", 0).show().animate({opacity:0.7});
	$("#overlayContainer").show();
	$("#overlayContent").height(50).css("overflow", "hidden");
	$("#overlay").width(50).addClass("loading").css("overflow", "hidden");

	/*
	   On cache les select pour IE
	*/
	
	if ( vanilla.isBoggyIE )
	{
	    this.selects = $("#overlayBackground").prevAll().find("select:visible").hide();
	}

	this._center();
    },

    hide : function()
    {
	if ( !$("#overlayBackground").is(":visible") )
	{
	    return;
	}

	$("#overlayContent").empty();
	$("#overlayContainer").hide();
	$("#overlayBackground").fadeOut
	(
	    500,
	    function()
	    {
		// pour IE6 ....
		$(this).css("display", "none");
	    }
	);

	if ( vanilla.isBoggyIE && this.selects )
	{
	    this.selects.show();
	    this.selects = null
	}

	//$(document).scrollTop(this.documentScroll);
    },

    _adjustSize : function()
    {
	var self = this;

	$("#overlayContainer")
	.wait
	(
	    0, 
	    function()
	    {
		var o = $("#overlay");
		var ow = o.width(); 
		
		// pour IE7 on doit passer en width auto pour la mesurer
		o.width("auto");

		var c = $("#overlayContent");
		var w = c.css("position", "absolute").width();
		var h = c.height("auto").height();

		// on la restore avant l'animation
		o.width(ow);

		c.css("position", "static").height(50);

		o.removeClass("loading").animate
		(
		    {
			width : w
		    },
		    {
			complete : function()
			{
			    $(this).css("overflow", "visible");
			}
		    }
		);

		c.animate
		(
		    {
			height : h
		    },
		    {
			step : function()
			{
			   self._center();
			}, 

			complete : function()
			{
			    c.css("visibility", "visible").css("overflow", "visible");
			}
		    }
		);
	    }
	);
    },

    _center : function()
    {
	if ( !$("#overlayBackground").is(":visible") )
	{
	    return;
	}

	var scrollTop	= $(document).scrollTop();
	var viewHeight	= $(window).height();
	var height	= $("#overlay").outerHeight();

	$("#overlay").css("top", Math.max(10, viewHeight/2 - height/2 + scrollTop));
    },

    _bindClose : function()
    {
	// on bind la fermeture
	$("#overlayContent a.close").click
	(
	    function(e)
	    {
		e.preventDefault();
		e.stopPropagation();

		campagnepublique.util.overlay.close();
	    }
	);
    }
};

