
/* Helper function to register multiple function calls with window.onload event */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

/* Menu rollovers hack for browsers that don't fully support css "hover" */
startList = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}


loaded = false;
var glowOnImages = new Array();
var glowOffImages = new Array();

/* Sets mouseover and mouseout actions for all images within a container object with id=divName */
function enableGlow(divName, imgType, glowExtension) {

	var bannerImagesDiv = document.getElementById(divName);
	var imagesArray = bannerImagesDiv.getElementsByTagName("img");
	cnt = 0;

	for (var ii = 0; ii < imagesArray.length; ii++) {
		if (imagesArray[ii].parentNode.tagName == "A") {
			preload_image_object = new Image();
			// If already a "glow" image link, use same image on mouseover
			if (imagesArray[ii].src.indexOf(glowExtension+"."+imgType) != -1)
				preload_image_object.src = imagesArray[ii].src;
			else
				preload_image_object.src = imagesArray[ii].src.substring(0, imagesArray[ii].src.indexOf("."+imgType)) + glowExtension + "." + imgType;

			glowOffImages[imagesArray[ii].id] = imagesArray[ii].src;
			glowOnImages[imagesArray[ii].id] = preload_image_object.src;
			imagesArray[ii].onmouseover=new Function('glowOn(this);');
			imagesArray[ii].onmouseout=new Function('glowOff(this);');
		}
	}

	loaded = true;

}

function init() {

	enableGlow("nav", "gif", "-on");
	
	setSelectedMenuItem();
	
	/*toggleMenuImgOff("img-home", "submenu-home");*/
	toggleMenuImgOff("img-news", "submenu-news");
	toggleMenuImgOff("img-education", "submenu-education");
	toggleMenuImgOff("img-about", "submenu-about");
	toggleMenuImgOff("img-events", "submenu-events");
	toggleMenuImgOff("img-join", "submenu-join");
	toggleMenuImgOff("img-member", "submenu-member");
	toggleMenuImgOff("img-jobs", "submenu-jobs");
	toggleMenuImgOff("img-community", "submenu-community");
	/*toggleMenuImgOff("img-contact", "submenu-contact");*/
	
	document.getElementById("submenu-news").onmouseover=new Function('glowOnIndirect("img-news")');
	document.getElementById("submenu-education").onmouseover=new Function('glowOnIndirect("img-education")');
	document.getElementById("submenu-about").onmouseover=new Function('glowOnIndirect("img-about")');
	document.getElementById("submenu-events").onmouseover=new Function('glowOnIndirect("img-events")');
	document.getElementById("submenu-join").onmouseover=new Function('glowOnIndirect("img-join")');
	document.getElementById("submenu-member").onmouseover=new Function('glowOnIndirect("img-member")');
	document.getElementById("submenu-jobs").onmouseover=new Function('glowOnIndirect("img-jobs")');
	document.getElementById("submenu-community").onmouseover=new Function('glowOnIndirect("img-community")');	

    var defaultSearchBoxText = "Find news, events, jobs, and more...";
	var searchBox = document.getElementById("input-search");
	searchBox.onfocus = function() {
	   if (this.value == defaultSearchBoxText) {
		   this.value = "";
	   }
	}
	searchBox.onblur = function() {
		   if (this.value == "") {
			   this.value = defaultSearchBoxText;
		   }
	}	
}

/* Function causes menu image to toggle off when the mouse leaves the associated submenu area */
function toggleMenuImgOff(menuImgId, submenuId) {
	document.getElementById(submenuId).onmouseout = function(e){
		// block below reduces image flicker by cancelling mouseouts triggered within the container element
		if (!e) { e = window.event; }
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		if (reltg == null) {
			glowOffIndirect(menuImgId);
			return;
		}
		while (reltg.tagName != 'BODY'){
			if (reltg.id == this.id){return;}
			reltg = reltg.parentNode;
		}
		// safe to proceed
		glowOffIndirect(menuImgId);
	}	
}

function glowOn(img) {
	if (loaded)
		img.src = glowOnImages[img.id];
}

function glowOff(img) {
	//alert("here - glowoff");
	if (loaded)
		img.src = glowOffImages[img.id];
}


function glowOnIndirect(imgId) {
	if (loaded)
		document.getElementById(imgId).src = glowOnImages[imgId];
}

function glowOffIndirect(imgId, oEvent) {
	if (loaded)
		document.getElementById(imgId).src = glowOffImages[imgId];
}

function setSelectedMenuItem() {
	var currentURL = document.location.href;
	if (currentURL.indexOf("/news/") != -1) {
		document.getElementById("img-news").src = 'images/menu-news-select.gif';
		glowOffImages["img-news"] = 'images/menu-news-select.gif';
	}
	else if (currentURL.indexOf("/education/") != -1) {
		document.getElementById("img-education").src = 'images/menu-education-select.gif';
		glowOffImages["img-education"] = 'images/menu-education-select.gif';
	}	
	else if (currentURL.indexOf("/join/") != -1) {
		document.getElementById("img-join").src = 'images/menu-join-select.gif';
		glowOffImages["img-join"] = 'images/menu-join-select.gif';
	}
	else if (currentURL.indexOf("/contact/") != -1) {
		document.getElementById("img-contact").src = 'images/menu-contact-select.gif';
		glowOffImages["img-contact"] = 'images/menu-contact-select.gif';
	}		
	else if (currentURL.indexOf("/about/") != -1) {
		document.getElementById("img-about").src = 'images/menu-about-select.gif';
		glowOffImages["img-about"] = 'images/menu-about-select.gif';
	}		
	else if (currentURL.indexOf("/events/") != -1) {
		document.getElementById("img-events").src = 'images/menu-events-select.gif';
		glowOffImages["img-events"] = 'images/menu-events-select.gif';
	}		
	else if (currentURL.indexOf("/jobs/") != -1) {
		document.getElementById("img-jobs").src = 'images/menu-jobs-select.gif';
		glowOffImages["img-jobs"] = 'images/menu-jobs-select.gif';
	}		
	else if (currentURL.indexOf("/networking/") != -1) {
		document.getElementById("img-community").src = 'images/menu-networking-select.gif';
		glowOffImages["img-community"] = 'images/menu-networking-select.gif';
	}		
	else if (currentURL.indexOf("/database/") != -1) {
		document.getElementById("img-member").src = 'images/menu-database-select.gif';
		glowOffImages["img-member"] = 'images/menu-database-select.gif';
	}
	else {
		document.getElementById("img-home").src = 'images/menu-home-select.gif';
		glowOffImages["img-home"] = 'images/menu-home-select.gif';
	}			
		
}

addLoadEvent(startList);
addLoadEvent(init);

