LiquidToolbox = {

    oOriginalOffset : { left : 0, top: 0 },
    oCurrentBodyDimensions : { width : 0, height : 0 },
    loadUris : [],

    show : function(strToolboxID, bModalWindow, bSetCenteret, strLoadUri, strActionValue)
    {
        // Create a filler to make the toolbox modal
        if(bModalWindow)
        {
            this.createModalFiller(strToolboxID);
        }

        // Move the toolbox to the bottom of the document (DOM wise)
        $(strToolboxID).style.display = "block";

        if(Prototype.Browser.IE)
        {
            document.body.insertAdjacentElement("beforeEnd", $(strToolboxID));
        }
        else
        {
            document.body.insert( { bottom: $(strToolboxID) } );
        }

        // Remember the offsets before we center the toolbox
        this.oOriginalOffset.left = $(strToolboxID).viewportOffset().left;
        this.oOriginalOffset.top = $(strToolboxID).viewportOffset().top;

        // Should be center the toolbox to the viewport?
        if(bSetCenteret)
        {
            // Figure out how many pixels it takes to positionate the Toolbox at the center of the screen
            var iLeft = (document.viewport.getDimensions().width * 0.5);
            var iTop = (document.viewport.getDimensions().height * 0.5);

            // Now, take 50% of the toolbox' dimensions, and substract that from the calculated left variable.
            iLeft -= ($(strToolboxID).getDimensions().width * 0.5);
            iTop -= ($(strToolboxID).getDimensions().height * 0.5);

            $(strToolboxID).style.left = parseInt(iLeft, 10) + "px";
            $(strToolboxID).style.top = parseInt(iTop, 10) + "px";
        }

        // Check if the content should be loaded by an ajax request
        if(strLoadUri != null)
        {
            var oParam = { }
            oParam.postBody = strActionValue;
            oParam.evalScripts = true;

            new Ajax.Updater($(strToolboxID + "_Content"), strLoadUri, oParam);
        }

        return;
    },
    close : function(strToolboxID)
    {
        // Remove the filler, if there is any
        if($("liquidToolboxFiller") != null)
        {
            $("liquidToolboxFiller").parentNode.removeChild($("liquidToolboxFiller"));
        }

        // Rest the toolbox' position
        $(strToolboxID).style.left = "0px";
        $(strToolboxID).style.top = "0px";

        // Hide the toolbox
        $(strToolboxID).style.display = "none";

        return;
    },
    createModalFiller : function(strToolboxID)
    {
        // Create the filler
        oFiller = document.createElement("div");
        oFiller.id                      = "liquidToolboxFiller";
        oFiller.className               = "LiquidToolbox_ModalFiller";
        oFiller.style.zIndex            = document.body.childNodes.length;
        oFiller.style.top               = "0px";
        oFiller.style.left              = "0px";
        oFiller.style.height            = LiquidToolbox.getFillerDimension().height;
        oFiller.style.width             = LiquidToolbox.getFillerDimension().width;
        oFiller.innerHTML               = "&nbsp;";

        // Now, insert the filler as an adjacent element right, as the first node after the body tag.
        // Notice, that the Element.insert prototype method as a bug in IE6 and IE7. Therfore, we use IE's own insertAdjacentElement method.
        if(Prototype.Browser.IE)
        {
            document.body.insertAdjacentElement("beforeEnd", oFiller);
        }
        else
        {
            document.body.insert( { bottom: oFiller } );
        }

        // If any strToolboxID is proviced. Set the zIndex of this element, to the same as the fillers + 1
        if(strToolboxID != null)
        {
            $(strToolboxID).style.zIndex = oFiller.style.zIndex + 1;
        }

        // Append a onresize event
        fResizeFiller = function()
        {
            oFiller.style.height = LiquidToolbox.getFillerDimension().height;
            oFiller.style.width  = LiquidToolbox.getFillerDimension().width;

            return;
        }
        JoeDex.EventHandler.addEvent("window.onresize", fResizeFiller);

        return;
    },
    getFillerDimension : function()
    {
        iBodyHeight = JoeDex.Base.getBodyDimensions().height;
        iViewportHeight = document.viewport.getHeight();
        iViewportWidth  = document.viewport.getWidth();

        if(iBodyHeight < iViewportHeight)
        {
            iBodyHeight = iViewportHeight;
        }

        return { height: iBodyHeight + "px", width: iViewportWidth  + "px" }
    },
    startMove : function(strID, e)
    {
        // Get the event position the called this method
        oPos = JoeDex.Base.getEventPosition(e);

        // Save the current body dimensions
        this.oCurrentBodyDimensions = JoeDex.Base.getBodyDimensions();

        // Figure out, how many pixels we are "inside" the toolbox
        var iX = oPos.x - $(strID).cumulativeOffset().left;
        var iY = oPos.y - $(strID).cumulativeOffset().top;

        fMoveToolbox = function(e)
        {
            var oPos = JoeDex.Base.getEventPosition(e);

            // Calculate the next position of the toolbox
            var iPosX = (oPos.x - iX);
            var iPosY = (oPos.y - iY);

            $(strID).style.left = iPosX + "px";
            $(strID).style.top = iPosY + "px";

            // Remember the current positions... If the move aint possible, we reset it
            /*
            iLeft = $(strID).style.left;
            iTop = $(strID).style.top;

            $(strID).style.left = iPosX + "px";
            $(strID).style.top = iPosY + "px";

            if($(strID).cumulativeOffset().left < 0) $(strID).style.left = iLeft;
            if($(strID).cumulativeOffset().top < 0) $(strID).style.top = iTop;
            if($(strID).cumulativeOffset().left + $(strID).getDimensions().width + 2 > LiquidToolbox.oCurrentBodyDimensions.width) $(strID).style.left = iLeft;
            if($(strID).cumulativeOffset().top + $(strID).getDimensions().height > LiquidToolbox.oCurrentBodyDimensions.height) $(strID).style.top = iTop;
            */

            return false;
        }

        fStopMove = function()
        {
            document.onmousemove = null;

            return;
        }

        document.onmousemove = fMoveToolbox;
        JoeDex.EventHandler.addEvent("document.onmouseup", fStopMove);

        return false;
    }

}