<!--

/************************************************************************/
/* gui_dragndrop.js                                                     */
/*----------------------------------------------------------------------*/
/* Implements the drag and drop feature used to move dialog boxes.      */
/*----------------------------------------------------------------------*/
/* File created 30Apr08 acurtis                                         */
/*----------------------------------------------------------------------*/
/* Copyright (c) 2008-2009 Avaya Inc.                                   */
/************************************************************************/

// Starts the Drag & Drop operation.
function DragNDrop( e, id )
{
  // DragNDrop isn't appropriate for mobile devices
  if( gBrowser.IsTiny() )
  {
    return;
  }

  if( !e )
  {
    e = window.event;
  }

  gDragNDropElem = document.getElementById( id );

  if( gDragNDropElem && gDragNDropElem.id )
  {
    // Put dragged dialog on top
    RaiseDialog( gDragNDropElem.id );

    // Enable/disable controls etc now that new dlg is in focus.
    RedrawAll();


    var cursor_x = 0;
    var cursor_y = 0;

    var elem_x = parseInt( gDragNDropElem.style.left, 10 );
    var elem_y = parseInt( gDragNDropElem.style.top,  10 );

    if( gBrowser.type == eBT_IE )
    {
      cursor_x = e.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
      cursor_y = e.clientY + document.documentElement.scrollTop  + document.body.scrollTop;
    }
    else
    {
      cursor_x = e.clientX + window.scrollX;
      cursor_y = e.clientY + window.scrollY;
    }
    gDragNDropElem.offset_x = elem_x - cursor_x;
    gDragNDropElem.offset_y = elem_y - cursor_y;

    AttachDragNDropHandler( e );
  }

  if( window.event )
  {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }

  if( e.preventDefault )
  {
    e.preventDefault();
  }
}

function DragNDropHandler( e )
{
  if( !e )
  {
    e = window.event;
  }

  if( gDragNDropElem && gDragNDropElem.id )
  {
    var cursor_x = 0;
    var cursor_y = 0;
    if( gBrowser.type == eBT_IE )
    {
      cursor_x = e.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
      cursor_y = e.clientY + document.documentElement.scrollTop  + document.body.scrollTop;
    }
    else
    {
      cursor_x = e.clientX + window.scrollX;
      cursor_y = e.clientY + window.scrollY;
    }

    var x = cursor_x + gDragNDropElem.offset_x;
    var y = cursor_y + gDragNDropElem.offset_y;
    // Keep at least 10px from top of screen.
    if( y < 10 )
    {
      y = 10;
    }
    gDragNDropElem.style.left = ( x ) + 'px';
    gDragNDropElem.style.top  = ( y ) + 'px';

  }
  if( window.event )
  {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }

  if( e.preventDefault )
  {
    e.preventDefault();
  }

  e.cancelBubble = true;
  if( e.stopPropagation )
  {
    e.stopPropagation();
  }
}

function AttachDragNDropHandler(e)
{
  if( !e )
  {
    e = window.event;
  }

  if( window.event )
  {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }

  if( e.preventDefault )
  {
    e.preventDefault();
  }

  if( document.addEventListener )
  {
    document.addEventListener( "mousemove", DragNDropHandler, true );
    document.addEventListener( "mouseup", DetachDragNDropHandler, true );
  }
  else
  {
    document.attachEvent( "onmousemove", DragNDropHandler );
    document.attachEvent( "onmouseup", DetachDragNDropHandler );
  }


  IE6DragDropWorkaround( gDragNDropElem.id, true );
}

function DetachDragNDropHandler()
{
  if( document.removeEventListener )
  {
    document.removeEventListener( "mousemove", DragNDropHandler,   true );
    document.removeEventListener( "mouseup",   DetachDragNDropHandler, true );
  }
  else
  {
    document.detachEvent( "onmousemove", DragNDropHandler );
    document.detachEvent( "onmouseup",   DetachDragNDropHandler );
  }

  IE6DragDropWorkaround( gDragNDropElem.id, false );

  gDragNDropElem = null;

}



//-->

