/**
 * @author: Anatolij Rau, MiB
 * @copyright: Anatolij Rau & RAGBIT GmbH
 * @access: 2011-01-19
 * @version: 2.2.0
 *
 * v2.2.0: Fixed getWindowSize to use clientWidth and clientHeight as primary source
**/
function dimensionsClass() {

	this.is_ie = document.all ? true : false;
	/* --> get mouse position */
	this.mPos = new Object();
	this.mPos.x = 0;
	this.mPos.y = 0;
	/* <-- get mouse position */
	
	/* --> get sizes */
	this.size = new Object();
	this.size.width = 0;
	this.size.height = 0;
	/* <-- get sizes */

	/* --> get window sizes */
	this.windowSize = new Object();
	this.windowSize.width = 0;
	this.windowSize.height = 0;
	/* <-- get window sizes */

	/* --> get scroll positions */
	this.scrollPos = new Object();
	this.scrollPos.x = 0;
	this.scrollPos.y = 0;
	/* <-- get scroll positions */

	/* --> get positions */
	this.tagPos = new Object();
	this.tagPos.x = 0;
	this.tagPos.y = 0;
	/* <-- get positions */

	
	this.mousemove_event = new Object();
	
	/** ********************* Fintkonen ************************* */
	/**
	 * Get mouse position from top and left (incl. scrolling)
	 */
	this.getMousePos = function(){
		
		if (typeof this.mPos != 'object') {
			this.mPos = new Object();
			this.mPos.x = 0;
			this.mPos.y = 0;
		}
		
		if (this.is_ie) {
			if (window.event && this && this.mPos && this.mPos.x) {
				this.mPos.x = parseInt(this.mousemove_event.clientX) + (document.body.scrollLeft ? parseInt(document.body.scrollLeft) : parseInt(document.documentElement.scrollLeft));
				this.mPos.y = parseInt(this.mousemove_event.clientY) + (document.body.scrollTop ? parseInt(document.body.scrollTop) : parseInt(document.documentElement.scrollTop));
			}
		}
		else { // grab the x-y pos.s if browser is NS
			if (this.mousemove_event) {
				this.mPos.x = this.mousemove_event.pageX;
				this.mPos.y = this.mousemove_event.pageY;
			}
		}
		if (this.mPos.x < 0 || isNaN(this.mPos.x)) 
			this.mPos.x = 0;
		if (this.mPos.y < 0 || isNaN(this.mPos.y)) 
			this.mPos.y = 0;
		
		return this.mPos;
	};
	
	/**
	 * get sizes of a object (Alfa)
	 */
	this.getSizeOf = function(element){
		this.size = new Object();
		this.size.width = 0;
		this.size.height = 0;
		
		if (typeof this.size == 'object') {
			this.size['width'] = element.offsetWidth;
			this.size['height'] = element.offsetHeight;
		}
		
		if ((element.tagName =="BODY" || element.tagName =="HTML") && element.scrollWidth) {
			var sw = parseInt(element.scrollWidth);
			var sh = parseInt(element.scrollHeight);
			if (this.size.width < sw) this.size.width = sw;
			if (this.size.height < sh) this.size.height = sh;
		}
		
		return this.size;
	};
	
	/**
	 * Get size of window "inner"
	 * @param {Object} winObj
	 * @return {Object}
	 */
	this.getWindowSize = function(winObj){
		this.windowSize = new Object();
		this.windowSize.width = 0;
		this.windowSize.height = 0;
		
		if (winObj == undefined) winObj = window;
		if (winObj.document.documentElement && winObj.document.documentElement.clientWidth) {
			this.windowSize.width = winObj.document.documentElement.clientWidth;
			this.windowSize.height = winObj.document.documentElement.clientHeight;
		} else if (this.windowSize.width == 0 && this.windowSize.height == 0) {
			this.windowSize.width = winObj.document.body.offsetWidth;
			this.windowSize.height = winObj.document.body.offsetHeight;
		} else if (winObj.innerWidth) {
			this.windowSize.width = winObj.innerWidth;
			this.windowSize.height = winObj.innerHeight;
		}
		
		return this.windowSize;
	};
	
	/**
	 * Scroll position of window or frame
	 * @param {Object} winObj
	 * @return {Object}
	 */
	this.getScrollPos = function(winObj) {
		if (winObj == undefined) winObj = window;
	
		this.scrollPos = new Object();
		this.scrollPos.x = 0;
		this.scrollPos.y = 0;
	
		if( typeof( winObj.pageYOffset ) == 'number' ) {
			//Netscape
			this.scrollPos.y = winObj.pageYOffset;
			this.scrollPos.x = winObj.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM
			this.scrollPos.y = document.body.scrollTop;
			this.scrollPos.x = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6
			this.scrollPos.y = document.documentElement.scrollTop;
			this.scrollPos.x = document.documentElement.scrollLeft;
		}
		return this.scrollPos;
	};
	
	/**
	 * Get left and top position of a element
	 * @param {Object} element
	 * @return {Object}
	 */
	this.getPosition = function(element) {
	  var elem=element,tagname="",x=0,y=0;
		this.tagPos = new Object();
		this.tagPos.x = 0;
		this.tagPos.y = 0;
		  
	  while ((typeof(elem)=="object")&&(typeof(elem.tagName)!="undefined")) {
	    y+=elem.offsetTop;
	    x+=elem.offsetLeft;
	    tagname=elem.tagName.toUpperCase();
	
	    if (tagname=="BODY")
	      elem=0;
	
	    if (typeof(elem)=="object")
	      if (typeof(elem.offsetParent)=="object")
	        elem=elem.offsetParent;
	  }
		/** body offset **/
		var bodySize = this.getSizeOf(document.body);
		var winSize =  this.getSizeOf(document.getElementsByTagName('html')[0]);
		x+=(winSize.width-bodySize.width)/2;
		y+=(winSize.height-bodySize.height)/2;
	
	  this.tagPos.x=x;
	  this.tagPos.y=y;
	  return this.tagPos;
	};
}

var dimensions = new dimensionsClass();

/*window.onload = function(){*/
	document.onmousemove = (function(e){
		if (typeof e != 'undefined') {
			for (var i in e) {
				dimensions.mousemove_event[i] = e[i];
			//dimensions.getMousePos();
			}
		}
		else {
			if (event.type == 'mousemove') {
				for (var i in window.event) 
					dimensions.mousemove_event[i] = window.event[i];
				dimensions.getMousePos();
			}
		}
	});
/*};*/

