//Element Position Scripts
//Tristan Harmer
//June 2005

//The scripts in this file are used to draw a box at the mouse when dragged.
//The box itself is a span element.
//A lot of the work here is based on information found at quirksmode.org.

//Assumptions & Info
//
// - The function getMouseClientPosition() is from position.js, so this must
//   be referenced within the calling document/page.
//
// - Throughout the comments you will see the following terms
//   -Box element. This is the element that forms the drag box.
//   -Target element. This is the static element above which the box element is to be drawn.
//
// - The box element should to have this style
//		position: absolute; 
//		top: 0px; 
//		left: 0px; 
//		width: 0px;
//		height: 0px;
//		display: none;
//		padding: 0px;
//		margin: 0px;
//		overflow: hidden;
//		border-collapse: collapse;

var boxVisible = false;
var startX = 0;
var startY = 0;

//Makes the box element visible and records the position clicked
function startBox(boxElement, x, y)
{

	startX = x;
	startY = y;
	
	if (boxElement.style)
	{
	
		boxElement.style.visibility = 'visible';
		boxElement.style.width = '0px';
		boxElement.style.height = '0px';
		boxElement.style.left = x + 'px';
		boxElement.style.top = y + 'px';		

	}
	
}

//Updates the box so so that it covers the area from the 
//initial click to the specified coordinates.
function updateBox(boxElement, x, y)
{

	var computedStyle = null;
	var topOffset = 0;
	var leftOffset = 0;
	var adjustedX = 0;
	var adjustedY = 0;

	if (!boxVisible) return;
	
	//**************************
	
	//Attempt to get computed style of element
	if (window.getComputedStyle) 
	{		
		computedStyle = window.getComputedStyle(boxElement, null)
		
	}
	else if (document.defaultView && document.defaultView.getComputedStyle)
	{
		computedStyle = document.defaultView.getComputedStyle(boxElement, null)
	}
	
	//Get combined width of border and padding (margin isn't considered part of the image)
	//Computed style is the preferred method.
	//All major browsers except IE windows support this.
	if (computedStyle)
	{

		//preferred method (ref: quirksmode.org)
		topOffset += parseInt(computedStyle.getPropertyValue('border-top-width').toLowerCase().replace("px", ""));
		if (isNaN(topOffset)) topOffset = 0;
		topOffset += parseInt(computedStyle.getPropertyValue('padding-top').toLowerCase().replace("px", ""));
		if (isNaN(topOffset)) topOffset = 0;
		
		leftOffset += parseInt(computedStyle.getPropertyValue('border-left-width').toLowerCase().replace("px", ""));
		if (isNaN(leftOffset)) leftOffset = 0;
		leftOffset += parseInt(computedStyle.getPropertyValue('padding-left').toLowerCase().replace("px", ""));
		if (isNaN(leftOffset)) leftOffset = 0;
		
	}
	else if (boxElement.currentStyle && navigator.userAgent.toLowerCase().indexOf('; mac') == -1)
	{

		//IE method
		topOffset += parseInt(boxElement.currentStyle.borderTopWidth.toLowerCase().replace("px", ""));
		if (isNaN(topOffset)) topOffset = 0;
		topOffset += parseInt(boxElement.currentStyle.paddingTop.toLowerCase().replace("px", ""));
		if (isNaN(topOffset)) topOffset = 0;
		topOffset += 1
		
		leftOffset += parseInt(boxElement.currentStyle.borderLeftWidth.toLowerCase().replace("px", ""));
		if (isNaN(leftOffset)) leftOffset = 0;
		leftOffset += parseInt(boxElement.currentStyle.paddingLeft.toLowerCase().replace("px", ""));
		if (isNaN(leftOffset)) leftOffset = 0;
		leftOffset += 1
		
		if (isNaN(topOffset)) topOffset = 0;
		if (isNaN(leftOffset)) leftOffset = 0;

	}
		
	//**************************
	
	if (boxElement.style)
	{
	
		if (x > startX) 
		{
			adjustedX = x - startX - leftOffset * 2;
			if (adjustedX < 0) adjustedX = 0;
			
			boxElement.style.left = startX + 'px';
			boxElement.style.width = adjustedX + 'px'; 
		}
		else if (x != 0)
		{
			adjustedX =  startX - x - leftOffset * 2;
			if (adjustedX < 0) adjustedX = 0;
			
			boxElement.style.left = x + 'px';
			boxElement.style.width = adjustedX + 'px'; 
		}
		
		if (y > startY) 
		{
			adjustedY = y - startY - topOffset * 2;
			if (adjustedY < 0) adjustedY = 0;
			
			boxElement.style.top = startY + 'px';
			boxElement.style.height = adjustedY + 'px';
		}
		else if (y != 0)
		{
			adjustedY =  startY - y - topOffset * 2;
			if (adjustedY < 0) adjustedY = 0;
			
			boxElement.style.top = y + 'px';
			boxElement.style.height = adjustedY + 'px'; 
		}

	}
	
}

function boxHide(boxElement)
{
	
		boxElement.style.visibility = 'hidden';
		boxElement.style.width = '0px';
		boxElement.style.height = '0px';
		boxElement.style.left = '0px';
		boxElement.style.top = '0px';	
	
}

//Function that is called from the mousedown event of 
//the target and box element.
function boxMouseDown(boxElement, e)
{
	
	boxVisible = true; 
	
	var coord = getMouseClientPosition(e); 
	
	startBox(boxElement, coord.x, coord.y);
	
}

//Function that is called from the mousemove event of 
//the target and box element.
function boxMouseMove(boxElement, e)
{

		if (!boxVisible) return;
		//|| (!boxElement.currentStyle && !boxElement.computedStyle)
		
		var coord = getMouseClientPosition(e); 
		
		updateBox(boxElement, coord.x, coord.y);
}

//Function that is called from the mouseup event of 
//the target and box element.
function boxMouseUp(e)
{

	boxVisible = false;
	
}

//Function that is called from the mouseout event of 
//the target element and box element.
function boxMouseOut(boxElement, e)
{

	if (boxVisible) 
	{
	    
		if (e.relatedTarget && e.relatedTarget != boxElement)
		{
			boxHide(boxElement);
			//formToSubmit.submit();
		}
		else if (e.toElement && e.toElement != boxElement)
		{
			boxHide(boxElement);
			//formToSubmit.submit();
		}

	}
	
}

//Determines if the mouse click event was caused by a user clicking the right mouse button.
//Again, found on quirksmode.org.
function isRightClick(mouseClickEvent)
{

    var rightClick;
	if (!mouseClickEvent) var mouseClickEvent = window.event;
	if (mouseClickEvent.which) rightClick = (mouseClickEvent.which == 3);
	else if (mouseClickEvent.button) rightClick = (mouseClickEvent.button == 2);
	
	return rightClick;

}