var LT;
if (!LT) 
    LT = {};
LT.Dialog = {
    _options: {
        DivBack: 'LT_DialogDivBack',
        DivFore: 'LT_DialogDivFore',
        DivTitle: 'LT_DialogDivTitle',
        DivBody: 'LT_DialogDivBody',
        DivFoot: 'LT_DialogDivFoot',
        DivForeSize: [345, 220],
        BtnOK: 'LT_DialogBtnOK',
        BtnYES: 'LT_DialogBtnYES',
        BtnNO: 'LT_DialogBtnNO'
    },
    _inputs: [],
    Layers: {
        DivBack: null,
        DivFore: null,
        DivTitle: null,
        DivBody: null,
        DivFoot: null
    },
    Buttons: {
        BtnOK: null,
        BtnYES: null,
        BtnNO: null
    },
    Config: function(){
        for (var i = 0; i < arguments.length; i++) {
            if (typeof(arguments[i]) == "object") {
                for (var key in arguments[i]) {
                    this._options[key] = arguments[i][key];
                }
            }
        }
    },
    _Init: function(){
        for (var div in this.Layers) {
            this.Layers[div] = document.getElementById(this._options[div]);
            if (true) {
                var inputs = document.getElementsByTagName("input");
                for (var i = 0; i < inputs.length; i++) {
                    if (inputs[i].style.visibility != "hidden") 
                        this._inputs[this._inputs.length] = inputs[i];
                }
                var selects = document.getElementsByTagName("select");
                for (var i = 0; i < selects.length; i++) {
                    if (selects[i].style.visibility != "hidden") 
                        this._inputs[this._inputs.length] = selects[i];
                }
            }
            if (!this.Layers[div]) {
                this.Layers[div] = document.createElement("DIV");
                this.Layers[div].setAttribute("id", this._options[div]);
                this.Layers[div].ondragstart = function(){
                    return false;
                };
                this.Layers[div].onselectstart = function(){
                    return false;
                };
                this.Layers[div].oncontextmenu = function(){
                    return false;
                };
            }
            if (this._options[div + "Size"] instanceof Array) {
                this.Layers[div].style.width = this._options[div + "Size"][0] + "px";
                this.Layers[div].style.height = this._options[div + "Size"][1] + "px";
                this.Layers[div].size = this._options[div + "Size"];
            }
        };
        for (var btn in this.Buttons) {
            this.Buttons[btn] = document.getElementById(this._options[btn]);
            if (!this.Buttons[btn]) {
                this.Buttons[btn] = document.createElement("BUTTON");
                this.Buttons[btn].setAttribute("id", this._options[btn]);
            }
            this.Layers.DivFoot.appendChild(this.Buttons[btn]);
            this.Buttons[btn].style.display = "none";
        };
        with (this.Layers) {
            DivBack.onclick = null;
            DivBack.style.display = "none";
            DivFore.style.display = "none";
            DivFore.appendChild(DivTitle);
            DivFore.appendChild(DivBody);
            DivFore.appendChild(DivFoot);
            DivBack.appendChild(DivFore);
            document.body.appendChild(DivFore);
            document.body.appendChild(DivBack);
        };
            },
    Alert: function(title, body, size, label, callback){
        var _handle = this;
        if (size instanceof Array) 
            this.Config({
                DivForeSize: size
            });
        this._Init();
        this.Layers.DivTitle.innerHTML = title;
        this.Layers.DivBody.innerHTML = body;
        this.Buttons.BtnOK.innerHTML = "<span>" + (label || "OK") + "</span>";
        this.Buttons.BtnOK.onclick = function(){
            if (typeof callback == "function") 
                callback();
            _handle.Close();
        };
        this.Buttons.BtnOK.style.display = "block";
        this.Open();
    },
    Confirm: function(title, body, size, btnY, btnN, callback){
        var _handle = this;
        if (size instanceof Array) 
            this.Config({
                DivForeSize: size
            });
        this._Init();
        this.Layers.DivTitle.innerHTML = title;
        this.Layers.DivBody.innerHTML = body;
        this.Buttons.BtnYES.innerHTML = "<span>" + (btnY || "YES") + "</span>";
        this.Buttons.BtnNO.innerHTML = "<span>" + (btnN || "NO") + "</span>";
        this.Buttons.BtnYES.onclick = function(){
            if (typeof callback == "function") 
                callback(true);
            _handle.Close();
        };
        this.Buttons.BtnNO.onclick = function(){
            if (typeof callback == "function") 
                callback(false);
            _handle.Close();
        };
        this.Buttons.BtnYES.style.display = "block";
        this.Buttons.BtnNO.style.display = "block";
        this.Open();
    },
    Show: function(title, body, size, hide){
        var _handle = this;
        if (size instanceof Array) 
            this.Config({
                DivForeSize: size
            });
        this._Init();
        
        this.Layers.DivTitle.innerHTML = title;
        this.Layers.DivBody.innerHTML = body;
        if (hide) {
            _handle.Layers.DivBack.onclick = function(){
                _handle.Close();
            }
        };
        this.Open();
    },
    Open: function(){
        var _handle = this;
        this.Resize.apply(_handle);
        this.Layers.DivBack.style.display = "";
        this.Layers.DivFore.style.display = "";
        for (var o in this._inputs) {
            this._inputs[o].style.visibility = "hidden";
        }
        if (window.attachEvent) 
            window.attachEvent("onresize", function(){
                _handle.Resize.apply(_handle);
            });
        else 
            window.addEventListener("resize", function(){
                _handle.Resize.apply(_handle);
            }, false);
    },
    Close: function(){
        var _handle = this;
        var divList = ['DivBack', 'DivFore'];
        for (var div in divList) {
            this.Layers[divList[div]].style.display = "none";
            this.Layers[divList[div]].onclick = null;
        };
        for (var btn in this.Buttons) {
            this.Buttons[btn].style.display = "none";
            this.Buttons[btn].onclick = null;
        };
        for (var o in this._inputs) {
            this._inputs[o].style.visibility = "visible";
        }
        if (window.detachEvent) 
            window.detachEvent("onresize", function(){
                _handle.Resize.apply(_handle);
            });
        else 
            window.removeEventListener("resize", function(){
                _handle.Resize.apply(_handle);
            }, false);
    },
    Resize: function(){
        var sw = parseFloat(document.documentElement.scrollWidth);
        var cw = parseFloat(document.documentElement.clientWidth);
        var sh = parseFloat(document.documentElement.scrollHeight);
        var ch = parseFloat(document.documentElement.clientHeight);
        this.Layers.DivBack.style.width = (sw >= cw ? sw : cw) + "px";
        this.Layers.DivBack.style.height = (sh >= ch ? sh : ch) + "px";
        this.Layers.DivFore.style.left = parseFloat(document.documentElement.scrollLeft) + cw / 2 - parseFloat(this.Layers.DivFore.size[0]) / 2 + "px";
        this.Layers.DivFore.style.top = parseFloat(document.documentElement.scrollTop) + ch / 2 - parseFloat(this.Layers.DivFore.size[1]) / 2 + "px";
    }
};
