/**
	Provide simple interface to create DOM elements

	@file		DOMCore.js
	@path		/assets/js/DOMCore.js
	@version	1.3 - 2009/02/03 - Revised some JS calls to be more in line with xhtml.inc.php
	@date		2009/02/03
	@copyright	2006-2009 Ignition72, LLC
	@author		Joseph Wilson
	@link		www.ignition72.com
	@todo		complete documentation / cleanup comparible functions between xhtml.inc.php and DOMCore.js
*/

var DOMUtil = new Object();


/**
	addJavaScript
	Create Javascript reference in head of document

	@version	1.1	2009/02/03 - Added documentation
	@return		NULL
	@param		sSrc		str path to JS to include
	@param		sid			str	ID for newly created script
*/
DOMUtil.addJavaScript = function(sSrc,sId,oObj) {
	if (oObj) {
		if (typeof($(oObj)) != 'object'){
			setTimeout(DOMUtil.addJavaScript(sSrc,sId,oObj),3000);
		}
	}
	if (!$(sId)) {
		var oHead	= document.getElementsByTagName('head')[0];
		var oScript	= document.createElement('script');
		oScript.setAttribute('type','text/javascript');
		oScript.setAttribute('src',sSrc);
		oHead.appendChild(oScript);
		DOMUtil.setId(oScript,sId);
	}
};




/**
	addStyleSheet
	Create StyleSheet reference in head of document

	@version	1.1	2009/02/03 - Added documentation
	@return		NULL
	@param		sSrc		str path to CSS to include
	@param		sid			str	ID for newly created css link
*/
DOMUtil.addStyleSheet = function(sSrc,sId,onLoad) {
	if (!$(sId)) {
		var oHead	= document.getElementsByTagName('head')[0];
		var oSheet	= document.createElement('link');
		oSheet.rel	= 'stylesheet';
		oSheet.type	= 'text/css';
		oSheet.href	= sSrc;
		oHead.appendChild(oSheet);
		DOMUtil.setId(oSheet,sId);
	}
	if (onLoad) {
	  var stylesheets = document.styleSheets.length;
	  var onLoadListener = function() {
	    if (document.styleSheets.length > stylesheets) {
        onLoad.call();
      } else if (loop < 100) {
        loop++;
        setTimeout(onLoadListener,100);
      }
	  }
	}
};




/**
	insertAfter
	Similar to "insertBefore", but appends supplied element after target element

	@version	1.1	2009/02/03 - Added documentation
	@return		NULL
	@param		oTarget		str | obj	element ID or element object to attach after
	@param		oInsert		obj			object reference for element to attach
*/
DOMUtil.insertAfter = function (oTarget,oInsert) {
	var oTarget		= this.checkTarget(oTarget);
	var oParent		= oTarget.parentNode;
	if (oParent.lastChild == oTarget) {
		oParent.appendChild(oInsert);
	} else {
		oParent.insertBefore(oInsert,oTarget.nextSibling);
	}
};




/**
	swapNodes
	Swap DOM position of 2 DOM nodes

	@version	1.1	2009/02/03 - Added documentation
	@return		NULL
	@param		oNode1		str | obj	
	@param		oNode2		str | obj	
*/
DOMUtil.swapNodes = function (oNode1, oNode2) {
	var oNode1 = this.checkTarget(oNode1);
	var oNode2 = this.checkTarget(oNode2);
	var oNewNode = oNode2.parentNode.removeChild(oNode2);
	oNode1.parentNode.insertBefore(oNewNode,oNode1);
};




/**
	checkTarget
	Take string or object reference, verify it exists, extend it with prototype methods

	@version	1.1	2009/02/03 - Added documentation
	@return		obj | false if not located
	@param		oTarget		str | obj	element ID or element object to validate
*/
DOMUtil.checkTarget = function (oTarget) {
	if (typeof(oTarget) == 'string' || typeof(oTarget) == 'object') {
		var oTarget		= $(oTarget);
	} else if (typeof(oTarget) == 'undefined') {
		var oTarget		= $("'" + oTarget + "'");
	} else {
		return false;
	}
	return oTarget;
};




/**
	setId
	Set id attribute for supplied element

	@version	1.1	2009/02/03 - Added documentation
	@return		null
	@param		oObj	obj		object to set ID on
	@param		sId		str		Id to set
*/
DOMUtil.setId = function(oObj,sId) {
	if (sId && sId.length > 0) {
		oObj.id = sId;
	}
};




/**
	setClass
	Set class attribute for supplied element

	@version	1.1	2009/02/03 - Added documentation
	@return		null
	@param		oObj	obj		object to set ID on
	@param		sId		str		Id to set
*/
DOMUtil.setClass = function (oObj,sClass) {
	var oObj	= this.checkTarget(oObj);
	if (sClass) {
		if (isIE) {
			oObj.setAttribute('className',sClass);
		} else {
			oObj.setAttribute('class',sClass);
		}
	}
};




/**
	setSrc
	Set source attribute for supplied element

	@version	1.1	2009/02/03 - Added documentation
	@return		null
	@param		oObj	str | obj	object - IMAGE to set src attribute
	@param		sSrc	str			Src to set
*/
DOMUtil.setSrc = function (oObj,sSrc) {
	if (typeof(oObj) != 'object') {
		var oObj = this.checkTarget(oObj);
	}
	oObj.src = sSrc;
};



/**
	getClass
	Return an item's class as a string

	@version	1.1	2009/02/03 - Added documentation
	@return		string class name that has been applied to object, or '' if none.
	@param		oObj	str | obj	Object to return class from
*/
DOMUtil.getClass = function (oObj) {
	var oObj	= this.checkTarget(oObj);
	var sClass = false;
	if (isIE) {
		sClass	= oObj.getAttribute('className');
	} else {
		sClass	= oObj.getAttribute('class');
	}
	return sClass;
};

DOMUtil.createBr = function (oTarget) {
	var oTarget		= this.checkTarget(oTarget);
	var oObj		= document.createElement('br');
	oTarget.appendChild(oObj);
};

DOMUtil.createDiv = function (oTarget,sId,sClass,bDisplay) {
	/*
		oTarget		str/obj	Target DOM node or id to add to
		sId			str		unique DOM id
		sClass		str		class to apply
		bDisplay	bool	false:	sets inline style="display:none;" fixes prototype/script.aculo.us issues
							true:	set blank style
	*/

	var oTarget		= this.checkTarget(oTarget);
	var oDiv		= document.createElement('div');

	if (sId) {
		this.setId(oDiv,sId);
	}
	if (sClass) {
		this.setClass(oDiv,sClass);
	}
	if (bDisplay == false) {
		oDiv.style.display='none';
	} else {
		oDiv.style.display='';
	}
	oTarget.appendChild(oDiv);
	return $(oDiv);
};



/**
	createDl
	Create a <dl> definition list for population

	@version	1.0	2009/03/05 - Initial creation
	@return		Extended definition list element
	@param		oTarget		str|obj 	node or id of node to attach to
	@param		sid			str			unique DOM id to apply
	@param		sClass		str			class to apply
	@param		sTitle		str			title for DL <dl title="foo"></dl>
										shows on hover over DL
*/
DOMUtil.createDl = function (oTarget,sId,sClass,sTitle) {
	var oTarget = this.checkTarget(oTarget);
	var oItem = document.createElement('dl');
	if (sId) {
		this.setId(oItem,sId);
	}
	if (sClass) {
		this.setClass(oItem,sClass);
	}
	if (sTitle) {
		oItem.title = sTitle;
	}
	oTarget.appendChild(oItem);
	return $(oItem);
};



/**
	createDt
	Create a <dt> definition title

	@version	1.0	2009/03/05 - Initial creation
	@return		Extended definition title element
	@param		oTarget		str|obj 	node or id of node to attach to
	@param		sid			str			unique DOM id to apply
	@param		sClass		str			class to apply
*/
DOMUtil.createDt = function (oTarget,sId,sClass) {
	var oTarget = this.checkTarget(oTarget);
	if (oTarget.nodeName != 'DL') {
		alert('target is not a DL');
	}
	var oItem = document.createElement('dt');
	if (sId) {
		this.setId(oItem,sId);
	}
	if (sClass) {
		this.setClass(oItem,sClass);
	}
	oTarget.appendChild(oItem);
	return $(oItem);
};



/**
	createDd
	Create a <dd> definition list item

	@version	1.0	2009/03/05 - Initial creation
	@return		Extended definition item element
	@param		oTarget		str|obj 	node or id of node to attach to
	@param		sid			str			unique DOM id to apply
	@param		sClass		str			class to apply
*/
DOMUtil.createDd = function (oTarget,sId,sClass) {
	var oTarget = this.checkTarget(oTarget);
	if (oTarget.nodeName != 'DL') {
		alert('target is not a DL');
	}
	var oItem = document.createElement('dd');
	if (sId) {
		this.setId(oItem,sId);
	}
	if (sClass) {
		this.setClass(oItem,sClass);
	}
	oTarget.appendChild(oItem);
	return $(oItem);
};



DOMUtil.createHeading = function (oTarget,sText,sSize,sId,sClass) {
	/*
		oTarget		str/obj	Target DOM node or id to attach to
		sText		str		Inner HTML for the head element
		sSize		str		size (h1-h6) for head element
		sId			str		DOM id to apply
		sClass		str		CSS classname to apply
	*/
	if (!sSize) {
		sSize = 'h3';
	} else if (sSize.length == 1) {
		sSize = 'h' + sSize;
	}
	var oTarget		= this.checkTarget(oTarget);
	var oHeading	= document.createElement(sSize);

	if (sText && sText.length > 0) {
		this.createText(oHeading,sText);
	}
	if (sId) {
		this.setId(oHeading,sId);
	}
	if (sClass) {
		this.setClass(oHeading,sClass);
	}
	oTarget.appendChild(oHeading);
	return $(oHeading);
};

DOMUtil.createIframe = function (oTarget,sId,sSrc,sClass,iWidth,iHeight) {
	var oTarget		= this.checkTarget(oTarget);
	if (isIE) {
		var oIframe		= document.createElement('<iframe frameborder="0" border="0" src="' + sSrc + '" id="' + sId + '" width="' + iWidth + '" height="' + iHeight + '" class="' + sClass + '" ></iframe>');
	} else {
		var oIframe		= document.createElement('iframe');
		this.setId(oIframe,sId);
		if (sSrc) {
			this.setSrc(oIframe,sSrc);
		}
		if (sClass) {
			this.setClass(oIframe,sClass);
		}
		if (iWidth) {
			oIframe.style.width = iWidth;
		}
		if (iHeight) {
			oIframe.style.height = iHeight;
		}
	}

	oTarget.appendChild(oIframe);
	return $(oIframe);
};

DOMUtil.createImage = function (oTarget,sSrc,sAlt,sId,sClass) {
	/*
		oTarget obj,str	element to attach to
		sSrc	str		src attribute
		sAlt	str		Alt text settting
		sId		str		ID to apply to new image
		sClass	str		Classname to apply to new image
	*/
	var oTarget		= this.checkTarget(oTarget);
	var oImg		= document.createElement('img');

	this.setSrc(oImg,sSrc);

	if (sAlt && sAlt.length > 0) {
		oImg.alt = sAlt;
	}
	if (sId && sId.length > 0) {
		this.setId(oImg,sId);
	}
	if (sClass && sClass.length > 0) {
		this.setClass(oImg,sClass);
	}
	oTarget.appendChild(oImg);
	return $(oImg);
};


DOMUtil.createLi = function (oTarget,sText,sLink,sOnClick,sTarget) {
	/*
		oTarget	obj,str	element to attach list item to (usually a list)
		sText	str		text of li
		sLink	str		if we're stripping in a link
		sTarget	str		if we have a link, we may need a target
	
	*/
	var oTarget		= this.checkTarget(oTarget);
	var oLi			= document.createElement('li');

	oTarget.appendChild(oLi);

	if (sText) {
		oLi.id = oTarget.id + sText.toLowerCase();
		if (sLink) {
			this.createLink(oLi,sLink,sText,sTarget,sOnClick);
		} else {
			var oSpan	= this.createSpan(oLi);
			this.createText(oSpan,sText);
		}
	}
	return $(oLi);
};

DOMUtil.createLink = function (oTarget,sLink,sText,sTarget,sOnClick) {

	var oTarget		= this.checkTarget(oTarget);
	var oLink		= document.createElement('a');
	if (sText) {
		var oSpan	= this.createSpan(oLink);
		this.createText(oSpan,sText);
	}
	if (sOnClick) {
		oLink.href		= 'javascript:;';
		oLink.onclick = function() {
			new net.ContentLoader('/ajax.php',updateUI,DIVUtil.setSysmsg,'POST',sOnClick);
			return false;
		}
	} else {
		oLink.href		= sLink;
		oLink.onclick = function() {
			if (sTarget) {
				window.open(sLink, sTarget);
				return false;
			} else {
				document.location = sLink;
			}
		}
	}
	oTarget.appendChild(oLink);
	return $(oLink);
}

DOMUtil.createList = function (oTarget,sId,sType,sClass) {
	var oTarget		= this.checkTarget(oTarget);
	if (!sType) {
		var sType	= 'ul';
	}
	var oList		= document.createElement(sType);
	if (sId) {
		if (this.checkTarget(sId)) {
			var oOldList = this.checkTarget(sId);
			oOldList.parentNode.removeChild(oOldList);
		}
		this.setId(oList,sId);
	}
	if (sClass) {
		this.setClass(oList,sClass);
	}
	oTarget.appendChild(oList);
	return $(oList);
};

DOMUtil.createParagraph = function (oTarget,oText,sId,sClass) {
	var oTarget		= this.checkTarget(oTarget);
	var oParagraph	= document.createElement('p');

	if (!oText) {
		oText = '';
	}

	if (oText.length > 1) {
		if (typeof(oText) == 'string') {
			this.createText(oParagraph,oText);
		} else {
			for (ii = 0; ii < oText.length; ii++) {
				if (typeof(oText[ii]) == 'object') {
					oParagraph.appendChild(oText[ii]);
				}
			}
		}
	} else {
		if (typeof(oText) == 'object') {
			oParagraph.appendChild(oText);
		} else {
			this.createText(oParagraph,oText);
		}
	}
	if (sId) {
		var oOldElement = document.getElementById(sId);
		if (oOldElement) {
			oTarget.replaceChild(oParagraph,oOldElement);
		} else {
			oTarget.appendChild(oParagraph);
		}
		this.setId(oParagraph,sId);
	} else {
		oTarget.appendChild(oParagraph);
	}
	if (sClass) {
		this.setClass(oParagraph,sClass);
	}
	return $(oParagraph);
};



DOMUtil.createSilverLight = function (oTarget,sSrc,sId,iWidth,iHeight,sBkrd) {
	var oTarget = $(oTarget);
	Silverlight.createObject(sSrc,oTarget,sId,
		{	width: iWidth,
			height: iHeight,
			background: sBkrd,
			version:'2.0.31005.0'
		}
/* 	{ onError: alert, onLoad: alert } */
/* 	"param1=value1,param2=value2",  */
/* 	"context"    // context helper for onLoad handler. */
	);
	return false;
};


/**
	createSpan
	Creates a <span> element with provided attributes

	@version	1.1	2009/02/18 - Added id for span if provided.
	@return		extended span element
	@param		oTarget		obj|str	object or id of object to attach span to
	@param		sText		str		text for span
	@param		sClass		str		class to set for span
	@param		sId			str		id to set for span
*/
DOMUtil.createSpan = function (oTarget,sText,sClass,sId) {
	var oTarget		= $(oTarget);
	var oSpan		= document.createElement('span');

	if (sText && sText.length > 0) {
		this.createText(oSpan,sText);
	}
	if (sClass && sClass.length > 0) {
		this.setClass(oSpan,sClass);
	}
	if (sId && sId.length > 0) {
		this.setId(oSpan,sId);
	}
	oTarget.appendChild(oSpan);
	return $(oSpan);
};

DOMUtil.createText = function (oTarget,sText) {
	if (!sText || sText.length == 0) {
		return false;
	}
	var oTarget		= this.checkTarget(oTarget);
	var oText		= document.createTextNode(unescape(sText));
	oTarget.appendChild(oText);
};

DOMUtil.createTable = function (oTarget,sId, sClass) {
	var oTarget		= this.checkTarget(oTarget);
	var oTable		= document.createElement('table');
	var oOldTable	= document.getElementById(sId);
	var oTBody		= document.createElement('tbody');

	oTable.createTHead();
	oTable.appendChild(oTBody);

	if (oOldTable) {
		oTarget.replaceChild(oTable,oOldTable);
	} else {
		oTarget.appendChild(oTable);
	}
	if (sId && sId.length > 0) {
		this.setId(oTable,sId);
	}
	if (sClass && sClass.length > 0) {
		this.setClass(oTable,sClass);
	}
	return $(oTable);
};