function log( message ) {
	var node = document.createElement("p");
	node.appendChild( document.createTextNode(message ) );
	document.body.appendChild( node );
}

function lowlightLI(e) {
	lowlight(e, "li" );
}
function lowlightA(e) {
	lowlight(e, "a" );
}
function highlightLI(e) {
	highlight(e, "li" );
}
function highlightA(e) {
	highlight(e, "a" );
}
function lowlight(e, tag) {
	var target = getFirstAncestor( e.target, tag );
	klass = target.className;
	if ( klass.endsWith("-highlight" ) ) {
		target.className = klass.substring( 0, klass.length - 10 );
	}
}

function highlight(e,tag) {
	var target = getFirstAncestor( e.target, tag );
	klass = target.className;
	if ( !klass.endsWith("-highlight" ) ) {
		target.className = klass + '-highlight';
	}
}

String.prototype.endsWith = function(string)
{
 return (this.substr(this.length-string.length)==string);
}

function clearTargetTimeout(target ) {
	if ( target.getAttribute && target.getAttribute( 'timerid' ) != null) {
		clearTimeout( target.getAttribute('timerid') );
	}
}
function clearHideTimerHandler( e ) {
	var ancestor = getFirstNonTextAncestor( e.target );
	if ( ancestor.tagName.toLowerCase() == "a" ) {
		ancestor = ancestor.parentNode;
	}
	while ( ancestor ) {
		if ( ancestor.nodeType != 3 ) {
			clearTargetTimeout( ancestor );
		}
		ancestor = ancestor.parentNode;
	}
}

function getRandom(min,max)
{
	 return (Math.round(Math.random()*(max-min)))+min;
}

function startHideTimer( target ) {
	if (!target.id ) {
		target.id = "id" + getRandom( 9999, 999999 );
	}
	var timerId = setTimeout( "hideId('" + target.id + "');", 100 );
	target.setAttribute('timerid', timerId );
}
function startHideTimerHandler( e ) {
	var target = getFirstAncestor( e.target, 'ul' );
	startHideTimer( target );
}
function startHideNextNonTextSiblingTimer( e ) {
	var sibling = getNextNonTextSibling( getFirstNonTextAncestor( e.target ) );
	startHideTimer( sibling );
}

function hideId( id ) {
	var target = document.getElementById( id );
	hide( target );
}

function showId( id ) {
	var target = document.getElementById( id );
	show( target );
}

function hide( target ) {
	var elements = target.getElementsByTagName("ul");
	for ( var counter = 0; counter < elements.length; counter++ ) {
		elements[counter].style.display = 'none';
	}
	target.style.display = 'none';
}

function show( target ) {
	target.style.display = 'block';
}

function showNextNonTextSibling( e ) {
	if ( typeof(selectedSibling) != 'undefined' ) {
		hide( selectedSibling );
	}
	var sibling = getNextNonTextSibling( getFirstAncestor( e.target, "a" ) );
	show( sibling );
	selectedSibling = sibling;
	log( sibling.className );
}

function getChildrenByTagName( element, tag ) {
	var selected = new Array();
	for ( var counter = 0; counter < element.childNodes.length; counter++ ) {
		var child = element.childNodes[counter];
		if ( child.nodeType != 3 ) {
			if ( child.tagName.toLowerCase() == tag ) {
				selected[selected.length] =  child;
			}
		}
	}
	return selected;
}

function getFirstNonTextAncestor(target) {
	var ancestor = target;
	while ( ancestor.nodeType == 3 ) {
		ancestor = ancestor.parentNode;
	}
	return ancestor;
}

function getFirstAncestor(target, tag) {
	var ancestor = target;
	while ( ancestor != null && (ancestor.nodeType == 3 || ancestor.tagName.toLowerCase() != tag) ) {
		ancestor = ancestor.parentNode;
	}
	return ancestor;
}

function getNextNonTextSibling(target) {
	var sib = target.nextSibling;
	while ( sib != null && sib.nodeType == 3 ) {
		sib = sib.nextSibling;
	}
	return sib;
}

/* Credit to Andy Smith for putting the first principles together. See http://weblogs.asp.net/asmith/archive/2003/10/06/30744.aspx */
function XBrowserAddHandler(target, eventName, handler) {
	if (target.addEventListener)
		target.addEventListener(eventName, function(e){handler(XStandardizeEvent(e));}, false);
	else if (target.attachEvent)
		target.attachEvent("on" + eventName, function(e){handler(XStandardizeEvent(e));});
	else {
		var originalHandler = target["on" + eventName];
		if (originalHandler)
			target["on" + eventName] = function(e){originalHandler(XStandardizeEvent(e)); handler(XStandardizeEvent(e));};
		else
			target["on" + eventName] = function(e){handler(XStandardizeEvent(e));};
	}
}

/* Credit to Andy Smith for putting the first principles together. See http://weblogs.asp.net/asmith/archive/2003/10/06/30744.aspx */
function XStandardizeEvent(e) {
	if (typeof e == "undefined")
		e = window.event;

	if (!e.stopPropagation)
		e.stopPropagation = new Function('this.cancelBubble = true');
	if (!e.preventDefault)
		e.preventDefault = new Function('this.returnValue = true');
	if (!e.target && e.srcElement) {
		e.target = e.srcElement;
		if (e.type == 'onmouseout')
			e.relatedTarget = e.toElement
		else if (e.type == 'onmouseover')
			e.relatedTarget = e.fromElement
	}
	return e;
}

function openDesigner() {
	designerWin = window.open('designer/app/designer.html', 'Designer', "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=1,resizable=0,width=654,height=426,alwaysRaised=yes,dependent=no");
	designerWin.focus();
	return false;
}


