var defaultNavText = '';

function setNavText(newText){
	if(document.getElementById('content_title')){
		document.getElementById('content_title').innerHTML = newText;
	}
}

function clearNavText(){
	if(document.getElementById('content_title')){
		document.getElementById('content_title').innerHTML = defaultNavText;
	}
}

function getNavText(elemID){
	var strNavText = '&nbsp;';
	switch(elemID){
		case 'ch_01':
			strNavText = '<b>Chapter 1:</b> (3:06)<br /> "Teaser" introduction for <i>New Orleans</i> on <i>American Experience</i>.';
			break;
		case 'ch_02':
			strNavText = '<b>Chapter 2:</b> (10:55)<br /> The French establish New Orleans in 1718 and an urban culture develops from its diverse population.';
			break;
		case 'ch_03':
			strNavText = '<b>Chapter 3:</b> (3:00)<br /> A cemetery caretaker discusses the important connection between past and present in New Orleans.';
			break;
		case 'ch_04':
			strNavText = '<b>Chapter 4:</b> (10:36)<br /> New Orleans struggles with reconstruction, racial turmoil and the emergence of the Crescent City White League.';
			break;
		case 'ch_05':
			strNavText = '<b>Chapter 5:</b> (3:45)<br /> The Mardi Gras carnival is rich with long-standing traditions.';
			break;
		case 'ch_06':
			strNavText = '<b>Chapter 6:</b> (8:43)<br /> Homer Plessy challenges the segregation of railroad cars in Louisiana in the Supreme Court case <i>Plessy v. Ferguson</i>.';
			break;
		case 'ch_07':
			strNavText = '<b>Chapter 7:</b> (5:20)<br /> Hurricane Katrina destroys the historic Dooky Chas\'s restaurant.';
			break;
		case 'ch_08':
			strNavText = '<b>Chapter 8:</b> (11:00)<br /> New Orleans is the birthplace of jazz and home to a rich tapestry of musical influences.';
			break;
		case 'ch_09':
			strNavText = '<b>Chapter 9:</b> (12:51)<br /> The overflowing Mississippi floods New Orleans in 1927, revealing weaknesses and errors in the city\'s planning and preparation.';
			break;
		case 'ch_10':
			strNavText = '<b>Chapter 10:</b> (3:44)<br /> The citizens of New Orleans pick up the pieces after Hurricane Katrina to plan for Mardi Gras.';
			break;
		case 'ch_11':
			strNavText = '<b>Chapter 11:</b> (9:27)<br /> In 1938 Tennessee Williams finds his inspiration in New Orleans.';
			break;
		case 'ch_12':
			strNavText = '<b>Chapter 12:</b> (3:11)<br /> One artist explains his affection for the French Quarter and the creative community that exists in New Orleans.';
			break;
		case 'ch_13':
			strNavText = '<b>Chapter 13:</b> (9:20)<br /> In 1949 Louis Armstrong returns to New Orleans and is disillusioned by the racial divisions and tensions now present in his hometown.';
			break;
		case 'ch_14':
			strNavText = '<b>Chapter 14:</b> (10:52)<br /> Beginning in the mid-1940s, many of the New Orleans\' middle-class residents move out of the city to the suburbs.';
			break;
		case 'ch_15':
			strNavText = '<b>Chapter 15:</b> (3:15)<br /> New Orleans manages to preserve its culture and vibrancy despite past hardships.';
			break;
		case 'ch_16':
			strNavText = '<b>Chapter 16:</b> (3:39)<br /> Credits';
			break;
	}
	return strNavText;
}

function initNavLinks() {
	if (!document.getElementById('content_title')) return;
	defaultNavText = document.getElementById('content_title').innerHTML;

	if (!document.getElementsByTagName || !document.getElementById('chpater_nav')) return;
	var anchors = document.getElementById('chpater_nav').getElementsByTagName('a');
	for (var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute('href') && anchor.id != 'auto_play'){  // ignores named anchors
			anchor.onmouseover = function(){ try{ setNavText(getNavText(this.id)); } catch(e) { /* catch exceptions */ } };
			anchor.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();
	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;
	}
}