/******************************************
* AWD JS Library
* Set: Global Functions
* Author: Matthew Camp
* Created: 15th February 2009
* Last Updated: 22nd February
* ________________________________________________________________________________
* GLOBAL FUNCTION LIST, these functions are shared with all the main functions
* ________________________________________________________________________________
* AddOnloadEvent(function) - adds an event during loading a page
* DetectBrowser() - returns the name of the clients browser with the first letter of each word in uppercase, eg Internet Explorer
* GetArgumentValues() - returns an array with all the arguments passed through a HTML class function call
* GetBodyDimensions() - returns an array with the [width,height] of the html body (entire page with scroll)
* GetWindowDimensions() - returns an array with the [width,height] of the clients internal window
* GetScrollPosition() - returns the position of the clients scrollbar
* LeftPosition() - returns the left position of an element in relation to the body
* TopPosition() - returns the top position of an element in relation to the body
* GetMouseLocation(e) - returns the [x,y] position of the clients mouse in an array - must be called from an event
*******************************************/
// allows to attach multiple onload event with multiple js scripts
function AddOnloadEvent(fnc){
	if(typeof window.addEventListener != "undefined"){
		window.addEventListener( "load", fnc, false );
	}
	else if(typeof window.attachEvent != "undefined"){
		window.attachEvent("onload", fnc);
	}
	else{
		if(window.onload != null){
			var oldOnload = window.onload;
			window.onload = function(e){
				oldOnload(e);
				window[fnc]();
			}
		}
		else{
			window.onload = fnc;
		}
	}
}
// Detects and returns the name of the clients browser
function DetectBrowser(){
	var agt = navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1){ 
		return 'Opera';
	}
	if (agt.indexOf("firefox") != -1){
		return 'Firefox';
	}
	if (agt.indexOf("safari") != -1){
		return 'Safari';
	}
	if (agt.indexOf("chrome") != -1){
		return 'Chrome';
	}
	if (agt.indexOf("msie") != -1){
		return 'Internet Explorer';
	}
	if (agt.indexOf("netscape") != -1){
		return 'Netscape';
	}
	if (agt.indexOf("mozilla/5.0") != -1){
		return 'Mozilla';
	}
	if (agt.indexOf('\/') != -1) {
		if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
			return navigator.userAgent.substr(0,agt.indexOf('\/'));
		}
		else{
			return 'Netscape';
		}
	}
	else if (agt.indexOf(' ') != -1){
		return navigator.userAgent.substr(0,agt.indexOf(' '));
	}
	else{
		return navigator.userAgent;
	}
}
// Retrieves the parameter from a class JavaScript call
function GetArgumentValues(currentClass, classString){
	var getValuePosition = currentClass.indexOf(classString + "(");
	var classLength = classString.length;
	var getString = true;
	var stringCount = 0;
	var maxString = "";
	var variableArray = new Array();
	var count = 0;
	while(getString){
		stringCount ++;
		currentCharacter = currentClass.charAt(classLength + getValuePosition + stringCount);
		if(currentCharacter == " "){
			continue;	
		}
		else if(currentCharacter == ","){
			variableArray[count] = maxString;
			maxString = "";
			count ++;
		}
		else if(currentCharacter == ")"){
			variableArray[count] = maxString;
			maxString = "";
			count ++;
			getString = false;
			break;
		}
		else{
			maxString += currentCharacter;
		}
	}
	return variableArray;
}
// Returns the width and height of the body tag within an array
function GetBodyDimensions(){
	if(DetectBrowser() == "Firefox"){
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} 
	else if(DetectBrowser() == "Safari"){
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} 
	else{
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	return arrayPageSizeWithScroll;
}
// Returns the clients window size as an array, not including scroll
function GetWindowDimensions(){ 
	if(DetectBrowser() == "Internet Explorer"){
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} 
	else{
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	}
	var dimensions = new Array(windowWidth, windowHeight);
	return dimensions;
}
// Returns the scroll location of the clients browser
function GetScrollPosition(){
	if(DetectBrowser() == "Safari"){
		scrollOffset = document.body.scrollTop;
	}
	else{
		scrollOffset = document.documentElement.scrollTop;
	}
	return scrollOffset;
}
// Returns the left position of an element in relation to the document
 function LeftPosition(element){
	var xPos = element.offsetLeft;
	var tempElement = element.offsetParent;
	while (tempElement != null) {
		xPos += tempElement.offsetLeft;
		tempElement = tempElement.offsetParent;
	}
	return xPos;	
}
// Returns the top position of an element in relation to the document
function TopPosition(element){
	var yPos = element.offsetTop;
	var tempElement = element.offsetParent;
	while (tempElement != null) {
		yPos += tempElement.offsetTop;
		tempElement = tempElement.offsetParent;
	}
	return yPos;
}
// Returns the coordinates of the mouse, NEEDS TO BE CALLED FROM AN EVENT TO WORK
function GetMouseLocation(e){
	if(DetectBrowser() == "Internet Explorer"){ 
		mouseX = event.clientX + document.documentElement.scrollLeft;
		mouseY = event.clientY + document.documentElement.scrollTop;
	} 
	else{
		mouseX = e.pageX;
		mouseY = e.pageY;
	}  
	if(mouseX < 0){mouseX = 0;}
	if(mouseY < 0){mouseY = 0;}
	mouseArray = new Array(mouseX,mouseY);
	return mouseArray;
}

