/*
 * Author: Rodney Rehm
 * Web: http://www.rodneyrehm.de
 * Get blockup at http://www.rodneyrehm.de/tools/blockup
 *
 * Date / Version: 12. July 2006
 *
 * Published under the Open Source BSD-License
 * http://www.opensource.org/licenses/bsd-license.php
 */


/*
 *
 * Core code from - quirksmode.org
 *
 */

/**
 * calculate dimensions of document and viewport
 **/
function pageDimensions()
{
	/* cache values */
	this.pageWidth = -1;
	this.pageHeight = -1;
	this.viewportWidth = -1;
	this.viewportHeight = -1;

	/**
	 * calculate dimensions - browser safe
	 **/
	this.refresh = function()
	{
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY)
		{
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else if(document.body.scrollHeight > document.body.offsetHeight)
		{
			// all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		else
		{
			// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		if(self.innerHeight)
		{
			// all except Explorer
			this.viewportWidth = self.innerWidth;
			this.viewportHeight = self.innerHeight;
		}
		else if(document.documentElement && document.documentElement.clientHeight)
		{
			// Explorer 6 Strict Mode
			this.viewportWidth = document.documentElement.clientWidth;
			this.viewportHeight = document.documentElement.clientHeight;
		}
		else if(document.body)
		{
			// other Explorers
			this.viewportWidth = document.body.clientWidth;
			this.viewportHeight = document.body.clientHeight;
		}

		// for small pages with total height less than height of the viewport
		if(yScroll < this.viewportHeight)
			this.pageHeight = this.viewportHeight;
		else
			this.pageHeight = yScroll;

		// for small pages with total width less than width of the viewport
		if(xScroll < this.viewportWidth)
			this.pageWidth = this.viewportWidth;
		else
			this.pageWidth = xScroll;
	};

	/**
	 * get the size of the viewport
	 * @returns Array(width, height)
	 **/
	this.getViewport = function()
	{
		return new Array(this.viewportWidth, this.viewportHeight);
	}

	/**
	 * get the width of the viewport
	 * @returns width (int)
	 **/
	this.getViewWidth = function()
	{
		return this.viewportWidth;
	}

	/**
	 * get the height of the viewport
	 * @returns height (int)
	 **/
	this.getViewHeight = function()
	{
		return this.viewportHeight;
	}

	/**
	 * get the size of the document
	 * @returns Array(width, height)
	 **/
	this.getDocument = function()
	{
		return new Array(this.pageWidth, this.pageHeight);
	}

	/**
	 * get the width of the document
	 * @returns width (int)
	 **/
	this.getDocWidth = function()
	{
		return this.pageWidth;
	}

	/**
	 * get the height of the document
	 * @returns height (int)
	 **/
	this.getDocHeight = function()
	{
		return this.pageHeight;
	}

	/**
	 * get the current scroll-position
	 * @returns scrollPosition (int)
	 **/
	this.getVerticalScroll = function()
	{
		if(self.pageYOffset)
		{
			yScroll = self.pageYOffset;
		}
		else if(document.documentElement && document.documentElement.scrollTop)
		{
			// Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		}
		else if(document.body)
		{
			// all other Explorers
			yScroll = document.body.scrollTop;
		}
		return yScroll;
	}

	/**
	 * get the computed value of a style-attribute
	 * @see http://www.robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/
	 * @param elem element to get computed style from
	 * @param cssAttribute compute style for attribute
	 * @return (string) value of the selected attribute
	 **/
	this.getStyle = function(elem, cssAttribute, toInt)
	{
		var strValue = '';
		if(document.defaultView && document.defaultView.getComputedStyle)
		{
			strValue = document.defaultView.getComputedStyle(elem, '').getPropertyValue(cssAttribute);
		}
		else if(elem.currentStyle)
		{
			try
			{
				cssAttribute = cssAttribute.replace(/\-(\w)/g, function (strMatch, p1)
				{
					return p1.toUpperCase();
				});
				strValue = elem.currentStyle[cssAttribute];
			}
			catch(e){ /* Used to prevent an error in IE 5.0 */ }
		}
		if(toInt) return parseInt(strValue);
		return strValue;
	}

	/* calculate dimensions on instantiation */
	this.refresh();
}
