//******************************************************************************
//
//    Deluxe Tree 1.0
//    Additional module for floatable & movable Tree Menu
//    http://deluxe-tree.com
//    (c) 2006, by deluxe-tree.com
//
//******************************************************************************


//##############################################################################
// Floatable Menu
//##############################################################################


function dt_getPageScrolling()
{
  return [(isIEComp?docElement.scrollLeft:pageXOffset), (isIEComp?docElement.scrollTop:pageYOffset)]
}


function dt_getCoord(iter, c, n)
// Calculates new coors for floatable menu
{
  var dx = (n-c) / iter;
  with (Math) if (abs(dx)<1) dx = abs(dx)/dx;
  return c + ((c!=n) ? dx : 0) + 'px';
}


function dt_ScrollWindow()
// Assigned to a scroll event of a page for floatable menu
{
  if (tmoveRec.isMoving) return;

  for (var j=0; j<dt_menu.ln(); j++)
      with (dt_menu[j])
      {
          if (floating && absPos)
          {
              var mObj    = dt_getObjectByID(id+'div');
              var mXY     = dt_getObjectStyleXY(mObj);

              var pageOff = dt_getPageScrolling();
              var x       = pageOff[0] + left - goffX;
              var y       = pageOff[1] + top  - goffY;

              with (mObj.style)
              {
                  if (floatingX && mXY[0]!=x) left = dt_getCoord(iterations, mXY[0], x);
                  if (floatingY && mXY[1]!=y) top  = dt_getCoord(iterations, mXY[1], y);
              }
          }
      }
}




//##############################################################################
// Movable Menu
//##############################################################################


var tuserMoveEvent = null;      // variable for saving user's mousemove event

function dt_selStart()
// Bans the ability to select a text if the menu is in a moving process
{
  return (tmoveRec.isMoving ? false : true)
}


function dt_getEventXY(e)
{
  if (isMAC && isIE) e = window.event;
  with (e)
      return [(isIE||isOP)?clientX:pageX, (isIE||isOP)?clientY:pageY];
}


function dt_assignMoveEvent()
// Assigns mousemove & selectstart events to the document
{
  if (dtdo.attachEvent)
  {
      dtdo.attachEvent('onmousemove', dt_Move);
      if (isIE) dtdo.attachEvent("onselectstart", dt_selStart);
  }
  else
  {
      tuserMoveEvent = dtdo.onmousemove;                              // remember user's event
      dtdo.onmousemove = function(e)                                  // create new mousemove event
                        {
                            dt_Move(e);
                            if (tuserMoveEvent) tuserMoveEvent();    // call user's event
                            return true;
                        }
  }
}


function dt_startMoving(evnt, mInd)
// Detects current dX, dY of mouse cursor relatively the left-top menu corner and begins a moving capture
{
  with (tmoveRec)
  {
      if (!tisLoaded || isMoving) return;


      mVar = dt_menu[mInd];
      mObj = dt_getObjectByID(mVar.id+'div');                          // get current processed menu
      var mouseXY = dt_getEventXY(evnt);
      var mXY     = dt_getObjectStyleXY(mObj);
      var pOff    = isIEComp ? dt_getPageScrolling() : [0,0];
      cX = mouseXY[0] - mXY[0] + pOff[0];
      cY = mouseXY[1] - mXY[1] + pOff[1];
      isMoving = 1;                                                      // capture current menu
  }
}


function dt_Move(event)
// Sets new menu coords during a menu moving process
{
  with (tmoveRec)
      if (isMoving && tisLoaded)                         // the menu is in a moving process
      {
          var mouseXY = dt_getEventXY(event);
          var pOff    = isIEComp ? dt_getPageScrolling() : [0,0];
          var nX = mouseXY[0] - cX + pOff[0] + goffX;
          var nY = mouseXY[1] - cY + pOff[1] + goffY;

          with (mObj.style)
          {
              left = ((nX>=0)?nX:0) + 'px';
              top  = ((nY>=0)?nY:0) + 'px';
          }
      }
  return true;
}


function dt_stopMoving()
// Uncaptures the current movable menu
{
  with (tmoveRec)
  {
      if (!tisLoaded || !isMoving) return;
      
      var pOff  = dt_getPageScrolling();
      var mXY   = dt_getObjectStyleXY(mObj);
      mVar.left = mXY[0] - pOff[0];                                      // set new absolute coords
      mVar.top  = mXY[1] - pOff[1];
      isMoving = 0;                                                      // uncapture the current moveable menu
  }
}


