function highlight(event) {
	var thisEl = getElementRef(event, this);
	eval(thisEl.id + ".onMouse('on');");
}

function unhighlight(event) {
	var thisEl = getElementRef(event, this);
	eval(thisEl.id + ".onMouse('off');");
}

function onclickwrapper(event) {
	var thisEl = getElementRef(event, this);
	nb.navigate(thisEl.id);
}

function returnFalseWrapper() {
	return false;	
}

function getElementRef(event, thisRef) {
	//alert(thisRef.id);
	
	if (thisRef && thisRef.id) {
		var thisId =  thisRef.id;
	} else {
		// MIE:
		var thisId = event.srcElement.id;
	}
	
	return document.getElementById(thisId);
}

function NavItem(elementId, url, largeImage, smallImage, imageTitle) {
	this.name = elementId;
	this.elementId = elementId;
	this.url = url;
	this.largeImagePath = largeImage;
	this.smallImagePath = smallImage;
	this.imageTitle = imageTitle
	this.isCurrent = false;
	// Preload the images:
	this.largeImage = new Image();
	this.largeImage.src = largeImage;
	this.smallImage = new Image();
	this.smallImage.src = smallImage;
	// Methods:
	this.onMouse = navItemRollOver;
	
}

function navItemRollOver(newState) {
	//alert("highlight called");
	if (!this.isCurrent) {
		var wrapperCell = document.getElementById(this.elementId + "Cell");	
		
		if (wrapperCell) {
			if (newState == "on") {
				//alert(wrapperCell.id + ' and ' + this.smallImage.src);
				var thisSrc = this.smallImage.src;
			} else {
				var thisSrc = "images/spacer.gif";
			}
			
			wrapperCell.style.backgroundImage = 'url(' + thisSrc + ')'; 
		}
	}
}

// --------------------------------------------------
function NavBar() {
	this.navItems =  Array();
	this.target = "content";
	this.addItem = addNavigationItem;
	this.init = initializeNavBar;
	
	this.navigate = navBarNavigate;
}

function addNavigationItem(newNavItem) {
	// Add the obj to the array:
	var newIndex = this.navItems.length;
	this.navItems.length++;
	this.navItems[newIndex] = newNavItem;
}

function initializeNavBar() {
	var len = this.navItems.length;
	
	for (var i=0; i<len; i++) {
		var thisNavItem = document.getElementById(this.navItems[i].elementId);
				
		if (thisNavItem) {
			if (thisNavItem.addEventListener) {
				thisNavItem.addEventListener("mouseover", highlight, false);
				thisNavItem.addEventListener("mouseout", unhighlight, false);
				thisNavItem.addEventListener("click", returnFalseWrapper, false);
				thisNavItem.addEventListener("click", onclickwrapper, false);
				//thisNavItem.addEventListener("click", returnFalseWrapper, false);
			} else if (thisNavItem.attachEvent) {
				// This is MIE:
				window.event.cancelBubble = true;
				thisNavItem.attachEvent("onmouseover", highlight);
				thisNavItem.attachEvent("onmouseout", unhighlight);
				thisNavItem.attachEvent("onclick", returnFalseWrapper);
				thisNavItem.attachEvent("onclick", onclickwrapper);
			}
		}
	}	
}

function navBarNavigate(destination) {
	var len = this.navItems.length;
	
	for (var i=0; i<len; i++) {
		if (destination == this.navItems[i].name) {
			this.navItems[i].isCurrent = true;
			window.frames["content"].location.href = this.navItems[i].url;
			//alert(this.navItems[i].url);
			// Update the page image:
			var thisImg = document.getElementById("mainImg");
			thisImg.src = this.navItems[i].largeImage.src;
			if (this.navItems[i].imageTitle) {
				thisImg.title = this.navItems[i].imageTitle;
				thisImg.alt = this.navItems[i].imageTitle;
			}
		} else {
			this.navItems[i].isCurrent = false;
			this.navItems[i].onMouse("off");
			// Clear the background image:	
		}
	}
	//return false;
}


var nb = new NavBar();

var navHome = new NavItem("navHome", "content/contact.html", "images/photo1.jpg", "images/photo1_small.gif");
nb.addItem(navHome);
var navResearch = new NavItem("navResearch", "content/research.html", "images/photo2.jpg", "images/photo2_small.jpg");
nb.addItem(navResearch);
var navTeaching = new NavItem("navTeaching", "content/teaching.html", "images/photo3.jpg", "images/photo3_small.gif");
nb.addItem(navTeaching);
var navPublications = new NavItem("navPublications", "content/publications.html", "images/photo4.jpg", "images/photo4_small.gif");
nb.addItem(navPublications);
var navLectures = new NavItem("navLectures", "content/lectures.html", "images/photo5.jpg", "images/photo5_small.gif");
nb.addItem(navLectures);
var navCv = new NavItem("navCv", "content/cv.html", "images/photo6.jpg", "images/photo6_small.gif");
nb.addItem(navCv);
