﻿function ScreenDimensions()
{
	function cb()
	{
		// temp is the return value
		// a is the loop variable
		// newVal is the variable of which to check the value
		var temp = 0, a, arg, newVal;
		// Go through all the arguments of the function,
		// except the last (the default value)
		for (a = 0; a < arguments.length - 1; a++)
		{
			// arg is the current argument being checked
			arg = arguments[a];
			// if there are two parts to this argument, combine them
			// e.g. new Array("document.body", "clientHeight")
			// becomes document.body.clientHeight
			newVal = ((arg[1]) ? arg[0] + "." + arg[1] : arg[0]);
			// Check to see if both parts of the array exist and are non-zero,
			// e.g. document.body and document.body.clientHeight
			// checking for document.body.clientHeight when
			// document.body doesn't exist gives an error,
			// hence both arg[0] and newVal.
			// If a value has been found previously, ((temp) ? ... : "")
			// check to see if the new value is less than the last
			// If all this is true, this is your new value.
			eval("if ((" + arg[0] + ") && (" + newVal + ")" + ((temp) ? " && (" + newVal + " < " + temp + ")" : "") + ") { var temp = " + newVal + "; }");
		}
		// If a value was found, return it.
		// If not, resort to the default (last argument)
		return ((temp) ? temp : arguments[arguments.length - 1]);
	}

	var c = "client", d = "document", h = "Height", i = "inner", p = '.', s = "scroll", v = "Width", w = "window";
	var db = d + ".body", de = d + p + d + "Element", sl = s + "Left", st = s + "Top";
	// window.innerHeight, document.documentElement.clientHeight, document.body.clientHeight
	// window.pageXOffset, document.documentElement.scrollLeft, document.body.scrollLeft
	// window.pageYOffset, document.documentElement.scrollTop, document.body.scrollTop
	// window.innerWidth, document.documentElement.clientWidth, document.body.clientWidth
	this.height = cb(new Array(w + p + i + h), new Array(de, c + h), new Array(db, c + h), 0);
	this.scrollLeft = cb(new Array(w + ".pageXOffset"), new Array(de, sl), new Array(db, sl), 0);
	this.scrollTop = cb(new Array(w + ".pageYOffset"), new Array(de, st), new Array(db, st), 0);
	this.width = cb(new Array(w + p + i + v), new Array(de, c + v), new Array(db, c + v), 0);

}

function Dimensions()
{
	function Dimens()
	{
		var w = 0;
		var h = 0;
		//IE
		if(!window.innerWidth)
		{
			//strict mode
			if(!(top.document.documentElement.clientWidth == 0))
			{
				w = top.document.documentElement.clientWidth;
				h = top.document.documentElement.clientHeight;
			}
			//quirks mode
			else
			{
				w = top.document.body.clientWidth;
				h = top.document.body.clientHeight;
			}
		}
		//w3c
		else
		{
			w = window.top.innerWidth;
			h = window.top.innerHeight;
		}
		return {width:w,height:h};
	}

	var size = Dimens();
	this.width = size.width;
	this.height = size.height;
}