var goboBg, goboFrame, onDialogClose=null;

function gobo() {
    this.create = function() {
        if (isIE()) {
            if (isNull(goboFrame)) {
                goboFrame = document.createElement("iframe");
                goboFrame.id = 'goboFrame';
            }

            goboFrame.frameBorder = 'no';
            goboFrame.scrolling = 'no';
            goboFrame.width = document.documentElement.clientWidth + 'px';
            goboFrame.height = document.documentElement.scrollHeight + 'px';
            document.body.appendChild(goboFrame);
        }
        else {
            if (isNull(goboBg)) {
                goboBg = document.createElement('div');
                goboBg.id = 'goboBg';
            }

            goboBg.style.width = '100%';
            goboBg.style.height = document.documentElement.scrollHeight + 'px';
            goboBg.style.display = 'block';
            document.body.appendChild(goboBg);
        }
    }

    this.destroy = function() {
        if (exists('goboFrame')) {
            document.body.removeChild(document.getElementById('goboFrame'));
        }

        if (exists('goboBg')) {
            document.body.removeChild(document.getElementById('goboBg'));
        }
    }
}
function dialog(width, height, title) {
    var dialogWraper, dialogHeader, dialogBody;

    this.init = function() {
        var st = document.body.scrollTop + document.documentElement.scrollTop;

        var bg = new gobo();
        bg.create();

        if (isNull(dialogWraper)) {
            dialogWraper = document.createElement('div');
            dialogWraper.id = 'dialogWraper';
        }
        dialogWraper.style.width = (width + 5) + 'px';
        dialogWraper.style.height = (height + 30) + 'px';
        dialogWraper.style.left = (document.documentElement.clientWidth - width) / 2 + 'px';
        dialogWraper.style.top = (document.documentElement.clientHeight - height) / 2 + (st > 0 ? st : 0) + 'px';
        document.body.appendChild(dialogWraper);

        if (isNull(dialogHeader)) {
            dialogHeader = document.createElement('div');
            dialogHeader.id = 'dialogHeader';

        }
        dialogWraper.appendChild(dialogHeader);

        if (!exists('closeCommand')) {
            var close = document.createElement('div');
            close.id = 'closeCommand';
            close.className = 'close';
            close.innerHTML = '<a href="javascript:new dialog().close();"><img src="http://pics.sigbang.com/dialog/close.gif" alt="" style="border:0" /></a>';
            dialogHeader.appendChild(close);
        }

        if (!exists('dialogCaption')) {
            var caption = document.createElement('div');
            caption.id = 'dialogCaption';
            caption.className = 'caption';
            caption.innerHTML = '<div>' + title + '</div>';
            dialogHeader.appendChild(caption);
        }

        if (isNull(dialogBody)) {
            dialogBody = document.createElement('div');
            dialogBody.id = 'dialogBody';
        }
        dialogBody.style.height = height + 'px';
        dialogWraper.appendChild(dialogBody);
    }

    this.loadFrame = function(url) {
        this.init();
        var dialogBody = document.getElementById('dialogBody');
        if (dialogBody != null) {
            var dialogFrame = document.createElement('iframe');
            dialogFrame.id = 'dialogFrame';
            dialogFrame.frameBorder = 'no';
            dialogFrame.scrolling = 'no';
            dialogFrame.width = '100%';
            dialogFrame.height = '100%';
            dialogFrame.src = url;
            dialogBody.appendChild(dialogFrame);
        }
    }

    this.loadForm = function(form) {
        this.init();
        var dialogBody = document.getElementById('dialogBody');
        if (dialogBody != null) {
            var dialogForm = document.createElement('div');
            dialogForm.id = 'dialogForm';
            dialogForm.innerHTML = form;
            dialogBody.appendChild(dialogForm);
        }
    }

    this.layout = function() {
        this.init();
        var dialogBody = document.getElementById('dialogBody');
        if (dialogBody != null) {
            var wraper = document.createElement('div');
            wraper.className = 'rowWraper';
            dialogBody.appendChild(wraper);

            var tokenContainer = document.createElement('div');
            tokenContainer.className = 'token';
            wraper.appendChild(tokenContainer);

            var token = document.createElement('img');
            token.id = 'msgToken';
            tokenContainer.appendChild(token);

            var msgContainer = document.createElement('div');
            msgContainer.id = 'msgContainer';
            msgContainer.className = 'msg';
            wraper.appendChild(msgContainer);

            var hack = document.createElement('div');
            hack.className = 'hack';
            wraper.appendChild(hack);

            wraper = document.createElement('div');
            wraper.className = 'confirm';
            wraper.innerHTML = '<ul><li><a href="javascript:new dialog().close();" >确定</a></li></ul>';
            dialogBody.appendChild(wraper);
        }
    }

    this.showMsg = function(msg) {
        this.layout();

        var token = document.getElementById('msgToken');
        if (token != null) {
            token.src = 'http://pics.sigbang.com/dialog/succeed.png';
        }

        var msgContainer = document.getElementById('msgContainer');
        if (msgContainer != null) {
            msgContainer.innerHTML = msg;
        }
    }

    this.showAlert = function(alertMsg) {
        this.layout();

        var token = document.getElementById('msgToken');
        if (token != null) {
            token.src = 'http://pics.sigbang.com/dialog/warning.png';
        }

        var msgContainer = document.getElementById('msgContainer');
        if (msgContainer != null) {
            msgContainer.innerHTML = alertMsg;
        }
    }

    this.showError = function(errorMsg) {
        this.layout();

        var token = document.getElementById('msgToken');
        if (token != null) {
            token.src = 'http://pics.sigbang.com/dialog/error.png';
        }

        var msgContainer = document.getElementById('msgContainer');
        if (msgContainer != null) {
            msgContainer.innerHTML = errorMsg;
        }
    }

    this.close = function(callback) {

        if (exists('dialogWraper')) {
            dialogWraper = document.getElementById('dialogWraper');

            if (exists('dialogBody')) {
                dialogWraper.removeChild(document.getElementById('dialogBody'));
            }

            if (exists('dialogHeader')) {
                dialogWraper.removeChild(document.getElementById('dialogHeader'));
            }

            document.body.removeChild(dialogWraper);
        }

        new gobo().destroy();

        if (callback) {
            if (onDialogClose != null) {
                if (typeof (onDialogClose.method) == 'function') {
                    if (onDialogClose.params != null)
                        onDialogClose.method.apply(null, onDialogClose.params);
                    else
                        onDialogClose.method.apply();
                }
                else if (typeof (onDialogClose) == 'function') {
                    onDialogClose.apply();
                }
                else if (typeof (onDialogClose) == 'string') {
                    eval(onDialogClose);
                } 
            }
        }
    }
}

function tipDialog(width, height) {
    var tipWarper, msgBox;

    this.init = function() {
        var st = document.body.scrollTop + document.documentElement.scrollTop;

        var bg = new gobo();
        bg.create();

        if (isNull(tipWarper)) {
            tipWarper = document.createElement('div');
            tipWarper.id = 'tipWraper';
        }
        tipWarper.style.width = width + 'px';
        tipWarper.style.left = (document.documentElement.clientWidth - width) / 2 + 'px';
        tipWarper.style.top = (document.documentElement.clientHeight - height) / 2 + (st > 0 ? st : 0) + 'px';
        document.body.appendChild(tipWarper);

        if (isNull(msgBox)) {
            msgBox = document.createElement('div');
            msgBox.id = 'msgBox';
        }
        tipWarper.appendChild(msgBox);
    }

    this.show = function(msg) {
        this.init();
        var msgBox = document.getElementById('msgBox');
        if (msgBox != null) {
            msgBox.innerHTML = msg;
        }
    }

    this.hide = function() {
        if (!isNull(this.tipWarper)) {
            var box = document.getElementById('msgBox');
            if (!isNull(box)) {
                this.tipWraper.removeChild(box);
            }

            document.body.removeChild(this.tipWraper);
        }

        new gobo().destroy();
    }
}

function openDialog(title, width, height, url) {
    var obj = new dialog(width, height, title);
    obj.loadFrame(url);
}
function closeDialog(callback) {
    var obj = new dialog();
    obj.close(callback);
}
function showForm(title, width, height, form) {
    var obj = new dialog(width, height, title);
    obj.loadForm(form);
}
function showTip(width, height, msg) {
    var obj = new tipDialog(width, height);
    obj.show(msg);
}
function hideTip() {
    var obj = new tipDialog();
    obj.hide();
}
function showMsg(title, width, height, msg) {
    var obj = new dialog(width, height, title);
    obj.showMsg(msg);
}
function showAlert(title, width, height, msg) {
    var obj = new dialog(width, height, title);
    obj.showAlert(msg);
}
function showError(title, width, height, msg) {
    var obj = new dialog(width, height, title);
    obj.showError(msg);
}
function exists(objId) {
    return document.getElementById(objId) != null;
}
function isIE() {
    if (document.all) return true;
    return false;
}
function isLowerIE7() {
    var sys = checkBrowser();
    return (sys.ie && sys.ie < 7);
}
function checkBrowser() {
    var sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/msie ([\d.]+)/)) ? sys.ie = s[1] :
        (s = ua.match(/firefox\/([\d.]+)/)) ? sys.firefox = s[1] :
        (s = ua.match(/chrome\/([\d.]+)/)) ? sys.chrome = s[1] :
        (s = ua.match(/opera.([\d.]+)/)) ? sys.opera = s[1] :
        (s = ua.match(/version\/([\d.]+).*safari/)) ? sys.safari = s[1] : 0;

    return sys;

    //    以下进行测试
    //    if (Sys.ie) document.write('IE: ' + Sys.ie);
    //    if (Sys.firefox) document.write('Firefox: ' + Sys.firefox);
    //    if (Sys.chrome) document.write('Chrome: ' + Sys.chrome);
    //    if (Sys.opera) document.write('Opera: ' + Sys.opera);
    //    if (Sys.safari) document.write('Safari: ' + Sys.safari);
}