﻿/*******************************************************************************
 *        程序 ID     ：    Flake
 *        程序名称    ：    Flake管理类
 *        处 理       ：    定义Flake和Flakes类
 *        功 能       ：    定义Flake和Flakes类
 *
 *        修改履历
 *        年  月  日 区分 所 属/担 当        内 容                     对应编号
 *        ---------- ---- ------------------ ------------------------- --------
 *        2007/10/16 编写 Intasect/陈冬    新規作成
 *        2007/10/17 变更 Intasect/陈冬    修改Transfer2flake因找不到  NO.C1016005
 *                                         focusedFlake而失效的问题
 *        2007/10/30 变更 Intasect/陈冬    1.将win.element.style.position的设置移到Flake.AddFlake中
 *                                         2.loadingEnd时检查是否有模式片,有则隐藏下拉框
 *        2008/03/14 变更 Intasect/陈冬    对应综合测试课题281
 * 
 *        Copyright ( C ) 2007 by Intasect communications(Chengdu), Co, Ltd.
 *        All rights reserved.
 *
********************************************************************************/
var Flake = Class.create();
Flake.prototype = {
  initialize : function(id,aspx,queryStr)
  {
    this.id        = id;
    this.aspx      = aspx;
    if(queryStr)
    {
      this.queryStr  = queryStr;
    }else{
      var _qstr = aspx.options.url.split("?")[1];
      this.queryStr = _qstr ? _qstr : "";
    }
    this.minimized = false;
    this.maximized = false;
  },

  //parent flake which opened this flake
  parent : null,

  //flake's win obj
  win : null,

  //temp title
  tempTitle : null,
  
  //function when close
  onClose : null,

  //edit flake
  Edit : function()
  {
    this.win.edit();
  },

  //minimize flake
  Minimize : function(evt,temp)
  {
    this.win.minimize();
    this.minimized = this.win.isMinimized();
    if(!temp)
    {
      Storage.notify(Storage.EVENT.onChangeStorage,
                     {
                       method          : Storage.CHANGE.UpdateFlake,
                       flake_id        : this.id,
                       flake_title     : this.win.options.title,
                       flake_width     : this.win.width,
                       flake_height    : this.win.height,
                       flake_minimized : this.win.isMinimized()?1:0
                     }
                    );
    }
    if(this.minimized)
    {
      this.win.disableButtonMaximize();
      this.win.disableButtonEdit();
    }else{
      this.win.enableButtonMaximize();
      this.win.enableButtonEdit();
    }
  },

  //maximize flake
  Maximize : function()
  {
    this.win.maximize();
    this.maximized = this.win.isMaximized();
    if(this.maximized)
    {
      this.win.disableButtonMinimize();
    }else{
      this.win.enableButtonMinimize();
    }
  },

  //close flake
  Close : function()
  {
    if(this.aspx.options.updateParent)
    {
      if(this.parent)
      {
        this.parent.Refresh();
      }
    }
    if(this.aspx.options.dialog && this.aspx.options.showParent)
    {
      //显示DDL
      if(this.parent)
      {
        this.parent.showSelect();
      }
    }
    Windows.close(this.win.getId(),event);
  },

  //refresh flake
  Refresh : function()
  {
    if(this.win.content.contentWindow.document.location.search != "?" + this.queryStr)
    {
      this.win.content.contentWindow.document.location.href = this.win.content.contentWindow.document.location.href.split("?")[0] + "?" + this.queryStr;
    }else{
      this.win.refresh();
    }
  },

  //edit flake's title
  setTitle : function(title,temp)
  {
    if(temp && title.indexOf("<img") == -1)
    {
      this.tempTitle = title;
    }
    this.win.setTitle(title,temp);
    if(!temp)
    {
      Storage.notify(Storage.EVENT.onChangeStorage,
                     {
                       method          : Storage.CHANGE.UpdateFlake,
                       flake_id        : this.id,
                       flake_title     : this.win.options.title,
                       flake_width     : this.win.width,
                       flake_height    : this.win.height,
                       flake_minimized : this.win.isMinimized()?1:0
                     }
                    );
    }
  },

  getTitle : function()
  {
    var result = "";
    if(this.tempTitle && this.tempTitle.indexOf("<img") == -1)
    {
      result = this.tempTitle;
    }else{
      result = this.aspx.options.title;
    }
    return result;
  },

  //serialize flake to string for cookie
  toCookie : function()
  {
    var result = "";
    result +=                                "id"                  + CONST.Spliter.Flake.AttrValue + this.id;
    result += CONST.Spliter.Flake.AttrAttr + "title"               + CONST.Spliter.Flake.AttrValue + this.win.options.title;
    result += CONST.Spliter.Flake.AttrAttr + "width"               + CONST.Spliter.Flake.AttrValue + this.win.width;
    result += CONST.Spliter.Flake.AttrAttr + "height"              + CONST.Spliter.Flake.AttrValue + this.win.height;
    result += CONST.Spliter.Flake.AttrAttr + "minimized"           + CONST.Spliter.Flake.AttrValue + this.win.isMinimized();
    result += CONST.Spliter.Flake.AttrAttr + "aspx.options.aspxId" + CONST.Spliter.Flake.AttrValue + this.aspx.options.aspxId;
    if(!this.queryStr)
    {
      result += CONST.Spliter.Flake.AttrAttr + "queryStr"+CONST.Spliter.Flake.AttrValue;
    }else{
      result += CONST.Spliter.Flake.AttrAttr + "queryStr"+CONST.Spliter.Flake.AttrValue + this.queryStr.replaceAll("=",Storage.EQSTR).replaceAll("&",Storage.ANDSTR);
    }
    return result;
  },

  loadingBegin : function()
  {
    this.setTitle("<img src='" + CONST.SiteUrl + "/" + CONST.Res.ImgLoading + "'/>"+I18N.Get("LABEL_PleaseWait"),true);
  },

  loadingEnd : function()
  {
    var temp = true;
    this.setTitle(this.getTitle(),temp);
    if(!this.aspx.options.dialog && Windows.modalWindows.length > 0 && Pages.focusedPage.ContainsFlake(this))           // ADD 20071030
    {
      this.hideSelect();
    }
  },

  //页片内关闭模式框,启用功能按钮
  enableButtons : function()
  {
    this.win.enableButtons();
  },

  //页片内弹出模式框,禁用功能按钮
  disableButtons : function()
  {
    this.win.disableButtons();
  },

  //隐藏页片内下拉框
  hideSelect : function()
  {
    try{JSUtil.DisableElements(this.win.content.contentWindow.document.getElementsByTagName(CONST.HtmlTag.Select));}catch(e){}
  },

  //显示页片内下拉框
  showSelect : function()
  {
    try{JSUtil.EnableElements(this.win.content.contentWindow.document.getElementsByTagName(CONST.HtmlTag.Select));}catch(e){}
  },
  
  //调整高度
  HeightResize : function(win,h)
  {
    var _height;
    if(h != undefined){
        _height = h;
    }else{
        _height = win.document.body.offsetHeight < 200 ? 200 : win.document.body.offsetHeight;
    }
    this.win.setSize(this.aspx.options.width,Flakes._calcFlakeMaxHeight(_height));
    if(event == null)
    {
        //手动调用时,记录到aspx.options._imadeHeight
        this.aspx.options._imadeHeight = _height;
    }
    this.win._recenter();
  }
}

var Flakes = {
  nextId          : 10000,
  flakes          : [],
  focusedFlake    : null,
  HelpIcon        : CONST.Res.ImgHelp,
  effectFlake     : null,
  effectTimes     : 0,
  effectInterval  : null,
  DockWidth       : 320,
  HeightN         : 30,
  HeightS         : 19,
  BorderHeight    : 49,
  WidthW          : 4,
  WidthE          : 4,
  BorderWidth     : 8,
  MinHeight       : 150,
  
  Start : function()
  {
    I18N.Init();
    Flakes.Init();
    Storage.Init();
    Storage.notify(Storage.EVENT.onInit);
    Storage.Read();
    Storage.InitEnd();
  },

  Init : function()
  {
    this._InitAttributes();
    this._InitObserver();
  },

  _InitAttributes : function(){},

  _InitObserver    : function()
  {
    var myObserver = {
      onRemoveWin : function(eventName,win)
      {
        var flake = Flakes.flakes.findByKey("win",win);
        if(flake != null && flake.onClose){
           flake.onClose.call();
        }
        Flakes.flakes = Flakes.flakes.without(flake);
        //20071017 chendong add if condition
        if(Flakes.focusedFlake != null && flake.id == Flakes.focusedFlake.id)
        {
          Flakes.focusedFlake = Flakes.flakes.length >= 1 ? Flakes.flakes[Flakes.flakes.length-1] : null;
        }
      },
      
      onFocusWin : function(eventName,win)
      {
        var flake = Flakes.flakes.findByKey("win",win);
        //20071017 chendong add if condition
        if(flake != null)
        {
          Flakes.focusedFlake = flake;
        }
      },

      onResizeWin : function(eventName,win)
      {
        Storage.notify(Storage.EVENT.onChangeStorage,
                       {
                         method:Storage.CHANGE.UpdateFlake,
                         flake_id:Flakes.focusedFlake.id,
                         flake_title:win.options.title,
                         flake_width:win.width,
                         flake_height:win.height,
                         flake_minimized:win.isMinimized() ? 1 : 0
                       }
                      );
      },
      
      // chendong 20080115 add observer to auto resize modal Flake's height
      onBeforeRecenterWin : function(eventName,win)
      {
        if(event == null)
        {
            return;
        }
        try{
        win.setSize(win.width,Flakes._calcFlakeTrueHeight(Flakes.FindFlakeByID(win.getId()).aspx));
        }catch(e){}
      },

      onShowInnerModel : function(eventName,flake)
      {
        //禁用焦点页片功能按钮
        if(flake)
        {
          flake.disableButtons();
        }
      },

      onHideInnerModel : function(eventName,flake)
      {
        //启用焦点页片功能按钮
        if(flake)
        {
          flake.enableButtons();
        }
      }
    }
    this.addObserver(myObserver);
  },
  
  onWinDeactivate : function()
  {
    if(   event.toElement != null
       && event.srcElement.id.substring(0,1) != event.toElement.id.substring(0,1)
       && Flakes.focusedFlake != null
       && Flakes.focusedFlake.aspx.options.dialog)
    {
      event.srcElement.contentWindow.focus();
    }
  },

  _getNextId : function()
  {
    this.nextId ++;
    var date = new Date();
    return "F" + date.format("YYYYMMDD") + this.nextId;
  },

  _checkQueryStr : function(queryStr)
  {
    var result = queryStr;
    if(!result){return "";}
    if(result.length > 2000)
    {
      var nStr = "";
      var qParams = result.split("&");
      for(var i=0;i<qParams.length;i++)
      {
        var qParam = qParams[i].split("=");
        if(qParam[1].length>1000)
        {
          qParam[1] = qParam[1].substring(0,998);
          alert(qParam[1]);
        }
        nStr += qParam[0] + "=" + qParam[1];
        if(i!=qParams.length-1)
        {
          nStr += "&";
        }
      }
      result= nStr;
    }
    return result;
  },
  
  //计算页片最大高度
  _calcFlakeMaxHeight : function(h)
  {
    var trueHeight = window.document.documentElement.clientHeight;
    if(trueHeight < Flakes.MinHeight){trueHeight = Flakes.MinHeight;}
    trueHeight -= Flakes.BorderHeight;
    trueHeight = Math.min(trueHeight,h);
    return trueHeight;
  },
  
  //计算页片真实高度
  _calcFlakeTrueHeight : function(aspx,oldflake)
  {
    var trueHeight = aspx.options.height;
    if(event == null || (event.type != "resize" && event.type != "scroll"))
    {
        if(aspx.options._imadeHeight){trueHeight = aspx.options.height;}
    }else{
        if(aspx.options._imadeHeight){trueHeight = aspx.options._imadeHeight;}
    }
    if(oldflake){trueHeight = oldflake.height;}
    else if(aspx.options.dialog){trueHeight += Flakes.BorderHeight;}
    if(aspx.options.dialog)
    {
      if(window.document.documentElement.clientHeight < trueHeight)
      {
        trueHeight = window.document.documentElement.clientHeight;
        if(trueHeight < Flakes.MinHeight){trueHeight = Flakes.MinHeight;}
      }
      trueHeight -= Flakes.BorderHeight;
      //trueHeight = Math.min(trueHeight,aspx.options.height);
    }
    return trueHeight;
  },
  
  //计算页片ID
  _calcFlakeID : function(oldflake)
  {
    var flakeid;
    if(oldflake)
    {
      flakeid = oldflake.flakeid;
    }
    if(!flakeid)
    {
      flakeid = this._getNextId();
    }
    else if(flakeid.substring(1,9) == new Date().format("YYYYMMDD"))
    {
      this.nextId = Math.max(this.nextId,parseInt(flakeid.substring(9)));
    }
    return flakeid;
  },
  
  /*
   @param aspxId : 画面ID
   @param m : 从2级菜单打开时为2
   */
  Menu3AddFlake : function(aspxId,m)
  {
    if(Storage.ActorID == CONST.Actors.GUEST)
    {
        if(Menu1s.NeedLoginMenu.indexOf(Menu1s.focusedMenu1.focusedMenu3.id) != -1)
        {
            Menu1s.AddLoginFlake();
            return;
        }
    }
    if(aspxId == CONST.MsgBoxAspx)
    {
        //我的信箱
        var f = Flakes.AddFlake(aspxId);
        f.onClose = function()
        {
            Storage.GetNewMsg();
        }
        return;
    }
    
    //是否全屏画面
    var isFullAspx = false;
    if(CONST.FullAspx.grep(aspxId).length > 0 && aspxId != "")
    {
        isFullAspx = true;
    }
    
    if(isFullAspx)
    {
        document.getElementById(Menu1s.focusedMenu1.idPrev + "divMain2Full").style.display = "";
        document.getElementById(Menu1s.focusedMenu1.idPrev + "divMain2").style.display = "none";
    }else{
        document.getElementById(Menu1s.focusedMenu1.idPrev + "divMain2Full").style.display = "none";
        document.getElementById(Menu1s.focusedMenu1.idPrev + "divMain2").style.display = "";
    }
    
    //取得对应画面配置项
    var aspx = Aspxes.aspxes[aspxId];
    
    //未找到画面配置
    if(aspx == undefined){Message.Show(Message.MSG.AspxConfigNotFound,aspxId);return;}
    
    var frmFlk = isFullAspx ? Menu1s.focusedMenu1.frameFlakeFull : Menu1s.focusedMenu1.frameFlake;
    
    try{
        //隐藏原画面的SELECT和OBJECT对象
        if(frmFlk.src!="")
        {
            frmFlk.contentWindow.PageMaster.disableSpecialObject();
        }
    }catch(e){}
    
    frmFlk.src    = aspx.options.url;
    frmFlk.height = aspx.options.height;
    frmFlk.aspxId = aspxId;
    
    if(isFullAspx)
    {
        Menu1s.focusedMenu1.frameFlake.src = "";
    }
    
    if(event == null){
        return;
    }
    window.scrollTo(0,0);
    if(Menu1s.focusedMenu1.body != Menu1s.lastBody)
    {
        Menu1s.lastBody.style.display = CONST.CSS.Display.None;
        Menu1s.focusedMenu1.body.style.display = CONST.CSS.Display.Empty;
        Menu1s.lastBody = Menu1s.focusedMenu1.body;
    }
    
    // title
    frmFlk.aspxTitle = event.srcElement.innerText;
    var strRoute = Menu1s.focusedMenu1.text+" > "+Menu1s.focusedMenu1.focusedMenu2.text;
    if(m != 2){
        strRoute += " > " + event.srcElement.innerText;
    }
    
    // 路径
    var mnRut = isFullAspx ? Menu1s.focusedMenu1.menuRouteFull : Menu1s.focusedMenu1.menuRoute;
    mnRut.innerText = strRoute;
    
    // 提示信息(Tips)
    if(!isFullAspx)
    {
        Menu1s.focusedMenu1.pageTip.innerHTML = aspx.options.tips;
        // 广告
        Menu1s.focusedMenu1.miniAdsArea.innerHTML = AdsManager.GetMiniAds(aspx.options.ads);
    }
    
    Flakes.focusedFlake = null;
    
    Flakes.MaskFirstLoading(frmFlk);
    
    Menu1s.focusedMenu1.HasPage = true;
  },
  
  //添加首次加载的loading画面
  MaskFirstLoading : function(frmFlk)
  {
    if(Menu1s.focusedMenu1.adivMask){
        Menu1s.focusedMenu1.adivMask.removeNode(true);
    }
    var frameFlakeTop  = JSUtil.CalcAbsoluteOffset(frmFlk,"offsetTop");
    var frameFlakeLeft = JSUtil.CalcAbsoluteOffset(frmFlk,"offsetLeft");
    Menu1s.focusedMenu1.adivMask = document.createElement(CONST.HtmlTag.Div);
    Menu1s.focusedMenu1.adivMask.id = "FrameFlakeDivMask";
    Menu1s.focusedMenu1.adivMask.style.position = "absolute";
    Menu1s.focusedMenu1.adivMask.style.height = frmFlk.offsetHeight;
    Menu1s.focusedMenu1.adivMask.style.width  = frmFlk.offsetWidth;
    Menu1s.focusedMenu1.adivMask.style.pixelTop = frameFlakeTop;
    Menu1s.focusedMenu1.adivMask.style.pixelLeft = frameFlakeLeft;
    var loadingImg = document.createElement(CONST.HtmlTag.Img);
    loadingImg.width="32";
    loadingImg.height="32";
    loadingImg.src = "img/v2/loading.gif";
    var loadingDiv = document.createElement(CONST.HtmlTag.Div);
    loadingDiv.style.textAlign = "right";
    loadingDiv.appendChild(loadingImg);
    Menu1s.focusedMenu1.adivMask.appendChild(loadingDiv);
    Menu1s.focusedMenu1.body.appendChild(Menu1s.focusedMenu1.adivMask);
    if(!frmFlk.loadingEventAttached)
    {   
        frmFlk.attachEvent("onload",Flakes.UnMaskFirstLoading);
        frmFlk.loadingEventAttached = true;
    }
  },
  
  //隐藏首次加载的loading画面
  UnMaskFirstLoading : function()
  { 
    event.cancelBubble = true;
    var mask = Menu1s.Get(event.srcElement.menu1ID).adivMask;
    if(!mask)
    {
        return;
    }
    Menu1s.Get(event.srcElement.menu1ID).adivMask.removeNode(true);
    Menu1s.Get(event.srcElement.menu1ID).adivMask = null;
  },

  AddFlake : function(aspxId,queryStr,oldflake,noborder)
  { 
    //检查查询字符串(是否超长)
    queryStr = this._checkQueryStr(queryStr);
    
    //取得对应画面配置项
    var aspx = Aspxes.aspxes[aspxId];    
   
    //未找到画面配置
    if(aspx == undefined){Message.Show(Message.MSG.AspxConfigNotFound,aspxId);return;}
    
    //已打开该页片
    if(   this.flakes.length > 0
       && this.flakes[this.flakes.length-1].aspx.options.aspxId == aspx.options.aspxId)
    {
        if(this.flakes[this.flakes.length-1].win.element)
        {
            this.flakes[this.flakes.length-1].win.element.focus();
        }
        return;
    }
    
    //打开模式片 && 显示父页片 && 存在父页片 > 隐藏父页片中DDL
    if(aspx.options.dialog && aspx.options.showParent && Flakes.focusedFlake){Flakes.focusedFlake.hideSelect();}
    
    //检查页片唯一性
//    if(Flakes._checkOnlyOne(aspxId,aspx,queryStr)){return;}
    
    //计算页片真实高度
    var trueHeight = Flakes._calcFlakeTrueHeight(aspx,oldflake);
    
    //计算页片ID
    var flakeid = Flakes._calcFlakeID(oldflake);

//    if(!aspx.options.dialog && Pages.tarCell == -1)
//    {
//      Pages.tarCell = Pages.focusedPage.calcTarCell();
//    }
    var win = new Window(flakeid,
                         {
                           className:         "dialog",
                           minWidth:          100, 
                           minHeight:         20,
                           resizable:         true,
                           closable:          aspx.options.closable,
                           minimizable:       aspx.options.minimizable,
                           maximizable:       aspx.options.maximizable,
                           editable:          aspx.options.editable,
                           icon:              aspx.options.icon,
                           dialog:            aspx.options.dialog,
                           showParent:        aspx.options.showParent,
                           updateParent:      aspx.options.updateParent,
                           scroll:            aspx.options.scroll,
                           helpAspx:          aspx.options.helpAspx,
                           draggable:         aspx.options.dialog ? false : true,
                           userData:          null,
                           //showEffect:        Effect.BlindDown, 
                           showEffect:        Element.show, 
                           hideEffect:        Effect.SwitchOff, 
                           showEffectOptions: {},
                           hideEffectOptions: {},
                           effectOptions:     null,
                           parent:            document.body, //aspx.options.dialog ? document.body : Pages.focusedPage.table.cells[Pages.tarCell],
                           title:             oldflake ? I18N.Get(oldflake.title) : I18N.Get(aspx.options.title),
                           url:               (queryStr && queryStr != "" ? aspx.options.url.split("?")[0] + "?" + queryStr : aspx.options.url),
                           onload:            Prototype.emptyFunction,
                           width:             aspx.options.dialog ? (oldflake ? oldflake.width  : aspx.options.width) : Flakes.DockWidth,
                           height:            trueHeight,
                           opacity:           1,
                           recenterAuto:      true,
                           wiredDrag:         false,
                           noborder:          noborder
                         }
                        );
    win.setDestroyOnClose();
    var lastFlake = this.focusedFlake;
    if(aspx.options.dialog)
    {
      win.showCenter(true);
      win.element.setStyle({position : CONST.CSS.Position.Absolute});
    }else{
      win.show();
      win.element.setStyle({position : CONST.CSS.Position.Relative});          // ADD 20071030
    }

    var flake = new Flake(flakeid,aspx,queryStr);
    flake.win = win;
    
    //从其它页片打开,非从菜单打开 > 父页片为前一焦点页片
    if(event == null){flake.parent = lastFlake;}
    
    //置当前页片为焦点页片
    this.focusedFlake = flake;
    this.flakes.push(flake);

    flake.loadingBegin();

    return flake;
  },

  _parseUrl : function(tarUrl,srcUrl)
  {
    if(tarUrl.substring(0,4) == "http"){return tarUrl;}
    var srcArr = srcUrl.split("/");
    var tarArr = tarUrl.split("/");
    var idx = 0;
    var url = "";
    for(var i=0;i<srcArr.length;i++)
    {
      if(srcArr[i] != tarArr[i]){idx = i;break;}
    }
    var step = srcArr.length - idx - 1;
    for(var i=0;i<step;i++){url += "../";}
    for(var i=idx;i<tarArr.length;i++)
    {
      url += tarArr[i];
      if(i != tarArr.length-1){url += "/";}
    }
    return url;
  },

  AddFlakeByPost : function(aspxId,formId)
  {
    var srcFlake = Flakes.focusedFlake;
    var tarFlake = null;
    var tarAspx = Aspxes.aspxes[aspxId];
    if(tarAspx == null)
    {
      Message.Show(Message.MSG.AspxConfigNotFound,aspxId);
      return;
    }
    if(tarAspx.options.onlyOne){tarFlake = this.FindFlake(aspxId);}
    if(tarFlake == null){tarFlake = this.AddFlake(aspxId);}
    if(formId == undefined){formId = 0;}
    var srcForm = srcFlake.win.content.contentWindow.document.forms[formId];
    srcForm.target = tarFlake.win.content.id;
    var srcUrl = srcFlake.aspx.options.url;
    var tarUrl = tarAspx.options.url;
    srcForm.action = this._parseUrl(tarUrl,srcUrl);
    srcForm.submit();
  },

  FindParentFlake : function()
  {
    try{return Flakes.focusedFlake.parent;}catch(e){}
  },

  FindPnlEdit : function(win)
  {
    try{return JSUtil.getElementByIdEndWith(win.document,"pnlEdit");}catch(e){}
  },

  FindFlakeByID : function(id)
  {
    var result = null;
    for(var i=0;i<this.flakes.length;i++)
    {
      if(this.flakes[i].id == id)
      {
        result = this.flakes[i];
        break;
      }
    }
    return result;
  },

  FindParentFlakeWindow : function()
  {
    if(Windows.modalWindows.length > 1)
    {
        try{return this.FindParentFlake().win.content.contentWindow;}catch(e){}
    }
    else
    {
        var w;
        try
        {
            if(Menu1s.focusedMenu1.frameFlake.src != "")
            {
                w = Menu1s.focusedMenu1.frameFlake.contentWindow;
            }else{
                w = Menu1s.focusedMenu1.frameFlakeFull.contentWindow;
            }
        }
        catch(e)
        {
            w = Menu1s.focusedMenu1.frameFlakeFull.contentWindow;
        }
        return w;
    }
  },
  
  FindParentFlakeWindowFull : function()
  {
    if(Windows.modalWindows.length > 1)
    {
        try{return this.FindParentFlake().win.content.contentWindow;}catch(e){}
    }
    else
    {
        try{return Menu1s.focusedMenu1.frameFlakeFull.contentWindow;}catch(e){}
    }
  },

  FindFlake : function(aspxId,bool)
  {
    var result  = null;
    var results = new Array();
    this.flakes.each(
      function(f)
      {
        if(f.aspx.options.aspxId == aspxId)
        {
          result = f;
          results.push(f);
          if(!bool){throw $break;}
        }
      }
    );
    if(bool){return results;}
    else{return result;}
  },

  FindFlakeWindow : function(aspxId,bool)
  {
    var result  = null;
    var results = new Array();
    this.flakes.each(
      function(f)
      {
        if(f.aspx.options.aspxId == aspxId)
        {
          result = f.win.content.contentWindow;
          results.push(f.win.content.contentWindow);
          if(!bool)
          {
            throw $break;
          }
        }
      }
    );
    if(bool){return results;}
    else{return result;}
  },

  //edit focused flake
  EditMe : function(win)
  {
    var flake = Flakes.GetCurFlake(win);
    try{flake.Edit();}catch(e){}
  },

  //minimize focused flake
  MinimizeMe : function(win)
  {
    var flake = Flakes.GetCurFlake(win);
    try{flake.Minimize();}catch(e){}
  },

  //maximize focused flake
  MaximizeMe : function(win)
  {
    var flake = Flakes.GetCurFlake(win);
    try{flake.Maximize();}catch(e){}
  },

  //close focused flake
  CloseMe : function(win)
  {
    var flake = Flakes.GetCurFlake(win);
    try{flake.Close();}catch(e){}
  },

  //refresh focused flake
  RefreshMe : function(win)
  {
    var flake = Flakes.GetCurFlake(win);
    try{flake.Refresh();}catch(e){}
  },

  //close this flake and add new flake
  Transfer2Flake : function(aspxId,queryStr)
  {
    try{
      var focusid;
      if(this.focusedFlake)
      {
        focusid = this.focusedFlake.id;
        this.FindFlakeByID(focusid).Close();
        var newFocusFlake = this.AddFlake(aspxId,queryStr);
        this.focusedFlake = newFocusFlake;
      }else{
        this.Menu3AddFlake(aspxId,queryStr);
      }
      
    }catch(e){}
  },

  //edit focused flake's title
  SetTitle : function(title,temp)
  {
    try{this.focusedFlake.setTitle(title,temp);}catch(e){}
  },

  //get title
  GetTitle : function()
  {
    try{return this.focusedFlake.getTitle();}catch(e){}
  },

  LoadingBegin : function(id)
  {
    if(!id)
    {
      try{this.focusedFlake.loadingBegin();}catch(e){}
    }else{
      var f = Flakes.flakes.findByKey("id",id);
      if(f != null){f.loadingBegin();}
    }
  },

  LoadingEnd : function(id)
  {
    if(!id)
    {
      try{this.focusedFlake.loadingEnd();}catch(e){}
    }else{
      var f = Flakes.flakes.findByKey("id",id);
      if(f != null){f.loadingEnd();}
    }
  },

  ShowFocusEffect : function()
  {
    Flakes.effectTimes ++;
    var opa;
    if(Flakes.effectTimes < 5)
    {
      opa = 1-Flakes.effectTimes * 0.2;
      Flakes.effectFlake.win.element.setStyle({opacity : opa});
    }else if(Flakes.effectTimes < 10){
      opa = 0 + (Flakes.effectTimes-5) * 0.2;
      Flakes.effectFlake.win.element.setStyle({opacity : opa});
    }else{
      opa = 1;
      Flakes.effectFlake.win.element.setStyle({opacity : opa});
      Flakes.effectTimes = 0;
      Flakes.effectFlake = null;
      clearInterval(Flakes.effectInterval);
      Flakes.effectInterval = null;
    }
  },

  Focus : function(win)
  {
    var fid = Flakes.GetWinId(win);
    if(fid){Flakes.focusedFlake = Flakes.FindFlakeByID(fid);}
  },

  GetWinId : function(win)
  {
    var obj = win.frameElement;
    while(obj.tagName != CONST.HtmlTag.Div)
    {
      obj = obj.parentNode;
    }
    return obj.id;
  },

  GetCurFlake : function(win)
  {
    var flake = null;
    var winId = null;
    if(win){winId = Flakes.GetWinId(win);}
    if(winId){flake = Flakes.FindFlakeByID(winId);}
    else{flake = Flakes.focusedFlake;}
    return flake;
  },

  closeFlake : function(id)
  {
    if(!id) return;
    try{Flakes.FindFlakeByID(id).Close(event);}catch(e){}
  },

  minimizeFlake : function(id)
  {
    if(!id) return;
    try{Flakes.FindFlakeByID(id).Minimize(event);}catch(e){}
  },

  maximizeFlake : function(id)
  {
    if(!id) return;
    try{Flakes.FindFlakeByID(id).Maximize(event);}catch(e){}
  },

  editFlake : function(id)
  {
    if(!id) return;
    try{Flakes.FindFlakeByID(id).Edit(event);}catch(e){}
  },
  
  DisableCurrentPageFlakeSelect : function()
  {
    try{Menu1s.focusedMenu1.frameFlake.contentWindow.PageMaster.disableElements(Menu1s.focusedMenu1.frameFlake.contentWindow.document.getElementsByTagName(CONST.HtmlTag.Select));}catch(e){}
    try{Menu1s.focusedMenu1.frameFlake.contentWindow.PageMaster.disableElements(Menu1s.focusedMenu1.frameFlake.contentWindow.document.getElementsByTagName(CONST.HtmlTag.Object));}catch(e){}
    try{Menu1s.focusedMenu1.frameFlakeFull.contentWindow.PageMaster.disableElements(Menu1s.focusedMenu1.frameFlakeFull.contentWindow.document.getElementsByTagName(CONST.HtmlTag.Select));}catch(e){}
    try{Menu1s.focusedMenu1.frameFlakeFull.contentWindow.PageMaster.disableElements(Menu1s.focusedMenu1.frameFlakeFull.contentWindow.document.getElementsByTagName(CONST.HtmlTag.Object));}catch(e){}
    try{JSUtil.DisableElements(document.getElementsByTagName(CONST.HtmlTag.Object));}catch(e){}
  },

  EnableCurrentPageFlakeSelect : function()
  {
    try{Menu1s.focusedMenu1.frameFlake.contentWindow.PageMaster.enableElements(Menu1s.focusedMenu1.frameFlake.contentWindow.document.getElementsByTagName(CONST.HtmlTag.Select));}catch(e){}
    try{Menu1s.focusedMenu1.frameFlake.contentWindow.PageMaster.enableElements(Menu1s.focusedMenu1.frameFlake.contentWindow.document.getElementsByTagName(CONST.HtmlTag.Object));}catch(e){}
    try{Menu1s.focusedMenu1.frameFlakeFull.contentWindow.PageMaster.enableElements(Menu1s.focusedMenu1.frameFlakeFull.contentWindow.document.getElementsByTagName(CONST.HtmlTag.Select));}catch(e){}
    try{Menu1s.focusedMenu1.frameFlakeFull.contentWindow.PageMaster.enableElements(Menu1s.focusedMenu1.frameFlakeFull.contentWindow.document.getElementsByTagName(CONST.HtmlTag.Object));}catch(e){}
    try{JSUtil.EnableElements(document.getElementsByTagName(CONST.HtmlTag.Object));}catch(e){}
  },

  //处理按键事件  
  DoOnKeyDown : function(e)
  {
    if(Windows.modalWindows.length == 0)
    {
        //无模式窗口,不处理
        return;
    }
    //焦点定位到当前模式页
    Windows.modalWindows[Windows.modalWindows.length-1].element.focus();
    event.cancelBubble = true;
  }
}
Object.extend(Flakes,StaticObserver);

window.document.attachEvent("onkeydown",Flakes.DoOnKeyDown);
