/**
 * 窗口类
 * 需要载入 document.js, string.js
 * OK
 */
/*
var WindowStyle=function(){
  this.width=null;
  this.height=null;
  this.resizable=null;
  this.scroll=null;

//  var winStyle=WindowStyle.prototype;
  this.setWidth=function(width){
    this.width = width;
    return this;
  }
  this.setHeight=function(height){
    this.height=height;
    return this;
  }
  this.setResizable=function(resizable){
    this.resizable=resizable;
    return this;
  }
  this.setScroll=function(scroll){
    this.scroll=scroll;
    return this;
  }
}
*/
var Window = {


    listenWindowState:function(win) {
        var count = 0;
        var interval;
        var destroyInterval = function () {
            var readyStatus;
            readyStatus = win.document.readyState;
            if (readyStatus == "complete") {
                window.clearInterval(interval);
                Window.destroyProgress();
                //            Document.refreshStatus();
            }
            else if (readyStatus == "interactive") {
                count += 1;
                if (count > 20) {
                    window.clearInterval(interval);
                    Window.destroyProgress();
                    //              Document.refreshStatus();
                }
            }
        };
        try {
            interval = window.setInterval(destroyInterval, 50);
        }
        catch(e) {
            window.clearInterval(interval);
            Window.destroyProgress();
        }
    },


/**
 * 打开模态窗口
 * url:链接页面或action动作
 * width:打开模态窗口的宽度
 * height:打开模态窗口的高度
 * 注意：打开模态窗口的页面中要在<head>后面加上
 * <meta http-equiv="Pragma" content="no-cache">：禁止模态窗口缓存
 * <base target="_self"/>：模态窗口中的表单在本窗口中提交
 * 姜敏
 */
    modalWindow:function (url, width, height) {
        var sFeatures = "dialogWidth:" + width + "px; dialogHeight:" + height + "px; "
                + "help:no; scroll:no; center:yes; status:no;resizable:yes";
        return window.showModalDialog(url, window, sFeatures);
    },

/**
 * 打开普通窗口
 * url:链接页面或action动作
 * width:宽度
 * height:高度
 * resizable:是否可以改变窗口大小
 * progressTitle:进度条文本
 * 姜敏
 */
    openWindow:function (url, width, height, resizable, progressTitle) {
        var resizablebar = (resizable == null || resizable) ? "yes" : "no";
        var sFeatures = "scrollbars=yes, status=yes, resizable=" + resizablebar + ","
                + "toolbar=yes, menubar=yes, location=yes, titlebar=yes"
        if (width != null) {
            sFeatures += ", width=" + width;
        }
        if (height != null) {
            sFeatures += ", height=" + height;
        }
        var win
        if (progressTitle == null) {
            win = window.open(url, 'open' + StringUtil.randomChar(20), sFeatures);
        }
        else {
            win = window.open("progress.jsp?progressTitle=" + StringUtil.convertURIEncode(progressTitle),
                    'open' + StringUtil.randomChar(20), sFeatures);
            win.setTimeout(
                    function() {
                        win.location = url;
                    }, 200
                    )
        }
        return win;
    },

/**
 * 打开没有菜单和工具栏的窗口
 * url:链接页面或action动作
 * width:宽度
 * height:高度
 * resizable:是否可以拖动窗口大小
 * progressTitle:进度条
 */
    openNoBarWindow:function (url, width, height, resizable, progressTitle) {
        var resizablebar = (resizable == null || resizable) ? "yes" : "no";
        var sFeatures = "scrollbars=yes, status=no, resizable=" + resizablebar + ","
                + "toolbar=no, menubar=no, location=no, titlebar=no"
        if (width != null) {
            sFeatures += ", width=" + width;
        }
        if (height != null) {
            sFeatures += ", height=" + height;
        }
        var left = (screen.availWidth - width) / 2;
        var top = (screen.availHeight - height) / 2;
        sFeatures += ",top=" + top + ",left=" + left;
        var win;
        if (progressTitle == null) {
            win = window.open(url, 'openNoBar' + StringUtil.randomChar(20), sFeatures);
        }
        else {
            win = window.open("progress.jsp?progressTitle=" + StringUtil.convertURIEncode(progressTitle),
                    'openNoBar' + StringUtil.randomChar(20), sFeatures);
            win.setTimeout(
                    function() {
                        win.location = url;
                    }, 200
                    )
        }
        return win;
    },

/**
 * 打开全屏窗口
 * url:链接页面或action动作
 * progressTitle:进度条文字
 */
    openFullWindow:function (url, progressTitle) {
        var sFeatures = "toolbar=no, menubar=no, scrollbars=no, resizable=yes, "
                + "location=no, status=no, titlebar=no, width=" + (screen.width - 10) + ", "
                + "height=" + (screen.height - 60) + ", top=0, left=0";

        var win;
        if (progressTitle == null) {
            win = window.open(url, 'full' + StringUtil.randomChar(20), sFeatures);
        }
        else {
            win = window.open("progress.jsp?progressTitle=" + StringUtil.convertURIEncode(progressTitle),
                    'full' + StringUtil.randomChar(20), sFeatures);
            win.setTimeout(
                    function() {
                        win.location = url;
                    }, 200
                    )
        }
        return win;
    },

/*
createWindowProcess:function (win, url, progressTitle) {
  if (progressTitle == null) {
    win.location = url;
    return;
  }

  if (win.Window) {
    win.Window.createProgress(progressTitle);
  }

  win.setTimeout(
      function() {
        win.location = url;
      }, 200
      )
},

*/
/**
 * 打开虚拟窗口
 * src: 请求动作
 * title: 虚拟窗口的标题
 * width: 窗口宽度
 * height: 窗口高度
 * closeFunction：窗口关闭时执行的函数
 */
    openVirtualWindow:function (src, width, height, closeFunction) {

        var objSpans = window.document.body.getElementsByTagName("SPAN");
        for (var i = objSpans.length; i > 0; i--) {
            if (objSpans[i - 1].className == '2k3Window') {
                window.document.body.removeChild(objSpans[i - 1]);
            }
        }

        var bodyClientWidth = window.document.body.clientWidth;
        var bodyClientHeight = window.document.body.clientHeight;
        var bodyScrollTop = window.document.body.scrollTop;
        var bodyScrollLeft = window.document.body.scrollLeft;

        var tmpWidth = bodyClientWidth - width;
        var tmpHeight = bodyClientHeight - height;
        var left = (tmpWidth > 0) ? bodyScrollLeft + tmpWidth / 2 : bodyScrollLeft;
        var top = (tmpHeight > 0) ? bodyScrollTop + tmpHeight / 2 : bodyScrollTop;
        var objSpan = window.document.createElement("SPAN");
        objSpan.className = "2k3Window";

        objSpan.width = width;
        objSpan.height = height;
        objSpan.left = left;
        objSpan.top = top;
        if (closeFunction != null) {
            objSpan.closeFunction = closeFunction;
        }
        objSpan.src = src;
        window.document.body.appendChild(objSpan);
        return objSpan;
    },

/**
 * 关闭虚拟窗口
 */
    closeVirtualWindow:function () {
        var windowSpan = window.parent.document.getElementById(window.name).parentElement;
        window.parent.document.body.removeChild(windowSpan);
    },

/**
 * 创建一个模拟进度条
 * content进度条文本
 */
    createProgress:function (text) {
        if (window.document && window.document.body) {
            Window.destroyProgress();
            var progress = document.createElement("DIV");
            progress.id = "virtualProgresse";
            progress.className = "progress";
            progress.style.zIndex = 100;
            progress.style.position = "absolute";
            progress.onselectstart = function() {
                return false
            };
            var title = document.createElement("DIV");
            title.className = "title";
            title.innerHTML = "进度条";
            progress.appendChild(title);
            var content = document.createElement("DIV");
            content.className = "content";
            content.innerHTML = text;
            progress.appendChild(content);
            var marquee = document.createElement("MARQUEE");
            marquee.scrollAmount = 10;
            marquee.scrollDelay = 100;
            marquee.direction = "right";
            marquee.className = "marquee";
            var filterLeft = document.createElement("SPAN");
            filterLeft.className = "filterLeft";
            marquee.appendChild(filterLeft);
            var filterRight = document.createElement("SPAN");
            filterRight.className = "filterRight";
            marquee.appendChild(filterRight);
            progress.appendChild(marquee);
            var width = 300;
            var height = 90;
            var bodyClientWidth = window.document.body.clientWidth;
            var bodyClientHeight = window.document.body.clientHeight;
            var bodyScrollTop = window.document.body.scrollTop;
            var bodyScrollLeft = window.document.body.scrollLeft;
            var tmpWidth = bodyClientWidth - width;
            var tmpHeight = bodyClientHeight - height;
            var left = (tmpWidth > 0) ? bodyScrollLeft + tmpWidth / 2 : bodyScrollLeft;
            var top = (tmpHeight > 0) ? bodyScrollTop + tmpHeight / 2 : bodyScrollTop;
            progress.style.left = left;
            progress.style.top = top;
            var progressBackground = document.createElement("<iframe id='virtualProgresseBackground'>");
            progressBackground.frameBorder = '0';
            progressBackground.style.width = width;
            progressBackground.style.height = height;
            progressBackground.style.position = "absolute";
            progressBackground.style.left = left;
            progressBackground.style.top = top;
            document.body.appendChild(progressBackground);
            document.body.appendChild(progress);
        }
    },

/**
 * 销毁创建的进度条
 */
    destroyProgress:function () {
        var virtualProgresses = window.document.getElementsByName("virtualProgresse");
        for (var i = 0; i < virtualProgresses.length; i++) {
            window.document.body.removeChild(virtualProgresses[i]);
        }
        var virtualProgresseBackgrounds = window.document.getElementsByName("virtualProgresseBackground");
        for (var i = 0; i < virtualProgresseBackgrounds.length; i++) {
            window.document.body.removeChild(virtualProgresseBackgrounds[i]);
        }
    },


/**
 * 打开窗口，并显示指定对象的innerHTML
 * elementID 对象id
 */
    showSourceCode:function (elementID) {

        var win = Window.openNoBarWindow("about:blank", 640, 480, true);
        var doc = win.document.open("text/html", "replace");
        with (doc) {
            writeln("<html>");
            writeln("<head>");
            writeln("<\/head>");
            writeln("<body style='margin: 0px;overflow:hidden;'>");
            writeln("<textarea id='sourceTextArea' style='width:100%;height:100%;border:none;'></textarea>");
            writeln("<\/body>");
            writeln("<\/html>");
        }
        doc.close();
        if (elementID == null) {
            Document.getObject('sourceTextArea', win).value = document.getElementsByTagName("HTML")[0].outerHTML;
        }
        else {
            var element = Document.getObject(elementID);
            if (element) {
                Document.getObject('sourceTextArea', win).value = element.outerHTML;
            }
        }
    },

/**
 * 打开窗口，并显示指定tagName对象的innerHTML
 * tagName 指定类型的对象集合
 */
    showTagNameCode:function (tagName) {
        var win = Window.openNoBarWindow("about:blank", 640, 480, false);
        var doc = win.document.open("text/html", "replace");
        with (doc) {
            writeln("<html>");
            writeln("<head>");
            writeln("<\/head>");
            writeln("<body style='margin: 0px;overflow:hidden;'>");
            writeln("<textarea id='sourceTextArea' style='width:100%;height:100%;border:none;'></textarea>");
            writeln("<\/body>");
            writeln("<\/html>");
        }
        doc.close();
        var elements = document.getElementsByTagName(tagName);
        var text = '';
        for (var i = 0; i < elements.length; i++) {
            text += elements[i].innerHTML.concat("\r\n");
        }
        Document.getObject('sourceTextArea', win).value = text;
    },

/**
 * 显示IFrame中的innerHTML
 */
    showFrameCode:function (frameName) {
        var frame = window.frames[frameName];
        var win = Window.openNoBarWindow("about:blank", 640, 480, false);
        var doc = win.document.open("text/html", "replace");
        with (doc) {
            writeln("<html>");
            writeln("<head>");
            writeln("<\/head>");
            writeln("<body style='margin: 0px;overflow:hidden;'>");
            writeln("<textarea id='sourceTextArea' style='width:100%;height:100%;border:none;'></textarea>");
            writeln("<\/body>");
            writeln("<\/html>");
        }
        doc.close();
        Document.getObject('sourceTextArea', win).value = frame.document.getElementsByTagName("HTML")[0].outerHTML;
        //alert(frame.document.getElementsByTagName("HTML")[0].outerHTML);
    },

/**
 * 显示消息
 */
    showMessage:function(message, width, height) {
        if (!StringUtil.isEmpty(message)) {
            var MSG1 = new CLASS_MSN_MESSAGE("messagePopup", width, height, "消息提示：", message);
            MSG1.rect(null, null, null, null);
            MSG1.speed = 15;
            MSG1.step = 4;
            MSG1.show();
        }
    },

/**
 * 显示title
 * targetElement:目标对象
 * titleElement:title对象
 */
    showTitle:function(targetElement, titleElement, tagName) {
        var display = titleElement.style.display;
        //alert(display)
        titleElement.style.zIndex = 1000;
        titleElement.style.display = "block";

        var absPos = Document.getAbsolutePos(targetElement);
        var top = absPos.top;
        var left = absPos.left;
        var content = (tagName == null) ? "DIV" : tagName;
        var element = titleElement.getElementsByTagName(content)[0];
        if (element.value) element.value = targetElement.innerText;
        else element.innerText = targetElement.innerText;

        var x = event.offsetX;
        //鼠标指针位置相对于触发事件的对象的 x 坐标
        var y = event.offsetY;
        //鼠标指针位置相对于触发事件的对象的 y 坐标

        var width = titleElement.offsetWidth;
        var height = titleElement.offsetHeight;

        var scrollElement = Document.getScrollParent(targetElement);
        //滚动条容器宽度(不计算任何边距、边框、滚动条)
        var scrollWidth = scrollElement.clientWidth;
        //滚动条容器高度(不计算任何边距、边框、滚动条)
        var scrollHeight = scrollElement.clientHeight;
        //滚动条左边距
        var scrollLeft = scrollElement.scrollLeft;
        //滚动条上边距
        var scrollTop = scrollElement.scrollTop;

        if (top + y + height <= scrollTop + scrollHeight) {
            titleElement.style.top = top + y + 2;
            if (left + x + width <= scrollLeft + scrollWidth) {
                titleElement.style.flip = "";
                titleElement.style.left = left + x + 2;
            }
            else {
                titleElement.style.flip = "x";
                titleElement.style.left = left + x - width + 10;
            }
        }
        else {
            titleElement.style.top = top + y - height + 10;
            if (left + x + width <= scrollLeft + scrollWidth) {
                titleElement.style.flip = "y";
                titleElement.style.left = left + x + 5;
            }
            else {
                titleElement.style.flip = "x y";
                titleElement.style.left = left + x - width + 10;
            }
        }
        var xyz = function() {
            var obj = event.srcElement;
            if (!targetElement.contains(obj) && !titleElement.contains(obj)) {
                titleElement.style.display = "none";
                document.body.detachEvent('onmousedown', xyz);
            }
        }
        if (display == "none") {
            document.body.attachEvent('onmousedown', xyz);
        }
    }


    /*
      includeProgressCssJs:function(win) {
        var head = win.document.getElementsByTagName("HEAD")[0];
        var script = win.document.createElement("SCRIPT");
        script.type = "text/javascript";
        script.src = 'js/common/window.js';
        script.defer = true;
        head.appendChild(script);

        var link = win.document.createElement("link");
        link.rel = 'stylesheet';
        link.type = 'text/css';
        link.href = "css/common/progress.css";
        head.appendChild(link);

      }
    */

}
