// Create the namespace
if( !window.com ) {
	/**
	 * @ignore
	 **/
	window.com = new Object();
}
if( !com.bigbad ) {
	/**
	 * @ignore
	 **/
	 com.bigbad = new Object();
}

/**
 * Inner Window Size Detector
 * @constructor
 * @version 2.1 (05/04/2005)
 * @author Gregory Ramsperger <gregory.ramsperger-at-bigbad.com>
 **/
com.bigbad.WindowSize = function() {
	if( window.innerHeight || document.body ) this._setFindBy();
}

/**
 * Class version
 * @type Number
 **/
com.bigbad.WindowSize.prototype.VERSION = 2.1;

/**
 * ID of method used to discover inner window size (used to set _findBy)
 * @type int
 * @private
 **/
com.bigbad.WindowSize.prototype.WINDOW_INNER = 0;

/**
 * ID of method used to discover inner window size (used to set _findBy)
 * @type int
 * @private
 **/
com.bigbad.WindowSize.prototype.BODY_CLIENT = 1;

/**
 * ID of method used to discover inner window size (used to set _findBy)
 * <p>IE version > 5</p>
 * @type int
 * @private
 **/
com.bigbad.WindowSize.prototype.HTML_CLIENT = 2;

/**
 * ID of method used to discover inner window size (used to set _findBy)
 * @type int
 * @private
 **/
com.bigbad.WindowSize.prototype.BODY_OFFSET = 3;

/**
 * ID of method used to discover inner window size (used to set _findBy).
 * <p>IE version < 5</p>
 * @type int
 * @private
 **/
com.bigbad.WindowSize.prototype.HTML_CLIENT_ELEMENT = 4;

/**
 * Discovered window inner width
 * @type int
 * @private
 **/
com.bigbad.WindowSize.prototype._width = 0;

/**
 * Discovered window inner height
 * @type int
 * @private
 **/
com.bigbad.WindowSize.prototype._height = 0;

/**
 * Discovered method to use when finding the window size
 * @type int
 * @private
 **/
com.bigbad.WindowSize.prototype._findBy = -1;

/**
 * Determine which method to use when discovering the inner window size.
 * <p>Sets _findBy variable</p>
 * @private
 **/
com.bigbad.WindowSize.prototype._setFindBy = function() {
	/* here is tricky part: various browsers have different methods of
	 * finding the actual width and height of a window.
	 * 
	 * Netscape (all versions) and Safari: this is easily gotten from
	 *    window.innerHeight and window.innerWidth
	 *
	 * Internet Explorer 6: width must be gotten from the HTML node
	 *    using document.body.parentNode.clientWidth
	 *
	 * Internet Explorer 5-: The size of the body represents the
	 *    actual width and height using document.body.clientWidth but
	 *    some versions use the offsetWidth instead of clientWidth.
     */
	if( window.innerWidth ) {
		this._findBy = this.WINDOW_INNER;
	} else {
		if( document.body ) {
			if( document.body.parentNode ) {

				// since certain HTML syntax errors can make <body> the child of things other than <head>, loop to find the <html> node
				var htmlNode = document.body;
				while( htmlNode.nodeName != "HTML" && htmlNode.parentNode ) {
					htmlNode = htmlNode.parentNode;
				}

				if( htmlNode.clientWidth ) {
					this._findBy = this.HTML_CLIENT;
				}
			}
			if( document.body.parentElement && this._findBy == -1) {

				// since certain HTML syntax errors can make <body> the child of things other than <head>, loop to find the <html> node
				var htmlElement = document.body;
				while( htmlElement.nodeName != "HTML" && htmlElement.parentElement ) {
					htmlElement = htmlElement.parentElement;
				}

				if( htmlElement.clientWidth ) {
					this._findBy = this.HTML_CLIENT_ELEMENT;
				}
			}
			if( this._findBy == -1 && document.body.clientWidth ) {
				this._findBy = this.BODY_CLIENT;
			} else if( this._findBy == -1 && document.body.offsetWidth ) {
				this._findBy = this.BODY_OFFSET;
			}
		}
	}
}

/**
 * Retrieve the Window size.
 * @return An object containing the inner width and hieght of the window. {width:int, height: int}
 * @type Object
 **/
com.bigbad.WindowSize.prototype.getInnerSize = function() {
	// if the _findBy variable is not set, try to find it.
	if( this._findBy < 0 ) this._setFindBy(); 

	switch( this._findBy ) {
		case this.WINDOW_INNER:
			this._width = window.innerWidth;
			this._height = window.innerHeight;
			break;
		case this.HTML_CLIENT:
			var htmlNode = document.body;

			// since certain HTML syntax errors can make <body> the child of things other than <head>, loop to find the <html> node
			while( htmlNode.nodeName != "HTML" && htmlNode.parentNode ) {
				htmlNode = htmlNode.parentNode;
			}
			this._width = htmlNode.clientWidth;
			this._height = htmlNode.clientHeight;
			break;
		case this.HTML_CLIENT_ELEMENT:
			var htmlElement = document.body;

			// since certain HTML syntax errors can make <body> the child of things other than <head>, loop to find the <html> node
			while( htmlElement.nodeName != "HTML" && htmlElement.parentElement ) {
				htmlElement = htmlElement.parentElement;
			}
			this._width = htmlElement.clientWidth;
			this._height = htmlElement.clientHeight;
			break;
		case this.BODY_CLIENT:
			this._width = document.body.clientWidth;
			this._height = document.body.clientHeight;
			break;
		case this.BODY_OFFSET:
			this._width = document.body.offsetWidth;
			this._height = document.body.offsetHeight;
			break;
		default:
			return null;
	}
	return {width:this._width,height:this._height,toString:function(){return "width: "+this.width+", height: "+this.height}};
}
