function setNavText(newText){
	if(document.getElementById('nav_text')){
		document.getElementById('nav_text').innerHTML = newText;
	}
}

function clearNavText(){
	if(document.getElementById('nav_text')){
		document.getElementById('nav_text').innerHTML = '&nbsp;';
	}
}

function getNavText(elemID){
	var strNavText = '&nbsp;';
	switch(elemID){
		case 'nav_program':
			strNavText = 'see the two hour long documentary';
			break;
		case 'nav_filmmore':
			strNavText = 'description, transcript, further reading, credits...';
			break;
		case 'nav_sfeature':
			strNavText = 'online poll, Theme and Variation, The Zulu Parade, Writing New Orleans, Gumbo as History';
			break;
		case 'nav_peopleevents':
			strNavText = 'more about the people and events featured in the film';
			break;
		case 'nav_map':
			strNavText = 'Crescent Cities: the development of New Orleans over time';
			break;
		case 'nav_gallery':
			strNavText = 'Greetings from Mardi Gras';
			break;
		case 'nav_timeline':
			strNavText = 'chronology of the city of New Orleans';
			break;
		case 'nav_tguide':
			strNavText = 'how to use this site in your classroom';
			break;
		case 'nav_amex':
			strNavText = 'American Experience home';
			break;
		case 'nav_home':
			strNavText = 'New Orleans home';
			break;
	}
	return strNavText;
}

function initNavLinks() {
	if (!document.getElementsByTagName || !document.getElementById('main_nav')) return;
	var anchors = document.getElementById('main_nav').getElementsByTagName('a');
	for (var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute('href')){  // ignores named anchors
			anchor.onmouseover = function(){ try{ setNavText(getNavText(this.id)); } catch(e) { /* catch exceptions */ } };
			anchor.onmouseout = function(){ try{ clearNavText(); } catch(e) { /* catch exceptions */ } };
		}
	}
}

function initNavMaps() {
	if (!document.getElementsByTagName || !document.getElementById('main_Map')) return;
	var areas = document.getElementById('main_Map').getElementsByTagName('area');
	for (var i = 0; i < areas.length; i++) {
		var area = areas[i];
		if (area.getAttribute('href')){  // ignores named anchors
			area.onmouseover = function(){ try{ setNavText(getNavText(this.id)); } catch(e) { /* catch exceptions */ } };
			area.onmouseout = function(){ try{ clearNavText(); } catch(e) { /* catch exceptions */ } };
		}
	}
}

// for preloading images and rollovers
var aryLoadImg = new Array();
var ncImgOver = '_over';
var ncClsRoll = 'roll';

function loadImg(imgFile, imgType){
	var i = aryLoadImg.length;
	aryLoadImg[i] = new Image();
	aryLoadImg[i].src = imgFile + '.' + imgType;
	return i;
}

function swapImg(imgId, imgSrc){ if(document.images){ document.images[imgId].src = imgSrc; } }

function initImages() {
	if (!document.getElementsByTagName) return;
	var aryImages = document.getElementsByTagName('img');
	var regex_roll = new RegExp('\\b'+ncClsRoll+'\\b');
	for (var i = 0; i < aryImages.length; i++) {
		var thisImg = aryImages[i];
		// this is used for single image roll overs
		if(thisImg.className.match(regex_roll)){
			var imgFile = thisImg.src.substr(0, thisImg.src.length-4);
			var imgType = thisImg.src.substr(thisImg.src.length-3, thisImg.src.length);
			var roll_idx = loadImg(imgFile+ncImgOver, imgType);
			thisImg.roll_idx = roll_idx;
			thisImg.onmouseover = function(){
				try {
					swapImg( this.id, aryLoadImg[this.roll_idx].src );
				} catch(e) { /*alert(e);*/ }
			};
			thisImg.onmouseout = function(){
				try {
					var regex_ncImgOver = new RegExp(ncImgOver+'\.');
					swapImg( this.id, this.src.replace(regex_ncImgOver, '.') );
				} catch(e) { /*alert(e);*/ }
			};
		}
	}
}

function swap(elemID, imgSrc){ if(document.images){ document.images[elemID].src = imgSrc; } }

window.onload=function(){
	initNavLinks();
	initNavMaps();
	initImages();
};

// utility function for safe class name managment
function manClsNames( action, obj, cls1, cls2 ){
	switch (action){
	case 'swap':
		obj.className = !manClsNames('check', obj, cls1) ? obj.className.replace( cls2, cls1 ) : obj.className.replace( cls1, cls2 );
		break;
	case 'add':
		if( !manClsNames('check', obj, cls1) ){ obj.className += obj.className ? ' '+cls1 : cls1; }
		break;
	case 'remove':
		var rep = obj.className.match(' '+cls1) ? ' '+cls1 : cls1;
		obj.className = obj.className.replace( rep, '' );
		break;
	case 'check':
		return new RegExp('\\b'+cls1+'\\b').test(obj.className)
		break;
	}
}