/**
 * 窗口类
 * 需要载入 document.js, string.js
 * OK
 */

var Window = {
//    messageWindow:null,

    listenWindowState:function(win) {
        var count = 0;
        var interval;
        var destroyInterval = function () {
          try{
            var readyStatus;
            if(!win||!win.document) {
              window.clearInterval(interval);
              Window.destroyProgress();
            }
            readyStatus = win.document.readyState;
            if (readyStatus == "complete") {
                window.clearInterval(interval);
                Window.destroyProgress();
            }
            else if (readyStatus == "interactive") {
                count += 1;
                if (count > 30) {
                    window.clearInterval(interval);
                    Window.destroyProgress();
                }
            }
          }
          catch(ex) {
              window.clearInterval(interval);
              if(Window)Window.destroyProgress();
          }
        };
        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 w = width;
    var h = height;
    if (w==null) w=800;
    if (h==null) h=600;
    var left = (screen.availWidth - w) / 2;
    var top = (screen.availHeight - h) / 2;
    var sFeatures = "toolbar=yes, menubar=yes, scrollbars=yes, resizable="+resizablebar+", " +
                    "location=yes, status=yes, titlebar=yes,height="+h+",width="+w;
    sFeatures+=",left="+left+",top="+top;
    var win
    if (progressTitle == null) {
      win = window.open(url, 'open' + StringUtil.randomChar(20), sFeatures);
      win.resizeTo(w,h);
    }
    else {
      win = window.open("about:blank", 'open' + StringUtil.randomChar(20), sFeatures);
      win.resizeTo(w,h);
      Window.writeProgress(progressTitle, win);
//      window.setTimeout(function(){
        win.location = url;
//      },200)
    }
    return win;
  },

  /**
 * 打开没有菜单和工具栏的窗口
 * url:链接页面或action动作
 * width:宽度
 * height:高度
 * resizable:是否可以拖动窗口大小
 * progressTitle:进度条
 */
    openNoBarWindow:function (url, width, height, resizable, progressTitle) {
      var w = width;
      var h = height;
      if (w==null) w=800;
      if (h==null) h=600;
        var resizablebar = (resizable == null || resizable) ? "yes" : "no";
        var sFeatures = "scrollbars=yes, status=no, resizable=" + resizablebar + ","
                + "toolbar=no, menubar=no, location=no, titlebar=no,width="+w+",height="+h;

        var left = (screen.availWidth - w) / 2;
        var top = (screen.availHeight - h) / 2;
        sFeatures += ",top=" + top + ",left=" + left;
        var win;
        if (progressTitle == null) {
            win = window.open(url, 'openNoBar' + StringUtil.randomChar(20), sFeatures);
          win.resizeTo(w,h);
        }
        else {
          win = window.open("about:blank", 'open' + StringUtil.randomChar(20), sFeatures);
          win.resizeTo(w,h);
          Window.writeProgress(progressTitle, win);
//          window.setTimeout(function(){
            win.location = url;
//          },200)
        }
        return win;
    },

/**
 * 打开全屏窗口,并关闭父窗口
 * url:链接页面或action动作
 * progressTitle:进度条文字
 */
  fullWindow:function (url, progressTitle) {
  	var newWin=Window.openFullWindow(url, progressTitle);
	var winOpener = newWin.opener;
    if (winOpener != null && winOpener != newWin && !winOpener.closed) {
      winOpener.opener = null;
      winOpener.close();
    }
  },
  
  /**
 * 打开全屏窗口
 * url:链接页面或action动作
 * progressTitle:进度条文字
 */
  openFullWindow:function (url, progressTitle) {
    var height = screen.availHeight;
    var width = screen.availWidth;
    var sFeatures = "toolbar=no, menubar=no, scrollbars=yes, resizable=yes, "
        + "location=no, status=no, titlebar=no, width="+width+", "
        + "height="+height+", top=0, left=0";

    var win;
    if (progressTitle == null) {
      win = window.open(url, 'full' + StringUtil.randomChar(20), sFeatures);
      win.resizeTo(width, height);
    }
    else {
      win = window.open("about:blank", 'open' + StringUtil.randomChar(20), sFeatures);
      win.resizeTo(width, height);
      Window.writeProgress(progressTitle, win);
//      window.setTimeout(function(){
        win.location = url;
//      },200)
    }
    return win;
  },


/**
 * 创建一个模拟进度条
 * 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 = 5000;
            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");
            progressBackground.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);

/*
            window.setTimeout(function(){
              if(window.document && window.document.body) {
                try{
                  window.document.body.removeChild(progress);
                  window.document.body.removeChild(progressBackground);
                }
                catch(e){
                  alert(e); 
                }
              }
            },10000);
*/
        }
    },

/**
 * 销毁创建的进度条
 */
    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 = Document.getObject(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 = 10;
            MSG1.step = 4;
//            MSG1.autoHide = true;
//            MSG1.timeout = 5;
            MSG1.show();
        }
    },


  /**
   * 向指定窗口中写进度条
   * @param text
   * @param win
   */
  writeProgress:function (text,win) {
    var style1="width:300px;height:90px;border:2px outset;text-align:center;background-color:#ffffff;";
    var style2="width:298px;height:18px;padding:2px;text-align:left;font-size:12px;color:#ffffff;font-weight:bold;background-color:#3E71D7;"+
               "filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#003399', EndColorStr='#A6CAF0');";
    var style3="width:298px;height:45px;padding-top:18px;font-size:12px ;word-break:keep-all;background-color:#ffffff;overflow:hidden;";
    var style4="width:260px;height:8px;font-size:6px;border:2px inset;background-color:#003399;";
    var style5="width:130px;height:8px;font-size:6px;"+
               "filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr=#003399, EndColorStr=#A5C9F0);";
    var style6="width:130px;height:8px;font-size:6px;"+
               "filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr=#A5C9F0, EndColorStr=#003399);";
    var count=0;
    try{
      while(win==null||win.document==null||win.document.body==null){
        count++;
        if(count==99999999) break;
      }
      win.document.body.style.border="none";
      win.document.body.style.margin="0";
      win.document.body.innerHTML="<table width='100%' height='100%'>"+
                                  "<tr><td align='center' vAlign='middle'>"+
                                  "<div style=\""+style1+"\">"+
                                  "<div style=\""+style2+"\">进度条</div>"+
                                  "<div style=\""+style3+"\">"+text+"</div>"+
                                  "<MARQUEE scrollAmount='10' scrollDelay='100' direction='right' style=\""+style4+"\">"+
                                  "<span style=\""+style5+"\"></span><span style=\""+style6+"\"></span>"+
                                  "</MARQUEE>"+
                                  "</div>"+
                                  "</td></tr></table>";
    }
    catch(e){

    }
  },
  /**
   * 重新定义页面iframe与frameContent的高度一致
   * @param frameName
   */
  resizeFrame:function(frameName){
    var parWin = window.parent;
      var scrollHeight= document.body.scrollHeight;
      var frame=parWin.document.getElementById(frameName);
      frame.style.height = scrollHeight;
  },
  /**
   * 切换分页和添加修改iframe显示
   * @param divObj 分页div对象
   * @param frameObj 添加修改iframe对象
   */
  switchFrame:function(divObj,frameObj){
    Document.showHiddenObject(divObj);
    Document.showHiddenObject(frameObj);
  }
}
