window.getSize = function window_getSize(excludeScroll) {
    var width = 0;
    var height = 0;
	var scroll = { 'x' : 0, 'y' : 0};
	
    /* Non-IE */
    if (typeof(window.innerWidth) == 'number') {
        width = window.innerWidth;
        height = window.innerHeight;
    }
    /* IE 6+ in 'standards compliant mode' */
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        width = document.documentElement.clientWidth;
        height = document.documentElement.clientHeight;
    }
    /* IE 4 compatible */
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        width = document.body.clientWidth;
        height = document.body.clientHeight;
    }
	
    if (!excludeScroll) {
    	scroll = window.getScrollbarSize({ 'width' : width, 'height' : height });
    }
    
    return { 'width' : width - scroll.x, 'height' : height - scroll.y };
}
window.getScrollPosition = function window_getScrollPosition() {
    var y = 0;
    var x = 0;
    /* Netscape */
    if (typeof(window.pageYOffset) == 'number') {
        y = window.pageYOffset;
        x = window.pageXOffset;
    }
    /* DOM */
    else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        y = document.body.scrollTop;
        x = document.body.scrollLeft;
    }
    /* IE6 */
    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        y = document.documentElement.scrollTop;
        x = document.documentElement.scrollLeft;
    }
    return { 'x' : x, 'y' : y };
}
window.centerElement = function window_centerElement(elem, last_pos) {
	var window_size = window.getSize();
	var scrolling = window.getScrollPosition();
	var document_size = document.getSize();			
	var element = { width : elem.offsetWidth, height : elem.offsetHeight };
	
	var posx = Math.round(((window_size.width - element.width) / 2) + scrolling.x);
	var posy = Math.round(((window_size.height - element.height) / 2) + scrolling.y);
	
	var scrollVisible = window.getScrollbarVisibility(window_size);
	
	if (last_pos.x != null) {
		if (last_pos.x < posx && window_size.width < element.width) {
	    	 if ((posx - last_pos.x) < (element.width - window_size.width) / 2) {
	    	 	posx = last_pos.x;
	    	 }
	    	 else {
	    	 	posx = scrolling.x - ((element.width - window_size.width));
	    	 	
	    	 	if (navigator.current.browser.name == "firefox" && scrollVisible.x) {	
	    	 		posx -= 20;
	    	 	}
	    	 }
	    }
	    else if (last_pos.x > posx && window_size.width < element.width) {
	    	 if ((last_pos.x - posx) < (element.width - window_size.width) / 2) {
	    	 	posx = last_pos.x;
	    	 }
	    	 else {
	    	 	posx = scrolling.x;
	    	 }
	    }
	    
	    if (posx < 0) {
	        posx = 0;
	    }
	    
	    elem.style.left = posx + 'px';		
	}
	if (last_pos.y != null) {
	   if (last_pos.y < posy && window_size.height < element.height) {
	    	 if ((posy - last_pos.y) < (element.height - window_size.height) / 2) {
	    	 	posy = last_pos.y;
	    	 }
	    	 else {
	    	 	posy = scrolling.y - ((element.height - window_size.height));
	    	 	
	    	 	if (navigator.current.browser.name == "firefox" && scrollVisible.y) {
	    	 		posy -= 20;
	    	 	}		    	 	
	    	 	
	    	 }
	    }
	    else if (last_pos.y > posy && window_size.height < element.height) {
	    	 if ((last_pos.y - posy) < (element.height - window_size.height) / 2) {
	    	 	posy = last_pos.y;
	    	 }
	    	 else {
	    	 	posy = scrolling.y;
	    	 }
	    }
	
		if (posy < 0) {
        	posy = 0;
    	}
    	
    	elem.style.top = posy + 'px';
	}
    
    return { 'x': posx, 'y': posy };	
}
window.getScrollbarVisibility = function window_getScrollbarVisibility(window_size) {
	var body;
	
	// use document.documentElement if browser is in standard render mode
	if (document.compatMode && document.compatMode == 'CSS1Compat') {
		body = document.documentElement;
	}
	// use document.body if browser is in quirks render mode
	else {
		body = document.body;
	}
	
	return {
		'x' : ((body.offsetWidth <= body.scrollWidth) && (body.scrollWidth > window_size.width)),
		'y' : ((body.offsetHeight <= body.scrollHeight) && (body.scrollHeight > window_size.height))
	};
}
window.getScrollbarSize = function window_getScrollbarSize(window_size) {
	var scrollVisibility = window.getScrollbarVisibility(window_size);
	
	var scroll = { 'x' : 0, 'y' : 0 };
	if (navigator.current.browser.name == "msie" && navigator.current.browser.version < 7) {
		// substract for IE scrollbars
		if (!scrollVisibility.x) {
			scroll.x += 20;
		}
		else if (!scrollVisibility.y) {
			scroll.y += 16;
		}
		
		// substract for IE bottom 'border'
		if (!scrollVisibility.y) {
			scroll.y += 4;
		}
	}
	else if (navigator.current.browser.name == "firefox"){
		// substract for firefox scrollbars
		if (navigator.current.browser.version < 2) {
			if (scrollVisibility.y && !scrollVisibility.x) {
				scroll.x += 16;
			}
		}
		else {	
			if (scrollVisibility.y && !scrollVisibility.x) {
				scroll.x += 17;
			}
		}
		
		if (scrollVisibility.x && !scrollVisibility.y) {
			scroll.y += 16;
		}
	}
	
	return scroll;
}