/****************************************
*    Mouse Coords Function              *
*****************************************/

// This function gets the x and y coordinates of the cursor and returns them as an object
function mouse_coords(ev){

	// Get the x and y mouse position
	// pageX and pageY are used for Firefox and clientX and clientY are used by IE
	if(ev.pageX || ev.pageY) {
		var x = ev.pageX;
		var y = ev.pageY;
	} else {
		var x = ev.clientX + document.body.scrollLeft - document.body.clientLeft;
		var y = ev.clientY + document.body.scrollTop  - document.body.clientTop;
	};

	// Check to see if the x and y are bigger than zero if they are less than zero
	// set them to zero
	x = (x<0)?0:x;
	y = (y<0)?0:y;

	// Return the x and y mouse position as an object
	return {
		x:x, y:y
	};
}


/****************************************
*    Get Mouse Offset Function          *
*****************************************/

// This function returns the offset position of an object to the cursor
function get_mouse_offset(target, ev){

	// Get the event object
	ev = ev || window.event;

	// Get the document position
	var docPos    = get_position(target);

	// Get the mouse coords
	var mouse_position  = mouse_coords(ev);

	// Calculate the x and y mouse offsets
	var x = mouse_position.x - docPos.x;
	var y = mouse_position.y - docPos.y

	// Return the mouse offset x and y as an object
	return {
		x:x, y:y
	};
}


/****************************************
*    Get Mouse Offset Function          *
*****************************************/

function get_position(e){

	// Set the top and left postion to default to zero
	var left = 0;
	var top  = 0;

	// Loop to get the objects top and left postion
	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	// Add the offset left and top to the variable
	left += e.offsetLeft;
	top  += e.offsetTop;

	// Return the objects x and y postions as an object
	return {
		x:left, y:top
	};
}