//##########################################################################################

/*
Copyright (C) 2005-2009 WebLegs, Inc.
This program is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see <http://www.gnu.org/licenses/>.
*/

//##########################################################################################

//--> Begin :: Namespace Check
	//define vars for needed objects
	var WebLegs;
	
	//is our namespace present?
	if(!WebLegs) {
		WebLegs = {};
	}
//<-- End :: Namespace Check

//##########################################################################################

//--> Begin Method :: Constructor
	WebLegs.BrowserWindow = function() {
		//do nothing
	};
//<-- End Method :: Constructor

//##########################################################################################

//--> Begin Method :: $
	//make a getElementById shortcut
	WebLegs.BrowserWindow.prototype.$ = function(key) {
		return document.getElementById(key);
	};
//<-- End Method :: $

//##########################################################################################

//--> Begin Function :: IncludeCSS
	WebLegs.BrowserWindow.prototype.IncludeCSS = function(URL){
		var HeadTag = document.getElementsByTagName("head");
		if(HeadTag[0] != null){
			var Tag = document.createElement("link");
			Tag.setAttribute("href", URL);	
			Tag.setAttribute("rel", "stylesheet");
			Tag.setAttribute("type", "text/css");
			HeadTag[0].appendChild(Tag);
		}
	}
//<-- End Function :: IncludeCSS

//##########################################################################################

//--> Begin Function :: IncludeJS
	WebLegs.BrowserWindow.prototype.IncludeJS = function(URL){
		var HeadTag = document.getElementsByTagName("head");
		if(HeadTag[0] != null){
			var Tag = document.createElement("script");
			Tag.setAttribute("src", URL);	
			Tag.setAttribute("type", "text/javascript");
			HeadTag[0].appendChild(Tag);
		}
	}
//<-- End Function :: IncludeJS

//##########################################################################################

//--> Begin Method :: GetEvent
	//make a getElementById shortcut
	WebLegs.BrowserWindow.prototype.GetEvent = function(Event) {
		var EventObj = {};
		EventObj.Target;
		EventObj.Type;
		
		//target
			//check for anti-event model (usually ie)
			if(!Event) {
				var Event = window.event;
			}
			//usually ff
			if(Event.target) {
				EventObj.Target = Event.target;
			}
			else if(Event.srcElement) {
				EventObj.Target = Event.srcElement;
			}
			//safari bug?
			if(EventObj.Target.nodeType == 3) {
				EventObj.Target = EventObj.Target.parentNode;
			}
		//end target
		
		//type
		EventObj.Type = Event.type;
		
		//keyboard
			//setup nested keyboard collection
			EventObj.Keyboard = {};
			
			//key codes
				//usually ie
				if(Event.keyCode) {
					EventObj.Keyboard.CharacterCode = Event.keyCode;
				}
				//usuall ff
				else if(Event.which) {
					EventObj.Keyboard.CharacterCode = Event.which;
				}
			//end key codes
			
			//actual character
			EventObj.Keyboard.Character = String.fromCharCode(EventObj.Keyboard.CharacterCode);
			
			//special keys
				EventObj.Keyboard.AltKey = Event.altKey;
				EventObj.Keyboard.CtrlKey = Event.ctrlKey;
				EventObj.Keyboard.MetaKey = Event.metaKey;
				EventObj.Keyboard.ShiftKey = Event.shiftKey;
			//end special keys
		//end keyboard
		
		return EventObj;
	};
//<-- End Method :: GetEvent

//##########################################################################################

//--> Begin Method :: GetWindowWidth
	//this method returns the canvas width (in pixels) for the browser or frame
	WebLegs.BrowserWindow.prototype.GetWindowWidth = function() {
		if(self.innerWidth) {
			//in firefox we want to remove the scrollbar
			//from this measurement if it exists
			if(document.getDocumentHeight() > self.innerHeight) {
				return self.innerWidth - 19;
			}
			return self.innerWidth;
		}
		else if(document.documentElement && document.documentElement.clientWidth) {
			return document.documentElement.clientWidth;
		}
		else if(document.body) {
			return document.body.clientWidth;
		}
		else{
			return 0;
		}
	};
//<-- End Method :: GetWindowWidth

//##########################################################################################

//--> Begin Method :: GetWindowHeight
	//this method returns the canvas height (in pixels) for the browser or frame
	WebLegs.BrowserWindow.prototype.GetWindowHeight = function() {
		if(self.innerHeight) {
			//in firefox we want to remove the scrollbar
			//from this measurement if it exists
			if(document.getDocumentWidth() > self.innerWidth) {
				return self.innerHeight - 19;
			}
			return self.innerHeight;
		}
		else if(document.documentElement && document.documentElement.clientHeight) {
			return document.documentElement.clientHeight;
		}
		else if(document.body) {
			return document.body.clientHeight;
		}
		else{
			return 0;
		}
	};
//<-- End Method :: GetWindowHeight

//##########################################################################################

//--> Begin Method :: GetDocumentHeight
	//get document height
	WebLegs.BrowserWindow.prototype.GetDocumentHeight = function() {
		var test1 = document.body.scrollHeight;
		var test2 = document.body.offsetHeight
		//all but Explorer Mac
		if (test1 > test2) {
			return document.body.scrollHeight;
		}
		else {
			//Explorer Mac
			//would also work in Explorer 6 Strict, Mozilla and Safari
			return document.body.offsetHeight;
		}
	};
//<-- End Method :: GetDocumentHeight

//##########################################################################################

//--> Begin Method :: GetDocumentWidth
	//get document width
	WebLegs.BrowserWindow.prototype.GetDocumentWidth = function() {
		var test1 = document.body.scrollWidth;
		var test2 = document.body.offsetWidth;
		//all but Explorer Mac
		if (test1 > test2) {
			return document.body.scrollWidth;
		}
		else {
			//Explorer Mac
			//would also work in Explorer 6 Strict, Mozilla and Safari
			return document.body.offsetWidth;
		}
	};
//<-- End Method :: GetDocumentWidth

//##########################################################################################

//--> Begin Method :: GetScrolledX
	//this method returns the X offset (horizontal scroll amount)
	WebLegs.BrowserWindow.prototype.GetScrolledX = function() {
		//all except IE
		if(self.pageXOffset) {
			return self.pageXOffset;
		}
		//IE 6 strict
		else if(document.documentElement && document.documentElement.scrollLeft) {
			return document.documentElement.scrollLeft;
		}
		//all others
		else if(document.body) {
			return document.body.scrollLeft;
		}
	};
//<-- End Method :: GetScrolledX

//##########################################################################################

//--> Begin Method :: GetScrolledY
	//this method returns the Y offset (vertical scroll amount)
	WebLegs.BrowserWindow.prototype.GetScrolledY = function() {
		//all except IE
		if(self.pageYOffset) {
			return self.pageYOffset;
		}
		//IE 6 strict
		else if(document.documentElement && document.documentElement.scrollTop) {
			return document.documentElement.scrollTop;
		}
		//all others
		else if(document.body) {
			return document.body.scrollTop;
		}
	};
//<-- End Method :: GetScrolledY

//##########################################################################################

//--> Begin Method :: GetElementHeight
	//this method returns the height of a rendered element
	WebLegs.BrowserWindow.prototype.GetElementHeight = function(element) {
		var elementHeight = 0;
		if(element.offsetHeight) {
			elementHeight = element.offsetHeight;
		}
		else if(element.clip && element.clip.height) {
			elementHeight = element.clip.height;
		}
		else if(element.style && element.style.pixelHeight) {
			elementHeight = element.style.pixelHeight;
		}
		return parseInt(elementHeight);
	};
//<-- End Method :: GetElementHeight

//##########################################################################################

//--> Begin Method :: GetElementWidth
	//this method returns the width of a rendered element
	WebLegs.BrowserWindow.prototype.GetElementWidth = function(element) {
		var elementWidth = 0;
		if(element.offsetWidth) {
			elementWidth = element.offsetWidth;
		}
		else if(element.clip && element.clip.width) {
			elementWidth = element.clip.width;
		}
		else if(element.style && element.style.pixelWidth) {
			elementWidth = element.style.pixelWidth;
		}
		return parseInt(elementWidth);
	};
//<-- End Method :: GetElementWidth

//##########################################################################################

//--> Begin Method :: GetElementTop
	//this method returns the top coordinate for an element
	WebLegs.BrowserWindow.prototype.GetElementTop = function(element) {
		var elementTop = 0;
		elementTop = element.offsetTop;
		var ParentElement = element.offsetParent;
		while(ParentElement != null) {
  			elementTop += ParentElement.offsetTop;
	  		ParentElement = ParentElement.offsetParent;
  		}
		return elementTop;
	};
//<-- End Method :: GetElementTop

//##########################################################################################

//--> Begin Method :: GetElementLeft
	//this method returns the left coordinate for an element
	WebLegs.BrowserWindow.prototype.GetElementLeft = function(element) {
		var elementLeft = 0;
		elementLeft = element.offsetLeft;
		var ParentElement = element.offsetParent;
		while(ParentElement != null) {
  			elementLeft += ParentElement.offsetLeft;
	  		ParentElement = ParentElement.offsetParent;
  		}
		return elementLeft;
	};
//<-- End Method :: GetElementLeft

//##########################################################################################