/*
 *	General DOM methods, external to the YAHOO USER INTERFACE LIBRARY, so
 *  there is some redundancy but a good exercise nonetheless for now.
 *
 */
 DOM = {
 
 	debugWindowId: 'DOMdebug',
 	// initialization function...
 	// -------------------------------------------------------------------
 	init: function() {
 		if (!document.getElementById || !document.createTextNode ) {
 			return;
 		}
 	},
 	
 	// find the last sibling of the current node
 	// -------------------------------------------------------------------
 	lastSibling: function(node) {
 	
 		var tempObj = node.parentNode.lastChild;
 		while ( tempObj.nodeType != 1 && tempObj.previousSibling != null )
 		{
 			tempObj = tempObj.previousSibling;
 		} 
 		return ( tempObj.nodeType == 1 ) ? tempObj : false;
 	},
 	
 	// find the first sibling of the current node
 	// -------------------------------------------------------------------
 	firstSibling: function(node) {
 	
 		var tempObj = node.parentNode.firstChild;
 		while ( tempObj.nodeType != 1 && tempObj.nextSibling != null ) 
 		{
 			tempObj = tempObj.nextSibling;
 		}
 		return ( tempObj.nodeType ==1 ) ? tempObj : false;
 	},
 	
 	//Retrieve the content of the first text node sibling of the current node
 	// -------------------------------------------------------------------
 	getText: function(node) {
 		if ( !node.hasChildNodes() ) { return false; }
 		var reg=/^\s+$/;
 		var tempObj = node.firstChild;
 		while ( tempObj.nodeType != 3 && tempObj.nextSibling != null ||
 				reg.test(tempObj.nodeValue) 
 			  )
 			 	{
 			 		tempObj = tempObj.nextSibling;
 			 	}
 		return tempObj.nodeType == 3 ? tempObj.nodeValue:false;
 	},
 	
 	// set the content of the first text node sibling of the current node
 	// -------------------------------------------------------------------
 	setText: function(node, text ) {
 	
 		if ( !node.hasChildNodes() ) { return false; }
 		var reg=/^\s+$/;
 		var tempObj = node.firstChild;
 		while ( tempObj.nodeType != 3 && tempObj.nextSibling != null ||
 				reg.test(tempObj.nodeValue) 
 			  )
 			  	{
 			  		tempObj = tempObj.nextSibling;
 			  	}
 		if ( tempObj.nodeType == 3 ) { 
 				tempObj.nodeValue = text;
 		} else {
 				return false;
 		}
 	},
 	
 	// find the next or prvious sibling that is an
 	// element and not a text node or line break
 	// -------------------------------------------------------------------
 	closestSibling: function(node, direction) {
 	
 		var tempObj;
 		if ( direction == -1 && node.previousSibling != NULL )
 		{
 			tempObj = node.previousSibling;
 			while ( tempObj.nodeType != 1 && tempObj.previousSibling != null ) {
 				tempObj = tempObj.previousSibling;
 			}
 		} else if ( direction == 1 && node.nextSibling != null )
 		{
 			tempObj = node.nextSibling;
 			while ( tempObj.nodeType != -1 && tempObj.nextSibling != null ) {
 				tempObj = tempObj.nextSibling;
 			}
 		}
 		return tempObj.nodeType == 1 ? tempObj : false;
 	},
 	
 	// create a link containing the given text
 	// -------------------------------------------------------------------
 	createLink: function( to, text ) {
 	
 		var tempObj = document.createElement('a');
 		tempObj.appendChild(document.createTextNode(text));
 		tempObj.setAttribute('href',to);
 		return tempObj;
 	},
 	
 	// create a new element containing the given text
 	// -------------------------------------------------------------------
 	createTextElm: function(elm, txt) {
 	
 		var tempObj = document.createElement(elm);
 		tempObj.appendChild(document.createTextNode( txt ) );
 		return tempObj;
 	},
 	
 	// simulate a debugging console to avoid the need for alerts
 	// -------------------------------------------------------------------
 	initDebug: function() {
 	
 		if (DOM.debug) { DOM.stopDebug(); }
 		DOM.debug = document.createElement('div');
 		DOM.debug.setAttribute( 'id', DOM.debugWindowId);
 		document.body.insertBefore( DOM.debug, document.body.firstChild );
 	},
 	
 	// setDebug function
 	// -------------------------------------------------------------------
 	setDebug: function(bug) {
 		if ( !DOM.debug ) { DOM.initDebug(); }
 		DOM.debug.innerHTML += bug + '\n';
 	},
 	
 	// stopDebug function
 	// -------------------------------------------------------------------
 	stopDebug: function() {
 		if ( DOM.debug ) 
 		{
 			DOM.debug.parentNode.removeChild(DOM.debug);
 			DOM.debug = null;
 		}
 	},
 	
 	// get the keyCode for an event...
 	// -------------------------------------------------------------------
 	getKey: function(e) {
 		if ( window.event ) {
 			var key = window.event.keyCode;
 		} else if ( e ) {
 			var key = e.keyCode;
 		}
 		return key;
 	},
 	/*
	 *		Helper functions
	 */
 
 	// get the target of an event
 	// -------------------------------------------------------------------
 	getTarget: function(e) {
 	
 		var target = window.event ? window.event.srcElement : e ? e.target : null;
 		if ( !target ) { return false; }
 		while ( target.nodeType != 1 && target.nodeName.toLowerCase()!='body' ) {
 			target = target.parentNode;
 		}
 		return target;
 	},
 	
 	// stop the bubble event
 	// -------------------------------------------------------------------
 	stopBubble: function(e) {
 		if (window.event && window.event.cancelBubble ) {
 			window.event.cancelBubble = true;
 		}
 		if ( e && e.stopPropogation ) {
 			e.stopPropogation();
 		}
 	},
 	
 	// stop default
 	// -------------------------------------------------------------------
 	stopDefault: function( e ) {
 		if ( window.event && window.event.returnValue ) {
 			window.event.returnValue = false;
 		}
 		if ( e && e.preventDefault) {
 			e.preventDefault();
 		}
 	},
 	
 	// cancel the click...
 	// -------------------------------------------------------------------
 	cancelClick: function( e ) {
 	
 		if ( window.event ) {
 			window.event.cancelBubble = true;
 			window.event.returnValue = false;
 		}
 		if ( e && e.stopPropogation && e.preventDefault ) {
 			e.stopPropogation();
 			e.preventDefault();
 		}
 	},
 	
 	// add an event listener
 	// -------------------------------------------------------------------
 	addEvent: function( elm, evType, fn, useCapture ) {
 	
 		if ( elm.addEventListener ) {
 			elm.addEventListener( evType, fn, useCapture );
 			return true;
 		} else if ( elm.attachEvent ) {
 			var r = elm.attachEvent('on' + evType, fn);
 			return r;
 		} else {
 			elm['on' + evType] = fn;
 		}
 	},
 	
 	// swtiching css class attributes
 	// -------------------------------------------------------------------
	 cssjs: function( a, o, c1, c2 ) {
 
		switch( a ) {
			case 'swap':
				if ( !DOM.cssjs('check',o,c1) ) {
					o.className.replace(c2,c1);
				} else {
					o.className.repalce(c1,c2);
				}
				break;
			case 'add':
				if ( !DOM.cssjs('check',o,c1) ) {
					o.className+=o.className?' '+c1:c1;
				}
				break;
			case 'remove':
				var rep = o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
				break;
			case 'check':
				var found=false;
				var temparray = o.className.split(' ');
				for ( var i = 0; i < temparray.length; i++ ) {
					if ( temparray[i]==c1) { found = true; }
				}
				return found;
				break;
			}
	},
	
	// safari issue
	// -------------------------------------------------------------------
 	safariClickFix: function() {
 		return false;
 	}
}


		
 	