
// Position of the mouse
var mouseX = 0;
var mouseY = 0;

// Relative offsetLeft/Top of the element on grab
var grabX = 0;
var grabY = 0;

// Position of mouse on grab
var origX = 0;
var origY = 0;

var dragobj;
var canGrab = true;

var xBool = false;
var yBool = false;
var dragFun;
var dropFun;
var grabFun;


function scrollWindow()
{
	var winH = getWindowDimensions()[1];
	var scrollY = window.pageYOffset;
	if ((mouseY-scrollY) < 50)
	{
			window.scrollBy(0, -10);
	}
	else
	{
		if (mouseY > ((winH+scrollY)-20))
		{
			window.scrollBy(0, 10);
		}
	}		
}






// http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function getWindowDimensions() {
  var hwArray = new Array();
  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+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  hwArray.push(myWidth);
  hwArray.push(myHeight);
  return hwArray;
}




function getY( oElement )
{
	var iReturnValue = 0;
	while( oElement != null )
	{
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}


function getX( oElement )
{
	var iReturnValue = 0;
	while( oElement != null )
	{
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}




function falsefunc() { return false; } // used to block cascading events

function moveInit()
{
  document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE
  update();
}




function getmouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mouseX = e.pageX;
      mouseY = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      mouseX = e.clientX + document.body.scrollLeft;
      mouseY = e.clientY + document.body.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }
  }
}


/* -----------------------------------------------------------
// FUNCTIONS FOR MOVING PANELS
----------------------------------------------------------- */

function update(e)
{
  getmouseXY(e); // NS is passing (event), while IE is passing (null)

/*
  document.getElementById("mousePos").innerHTML = "Mouse: " + mouseX + "," + mouseY;
  if (dragobj!=null)
  {
	  document.getElementById("grabPos").innerHTML = "Grab: " + grabX + "," + grabY;  
	  document.getElementById("origPos").innerHTML = "Orig: " + origX + "," + origY;  
	  document.getElementById("objPos").innerHTML = "Obj: " + getX(dragobj) + "," + getY(dragobj);  
  }
*/

}

function grab(context,xb,yb,grabFunction,dragFunction,dropFunction)
{

	if (canGrab)
	{
	
		canGrab = false;

		document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
		dragobj = context;
			
		// Set the mouse location on grab
		grabX = mouseX;
		grabY = mouseY;
		
		// Set the object's location on grab
		origX = dragobj.offsetLeft;
		origY = dragobj.offsetTop;
		
		dragobj.style.position = "absolute";
	
		dragFun = dragFunction;
		dropFun = dropFunction;
		grabFun = grabFunction;
		xBool = xb;
		yBool = yb;
		
		if (grabFun != null)
		{
			grabFun();
		}
		
		//  dragobj.style.top = (getY(dragobj)) + "px";
		
		document.onmousemove = drag;
		document.onmouseup = drop;
		
		update();
		drag();

	}	
  
}



function drag(e) // parameter passing is important for NS family 
{
  if (dragobj)
  {
  
    // Calculate the new position of the element
	var newX = origX + (mouseX - grabX);
	var newY = origY + (mouseY - grabY);

    
    
    if (yBool===true)
    {
	    dragobj.style.top = newY.toString(10)+"px";
	}
    if (xBool===true)
    {
	    dragobj.style.left = newX.toString(10)+"px";
	}
	if (dragFun != null)
	{
		dragFun();
	}

//	checkParent();

		
  }
  update(e);
  return false; // in IE this prevents cascading of events, thus text selection is disabled
}






function drop()
{
  xBool = false;
  yBool = false;
  
  if (dropFun != null)
  {
	dropFun();
  }
  
  checkShiftGroup = null;
  dragFun = null;
  grabFun = null;
  dropFun = null;
  
  if (dragobj)
  {
    dragobj.style.zIndex = 0;
    dragobj = null;
  }
  
  canGrab = true;

  update();
  document.onmousemove = update;
  document.onmouseup = null;
  document.onmousedown = null;   // re-enables text selection on NS
}







/* -----------------------------------------------------------
// FUNCTIONS FOR RESIZING PANELS
----------------------------------------------------------- */

function startResizing(context)
{
  // in NS this prevents cascading of events, thus disabling text selection
  document.onmousedown = falsefunc;
  dragobj = context;
  document.onmousemove = resize;
  document.onmouseup = stopResizing;
  grabx = mouseX;
  graby = mouseY;
  elex = orix = dragobj.offsetLeft;
  eley = oriy = dragobj.offsetTop;
  update();  
}

function resize(e) // parameter passing is important for NS family 
{
  if (dragobj)
  {

    elex = orix + (mouseX-grabx);	// Causes improper jump on the first grab
    eley = oriy + (mouseY-graby);
	if (elex<0)
	{
	 elex += (0-elex);
	}
	if (eley<0)
	{
	 eley += (0-eley);
	}

    var new_width = Math.abs(orix-mouseX);
	var new_height = Math.abs(oriy-mouseY);

    dragobj.style.width = new_width + 'px';
    dragobj.style.height  = new_height + 'px';

    update(e);
    return false; // in IE this prevents cascading of events, thus text selection is disabled

  }

}

function stopResizing()
{
  if (dragobj)
  {
    dragobj.style.zIndex = 0;
    dragobj = null;
  }
  update();
  document.onmousemove = update;
  document.onmouseup = null;
  document.onmousedown = null;   // re-enables text selection on NS
}





