﻿/*
  BlueWindow等是精灵部落的原创代码，在作者未正式决定将其开源发布之前，任何组织和个人不得以任何方式获得
和传播此代码，
  不得以任何方式修改和使用此代码，否则，精灵部落将追究责任。 
   
  严正声明现在精灵部落正处在测试期间，所有代码均可能存在不稳定因素，所有代码除特别声明开源的，一律暂
不开源，一
  律严格保留所有权以及其他一切合法权益。谢谢！
  作者：廖彩彪 2007年12月 联系我：email  ——ublue2006@163.com——
*/
// JScript 文件
var BlueWindow=function(options)
{
  if(options!=null){
  var self=this;
  this.id=null;
  this.taskiconid=null;
  this.win=null;
  this.enabled=false;
  this.ismodal=false;
  
  this.options={
  parentwinid:null,
  childwinid:null,
  classname:BlueWindows.DefaultClassName,
  statusclassname:BlueWindows.StatusClassName,
  parent:document.body,
  closable:true,
  minsizeable:true,
  maxsizeable:true,
  resizeable:true,
  dragable:true,
  openmodal:false,
  showstatus:true,
  title:"BlueWindow",
  content:"&nbsp;",
  haseffect:true,
  minwidth:200,
  minheight:150,
  width:500,
  height:400,
  x:100,
  y:100
  };
  
  if(options.id!=null)this.id=options.id;
  if(options.url!=null)this.options.url=options.url;  
  if(options.classname!=null)this.options.classname=options.classname;
  if(options.statusclassname!=null)this.options.statusclassname=options.statusclassname;
  if(options.closable!=null)this.options.closable=options.closable;
  if(options.minsizeable!=null)this.options.minsizeable=options.minsizeable;
  if(options.maxsizeable!=null)this.options.maxsizeable=options.maxsizeable;
  if(options.resizeable!=null)this.options.resizeable=options.resizeable;
  if(options.dragable!=null)this.options.dragable=options.dragable;  
  if(options.title!=null)this.options.title=options.title;  
  if(options.content!=null)this.options.content=options.content; 
  if(options.haseffect!=null)this.options.haseffect=options.haseffect;
  if(options.minwidth!=null)this.options.minwidth=options.minwidth;
  if(options.minheight!=null)this.options.minheight=options.minheight;
  if(options.width!=null)this.options.width=options.width;
  if(options.height!=null)this.options.height=options.height;
  if(options.x!=null)this.options.x=options.x;
  if(options.y!=null)this.options.y=options.y;    
  if(options.icon!=null)this.options.icon=options.icon; 
  if(options.showstatus!=null)this.options.showstatus=options.showstatus;  
  this.options.openmodal=options.openmodal?options.openmodal:false;
  this.options.openmax=options.openmax?options.openmax:false;  
  this.options.opencenter=options.opencenter?options.opencenter:false;     
  this.options.status=options.status?options.status:"精灵空间"; 
  this.options.parentwinid=options.parentwinid?options.parentwinid:null;
  this.options.childwinid=options.childwinid?options.childwinid:null;
  
  if(this.options.haseffect==true&&this.options.openmax==true)this.options.haseffect=false;
  if(this.options.haseffect==true&&this.options.showstatus==false&&document.all)this.options.haseffect=false;
  
  this.storage=
  {
    subHeight:null,
    subWidth:null,
    state:"onhide",
    height:null,
    width:null,
    x:null,
    y:null,
    zindex:-1,
    drager:null    
  };
  
 this.initialize(options); }else{alert("缺少参数，初始化错误")}
  
};

BlueWindow.prototype={

initialize:function(options)
{  
  var tempstr=Math.random();
  tempstr=tempstr+"0";  
  this.id=options.id?options.id:"window_" + new Date().getTime()+tempstr.substring(2);
  this.taskiconid="task_"+this.id;
  if(BlueWindows.register(this)==true)
  {
  this.win=this._createWindow(this.id); 
  this.enabled=true;
  }
  else
  {
   this.enabled=false;
   if(this.options.openmodal==true)
   {
   var TempWin=BlueWindows.getWin(this.id);
   if(TempWin!=null)
   BlueWindows.showwin(this.id);
   BlueWindows.focuswin(this.id);
   }
   else
   alert("id为"+this.id+"的窗口已经在系统中存在，不能重复！"); 
  }
  //parseFloat
  //alert(this.storage.subHeight);
  
},

open:function(IsModal)
{ 
      if(this.enabled==true)
      {
          if(this.options.opencenter==true)
          {
            this.options.x=(document.body.clientWidth-this.options.width)/2;
            this.options.y=(document.body.clientHeight-this.options.height)/2;
          }
          if(this.options.x<0)this.options.x=0;
          if(this.options.y<0)this.options.y=0;
          
          BlueWinEffects.resizeWin({Win:this,StartWidth:this.options.minwidth,StartHeight:this.options.minheight,EndWidth:this.options.width,EndHeight:this.options.height,HasEffect:this.options.haseffect,EndX:this.options.x,EndY:this.options.y}); 
          BlueWinEffects.show({Win:this,HasEffect:this.options.haseffect});  
          var src=this.options.icon?this.options.icon.Src:null;
          var Icon=new TaskIcon({id:this.taskiconid,Src:src,Title:this.options.title,WinID:this.id});
          
          BlueTaskBar.InsertIcon({icon:Icon});
          BlueWindows.focuswin(this.id);
                   
          if(IsModal!=null&&IsModal==true)
          {
            this.ismodal=true;
            BlueWindows.ShowModalCover();  
            
            this.storage.zindex++;
            this.win.style.zIndex=this.storage.zindex;
            var winZindex=this.storage.zindex;
            BlueWindows.SetModalCoverZindex(winZindex-1);
          } 
          
          if(this.options.showstatus==false)
          {
            this.hidestatus();
          }
          
          if(this.options.openmax==true)
          {
            BlueWindows.maximize(this.id);
          } 
      }   
  
},

_createWindow: function(id) {
    var className = this.options.classname;
    var statusclassname=this.options.statusclassname;
    var win = document.createElement("div");
    var blank = "../themes/blank.gif";
    win.setAttribute('id', id);
    win.className = "dialog";
    win.style.width=this.options.minwidth;
    win.style.height=this.options.minheight;
    
    var content;
    if (this.options.url)
      content= "<iframe frameborder=\"0\" name=\"" + id + "_content\"  id=\"" + id + "_content\" src=\"" + this.options.url + "\" style=\"width:100%\"> </iframe>";
    else
      content ="<div id=\"" + id + "_content\" class=\"" +className + "_content\" style='width:100%'>"+this.options.content+"</div>";    
    
    var status=this.options.status;
    
    var closeDiv = this.options.closable ? "<div class='"+ className +"_close' id='"+ id +"_close' onclick='BlueWindows.close(\""+ id +"\", event)'> </div>" : "";
    var minDiv = this.options.minsizeable ? "<div class='"+ className + "_minimize' id='"+ id +"_minimize' onclick='BlueWindows.minimize(\""+ id+"\", event)'> </div>" : "";
    var maxDiv = this.options.maxsizeable ? "<div class='"+ className + "_maximize' id='"+ id +"_maximize' onclick='BlueWindows.maximize(\""+ id +"\", event)' > </div>" : "";
	
	if(this.options.minsizeable==true&&this.options.maxsizeable==false)
	    maxDiv ="<div class='"+ className + "_maximize_disable' id='"+ id +"_maximize'> </div>";
	if(this.options.closable==false&&(this.options.minsizeable==true||this.options.maxsizeable==true))
	   closeDiv = "<div class='"+ className +"_close_disable' id='"+ id +"_close'> </div>";
	
	var dbclickmaxsize=this.options.maxsizeable ?"ondblclick='BlueWindows.maximize(\""+ id +"\", event)'":"";
	
	var StatusIconHTML="";
	if(this.options.icon!=null)	
	{
	var iconDiv="<div class='"+className + "_Icon' >"+this.options.icon.HTML+"</div>";
	// alert(this.options.icon.HTML);
	var StatusIcon=new BlueIcon({Width:50,Height:50,Src:this.options.icon.Src});
	StatusIconHTML=StatusIcon.HTML;
	}
	else
	{
	var iconDiv="<div class='"+className + "_Icon' >"+BlueWindows.defaulticon().HTML+"</div>";
	var StatusIcon=new BlueIcon({Width:50,Height:50,Src:BlueWindows.defaulticon().Src});
	StatusIconHTML=StatusIcon.HTML;
	}
	
    var seAttributes = this.options.resizable ? "class='" + className + "_sizer' id='" + id + "_sizer'" : "class='"  + className + "_se'";
    win.innerHTML = closeDiv + minDiv + maxDiv + iconDiv+"\
      <table id='"+ id +"_row1' class=\"top table_window \" "+dbclickmaxsize+" oncontextmenu=\"BlueWindows.TopContextmenu('"+this.id+"',event)\">\
        <tr>\
          <td class='"+ className +"_nw'></td>\
          <td class='"+ className +"_n' id='"+ id +"_titletd'>\
          <div id='"+ id +"_title' class='"+ className +"_title' style='overflow:hidden; height:18px;'>"+ this.options.title +"</div></td>\
          <td class='"+ className +"_ne'></td>\
        </tr>\
      </table>\
      <table id='"+ id +"_row2' class=\"mid table_window\">\
        <tr>\
          <td class='"+ className +"_w'></td>\
            <td id='"+ id +"_table_content' class='"+ className +"_content' valign='top'>" + content + "</td>\
          <td class='"+ className +"_e' ></td>\
        </tr>\
      </table>\
      <table class=\"mid table_window\"  id=\""+id+"_StatusArea\">\
        <tr>\
          <td class='"+ className +"_w'></td>\
            <td id='"+ id +"_table_status' class='"+ className +"_status'>\
			<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\""+statusclassname+"_Status\">\
			  <tr>\
				<td class=\""+statusclassname+"_Status_Top\">&nbsp;</td>\
			  </tr>\
			  <tr>\
				<td class=\""+statusclassname+"_Status_Content\">\
				<table style=\"width:100%;\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\
					<tr>\
					  <td id=\""+id+"_StatusIcon\" style=\"width:60px; height:50px; \">"+StatusIconHTML+"</td>\
					  <td style='font-size:12px; color:#004F7C; overflow:hidden;' id='"+ id +"_statuscontenttd'>\
					  <div id='"+ id +"_statuscontent' style='width:100%; height:50px; font-size:12px; color:#004F7C; overflow:hidden;'>\
					  <strong>"+this.options.title+"</strong><br><span id=\""+id+"_BlueWinStatus\">"+ status + "</span></div></td>\
					</tr>\
				  </table>\
				</td>\
			  </tr>\
			</table>\
			</td>\
          <td class='"+ className +"_e'></td>\
        </tr>\
      </table>\
        <table id='"+ id +"_row4' class=\"bot table_window\">\
        <tr>\
          <td class='"+ className +"_sw'></td>\
            <td class='"+ className +"_s' style=\"font-size:2px;\">&nbsp;</td>\
            <td " + seAttributes + " id='"+id+"_resizer' style='cursor:nw-resize;'></td>\
        </tr>\
      </table>\
    ";
    //Element.hide(win);   
    this.win=win; 
    this.options.parent.insertBefore(win, this.options.parent.firstChild);  
    //win.style.display="none";      
    this.storage.subHeight=document.getElementById(id+"_row1").offsetHeight+document.getElementById(id+"_StatusArea").offsetHeight+document.getElementById(id+"_row4").offsetHeight;
    this.storage.subWidth=BlueEffects.getCurrentSize({testClass:className +"_w"}).width+BlueEffects.getCurrentSize({testClass:className +"_e"}).width;
   
    win.style.display="none"; 
    var MyWin=BlueWindows.getWin(id);    
    
    if(this.options.dragable==true) 
    {  
      var Opt={DragBar:document.getElementById(id+"_row1"),DragObj:win,Start:function(){BlueWindows.focuswin(id);},Move:function(){BlueWindows.store(id);}};
     //BlueEffects.Drag();   
     this.storage.drager=new BlueDrag(Opt);
     
     } 
        
    if(this.options.resizeable==true)      
    BlueWinEffects.InitReSize({ReSizeBar:document.getElementById(id+"_resizer"),Win:MyWin});  
    return win;
  },
  
  settitle:function(title)
  {
     this.options.title=title;
     document.getElementById(this.id +"_title").innerHTML=this.options.title;
  },
  
  setcontent:function(content)
  {
     if (!this.options.url)
     {
       var _content=document.getElementById(this.id+"_content");
       _content.innerHTML=content;
     }
  },
  
  hidestatus:function()
  {    
    var _StatusArea=document.getElementById(this.id+"_StatusArea");
    if( _StatusArea.style.display!="none")
    {
    this.win.style.height=(this.win.offsetHeight-_StatusArea.offsetHeight)+"px"; 
    this.storage.subHeight-=_StatusArea.offsetHeight;
    _StatusArea.style.display="none";
    BlueWinEffects.resizeWin({Win:this,HasEffect:this.options.haseffect,EndHeight:this.storage.height+_StatusArea.offsetHeight,EndWidth:this.storage.width});
    }
  },
  maxsize:function()
  {
  BlueWindows.maximize(this.id);
  }
  
};

var BlueWindows={

Windows:[],
TaskBarHeight:38,
focusedwin:null,
maxzindex:-1,
menuWidth:120,
menuHeight:110,
DefaultClassName:"Vista",
StatusClassName:"Vista",
allhide:false,

defaulticon:function()
{
 if(BlueIcons!=null)
 {
 var myicon=new BlueIcon({Width:25,Height:25});
 return myicon;}
},
 
register:function(Win){
    if(BlueWindows.getWin(Win.id)==null)
    {
      if(BlueWindows.maxzindex==-1)
      BlueWindows.maxzindex=1;
      else
      BlueWindows.maxzindex++;      
      Win.storage.zindex=BlueWindows.maxzindex;      
      BlueWindows.Windows.push(Win);	
      return true;
    }
    else
      return false;
},
 
getWin:function(id){
  var TempWin=null;
  for(var i=0;i<BlueWindows.Windows.length;i++)
  {
    if(BlueWindows.Windows[i].id==id)
	{
	TempWin=BlueWindows.Windows[i];
	break;
	}
  }
  return TempWin;
},

close:function(id)
{ 
  try{HideAllMenu();}
  catch(err){}
  
  var Win=BlueWindows.getWin(id);  
  if(Win!=null){
  if(Win.ismodal==true)
  {
    BlueWindows.HideModalCover();
    
    if(Win.options.parentwinid!=null)
    {
    var ParentWin=BlueWindows.getWin(Win.options.parentwinid);
    if(ParentWin!=null)
    ParentWin.options.childwinid=null;
    }
    
  }  
    BlueWinEffects.close({Win:Win,HasEffect:Win.options.haseffect});  
  }
},

minimize:function(id)
{
  try{HideAllMenu();}
  catch(err){}
  var Win=BlueWindows.getWin(id);  
  BlueWinEffects.hide({Win:Win,HasEffect:Win.options.haseffect});
  Win.storage.state="onhide";
},

hideall:function()
{
    if(BlueWindows.allhide==false)
    {
       if(BlueWindows.Windows.length>0)
       {
         for(var i=0;i<BlueWindows.Windows.length;i++)
         {
             var Win=BlueWindows.Windows[i];  
              BlueWinEffects.hide({Win:Win,HasEffect:false});
              Win.storage.state="onhide";
         }
         
         BlueWindows.allhide=true;
       }
   }
   else
   {
       if(BlueWindows.Windows.length>0)
       {
         for(var i=0;i<BlueWindows.Windows.length;i++)
         {
             var Win=BlueWindows.Windows[i];  
              BlueWinEffects.show({Win:Win,HasEffect:false}); 
              Win.storage.state="onshow";
         }
         
         BlueWindows.allhide=false;
       }
   }
   
},

maximize:function(id)
{
  try{HideAllMenu();}
  catch(err){}
  var Win=BlueWindows.getWin(id);
  var Width=document.body.clientWidth;
  var Height=document.body.clientHeight;
  if(Win.storage.state=="onmaxsize")
  {
     BlueWinEffects.resizeWin({Win:Win,HasEffect:Win.options.haseffect,EndWidth:Win.storage.width,EndHeight:Win.storage.height,EndX:Win.storage.x,EndY:Win.storage.y});
     Win.storage.state="onshow";    
     Win.storage.drager.InitDrag();
  }
  else if(Win.storage.state=="onshow")
  {  
  BlueWinEffects.resizeWin({Win:Win,HasEffect:Win.options.haseffect,EndWidth:Width,EndHeight:Height-BlueWindows.TaskBarHeight,EndX:0,EndY:0,Store:false});
  Win.storage.state="onmaxsize"; 
   Win.storage.drager.RemoveDrag();
  } 
  BlueWindows.focuswin(id); 
  if(Win.storage.state=="onhide")
  BlueWinEffects.show({Win:Win,HasEffect:Win.options.haseffect});   
},

focuswin:function(id)
{
 if(BlueWindows.focusedwin==null||(BlueWindows.focusedwin!=null&&BlueWindows.focusedwin.id!=id)){
  
 var Win=BlueWindows.getWin(id);
 
 if(Win.options.childwinid!=null&&BlueWindows.getWin(Win.options.childwinid)!=null)
 {
  var childid=Win.options.childwinid;
  Win=null;
  Win=BlueWindows.getWin(childid);  
 }
 
 BlueWindows.focusedwin=Win;
 
 BlueWindows.maxzindex++;      
 Win.storage.zindex=BlueWindows.maxzindex; 
 Win.win.style.zIndex=Win.storage.zindex;
 
 BlueTaskBar.FocuseIcon(Win.taskiconid);

 } 
},

showwin:function(id)
{
  var Win=BlueWindows.getWin(id);
  if(Win.storage.state=="onhide")
  BlueWinEffects.show({Win:Win,HasEffect:Win.options.haseffect}); 
},

removewin:function(id)
{
  var Index=-1;
  var Win=null;
  for(var i=0;i<BlueWindows.Windows.length;i++)
  {
    if(BlueWindows.Windows[i].id==id)
	{
	 Index=i;
	 Win=BlueWindows.Windows[i];
	 break;
	}
  }
  if(Index!=-1)
  {
   if(Win==BlueWindows.focusedwin)
   BlueWindows.focusedwin=null;
   
   if(Win.storage.drager!=null)
   {
    try{Win.storage.drager.RemoveDrag();}catch(err){}
    Win.storage.drager=null;
   }
    
   Win.win.innerHTML="";
   Win.options.parent.removeChild(Win.win);
   Win=null;
   BlueWindows.Windows.splice(Index,1); 
  
  if(document.all)
   CollectGarbage();
  } 
},

TopContextmenu:function(id,event)
{
  var x=event.clientX;
  var y=event.clientY;
  
  if(x>document.body.clientWidth)
   x=x-BlueWindows.menuWidth;
  if(y+BlueWindows.menuHeight>document.body.clientHeight)
   y=y-BlueWindows.menuHeight;
  
  var e=event;
  
   if ( e && e.button == 2 && e.preventDefault )
        {      
         TaskIconRightMenu.Show(id,x,y); 
        e.preventDefault();        
        }
    else{  
        TaskIconRightMenu.Show(id,x,y);     
        window.event.returnValue = false;
        }  
},

store:function(id)
{ 
 var Win=BlueWindows.getWin(id); 
 Win.storage.x=Win.win.offsetLeft;
 Win.storage.y=Win.win.offsetTop; 
},

 ShowModalCover:function ()
  {
   var cover=document.getElementById("modalcover");
   if(cover==null)
   cover=BlueWindows.CreateModalCover();
   cover.style.display="block";  
   cover.style.height=document.body.clientHeight;
   cover.style.width=document.body.clientWidth; 
  },
  
  SetModalCoverZindex:function(zindex)
  {
   var cover=document.getElementById("modalcover");
   if(cover!=null)
   cover.style.zIndex=zindex;
  },
  
  HideModalCover:function()
  {
   var cover=document.getElementById("modalcover");
   if(cover==null)
   cover=BlueWindows.CreateModalCover();
   cover.style.display="none";
  },
     
  CreateModalCover:function()
  {
      var cover = document.createElement("div");
      cover.setAttribute('id', "modalcover");
      cover.style.height=document.body.clientHeight;
      cover.style.width=document.body.clientWidth;
      cover.style.zIndex=0;
      cover.style.position="absolute";
      cover.style.backgroundColor="#000000";
      cover.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=10)" ;  
      cover.style.MozOpacity=0.1;
      cover.style.display="none";
            
      try{
      document.body.insertBefore(cover, document.body.firstChild); return cover;}catch(err){alert("document.body对象不能为空"); return null;}    
  }

}


