
/*------------------------------------
|
|  Add load event - loads functions in
|  and unbtrusive manner.
|
*************************************/

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}


/*------------------------------------
|
|  Add events object.
|
*************************************/


var xb =
{
	evtHash: [],

	ieGetUniqueID: function(_elem)
	{
		if (_elem === window)
		{
			return 'theWindow';
		}
		else if (_elem === document)
		{
			return 'theDocument';
		}
		else
		{
			return _elem.uniqueID;
		}
	},

	addEvent: function(_elem, _evtName, _fn, _useCapture)
	{
		if (typeof _elem.addEventListener != 'undefined')
		{
			_elem.addEventListener(_evtName, _fn, _useCapture);
		}
		else if (typeof _elem.attachEvent != 'undefined')
		{
			var key = '{FNKEY::obj_' + xb.ieGetUniqueID(_elem) + '::evt_' + _evtName + '::fn_' + _fn + '}';
			var f = xb.evtHash[key];
			
			if (typeof f != 'undefined')
			{
				return;
			}

			f = function()
			{
				_fn.call(_elem);
			};

			xb.evtHash[key] = f;
			_elem.attachEvent('on' + _evtName, f);

			// attach unload event to the window to clean up possibly IE memory leaks
			window.attachEvent('onunload', function()
			{
				_elem.detachEvent('on' + _evtName, f);
			});

			key = null;
			//f = null;   /* DON'T null this out, or we won't be able to detach it */
		}
		else
		{
			_elem['on' + _evtName] = _fn;
		}
	},

	removeEvent: function(_elem, _evtName, _fn, _useCapture)
	{
		if (typeof _elem.removeEventListener != 'undefined')
		{
			_elem.removeEventListener(_evtName, _fn, _useCapture);
		}
		else if (typeof _elem.detachEvent != 'undefined')
		{
			var key = '{FNKEY::obj_' + xb.ieGetUniqueID(_elem) + '::evt' + _evtName + '::fn_' + _fn + '}';
			var f = xb.evtHash[key];
			if (typeof f != 'undefined')
			{
				_elem.detachEvent('on' + _evtName, f);
				delete xb.evtHash[key];
			}

			key = null;
			//f = null;   /* DON'T null this out, or we won't be able to detach it */
		}
	}
};


/*--------------------

String replace all

----------------------*/

function replaceAll(Source,stringToFind,stringToReplace)
{
	var temp = Source;
	var index = temp.indexOf(stringToFind);
	while(index != -1)
	{
		temp = temp.replace(stringToFind,stringToReplace);
		index = temp.indexOf(stringToFind);
	}
	
	return temp;
}


/*---------------------------------------

Get page dimensions, returns an object

----------------------------------------*/

function getPageDimensions()
{
	var dims = new Object();
	var width,height;
	if (window.innerHeight) // all except Explorer
	{
		width = window.innerWidth;
		height = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		width = document.body.clientWidth;
		height = document.body.clientHeight;
	}
	
	dims.width = width;
	dims.height = height;
	
	return dims;
}

function getScrollDimensions()
{
	var pageDims = getPageDimensions()
	var dims = new Object();
	var width,height;
	
	if (window.innerHeight && window.scrollMaxY)
	{// Firefox
		height = window.innerHeight + window.scrollMaxY;
		width = window.innerWidth + window.scrollMaxX;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		height = document.body.scrollHeight;
		width = document.body.scrollWidth;
	}
	else
	{ // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		height = document.body.offsetHeight;
		width = document.body.offsetWidth;
  	}

	
	//dims.width = pageDims.width + width;
	//dims.height = pageDims.height + height;
	dims.width = width;
	dims.height = height;
	
	if(width < pageDims.width)
	{
		dims.width = pageDims.width;
	}
	
	if(height < pageDims.height)
	{
		dims.height = pageDims.height;
	}
	
	return dims;
}

