/* Function to addEventListener to onload
 * @param func - a function which should be executed once the page has loaded
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * it will work even if something has previously been assigned to window.onload
 * without using addLoadEvent itself. 
 */

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

/* Function to addEventListener to onunload
 * @param func - a function which should be executed once the page has loaded
 * Modified from http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * it will work even if something has previously been assigned to window.onunload
 * without using addLoadEvent itself. 
 */

function addUnloadEvent(func) {
    var oldonunload = window.onunload;
    if (typeof window.onunload != 'function') {
        window.onunload = func;
    } else {
        window.onunload = function() {
            oldonunload();
            func();
        }
    }
}

/*
 * Function to clear a text field
 * @param thefield
 *     the field to clear
 */

function cleartext(thefield) {
	if (thefield.defaultValue == thefield.value) {
		thefield.value = "";
	}
}

/*
 * Function to change an image
 * @param highlight
 *     the link that was clicked
 * @param pic
 *     the src of the image to load
 * @param alttext
 *     the alt text of the new image
 */
 
function changePic(highlight,pic,alttext) {
	//change selected state
	current = document.getElementsByTagName('a');
	for(i=0;i<current.length;i++){
		if(current[i].className == 'selectedpic'){
			current[i].className = 'normal';
		}
	}
	highlight.className = 'selectedpic';
	
	//change image
	currentimg = document.getElementsByTagName('img');
	for(i=0;i<currentimg.length;i++){
		if(currentimg[i].className == 'casestudy'){
			currentimg[i].src = pic;
			currentimg[i].alt = alttext;
		}
	}
}

/*
 * Function to change the featured client
 * @param highlight
 *     the link that was clicked
 * @param pic
 *     the src of the image to load
 * @param alttext
 *     the alt text of the new image
 * @param infotext
 *     the information text of the new client
 */

function changeClient(highlight,pic,alttext,infotext) {
	
	//alert (pic);
	//change image
	currentimg = document.getElementById('feature');
	currentimg.src = pic;
	currentimg.alt = alttext;
	
	//change text
	currenttext = document.getElementById('info');
	currenttext.innerHTML = infotext;
	
	//change selected state
	current = document.getElementsByTagName('a');
	for(i=0;i<current.length;i++){
		if(current[i].className == 'active'){
			current[i].className = 'normal';
		}
	}
	highlight.className = 'active';
}

/*
 * Function to clear submenues
 */

function clearMenu() {
	//navRoot = document.getElementById("tools");
	if(menuTimer != null){
		clearTimeout(menuTimer);
	}
	for (i=0; i<navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];
		if (node.nodeName.toUpperCase() == "LI") {
			node.className = node.className.replace(/\s?hover/, "");
		}
	}
	navRoot.className = navRoot.className.replace(/\s?hover/, "");
}

/*
 * Function to set up .hover classes on navigation menu and submenus
 */
startList = function() {
	menuTimer = null;
	if (document.getElementById) {
		navRoot = document.getElementById("tools");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName.toUpperCase() == "LI" && node.childNodes.length > 1) {
				node.childNodes[0].onmouseover=function() {
					clearMenu();
					navRoot.className += " hover";
					this.parentNode.className += " hover";
				}
				if(node.childNodes[2] != undefined){
					node.childNodes[2].onmouseover=function() {
						if(menuTimer != null){
							clearTimeout(menuTimer);
						}
					}
				}
				node.onmouseout=function() {
					menuTimer = setTimeout('clearMenu();', 300);
				}
			}
		}
	}
}

addLoadEvent(startList);