﻿
var globalZIndex = 1001; // 对话框层次
var nonepng = (/(msie)\s*(\d+(\.\d+)?)/i).exec(navigator.userAgent) && parseFloat(RegExp.$2) < 7; // 不能显示png
var imagePath = "/images/dialog/"; // 图片路径

function addEventHandler(target, type, func) {
    if (target.addEventListener)
        target.addEventListener(type, func, false);
    else if (target.attachEvent)
        target.attachEvent("on" + type, func);
    else target["on" + type] = func;
}

function currentStyle(element) {
    return element.currentStyle || document.defaultView.getComputedStyle(element, null);
}

function removeEventHandler(target, type, func) {
    if (target.removeEventListener)
        target.removeEventListener(type, func, false);
    else if (target.detachEvent)
        target.detachEvent("on" + type, func);
    else delete target["on" + type];
}

function DragEngine(target, drag, bounds) {
    this.target = target;
    this.drag = drag || traget;
    this.drag.style.left = target.style.left;
    this.drag.style.top = target.style.top;
    this.drag.style.position = "absolute";
    this.downPoint = {}; // 鼠标按下的坐标
    this.offset = {
        x: parseInt(currentStyle(this.drag).marginLeft) || 0,
        y: parseInt(currentStyle(this.drag).marginTop) || 0
    };

    this.bounds = bounds || { left: 0, top: 0, right: -1, bottom: -1 };
    var dragEngine = this;

    this.documentMousemove = function(e) {
        if (window.getSelection)
            getSelection().removeAllRanges();
        else if (document.selection && document.selection.empty)
            document.selection.empty();
        var left = e.clientX - dragEngine.downPoint.x + dragEngine.offset.x;
        var top = e.clientY - dragEngine.downPoint.y + dragEngine.offset.y;
        if (parseInt(dragEngine.bounds.right) >= 0)
            left = Math.min(left, dragEngine.bounds.right - dragEngine.drag.offsetWidth);
        if (parseInt(dragEngine.bounds.bottom) >= 0)
            top = Math.min(top, dragEngine.bounds.bottom - dragEngine.drag.offsetHeight);
        if (parseInt(dragEngine.bounds.left) >= 0)
            left = Math.max(left, dragEngine.bounds.left);
        if (parseInt(dragEngine.bounds.top) >= 0)
            top = Math.max(top, dragEngine.bounds.top);

        var w = currentDialog.dx - parseInt(currentDialog.div_dialog.style.width), h = currentDialog.dy - parseInt(currentDialog.div_dialog.style.height);
        if (top > h) top = h;
        if (left > w) left = w;

        dragEngine.drag.style.left = left + "px";
        dragEngine.drag.style.top = top + "px";
        if (typeof dragEngine.onmove == "function") dragEngine.onmove(dragEngine);
    };
    this.documentMouseup = function(e) {
        var iframes = dragEngine.drag.getElementsByTagName("iframe");
        for (var i = 0; i < iframes.length; i++) {
            iframes[i].style.display = "";
        }
        removeEventHandler(document, "mousemove", dragEngine.documentMousemove);
        removeEventHandler(document, "mouseup", dragEngine.documentMouseup);
        removeEventHandler(dragEngine.target, "losecapture", dragEngine.documentMouseup);
        if (dragEngine.target.releaseCapture) dragEngine.target.releaseCapture();
        removeEventHandler(window, "blur", dragEngine.documentMouseup);
        if (typeof dragEngine.onstop == "function") dragEngine.onstop(dragEngine);
    };
    this.targetMousedown = function(e) {
        if (!e.which && e.button)
            e.which = e.button & 1 ? 1 : (e.button & 2 ? 3 : (e.button & 4 ? 2 : 0));
        if (e.which != 1) return;
        var iframes = dragEngine.drag.getElementsByTagName("iframe");
        for (var i = 0; i < iframes.length; i++) {
            iframes[i].style.display = "none";
        }

        dragEngine.downPoint.x = e.clientX - dragEngine.drag.offsetLeft;
        dragEngine.downPoint.y = e.clientY - dragEngine.drag.offsetTop;
        addEventHandler(document, "mousemove", dragEngine.documentMousemove);
        addEventHandler(document, "mouseup", dragEngine.documentMouseup);
        addEventHandler(dragEngine.target, "losecapture", dragEngine.documentMouseup);
        if (dragEngine.target.setCapture) dragEngine.target.setCapture();
        addEventHandler(window, "blur", dragEngine.documentMouseup);
        if (e.preventDefault) e.preventDefault();
        if (typeof dragEngine.onstart == "function") dragEngine.onstart(dragEngine);
    };
    addEventHandler(this.target, "mousedown", this.targetMousedown);
}


DragEngine.prototype.dispose = function() {
    removeEventHandler(this.target, "mousedown", this.targetMousedown);
}

function getDocumentBounds() {
    if (document.documentElement && document.compatMode == "CSS1Compat") {
        return {
            st: document.documentElement.scrollTop,
            sl: document.documentElement.scrollLeft,
            sw: document.documentElement.scrollWidth,
            sh: document.documentElement.scrollHeight,
            cw: document.documentElement.clientWidth,
            ch: document.documentElement.clientHeight
        }
    } else if (document.body) {
        return {
            st: document.body.scrollTop,
            sl: document.body.scrollLeft,
            sw: document.body.scrollWidth,
            sh: document.body.scrollHeight,
            cw: document.body.clientWidth,
            ch: document.body.clientHeight
        }
    }
}

function Dialog(title, left, top, width, height, closefree, fixup) {

    this.dx = document.body.clientWidth;
    var bHeight = document.body.clientHeight;
    var wHeight = document.documentElement.clientHeight;
    this.dy = bHeight > wHeight ? bHeight : wHeight;

    this.closefree = closefree;
    this.div_dialog = document.createElement("div");
    this.div_dialog.style.textAlign = "left";
    this.div_dialog.style.display = "none";
    this.div_dialog.style.position = "absolute";
    this.div_dialog.style.borderStyle = "none";
    this.div_dialog.style.borderWidth = "0px";
    this.div_dialog.style.zIndex = globalZIndex;
    this.edgeWidth = 25; // 边宽
    document.body.appendChild(this.div_dialog);

    this.table_dialog = document.createElement("table");
    this.table_dialog.cellPadding = "0px";
    this.table_dialog.cellSpacing = "0px";
    this.div_dialog.appendChild(this.table_dialog);

    this.tr_top = this.table_dialog.insertRow(-1);
    this.tr_top.style.height = "40px";

    this.tr_middle = this.table_dialog.insertRow(-1);

    this.tr_bottom = this.table_dialog.insertRow(-1);
    this.tr_bottom.style.height = "25px";

    this.td_top_left = this.tr_top.insertCell(-1);
    this.td_top_left.style.backgroundImage = "url(" + imagePath + "corner.png)";
    this.td_top_left.style.backgroundPosition = "0px -100px";
    this.td_top_left.style.backgroundRepeat = "no-repeat";
    this.td_top_left.style.width = this.edgeWidth + "px";

    this.td_top_center = this.tr_top.insertCell(-1);

    this.td_top_center.style.backgroundImage = "url(" + imagePath + "vertical.png)";
    this.td_top_center.style.width = (width - this.edgeWidth * 2) + "px";
    this.td_top_center.style.backgroundPosition = "0px -100px";
    this.td_top_center.style.backgroundRepeat = "repeat-x";

    this.div_title = document.createElement("div");
    this.div_title.style.styleFloat = "left";
    this.div_title.style.cssFloat = "left";
    this.div_title.style.fontWeight = "bold";

    if (!fixup) {
        this.div_title.style.cursor = "move";
        this.dragEngine = new DragEngine(this.div_title, this.div_dialog);
    }
    this.div_title.style.height = "29px";
    this.div_title.style.verticalAlign = "middle";
    this.div_title.style.lineHeight = "29px";
    this.div_title.style.marginTop = "11px";
    this.div_title.dialog = this;
    this.td_top_center.appendChild(this.div_title);

    this.div_close = document.createElement("div");
    this.div_close.style.styleFloat = "right";
    this.div_close.style.cssFloat = "right";
    this.td_top_center.appendChild(this.div_close);

    this.img_close = document.createElement("img");
    this.img_close.src = imagePath + "blank.gif";
    this.img_close.style.backgroundImage = "url(" + imagePath + "closebtn.gif)";
    this.img_close.style.width = "44px";
    this.img_close.style.height = "19px";
    this.img_close.style.backgroundRepeat = "no-repeat";
    this.img_close.style.marginTop = "12px";
    try {
        this.img_close.style.cursor = "pointer";
    }
    catch (e) {
        this.img_close.style.cursor = "hand";
    }
    this.img_close.onmouseover = function() {
        this.hot = true;
        this.doChange();
    }
    this.img_close.onmouseout = function() {
        this.down = false;
        this.hot = false;
        this.doChange();
    }
    this.img_close.onmousedown = function() {
        this.down = true;
        this.doChange();
    }
    this.img_close.onmouseup = function() {
        this.down = false;
        this.doChange();
    }
    this.img_close.onclick = function() {
        if (!this.dialog) return;
        this.dialog.close();
    }
    this.img_close.dialog = this;
    this.img_close.hot = false;
    this.img_close.down = false;
    this.img_close.doChange = function() {
        if (this.disabled)
            this.style.backgroundPosition = "-132px 0px";
        else if (this.down)
            this.style.backgroundPosition = "-88px 0px";
        else if (this.hot)
            this.style.backgroundPosition = "-44px 0px";
        else this.style.backgroundPosition = "0px 0px";
    }
    this.div_close.appendChild(this.img_close)

    this.td_top_right = this.tr_top.insertCell(-1);
    this.td_top_right.style.backgroundImage = "url(" + imagePath + "corner.png)";
    this.td_top_right.style.backgroundRepeat = "no-repeat";
    this.td_top_right.style.backgroundPosition = "-75px -100px";
    this.td_top_right.style.width = this.edgeWidth + "px";

    this.td_middle_left = this.tr_middle.insertCell(-1);
    this.td_middle_left.style.backgroundImage = "url(" + imagePath + "horizontal.png)";
    this.td_middle_left.style.backgroundPosition = "-100px 0px";
    this.td_middle_left.style.backgroundRepeat = "repeat-y";

    this.td_middle_center = this.tr_middle.insertCell(-1);
    this.td_middle_center.style.backgroundColor = "White";

    this.div_html = document.createElement("div");
    this.td_middle_center.appendChild(this.div_html);

    this.td_middle_right = this.tr_middle.insertCell(-1);
    this.td_middle_right.style.backgroundImage = "url(" + imagePath + "horizontal.png)";
    this.td_middle_right.style.backgroundPosition = "-175px 0px";
    this.td_middle_right.style.backgroundRepeat = "repeat-y";

    this.td_bottom_left = this.tr_bottom.insertCell(-1);
    this.td_bottom_left.style.backgroundImage = "url(" + imagePath + "corner.png)";
    this.td_bottom_left.style.backgroundRepeat = "no-repeat";
    this.td_bottom_left.style.backgroundPosition = "0px -175px";

    this.td_bottom_center = this.tr_bottom.insertCell(-1);
    this.td_bottom_center.style.backgroundImage = "url(" + imagePath + "vertical.png)";
    this.td_bottom_center.style.backgroundPosition = "0px -175px";
    this.td_bottom_center.style.backgroundRepeat = "repeat-x";

    this.td_bottom_right = this.tr_bottom.insertCell(-1);
    this.td_bottom_right.style.backgroundImage = "url(" + imagePath + "corner.png)";
    this.td_bottom_right.style.backgroundRepeat = "no-repeat";
    this.td_bottom_right.style.backgroundPosition = "-75px -175px";
    this.resize(left, top, width, height);
    this.setTitle(title);
}

Dialog.prototype.hide = function() {
    this.div_dialog.style.display = "none";
}

Dialog.prototype.resize = function(left, top, width, height) {
    var bounds = getDocumentBounds();
    if (typeof top == "undefined" || top < 0) top = Math.max((bounds.ch - height) / 2 + bounds.st, 10);
    if (typeof left == "undefined" || left < 0) left = Math.max((bounds.cw - width) / 2 + bounds.sl, 10);
    this.left = left;
    this.top = top;
    this.width = width;
    this.height = height;
    this.div_dialog.style.left = this.left + "px";
    this.div_dialog.style.top = this.top + "px";
    this.div_dialog.style.width = width + "px";
    this.div_dialog.style.height = height + "px";
    this.table_dialog.style.width = width + "px";
    this.table_dialog.style.height = height + "px";
    this.tr_middle.style.height = (height - 40 - 25) + "px";
    this.td_top_center.style.width = (width - this.edgeWidth * 2) + "px";
    this.div_title.style.width = (width - this.edgeWidth * 2 - 50) + "px";
    this.td_middle_center.style.height = (height - 40 - 25) + "px";
    this.div_html.style.width = (width - this.edgeWidth * 2) + "px";
    this.div_html.style.height = (height - 40 - 25) + "px";

    if (typeof this.onresize == "function") this.onresize(this);
}

Dialog.prototype.setTitle = function(title) {
    if (this.title == title) return;
    this.title = title;
    this.div_title.innerHTML = title;
}

Dialog.prototype.show = function() {
    this.div_dialog.style.left = this.left + "px";
    this.div_dialog.style.top = this.top + "px";
    this.div_dialog.style.display = "";
}

Dialog.prototype.close = function() {
    if (typeof this.onclose == "function") this.onclose(this);
    if (typeof (this.closefree) != "undefined" && this.closefree)
        this.dispose();
    else this.hide();
    if (CallBack) {
        CallBack();
    }
}

Dialog.prototype.dispose = function() {
    if (typeof this.ondispose == "function") this.ondispose(this);
    if (!fixup) this.dragEngine.dispose();
    this.td_middle_center.removeChild(this.div_html);
    this.div_close.removeChild(this.img_close)
    this.td_top_center.removeChild(this.div_close);
    this.td_top_center.removeChild(this.div_title);
    this.div_dialog.removeChild(this.table_dialog);
    document.body.removeChild(this.div_dialog);
}


var currentDialog = null;
function showWindow(opts) {
    if (!currentDialog) {
        if (!opts.width)
            opts.width = 200;
        if (!opts.height)
            opts.height = 200;

        var bounds = getDocumentBounds();
        currentDialog = new Dialog(opts.title, opts.left, opts.top,
			opts.width, opts.height, false, false);
        currentDialog.div_black = document.createElement("div");
        currentDialog.div_black.style.position = "absolute";
        currentDialog.div_black.style.borderStyle = "none";
        currentDialog.div_black.style.zIndex = globalZIndex - 1;
        currentDialog.div_black.style.left = "0px";
        currentDialog.div_black.style.top = "0px";
        currentDialog.div_black.style.height = currentDialog.dy + "px";
        currentDialog.div_black.style.width = currentDialog.dx + "px";
        currentDialog.div_black.style.filter = "alpha(opacity = 40)";
        currentDialog.div_black.style.opacity = "0.55857";
        currentDialog.div_black.style.backgroundColor = "#999999";
        currentDialog.div_black.style.display = "none";
        document.body.appendChild(currentDialog.div_black);
        currentDialog.div_black.style.display = "block";
        currentDialog.onresize = function() {
            //var bounds = getDocumentBounds();
            //currentDialog.div_black.style.top = bounds.st - 1000 + "px";
        }
        currentDialog.onclose = function() {
            currentDialog.div_black.style.display = "none";
        }
        currentDialog.ondispose = function() {
            document.body.removeChild(currentDialog.div_black);
            currentDialog = null;
        }
    } else {
        currentDialog.div_black.style.display = "block";
        currentDialog.resize(opts.left, opts.top, opts.width, opts.height);
        currentDialog.setTitle(opts.title);
    }
    if (typeof opts.url == "string") {
        currentDialog.div_html.innerHTML = "";
        currentDialog.iframe = document.createElement("iframe");
        currentDialog.iframe.width = (opts.width - currentDialog.edgeWidth * 2) + "px";
        currentDialog.iframe.height = (opts.height - 40 - 25) + "px";
        currentDialog.iframe.style.margin = "0";
        currentDialog.iframe.frameBorder = "0";
        currentDialog.iframe.src = opts.url;
        currentDialog.div_html.appendChild(currentDialog.iframe);
    } else if (typeof opts.content == "string") {
        currentDialog.div_html.innerHTML = opts.content;
    }
    else if (typeof opts.content == "object") {
        $(currentDialog.div_html).empty();
        currentDialog.div_html.appendChild(opts.content);
    }
    currentDialog.show();
}

var CallBack = null;
function showDialog(url, title, width, height, fun) {
    var bodyWith = document.body.clientWidth;
    var bHeight = document.body.clientHeight;
    var wHeight = document.documentElement.clientHeight;
    var bodyHeight = bHeight > wHeight ? bHeight : wHeight;

    var _width = width;
    var _height = height + 150;

    var dialogLeft = (bodyWith - _width) > 0 ? (bodyWith - _width) / 2 : 0;
    var dialogTop = (window.screen.availHeight - _height) / 2 + document.documentElement.scrollTop;


    showWindow({ url: url
		    , title: title
		    , top: dialogTop
		    , left: dialogLeft
		    , width: width + 60
		    , height: height + 80
    });

    if (typeof (fun) == "function") {
        CallBack = fun;
    }
}

function showDialog2(content, title, width, height) {
    var bodyWith = document.body.clientWidth;
    var bHeight = document.body.clientHeight;
    var wHeight = document.documentElement.clientHeight;
    var bodyHeight = bHeight > wHeight ? bHeight : wHeight;

    var _width = width;
    var _height = height + 150;

    var dialogLeft = (bodyWith - _width) > 0 ? (bodyWith - _width) / 2 : 0;
    var dialogTop = (window.screen.availHeight - _height) / 2 + document.documentElement.scrollTop;

    showWindow({ content: content
		    , title: title
		    , top: dialogTop
		    , left: dialogLeft
		    , width: width + 60
		    , height: height + 80
    });
}

function closeWindow() {
    $(currentDialog.div_html).empty();
    if (currentDialog) currentDialog.close();
}
