/**
 * Search.
 */
var Search = new function () {
	
	this.init = function () {
		
		var search = document.getElementById ( "compositesearch" );
		var string = search.value;
		
		if ( search != null ) {
			search.value = string;
			search.onfocus = function () {
				this.className = "active";
				if ( this.value == string ) {
					this.value = "";
				}
			}
			search.onblur = function () {
				this.className = "";
				if ( this.value == "" ) {
					this.value = string;
				}
			}
		}
	}
}

/**
 * Navigation lists.
 */
var NavigationLists = new function () {
	
	/**
	 * Locate and modify nl lists.
	 */
	this.init = function () {
		
		var i = 0, ul, uls = document.getElementsByTagName ( "ul" );
		while ( ul = uls.item ( i++ )) {
			if ( ul.className.indexOf ( "nl" ) >-1 ) {
				this.setup ( ul );
			}
		}
	}
	
	/**
	 * @param {HTMLUListElement} ul
	 */
	this.setup = function ( ul ) {
		
		ul.onmousedown = action;
		ul.onkeydown = action;
	}
	
	/**
	 * @param {MouseEvent} e
	 */
	function action ( e ) {
		
		e = e ? e : window.event;
		
		var isValid = true;
		if ( e.type == "keydown" && e.keyCode != 13 ) {
			isValid = false;
		}
		if ( isValid ) {
			var a = e.target ? e.target : e.srcElement;
			if ( a.nodeName.toLowerCase () == "a" ) {
				var ul = a.parentNode.getElementsByTagName ( "ul" ).item ( 0 );
				if ( ul ) {
					switch ( a.className ) {
						case "label" :
							a.className = "label on";
							ul.className = "on";
							break;
						case "label on" :
							a.className = "label";
							ul.className = "";
							break;
					}
				}
			}
		}
		if ( e.stopPropagation ) {
			e.stopPropagation ();
		} else {
			e.cancelBubble = true;
		}
	}
}

/*
 * Onload actions.
 */
window.onload = function () {
	
	Search.init ();
	NavigationLists.init ();
}