
/* 
shawl.qiu code
class: DragBox
version: 3.0
date: 2008-6-28

updated: ver, name, date, summary.
updated: ver, name, date, summary.

*/

if(typeof DragBoxCount=="undefined") DragBoxCount = 0;
if(typeof window.GlobalZIndex=="undefined") window.GlobalZIndex = 999;
if(typeof window.GlobalScreenSize=="undefined") window.GlobalScreenSize = fGetViewportSizeX();

if(typeof __GlobalDragBoxTitleTpl=="undefined")
{
  __GlobalDragBoxTitleTpl =
  [
  "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"DragBox_Title\" id=\"DragBoxTitleTplTable\">\n"
  ,"  <tr>  \n"
  ,"    <td class=\"DragBox_Left\"></td>\n"
  ,"    <td class=\"DragBox_Center\"></td>\n"
  ,"    <td class=\"DragBox_Right\"></td>\n"
  ,"  </tr>\n"
  ,"</table>\n"
  ].join('');
}/* if(typeof __GlobalDragBoxTitleTpl=="undefined") */

var GCount = 0;

function DragBox(SetIndex)
{
  var This = this;
  
  This.Set = [];
  This.Path = "/Js/QWJsLib/DragBox3/";
  This.Style = "Default";
  
  This.CloseText = "";
  This.MiniText = "";
  
  This.X = {};
  
  if(typeof SetIndex!="undefined") GlobalZIndex = SetIndex;
  This.Index = ++DragBoxCount;
 
  if(typeof __DragBoxTitleTplTable=="undefined")
  {
    var __Span = __E("span");
    __Span.innerHTML = __GlobalDragBoxTitleTpl;
    __Span.style.display = "none";
    document.body.appendChild(__Span);
    __DragBoxTitleTplTable = __Id("DragBoxTitleTplTable");
  }
}/* function DragBox(SetIndex) */

DragBox.prototype.Main = DragBox_Main;
DragBox.prototype.Add = DragBox_Add;
DragBox.prototype.RelativePath = fRelativePath;

function DragBox_Main()
{
  var This = this, detlaX = 0, detlaY = 0, Url = "";
  
  if(This.Set.length > 0)
  {
    for(var i=0, j=This.Set.length; i<j; i++)
    {
      This.Set[i].Ele.TitleEle.className = "DragBox_Title_UnActive";
    }
    This.Set[This.Set.length  -1].Ele.TitleEle.className = "DragBox_Title";
    fAddClass(This.Set[This.Set.length  -1].Ele, "DragBoxMain"+This.Index+"_Active");
  }
  
  for(var i = 0, j = This.Set.length; i < j; i++ )
  {
    var Ele = This.Set[i].Ele;
    
    if(Ele.DragBoxItemInited) continue;
    
    Ele.DragBoxItemInited = true;
    
    var TitleEle = Ele.TitleEle;
    TitleEle.Parent = Ele;
    Ele.This = This;
    TitleEle.onmousedown =
      function(e)
      {
        for(var i = 0, j = This.Set.length; i < j; i++ ){ This.Set[i].Ele.TitleEle.className = "DragBox_Title_UnActive"; }
        this.className = "DragBox_Title";
        DragBox_Dragging(this, e);
        return false;
      };    
    fAttachEvent(Ele, "onclick", InnerOnclick);
  }/* for(var i = 0, j = This.Set.length; i < j; i++ ) */
  
  function InnerOnclick(e)
  {
    for(var i = 0, j = This.Set.length; i < j; i++ )
    { 
      This.Set[i].Ele.TitleEle.className = "DragBox_Title_UnActive";
      fRemoveClassX(This.Set[i].Ele, "DragBoxMain"+This.Index+"_Active");
    }
    this.TitleEle.className = "DragBox_Title";
    this.style.zIndex =++ GlobalZIndex;
    fAddClass(this, "DragBoxMain"+This.Index+"_Active");
  }
  
  Url = fFormat("{0}Style/{1}/Style.css", This.Path, This.Style);
  var StyleId = "DragBoxMainStyle_"+This.Index;
  var StyleIdObj = document.getElementById(StyleId);
  if(StyleIdObj!=null) return;
  
  fXhExt(Url, DragBox_StyleCallback, [This, StyleId]);
  
}/* function DragBox_Main() */

function DragBox_StyleCallback(Ar, This, StyleId)
{
  Ar[0] = Ar[0].replace(/\{Path\}/gi, fFormat("{0}Style/{1}/", This.Path, This.Style));
  Ar[0] = Ar[0].replace(/DragBoxExampleClass/gi, "DragBoxMain"+This.Index);
  fAddStyleFromStringX(Ar[0], StyleId);
}/* function DragBox_StyleCallback(Ar, This, StyleId) */

function DragBox_Add(XEle, XTitle, SizeObj, XInheritEle, XStyle, ExtObj)
{
  var This = this;
  XEle = fSetObjectX(XEle);
  
  if(!ExtObj) ExtObj = {};
  if(XStyle){ for(var x in XStyle) XEle.style[x] = XStyle[x]; }
  
  if(typeof XInheritEle == "boolean"){if(XInheritEle)XInheritEle = XEle.parentNode;}  
  if(typeof XInheritEle != "undefined") XInheritEle = fSetObjectX(XInheritEle);
  fAddClass(XEle, "DragBoxMain" + DragBoxCount);
  
  if(typeof SizeObj=="undefined") SizeObj = {};
  if(typeof SizeObj.Width=="undefined"){SizeObj.Width = parseInt(XEle.offsetWidth);}  
  if(typeof SizeObj.Height=="undefined") SizeObj.Height = parseInt(XEle.offsetHeight);
  
  if(ExtObj.IsMini)
  { 
    for(var i=0, j=XEle.childNodes.length; i<j; i++)
    {
      var Item = XEle.childNodes[i];
      if(Item.nodeType!==1) continue;
      Item.OldDisplay = Item.style.display;
      Item.style.display = "none";      
    }   
    XEle.IsMini = true;
  }
  
  if(!XTitle) XTitle = "未命名";  
  
  XEle.style.width = SizeObj.Width + "px";
  
  var TempTbl = __DragBoxTitleTplTable.cloneNode(true);
  TempTbl.className = "DragBox_Title_UnActive";
  TempTbl.width = SizeObj.Width;
  
  var LeftCell = TempTbl.rows[0].cells[0], CenterCell = TempTbl.rows[0].cells[1], RightCell = TempTbl.rows[0].cells[2];
  
  CenterCell.innerHTML = XTitle;
  fAppendNodeFirst(XEle, TempTbl);
  XEle.style.position = "absolute";
  
  var MiniEle = new Element("div");
  MiniEle.id = XEle.id+"_Mini"+This.Index;
  MiniEle.className = "DragBox_Mini";
  MiniEle.innerHTML = This.MiniText;
  MiniEle.XEle = XEle;
  MiniEle.onclick = DragBox_MiniClick;
  fAppendNodeFirst(CenterCell, MiniEle);
  
  var CloseEle = new Element("div");
  CloseEle.id = XEle.id+"_Close"+This.Index;
  CloseEle.className = "DragBoxClose";
  CloseEle.innerHTML = This.CloseText;
  CloseEle.onclick = function(e){ XEle.style.display = "none"; return false; };
  fAppendNodeFirst(CenterCell, CloseEle);
  
  This.Set[This.Set.length] = {Ele : XEle, Title : XTitle, SizeObj: SizeObj, InheritEle : XInheritEle, Style:XStyle, ExtObj:ExtObj};
  
  XEle.TitleEle = TempTbl;
  XEle.InheritEle = XInheritEle;
  
  if(typeof SizeObj.Left=="number") XEle.style.left = SizeObj.Left + "px";  
  if(typeof SizeObj.Top == "number") XEle.style.top = SizeObj.Top + "px";
}/* function DragBox_Add(XEle, XTitle, SizeObj, XInheritEle, XStyle, ExtObj) */

function DragBox_MiniClick(e)
{ 
  var FirstTag = true;
  var XEle = this.XEle;
  
  for(var i=0, j=XEle.childNodes.length; i<j; i++)
  {
    var Item = XEle.childNodes[i];
    if(Item.nodeType===1)
    {
      if(!FirstTag)
      {
        if(!XEle.IsMini)
        {
          Item.OldDisplay = Item.style.display;
          Item.style.display = "none";
        } 
        else
        {
          if(!Item.OldDisplay) 
          {
            if(Item.style.display!="none") Item.OldDisplay = Item.style.display; else Item.OldDisplay = "block";
          }
          Item.style.display = Item.OldDisplay;
        }
      }
      
      FirstTag = false;
    }
  }
  
  if(!XEle.IsMini) XEle.IsMini = true;
  else XEle.IsMini = false;
}/* function DragBox_MiniClick(e) */

function DragBox_Dragging(me, evt)
{
  var me = me.Parent;
  evt = evt ? evt : window.event;
  me.style.zIndex =++ GlobalZIndex;
  var detlaX = evt.clientX - parseInt(me.offsetLeft);
  var detlaY = evt.clientY - parseInt(me.offsetTop);
  
  if(document.all)
  {
    me.attachEvent("onmousemove", move);
    me.attachEvent("onmouseup", up);
    me.setCapture();
  }
  else
  {
    document.addEventListener("mousemove", move, true);
    document.addEventListener("mouseup", up, true);
    evt.stopPropagation();
    evt.preventDefault();
  }
  
  function move(evt)
  {
    var Left = evt.clientX - detlaX;
    var Top = evt.clientY - detlaY;
    
    if(me.InheritEle != null)
    {
      if(me.InheritEle == document.documentElement || me.InheritEle == document.body)
      {
        var ExtPlus = 0;        
        if(IsIe()) ExtPlus =  -20;
        var PPos = fGetXYWH(document.documentElement);
        var MaxWidth = PPos.Left + PPos.Width + document.documentElement.scrollLeft + ExtPlus;
        var MaxHeight = GlobalScreenSize.Height + document.documentElement.scrollTop;
        
        if(Left <= document.documentElement.scrollLeft) Left = document.documentElement.scrollLeft;        
        if(Top <= document.documentElement.scrollTop) Top = document.documentElement.scrollTop;        
        if(Left + me.offsetWidth >= (MaxWidth)){ Left = MaxWidth - me.offsetWidth; }        
        if(Top + me.offsetHeight >= (MaxHeight)){ Top = MaxHeight - me.offsetHeight; }
      }
      else
      {
        var PPos = fGetXYWH(me.InheritEle), MaxWidth = PPos.Left + PPos.Width, MaxHeight = PPos.Top + PPos.Height;
        
        if(Left <= PPos.Left) Left = PPos.Left;        
        if(Top <= PPos.Top) Top = PPos.Top;        
        if(Left + me.offsetWidth >= (MaxWidth)){ Left = MaxWidth - me.offsetWidth; }        
        if(Top + me.offsetHeight >= (MaxHeight)){ Top = MaxHeight - me.offsetHeight; }
      }/* if(me.InheritEle==document.documentElement||me.InheritEle==document.body) */
    }
    else { defaultStatus = "";}
    
    me.style.left = Left + "px";
    me.style.top = Top + "px";
    
    if(document.all){ me.style.filter = "Alpha(Opacity=50)"; }
    else{ me.style.opacity = 0.5; evt.stopPropagation(); }
  }
  
  function up(evt)
  {
    if(document.all)
    {
      me.style.filter = "Alpha(Opacity=100)";
      me.detachEvent("onmousemove", move);
      me.detachEvent("onmouseup", up);
      me.releaseCapture();
    }
    else
    {
      me.style.opacity = 1;
      document.removeEventListener("mousemove", move, true);
      document.removeEventListener("mouseup", up, true);
      evt.stopPropagation();
    }
  }
}/* function DragBox_Dragging(me, evt) */

function fAttachEvent(Obj, sEvtName, Func, bCapture)
{/* shawl.qiu code, void return */
  if(document.addEventListener){sEvtName = sEvtName.slice(2);Obj.addEventListener(sEvtName, Func, bCapture);}
  else if(document.attachEvent)
  {
    if(Obj[sEvtName]==null){Obj[sEvtName] = Func;} else{Obj.attachEvent(sEvtName, Func);}
  }
}/* end function fAttachEvent(Obj, sEvtName, Func, bCapture) */

function Element(XTag){return document.createElement(XTag);}
function Text(XStr){return document.createTextElement(XStr);}

function fSetObjectX(Obj, sMsg, Dcu, bNoAlter)
{/* shawl.qiu code, return Element */
  if(!Obj) return null;
  if(!sMsg) sMsg = "无法获得对象!";
  if(!Dcu) Dcu = document;
  if(!bNoAlter) bNoAlter = false; 
  switch(Obj.constructor)
  {
    case String: Obj = Dcu.getElementById(Obj); break;   
    case Array: Obj = Obj[0]; break;
  } 
  if(Obj==null&&bNoAlter==false) throw new Error(sMsg); 
  return Obj;
}/* function fSetObjectX(Obj, sMsg, Dcu, bNoAlter) */

function fAppendNodeFirst(ContainerElement, ForAppendElement)
{/* shawl.qiu code return default */
  if(ContainerElement.childNodes.length===0) return ContainerElement.appendChild(ForAppendElement);
  return ContainerElement.insertBefore(ForAppendElement, ContainerElement.firstChild);
}/* function fAppendNodeFirst(ContainerElement, ForAppendElement) */

function fAddClass(XEle, XClass)
{/* shawl.qiu code, void return */
  if(!XClass) throw new Error("XClass 不能为空!");
  if(XEle.className!="") 
  {
    var Re = new RegExp("\\b"+XClass+"\\b\\s*", "");
    XEle.className = XEle.className.replace(Re, "");
    XEle.className = XClass+" "+XEle.className;
  }
  else XEle.className = XClass;
}/* end function fAddClass(XEle, XClass) */

function fRemoveClassX(XEle, XClass)
{/* shawl.qiu code, void return */
  if(!XClass) throw new Error("XClass 不能为空!");
  if(!XEle.className) return;
  if(XEle.className!="") 
  {
    var Re = new RegExp("\\b"+XClass+"\\b\\s*", "");
    XEle.className = XEle.className.replace(Re, "");
  }
}/* function fRemoveClassX(XEle, XClass) */

function fRelativePath(sRelative)
{/* shawl.qiu code, return string */ 
  var sUrl = document.URL;
  sUrl = sUrl.replace(/^.*?\:\/\/[^\/]+/, "").replace(/[^\/]+$/, "");
  if(!sRelative){return sUrl;}  
  if(!/\/$/.test(sUrl)){sUrl += "/";}
  
  if(/^\.\.\//.test(sRelative))
  {
    var Re = new RegExp("^\\.\\.\\/"), iCount = 0;    
    while(Re.exec(sRelative)!=null)
    {
      sRelative = sRelative.replace(Re, "");
      iCount++;
    }       
    for(var i=0; i<iCount; i++){sUrl = sUrl.replace(/[^\/]+\/$/, "");}    
    if(sUrl=="") return "/";  
    return sUrl+sRelative;
  }   
  sRelative = sRelative.replace(/^\.\//, ""); 
  return sUrl+sRelative;
}/* end function fRelativePath(sRelative) */

function fGetXYWH(Ele)
{/* shawl.qiu code, return object */
  var a = new Array();
  var t = Ele.offsetTop, l = Ele.offsetLeft, w = Ele.offsetWidth, h = Ele.offsetHeight; 
  while(Ele = Ele.offsetParent){ t += Ele.offsetTop; l += Ele.offsetLeft; } 
  return {Top:t, Left:l, Width:w, Height:h};
}/* end function fGetXYWH(Ele) */

function fGetViewportSizeX() 
{/* shawl.qiu code, return object */
  var myWidth = 0, myHeight = 0;
  if(typeof(window.innerWidth ) == 'number' ) 
  {/* Non-IE */
    myWidth = window.innerWidth; myHeight = window.innerHeight;
  } 
  else if 
  (
    document.documentElement && 
    ( document.documentElement.clientWidth || document.documentElement.clientHeight ) 
  ) 
  {/* IE 6 */
    myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight;
  } 
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
  {/* IE 4 */
    myWidth = document.body.clientWidth; myHeight = document.body.clientHeight;
  }
  return {
            Width:myWidth
            , Height:myHeight
            , MaxWidth: myWidth+document.documentElement.scrollLeft
            , MaxHeight: myHeight+document.documentElement.scrollTop
          };
}/* function fGetViewportSizeX()  */

function IsIe(){ return navigator.appName == 'Microsoft Internet Explorer';}

function fInsertNode(OldNode, NewNode, bBefore)
{/* shawl.qiu script, return default */
  var ParentNode = OldNode.parentNode;
  if(!ParentNode){document.body.appendChild(NewNode); return false; };
  if(bBefore){ ParentNode.insertBefore(NewNode,OldNode); return false; }
  ParentNode.replaceChild(NewNode, OldNode);
  return ParentNode.insertBefore(OldNode, NewNode);
}/* end function fInsertNode(OldNode, NewNode, bBefore) */

function __Id(sTag, oDocument)
{/* shawl.qiu code, return Element */
  if(!oDocument) oDocument = document;
  return oDocument.getElementById(sTag);
}/* end function __Id(sTag, oDocument) */

function __E(sTag, oDocument)
{/* shawl.qiu code, return Element */
  if(!oDocument) oDocument = document;
  return oDocument.createElement(sTag);
}/* end function __E(sTag, oDocument) */

function fFormat(sStr)
{/* shawl.qiu code, return string */
  var Len = arguments.length, Re = null;  
  switch(Len)
  {
    case 0: return "";
    case 1: return sStr;
  }
  for(var i=1, j=0; i<Len; i++, j++)
  {
    Re = new RegExp(["\\{", j, "\\}"].join(""), "g");
    sStr = sStr.replace(Re, arguments[i]);
  }   
  return sStr;
}/* function fFormat(sStr) */

function fXhExt(sUrl, oFunc, ExtArgAr, sMethod)
{/* shawl.qiu code, void return */
 if(!sMethod) sMethod = "GET";
 var xh;
  
 try{ xh = new XMLHttpRequest();} 
 catch(e){
  try{ xh = new ActiveXObject('microsoft.xmlhttp');} 
  catch(e){ try{ xh = new ActiveXObject("Msxml2.XMLHTTP");} catch(e){} }
 }
 xh.open(sMethod, sUrl);
 xh.onreadystatechange =
  function()
  {
   if(xh.readyState == 4 && xh.status == 200)
   {
    if(oFunc)
    {
     if(ExtArgAr)
     {
      if(ExtArgAr.constructor==Array){ ExtArgAr.unshift([xh.responseText]); oFunc.apply(null, ExtArgAr); }
      else oFunc([xh.responseText]);
     }
     else oFunc([xh.responseText]);
    } 
    else alert(xh.responseText);
   }
  };
 
 xh.send(null);
}/* end function fXhExt(sUrl, oFunc, ExtArgAr, sMethod) */

function fAddStyleFromStringX(sIpt, sId)
{/* shawl.qiu script, void return; func: IsIe */ 
  var EleStyle = document.createElement("style");
  EleStyle.type = "text/css";
  if(sId&&sId!="") 
  {
    EleStyle.id = sId;
    var TempEle = document.getElementById(sId);
    if(TempEle!=null){ TempEle.parentNode.removeChild(TempEle); }
  }
  if(IsIe()) EleStyle.styleSheet.cssText = sIpt; else EleStyle.innerHTML = sIpt;
  var EleHead=document.getElementsByTagName("head")[0]
  if(!EleHead) EleHead=document.body;
  EleHead.insertBefore(EleStyle, EleHead.firstChild); 
}/* function fAddStyleFromStringX(sIpt, sId) */

function IsIe(){ return navigator.appName == 'Microsoft Internet Explorer';}
