﻿var WindowManager =
{
    wnds: [],

    addWindow: function(wnd)
    {
        this.wnds.push(wnd);
    },

    hideAll: function()
    {
        for (var i = 0; i < this.wnds.length; i++)
        {
            this.wnds[i].hide();
        }
    }
};

var Window = Class.create({
    PADDING_X: 10,
    PADDING_Y: 10,

    initialize: function(id)
    {
        this.wnd = $(id);

        if (!this.wnd)
        {
            alert("Vindue ikke fundet: id = " + id);
            return;
        }

        this.content = this.wnd.select(".wndcontent")[0];

        if (this.wnd.hasClassName("closeable"))
        {
            var closebtn = document.createElement("img");
            closebtn.alt = "Luk";
            closebtn.src = "images/buttons/button-close.gif";
            closebtn.className = "wndclosebtn";
            Event.observe(closebtn, "click", this.closeBtnClick.bindAsEventListener(this));
            this.wnd.appendChild(closebtn);
        }

        WindowManager.addWindow(this);
    },

    getContent: function()
    {
        return this.content.innerHTML;
    },

    setContent: function(html)
    {
        this.content.innerHTML = html;
    },

    setText: function(text)
    {
        this.setContent("<p>" + text + "</p>");
    },

    closeBtnClick: function(e)
    {
        this.hide();
    },

    show: function(options)
    {
        options = options || {};

        if (!options.keepOthers)
        {
            WindowManager.hideAll();
        }

        if (options.offsetEle)
        {
            this.wnd.clonePosition(options.offsetEle, { setWidth: false, setHeight: false, offsetLeft: options.x || 0, offsetTop: options.y || 0 });
        }
        else
        {
            if (options.x)
            {
                this.wnd.style.left = options.x + "px";
            }

            if (options.y)
            {
                this.wnd.style.top = options.y + "px";
            }
        }

        // Make sure window is within page bounds:
        var x = parseInt(this.wnd.getStyle("left"));
        var y = parseInt(this.wnd.getStyle("top"));

        var maxLeft = $("page").getWidth() - this.wnd.getWidth() - this.PADDING_X;
        var maxTop = $("page").getHeight() - this.wnd.getHeight() - this.PADDING_Y;

        if (x > maxLeft)
        {
            this.wnd.setStyle({ left: maxLeft + "px" });
        }

        if (y > maxTop)
        {
            this.wnd.setStyle({ top: maxTop + "px" });
        }

        this.wnd.addClassName("wndvisible");

        this.modal = options.modal;
        if (options.modal)
        {
            $("modaloverlay").style.display = "block";
        }

        if (options.focus)
        {
            var inputs = this.wnd.select("input");

            if (inputs.length > 0)
            {
                inputs[0].focus();
            }
        }
    },

    hide: function()
    {
        this.wnd.removeClassName("wndvisible");
        if (this.modal)
        {
            $("modaloverlay").style.display = "none";
        }
    }
});

