var UtilsComponentResourcesDirectory = null;

function UtilsGetResourcesDirectory()
{
	if(UtilsComponentResourcesDirectory == null)
	{
		//if UtilsComponentResourcesDirectory is undefined, check for a script
		//referenced from the document named "Utils.js". Determine the components
		//path from it.
		var scripts = document.getElementsByTagName('script');
		for(var i=0; i<scripts.length; i++)
		{
			var scriptTag = scripts[i];
			var scriptSrc = scriptTag.getAttribute('src');
			if(scriptSrc != null)
			{
				var fileNameIndex = scriptSrc.indexOf('Utils.js');
				if(fileNameIndex != -1)
				{
					UtilsComponentResourcesDirectory = scriptSrc.substr(0, fileNameIndex);
					break;
				}
			}
		}
	}
	return UtilsComponentResourcesDirectory;
}

//Set the style left, top, width, and height of an element
function UtilsSetDimensions(element, coords)
{
	element.style.left = coords[0]+'px';
	element.style.top = coords[1]+'px';
	element.style.width = coords[2]+'px';
	element.style.height = coords[3]+'px';
}

//Set the opacity of an element; alpha is a value
//between 0.0 (transparent) and 1.0 (opaque)
function UtilsSetOpacity(element, alpha) {
	element.style.opacity = alpha; //for Safari, Firefox
	element.style.filter = 'alpha(opacity=' + alpha*100.0 + ')'; //for IE
}

//Preload an image, calling the appropriate callback when the operation is complete.
//The callback is passed the Image object as a parameter.
function UtilsPreloadImage(imageObject, imageUrl, successCallback, errorCallback, context)
{
	if(successCallback != null)
	{
		imageObject.onload = function() {successCallback(imageObject, context)};
	}
	if(errorCallback != null)
	{
		imageObject.onerror = function() {errorCallback(imageObject, context)};
		imageObject.onabort = function() {errorCallback(imageObject, context)};
	}
	
	imageObject.src = imageUrl;
	return imageObject;
}

function UtilsIsBody(element)
{
	return (/^(?:body|html)$/i).test(element.tagName);
}

function UtilsBorderBox(element)
{
	return UtilsGetStyle(element, '-moz-box-sizing') == 'border-box';
}

function UtilsTopBorder(element)
{
	return parseInt(UtilsGetStyle(element, 'border-top-width'));
}

function UtilsLeftBorder(element)
{
	return parseInt(UtilsGetStyle(element, 'border-left-width'));
}

function UtilsGetScroll(element)
{
	if(UtilsIsBody(element)) 
	{
		var win = window;
		doc = (!element.ownerDocument.compatMode || element.ownerDocument.compatMode == 'CSS1Compat') ? 
		element.ownerDocument.getElementsByTagName('html')[0] : element.ownerDocument.body;
		return {x: win.pageXOffset || doc.scrollLeft, y: win.pageYOffset || doc.scrollTop};
	}
	return {x: element.scrollLeft, y: element.scrollTop};
}

//Returns [x,y] pair for the position of an element on screen.
function UtilsGetAbsolutePosition(element)
{
	//Code adapted from MooTools Element.getOffsets()
	if(element.getBoundingClientRect)
	{
		var bound = element.getBoundingClientRect(),
		html = element.ownerDocument.documentElement,
		scroll = UtilsGetScroll(html),
		isFixed = (UtilsGetStyle(element, 'position') == 'fixed');
		return [
				parseInt(bound.left, 10) + ((isFixed) ? 0 : scroll.x) - html.clientLeft,
				parseInt(bound.top, 10) +  ((isFixed) ? 0 : scroll.y) - html.clientTop
				];
	}
	
	
	var pos = [0, 0];
	var orig = element;
	var isGecko = (document.getBoxObjectFor == undefined || element.clientTop == undefined) ? false : true; //Firefox 3+
	var isWebkit = (navigator.taintEnabled) ? false : true;
	if (UtilsIsBody(element)) return pos;
	
	while (element && !UtilsIsBody(element))
	{
		pos[0] += element.offsetLeft;
		pos[1] += element.offsetTop;
		
		if(isGecko)
		{
			if(!UtilsBorderBox(element))
			{
				pos[0] += UtilsLeftBorder(element);
				pos[1] += UtilsTopBorder(element);
			}
			var parent = element.parentNode;
			if(parent && UtilsGetStyle(parent, 'overflow') != 'visible')
			{
				pos[0] += UtilsLeftBorder(parent);
				pos[1] += UtilsTopBorder(parent);
			}
		} 
		else if(element != orig && isWebkit)
		{
			pos[0] += UtilsLeftBorder(element);
			pos[1] += UtilsTopBorder(element);
		}
		element = element.offsetParent;
	}
	if(isGecko && !UtilsBorderBox(orig))
	{
		pos[0] -= UtilsLeftBorder(orig);
		pos[1] -= UtilsTopBorder(orig);
	}
	
	return pos;
}

//Returns the visible rect that the user can see in the browser window [x,y,width,height].
function UtilsGetVisibleRect()
{
	var width, height, x, y;
	
	//From http://thewebdevelopmentblog.com/2008/10/tutorial-pop-overs-part-2-centering-the-pop-over/
    if (document.all) 
	{
		// IE
		width  = (document.documentElement.clientWidth) ? 
		document.documentElement.clientWidth : 
		document.body.clientWidth;
		height = (document.documentElement.clientHeight) ? 
		document.documentElement.clientHeight : 
		document.body.clientHeight;
		y = (document.documentElement.scrollTop) ? 
		document.documentElement.scrollTop : 
		document.body.scrollTop;
		x = (document.documentElement.scrollLeft) ? 
		document.documentElement.scrollLeft : 
		document.body.scrollLeft;
		
    } 
	else 
	{
		// Safari, Firefox
		width = window.innerWidth;
		height = window.innerHeight;
		y = window.pageYOffset;
		x = window.pageXOffset;
	}
	
	return [x, y, width, height];
}

//Takes a pixel value, which may end in 'px' and returns the integer portion
function UtilsGetPixelsAsInteger(pixelValue)
{
	var str = String(pixelValue);
	var val = parseInt(str.replace('px', ''));
	if(isNaN(val))
	{
		return 0;
	}
	return val;
}

//Get the currently applied style property value for an element.
function UtilsGetStyle(el, cssprop)
{
	if (el.currentStyle) //IE
		return el.currentStyle[cssprop]
	else if (document.defaultView && document.defaultView.getComputedStyle) //Firefox
		return document.defaultView.getComputedStyle(el, "")[cssprop]
	else //try and get inline style
		return el.style[cssprop]
}

//Add an event handler; eventName is the name of an event (e.g. 'click') and
//funct is a function; the event will be passed to funct as a parameter
function UtilsAddEventHandler(obj, eventName, funct)
{
	if(obj.addEventListener)
	{
		obj.addEventListener(eventName, funct, false);
	}
	else if(obj.attachEvent)
	{
		obj.attachEvent('on'+eventName, funct);
	}
}

function UtilsCancelBubble(e)
{
	if (!e && !(e = window.event))
		return;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function UtilsGetInitializedEvent(e)
{
	var evt = window.event || e;
	if(!evt.target) //if event obj doesn't support e.target, presume it does e.srcElement
		evt.target=evt.srcElement //extend obj with custom e.target prop
		return evt;
}


