﻿function JsDialog() {
    this.initialize();
};

JsDialog.create = function() {
    var obj = this["prototype"];
    
    obj["initialize"] = function() {
        this["url"] = "";
        this["argument"] = null;
        this["dialog"] = document.createElement("<div style='z-index:999;border:1px solid #2d5099; position:absolute; display:none;'>");
        this["dialog"]["innerHTML"] = "<table cellspacing='0' cellpadding='0' border='0' style='width:100%; height:100%'><tr><td style='height:21;'></td></tr><tr><td></td></tr></table>";
        
        this["titlediv"] = document.createElement("<div style='border-top:1px solid #719bc5; height:100%; border-left:1px solid #719bc5; border-right:1px solid #719bc5;'>");
        //this["titlediv"]["innerHTML"] = "<div style='overflow:hidden;height:1;border-top:1px solid #b0988c;border-bottom:1px solid #9c7e6f'></div>";
        this["dialog"]["firstChild"]["rows"][0]["cells"][0].appendChild(this["titlediv"]);
        this["title"] = document.createElement("<div style=\"height:21; border-bottom:1px solid #7095c0;background-color:#6283b6;filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#5473ae', EndColorStr='#7094c0')\">");
        this["titlediv"].appendChild(this["title"]);
        this["title"]["innerHTML"] = "<table cellspacing='0' style='width:100%; height:100%; cursor:default;' cellpadding='0' border='0'><tr><td>&nbsp;</td><td style='width:21;' title='关闭窗口'><img src='/Images/dialog_close.gif'></td></tr></table>";
        var div = document.createElement("<div style='border:3px solid #7094bf; border-top:1px; overflow:hidden; background-color:#ffffff; width:100%; height:100%;'>");
        this["contentdiv"] = document.createElement("<div style='border:1px solid #30499e; width:100%; height:100%; overflow:auto;'>");
        div.appendChild(this["contentdiv"]);
        this["dialog"]["firstChild"]["rows"][1]["cells"][0].appendChild(div);
        document["body"].appendChild(this["dialog"]);
        
        this["contentdiv"]["innerHTML"] = "<table style='width:100%; height:100%;' cellspacing='0' cellpadding='0' border='0'><tr><td style='height:100%;' style='text-align:center;'><img src='/Image/Loading.gif'></td></tr><tr><td style='height:100%;'></td></tr></table>";
        var table = this["contentdiv"]["firstChild"];
        this["loading"] = table["rows"][0]["cells"][0];
        this["frame"] = table["rows"][1]["cells"][0];
        
        this.bindEvent();
    };
    
    obj["setUrl"] = function(url) {
        this["url"] = url;
    };
    
    obj["setCloseDisplay"] = function(display) {
        this["title"]["all"]["tags"]("IMG")[0].style.display = display;
    };
    
    obj["bindEvent"] = function() {
        var dialog = this["dialog"];
        var self = this;
        this["titlediv"]["onmousedown"] = function() {
            dialog["xOffset"] = event["clientX"] - parseInt(dialog["style"]["left"]);
            dialog["yOffset"] = event["clientY"] - parseInt(dialog["style"]["top"]);
            
            document["onselectstart"] = function() { return false; }
            document["onmousemove"] = function() {
                dialog["style"]["left"] = event["clientX"] - dialog["xOffset"];
                dialog["style"]["top"] = event["clientY"] - dialog["yOffset"];
            };
            document["onmouseup"] = function() {
                document["onselectstart"] = null;
                document["onmousemove"] = null;
                document["onmouseup"] = null;
            };            
        };
        
        this["title"]["all"]["tags"]("IMG")[0]["onclick"] = function() {
            self.close();
        };
    };
    
    obj["close"] = function() {
        this["dialog"]["style"]["display"] = "none";
        document["pst"]["style"]["display"] = "none";
        document["body"]["style"]["overFlow"] = this["oldoverflow"];
        if (typeof(this["onclose"]) == "function") {
            this["onclose"].call();
        };
    };
    
    obj["createPst"] = function() {
        var div = document.createElement("<div style='z-index:900; position:absolute; left:0; top:0;display:none;width:100%; height:100%; background-color:#d2e1f0;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=60)'>");
        document["body"].appendChild(div);
        document["pst"] = div;
    };
    
    obj.createFrame = function() {
        this["frame"]["innerHTML"] = "";
        var frame = document.createElement("<iframe onload='_DA_Loaded(this)'  style='width:100%; height:100%; border:0px; margin-top:0px;' frameborder='0'>");
        frame["dialog"] = this;
        frame["src"] = this["url"];
          
        this["frame"].appendChild(frame);
    };
    
    obj["setTitle"] = function(title) {
        if (this["titletext"] == null) {
            this["titletext"] = document.createElement("<div style='position:relative; font-size:9pt;color:white;padding:2 0 0 4;'>");
            var cell = this["title"]["firstChild"]["rows"][0]["cells"][0];
            cell["innerHTML"] = "";
            cell.appendChild(this["titletext"]);
        };
        
        this["titletext"]["innerHTML"] = title;
    };
    
    obj["onload"] = function() {
        this["loading"]["parentNode"]["style"]["display"] = "none";
    };
    
    obj["busy"] = function() {
        this["loading"]["parentNode"]["style"]["display"] = "block";
    };
    
    obj["show"] = function(width, height) {        
        if (document["pst"] == null) {
            this.createPst();
        };
        this["oldoverflow"] = document["body"]["style"]["overflow"];
        var top = (document["body"]["clientHeight"] - height) / 2;
        top = top - (top / 2);
        var left = (document["body"]["clientWidth"] - width) / 2;
        this["dialog"]["style"]["left"] = left;
        this["dialog"]["style"]["top"] = top;
        this["dialog"]["style"]["width"] = width;
        this["dialog"]["style"]["height"] = height;
        this["loading"]["style"]["display"] = "block";
        this["dialog"]["style"]["display"] = "block";
        document["body"]["style"]["overflow"] = "hidden";
        
        document["pst"]["style"]["display"] = "block";
        this.createFrame();
        this["onclose"] = null;
    };
};

JsDialog.create();

function _DA_Loaded(e) {
    e["dialog"].onload();
};

var __dialog_ = null;
JsDialog.Show = function(title, url, width, height) {
    if (!__dialog_) {
        __dialog_ = new JsDialog();        
    };
    __dialog_.setTitle(title);
    __dialog_.setUrl(url);
    __dialog_.show(width, height);
};
JsDialog.Close = JsDialog.Hide = function() {
    if (__dialog_) {
        __dialog_.close();
    };
};
JsDialog.argument = function(arg) {
    if (__dialog_) {
        __dialog_ = new JsDialog(); 
        __dialog_["argument"] = arg;
    };
};
JsDialog.setArgument = function(arg) {
    if (!__dialog_) {
        __dialog_ = new JsDialog();
    };
    if (__dialog_) {        
        __dialog_["argument"] = arg;
    };
};
JsDialog.getArgument = function() {
    if (__dialog_) {
        return __dialog_["argument"];
    };
    return null;
};
JsDialog.setCloseDisplay = function(display) {
    if (!__dialog_) {
        __dialog_ = new JsDialog();
    };
    if (__dialog_) {
        __dialog_.setCloseDisplay(display);
    };
};
JsDialog.setPosition = function(x,y) {
    if (__dialog_) {
        __dialog_["dialog"]["style"]["left"] = x;
        __dialog_["dialog"]["style"]["top"] = y;
        __dialog_["dialog"]["style"]["zoom"] = 1;
    };
     
};

