/* define value */

var DRAG_SHADOW_DELTA_X = 3;
var DRAG_SHADOW_DELTA_Y = 3;
var _DragMovingObj = null;

function Drag(handleId, contentId)
{
   var hObj = document.getElementById(handleId);
   var cObj = document.getElementById(contentId);
   
   if(!hObj && !cObj)
      return null;
   else if(!hObj)
   {
      this.hId = contentId;
      this.cId = null;
      hObj = cObj;
      cObj = null;
      hObj.setAttribute("cId", "");
   }
   else if(!cObj)
   {
      this.hId = handleId;
      this.cId = null;
      cObj = null;
      hObj.setAttribute("cId", "");
   }
   else
   {
      this.hId = handleId;
      this.cId = contentId;
      hObj.setAttribute("cId", contentId);
   }

   with(hObj.style)
   {
      position = "absolute";
      display = "";
      visibility = "hidden";
      overflow = "hidden";
      zIndex = 4;
      top = "0px";
      left = "0px";
   }

   if(cObj)
   {
      with(cObj.style)
      {
         position = "absolute";
         display = "";
         visibility = "hidden";
         overflow = "hidden";
         top = parseInt(hObj.offsetTop, 10) + parseInt(hObj.offsetHeight, 10) + "px";
         left = parseInt(hObj.offsetLeft, 10) + "px";
         zIndex = 4;
      }
   }

   hObj.setAttribute("dragger", "false");
   this.Move = DragMove;
   this.Show = DragShow;
   this.Hide = DragHide;
   this.Enable = DragEnable;
   this.Disable = DragDisable;

   /* if the browse is IE, then create a iframe to fix the 'IE select element bug' */
   if(document.all)
   {
      var IFObj = document.createElement("IFRAME");


      IFObj.id = hObj.id + "_IF";

      with(IFObj.style)
      {
         position = "absolute";
         display = "none";
         width = hObj.offsetWidth;
         if(cObj)
           height = parseInt(hObj.offsetHeight, 10) + parseInt(cObj.offsetHeight, 10) + "px";
         else
           height = parseInt(hObj.offsetHeight, 10) + "px";
         top = parseInt(hObj.style.top, 10) + "px";
         left = parseInt(hObj.style.left, 10) + "px";
         zIndex = hObj.style.zIndex - 1;
      }
      document.getElementsByTagName("BODY")[0].appendChild(IFObj);
   }
   /* add shadow div, 060323, Howard */
   var SWObj = document.createElement("DIV");

   SWObj.id = hObj.id + "_SW";

   with(SWObj.style)
   {
      position = "absolute";
      display = "none";
      width = parseInt(hObj.offsetWidth, 10) + "px";
      if(cObj)
        height = parseInt(hObj.offsetHeight, 10) + parseInt(cObj.offsetHeight, 10) + "px";
      else
        height = parseInt(hObj.offsetHeight, 10) + "px";
      top = parseInt(hObj.style.top, 10) + DRAG_SHADOW_DELTA_Y + "px";
      left = parseInt(hObj.style.left, 10) + DRAG_SHADOW_DELTA_X + "px";
      zIndex = hObj.style.zIndex - 2;
      backgroundColor = "#999999";
      opacity = 0.4;
      MozOpacity = 0.4;
      filter = "alpha(opacity=40)";
   }
   document.getElementsByTagName("BODY")[0].appendChild(SWObj);
   
   /* init the position */
   this.Move(0, 0);
}

function DragMove(x, y, hId)
{
   if(hId == null)
   {
      hId = this.hId;
      var hObj = document.getElementById(this.hId);
      var cObj = document.getElementById(this.cId);
   }
   else
   {
      var hObj = document.getElementById(hId);
      if(hObj)
         var cObj = document.getElementById(hObj.getAttribute("cId"));
      else
         return;
   }

   /* if the browse is IE, then move iframe first to cover all element */
   if(document.all)
   {
      document.getElementById(hId + "_IF").style.left = parseInt(x, 10) + "px";
      document.getElementById(hId + "_IF").style.top = parseInt(y, 10) + "px";
   }
   /* move shadow div, 060323, Howard */
   document.getElementById(hId + "_SW").style.left = parseInt(x, 10) + DRAG_SHADOW_DELTA_X + "px";
   document.getElementById(hId + "_SW").style.top = parseInt(y, 10) + DRAG_SHADOW_DELTA_Y + "px";

   hObj.style.left = parseInt(x, 10) + "px";
   hObj.style.top = parseInt(y, 10) + "px";   

   if(cObj)
   {
      cObj.style.left = parseInt(hObj.offsetLeft, 10) + "px";
      cObj.style.top = parseInt(hObj.offsetTop, 10) + parseInt(hObj.offsetHeight, 10) + "px";
   }
}

function DragShow(x, y)
{
   if(x != null && y != null)
      this.Move(x, y);

   /* if the browse is IE, then show iframe first to cover all element */
   if(document.all)
      document.getElementById(this.hId + "_IF").style.display = "";
   
   /* show shadow div, 060323, Howard */
   document.getElementById(this.hId + "_SW").style.display = "";
      
   document.getElementById(this.hId).style.visibility = "visible";

   if(this.cId != null)
      document.getElementById(this.cId).style.visibility = "visible";
}

function DragHide()
{
   /* if the browse is IE, then hide iframe first to cover all element */
   if(document.all)
      document.getElementById(this.hId + "_IF").style.display = "none";

   /* hide shadow div, 060323, Howard */
   document.getElementById(this.hId + "_SW").style.display = "none";
   
   document.getElementById(this.hId).style.visibility = "hidden";

   if(this.cId != null)
      document.getElementById(this.cId).style.visibility = "hidden";
}

function DragEnable()
{
   var hObj = document.getElementById(this.hId);

   hObj.setAttribute("dragger", "true");
   hObj.setAttribute("deltaX", 0);
   hObj.setAttribute("deltaY", 0);
   hObj.onmouseover = new Function("this.style.cursor = 'move'");
   hObj.onmousedown = DragStart;
   document.onmouseup = DragStop;
}

function DragDisable()
{
   var hObj = document.getElementById(this.hId);

   hObj.setAttribute("dragger", "false");
   hObj.setAttribute("deltaX", 0);
   hObj.setAttribute("deltaY", 0);
   hObj.onmouseover = new Function("this.style.cursor = 'auto'");
   hObj.onmousedown = null;
}

function DragStop()
{
   document.onmousemove = null;
   _DragMovingObj = null;
}

function DragStart(e)
{
   var srcObj;

   if(document.all)
   {
      e = event;
      srcObj = event.srcElement;
   }
   else
      srcObj = e.target;
   
   while(true)
   {
      if("true" == srcObj.getAttribute("dragger"))
         break;
      if("BODY" == srcObj.parentNode.tagName)
         return false;
      if("false" == srcObj.getAttribute("dragger"))
         return false;
      srcObj = srcObj.parentNode;
   }

   srcObj.setAttribute("deltaX", parseInt(e.clientX, 10) - parseInt(srcObj.style.left, 10));
   srcObj.setAttribute("deltaY", parseInt(e.clientY, 10) - parseInt(srcObj.style.top, 10));
   _DragMovingObj = srcObj;
   document.onmousemove = DragMoveAuto;
   return false;
}

function DragMoveAuto(e)
{
   if(_DragMovingObj == null)
      return;
   if(document.all)
      e = event;
   if(_DragMovingObj.getAttribute("dragger") == "true")
   {
      DragMove(e.clientX - _DragMovingObj.getAttribute("deltaX") ,
               e.clientY - _DragMovingObj.getAttribute("deltaY") ,
               _DragMovingObj.id);
      return false;
   }
}

