/*==================================================================
	browser.js
	
	Author: James Edgeworth (2009) (unless otherwise credited)
	
	Different 'types' of browser support different groups of functions.
	This script provides functions which detect the browser type, and
	use their method of a specific task.


==================================================================*/

// Browser Type. Opera, Firefox, Chrome are treated as a netscape based browser (even though Opera isn't Netscape based).
// IE is treated as an "IE" type browser.
var browserType = '';


// Mouse positions. Updated only if getMouseXY is used.
var mouseX = 0;
var mouseY = 0;



/*
	detectBrowser()
	
	Detects the browser name and version. Returns either
	IE or NS. Opera is treated as NS as it follows the same standards.
*/

function detectBrowser()
{

	browserName = navigator.appName;
	browserVersion = parseFloat(navigator.appVersion);
	
	//browserType = '';
	
	switch ( browserName )
	{
		case 'Opera':
		case 'Netscape':
		
			browserType = 'NS';
			
		break;
		
		case 'Microsoft Internet Explorer':
		
			browserType = 'IE';
			
		break;
	}
	
	//return browserType;
	
}


/*
	addEventListener()
	
	Calls either .addEventListener, or attachEvent.
	
	(NOT YET TESTED) - the problem is that, for the same event, 
	different browsers may use different targets. E.g, attaching "onmousemove" to "window"
	is ok in NS, but bad in IE. IE wants "mousemove" attached to "document".
	
*/
function addEvent(targetObj, eventStr, callbackFunc)
{

	if ( browserType == '' )
	{
		detectBrowser();
	}
	
	if ( browserType == 'NS' )
	{
		targetObj.addEventListener(eventStr, callbackFunc, true);
	}
	else
	{
		targetObj.attachEvent(eventStr, callbackFunc);
	}
	
}





/*
	elementPosition()
	
	Calculates the exact position of an element
	by adding up the offsets of the element itself,
	and each parent element.
	
	Returns an array of two integers:
	0 - top
	1 - left
	
*/
function elementPosition(element)
{

	var top, left;
	
	top = 0;
	left = 0;
	
	top += element.offsetTop;
	left += element.offsetLeft;
	
	while ( element = element.offsetParent )
	{
		top += element.offsetTop;
		left += element.offsetLeft;
	}
	
	return [top, left];
	
}





/*
	getMouseXY()
	
	Obtains the coordinates of the mouse, placing the values into 
	two global variables.
	
*/
function getMouseXY(e)
{

	
	// IE, else firefox, etc
	if ( window.addEventListener == null )
	{
	
		if( !e )
		{
			e = document.event;
		}
	
		mouseX = e.clientX;
		mouseY = e.clientY;
		
	}
	else
	{
	
		if( !e )
		{
			e = window.event;
		}
	
		mouseX = e.pageX;
		mouseY = e.pageY;
	
	}
	
}


/*
	getPageWidth()
	
	Obtains the coordinates of the top right of the page.
	
*/
function getPageWidth()
{

	var width = 0;
	
	if ( window.innerWidth )
	{
		width = window.innerWidth;
	}
	
	else if ( document.documentElement.clientWidth )
	{
		width = document.documentElement.clientWidth;
	}
	
	return width;

}

/*
	getPageYOffset()
	
	Obtains the amount the page has scrolled.
	
*/
function getPageYOffset()
{

	var offset = 0;
	
	if ( window.pageYOffset )
	{
		offset = window.pageYOffset;
	}
	
	else if ( document.body.scrollTop )
	{
		offset = document.body.scrollTop;
	}
	
	else if ( document.documentElement.scrollTop )
	{
		offset = document.documentElement.scrollTop;
	}
	
	return offset;
	
}








// Detect the browser. Will be run when this script is included.
detectBrowser();