/*--------------------------------------------------------
 *  Function:  getObject
 *  
 *  Description:
 *  Accepts an object reference or a string, returns an
 *  object reference
 *  
 *  Parameters:
 *  pRef	string or object	ID of HTML DOM entity, or
 *								reference to an object
 *	Exceptions:
 *	ObjectNotFoundException
 *	InvalidObjectRefException
 *  
 *  Return:
 *  object		Reference to named or passed DOM object.
 *-------------------------------------------------------*/

function getObject(pRef) {
	var obj;

	if (typeof(pRef) == "object") {
		obj = pRef;
	} else if (typeof(pRef) == "string") {
		obj = document.getElementById(pRef);
		if (!obj) {
			throw new ObjectNotFoundException(pRef);
		}
	} else {
		throw new InvalidObjectRefException(pRef);
	}
	
	return obj;
}

/*--------------------------------------------------------
 *  Function:  filterByClassName
 *  
 *  Description:
 *  Filters an array of DOM elements, returning only
 *  elements that match a particular class name.
 *  
 *  Parameters:
 *  pObjects	array	Array of DOM elements to filter
 *  pClassName	string	The class name to match
 *  
 *  Return:
 *  array		An array of DOM objects with the matching
 *				class name.
 *-------------------------------------------------------*/

function filterByClassName(pObjects, pClassName) {
	var elements = new Array();

	for (var i = 0; i < pObjects.length; i++) {
		var obj = pObjects[i];
		if (obj.className) {
			var classNames = obj.className.split(' ');
			for (var j = 0; j < classNames.length; j++) {
				if (classNames[j] == pClassName) {
					elements[elements.length] = obj;
					break;
				}
			}
		}
	}
  
	return elements;
}

/*--------------------------------------------------------
 *  Function:  getObjectsByClassName
 *  
 *  Description:
 *  Returns an array of DOM objects that match a particular
 *  class name.
 *  
 *  Parameters:
 *  pClassName	string	The class name to match
 *  
 *  Return:
 *  array		An array of DOM objects with the matching
 *				class name.
 *-------------------------------------------------------*/

function getObjectsByClassName(pClassName) {
	var all = document.getElementsByTagName('*') || document.all;
	
	return filterByClassName(all, pClassName);
}

/*--------------------------------------------------------
 *  Function:  getObjectsByClassName
 *  
 *  Description:
 *  Returns an array of DOM objects that match a particular
 *  class name that are a descendent of pParentId
 *  
 *  Parameters:
 *  pParentId	string	The id of a parent node to start search
 *  pClassName	string	The class name to match
 *  
 *  Return:
 *  array		An array of DOM objects with the matching
 *				class name.
 *-------------------------------------------------------*/

function getChildObjectsByClassName(pParentId,pClassName) {
	var parent = document.getElementById(pParentId);
	var all = parent.getElementsByTagName('*');
	return filterByClassName(all,pClassName);
}

/*--------------------------------------------------------
 *  Function:  getChildrenByClassName
 *  
 *  Description:
 *  Accepts a reference to a DOM object and returns an array
 *  of child elements that match a particular class name.
 *  
 *  Parameters:
 *  pRef		string or object	The parent DOM element
 *									to search
 *  pClassName	string				The class name to match
 *  
 *  Return:
 *  array		An array of DOM objects with the matching
 *				class name.
 *-------------------------------------------------------*/

function getChildrenByClassName(pRef, pClassName) {
	var children = getObject(pRef).childNodes;

	return filterByClassName(children, pClassName);
}





/*--------------------------------------------------------
 *  Function:  createElement
 *  
 *  Description:
 *  Utility function for dynamic generation of DOM
 *  elements.  Sets attributes on the element based on a
 *  hash of attribute names and values.
 *
 *	Example:
 *		var newElement = createElement('a',
 *		{
 *			'class': 'myClass',
 *			'href': 'http://www.mydomain.com',
 *			'onmouseover': 'jsFunction();'
 *		});
 *  
 *  Parameters:
 *  pElement	string		DOM element to create
 *							(e.g. 'div', 'a', 'h1')
 *	pAttributes	hash		Hash of attributes and their
 *							values.
 *  
 *  Return:
 *  object		The newly created element
 *-------------------------------------------------------*/

function createElement(pElement, pAttributes) {
	// Make the element
	var newElement = document.createElement(pElement);
	
	// Set the attributes
	for (var attr in pAttributes) {
		switch (attr) {
			/*
				IE uses 'className' for setAttribute and getAttribute.
				All other browsers use 'class'.
				Setting the className property is cross-browser
			*/
			case 'class':
			case 'className':
				newElement.className = pAttributes[attr];
				break;
			/*
				setAttribute works for event handlers on Firefox, but not
				on IE.  Explicitly set the event handlers as functions
				using new Function constructor.
			*/
			case 'onblur':
				newElement.onblur = new Function(pAttributes[attr]);
				break;
			case 'onclick':
				newElement.onclick = new Function(pAttributes[attr]);
				break;
			case 'ondblclick':
				newElement.ondblclick = new Function(pAttributes[attr]);
				break;
			case 'onfocus':
				newElement.onfocus = new Function(pAttributes[attr]);
				break;
			case 'onkeydown':
				newElement.onkeydown = new Function(pAttributes[attr]);
				break;
			case 'onkeypress':
				newElement.onkeypress = new Function(pAttributes[attr]);
				break;
			case 'onkeyup':
				newElement.onkeyup = new Function(pAttributes[attr]);
				break;
			case 'onmousedown':
				newElement.onmousedown = new Function(pAttributes[attr]);
				break;
			case 'onmousemove':
				newElement.onmousemove = new Function(pAttributes[attr]);
				break;
			case 'onmouseout':
				newElement.onmouseout = new Function(pAttributes[attr]);
				break;
			case 'onmouseover':
				newElement.onmouseover = new Function(pAttributes[attr]);
				break;
			case 'onmouseup':
				newelement.onmouseup = new Function(pAttributes[attr]);
				break;
			case 'onresize':
				newElement.onresize = new Function(pAttributes[attr]);
				break;

			/* Default action is to set the attribute */
			default:
				newElement.setAttribute(attr, pAttributes[attr]);
				break;
		}
	}
	return newElement;
}


/*--------------------------------------------------------
 *  Class:  Preloader
 *  
 *  Description:
 *  Class for preloading page assets.  Currently supports
 *  preloading of images only.
 *
 *	Properties:
 *	- images: array of images to preload.
 *
 *  Methods:
 *	- intialize: Constructor.  Preloads any image sources
 *      passed in as an array of strings.
 *	- preload: Preloads an image source.
 *	- onloadHandler: Event handler for image onload events  
 *	- onerrorHandler: Event handler for image onerror events  
 *	- onabortHandler: Event handler for image onabort events  
 *  
 *  Exceptions:
 *	none
 *-------------------------------------------------------*/

//var Preloader = Class.create();
//var Preloader = new Object();

function Preloader() {
}

Preloader.prototype = {

	/*--------------------------------------------------------
	 *  Method:  initialize
	 *  
	 *  Description:
	 *  Constructor. Preloads an array of image sources.
	 *  Establishes images array for tracking preloaded images.
	 *
	 *  Parameters:
	 *  pImgArray	Array	An array string of image sources.
	 *  
	 *  Return:
	 *  none
	 *-------------------------------------------------------*/

	initialize: function(pImgArray) {
		this.images = new Array;
		for (var i = 0; i < pImgArray.length; i++) {
			this.preload(pImgArray[i]);
		}
	},


	/*--------------------------------------------------------
	 *  Method:  onabortHandler
	 *  
	 *  Description:
	 *  Placeholder event handler for image onabort event.
	 *
	 *  Parameters:
	 *  none
	 *  
	 *  Return:
	 *  none
	 *-------------------------------------------------------*/
	
	onabortHandler: function() {
		// alert(this.src + ' aborted.');
	}
}


/*--------------------------------------------------------
 *	Function:  tabSwap
 *	
 *	Description:
 *	changes visible tab in a standard tabswap module
 *
 *	Parameters:
 *	tabsetId	string		Reference to tabswap module id
 *	node		object		selected node
 *
 *	Return:
 *	none
 *------------------------------------------------------*/

function tabSwap(tabsetId,node) {

	var selectedNode = node.parentNode;
	
	if (selectedNode.className.indexOf("Selected") > -1) {
		return false;
	}

	var contentContainer = getChildrenByClassName(tabsetId,"tabContentGroup")[0];
	var tabContainer = getChildrenByClassName(tabsetId,"tabs")[0];

	//swap tabs
	var tabNodes = tabContainer.childNodes;
	var tabCounter = 0;
	var index = 0;
	for (var i=0; i<tabNodes.length; i++) {
		if (tabNodes[i].nodeType == 1) {
			var className = tabNodes[i].className;
			if (tabNodes[i] == selectedNode) {
				tabNodes[i].className = className+"Selected";
				index = tabCounter;
			} else {
				tabNodes[i].className = className.replace("Selected", "");
			}
			tabCounter++;
		}
	}
		
	//swap content	
	var contentNodes = contentContainer.childNodes;
	var contentCounter = 0;
	for (var i=0; i<contentNodes.length; i++) {
		if (contentNodes[i].nodeType == 1) {
			if (contentCounter == index) {
				contentNodes[i].style.display = "block";
			} else {
				contentNodes[i].style.display = "none";
			}
			contentCounter++;
		}
	}
	
}