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> (1:00)<br /> Opening Credits for <i>Alexander Hamilton</i>.';
			break;
		case 'ch_02':
			strNavText = '<b>Chapter 2:</b> (4:12)<br /> Founding Father Gouverneur Morris struggles to define Alexander Hamilton\'s character.';
			break;
		case 'ch_03':
			strNavText = '<b>Chapter 3:</b> (8:19)<br /> Young Hamilton overcomes family tragedy and hardship, gaining notice for his intelligence and hard work as a shipping clerk on the Caribbean island of St. Croix.';
			break;
		case 'ch_04':
			strNavText = '<b>Chapter 4:</b> (4:48)<br /> In 1773 Hamilton arrives in the American colonies to study at King\'s College in New York. He embraces the revolutionary cause.';
			break;
		case 'ch_05':
			strNavText = '<b>Chapter 5:</b> (2:54)<br /> Hamilton\'s accomplishments gain him the patronage of General George Washington.';
			break;
		case 'ch_06':
			strNavText = '<b>Chapter 6:</b> (5:12)<br /> Hamilton marries Betsy Schuyler, the daughter of the wealthy and powerful General Philip Schuyler.';
			break;
		case 'ch_07':
			strNavText = '<b>Chapter 7:</b> (5:14)<br /> Hamilton wins recognition commanding a battalion during the revolutionary victory at Yorktown, Virginia.';
			break;
		case 'ch_08':
			strNavText = '<b>Chapter 8:</b> (13:12)<br /> Hamilton\'s Federalist Papers play a central role in the ratification of the U.S. Constitution.';
			break;
		case 'ch_09':
			strNavText = '<b>Chapter 9:</b> (9:22)<br /> As the nation\'s first Secretary of the Treasury, Hamilton centralizes power by having the central government assume the debts of the individual states.';
			break;
		case 'ch_10':
			strNavText = '<b>Chapter 10:</b> (4:59)<br /> An affair with Maria Reynolds leads Hamilton to fall victim to blackmail.';
			break;
		case 'ch_11':
			strNavText = '<b>Chapter 11:</b> (14:31)<br /> Hamilton and other Federalists clash with Jefferson\'s Republican Party over the future of the new democracy.';
			break;
		case 'ch_12':
			strNavText = '<b>Chapter 12:</b> (5:11)<br /> Hamilton grooms his eldest son Philip for a future in politics.';
			break;
		case 'ch_13':
			strNavText = '<b>Chapter 13:</b> (5:26)<br /> Hamilton\'s public admission of his affair with Maria Reynolds backfires.';
			break;
		case 'ch_14':
			strNavText = '<b>Chapter 14:</b> (8:04)<br /> Hamilton\'s campaign against John Adams\' bid for re-election destroys his own Federalist Party.';
			break;
		case 'ch_15':
			strNavText = '<b>Chapter 15:</b> (6:22)<br /> Hamilton becomes a political outsider after Thomas Jefferson wins the presidency.';
			break;
		case 'ch_16':
			strNavText = '<b>Chapter 16:</b> (2:28)<br /> Hamilton\'s hopes and dreams for his eldest son are crushed when Philip is killed in a duel.';
			break;
		case 'ch_17':
			strNavText = '<b>Chapter 17:</b> (8:10)<br /> Offended by Hamilton\'s sharp criticism, Aaron Burr challenges Hamilton to a duel.';
			break;
		case 'ch_18':
			strNavText = '<b>Chapter 18:</b> (3:22)<br /> The legacy of Alexander Hamilton lies in the creation of systems that forged a united nation.';
			break;
		case 'ch_19':
			strNavText = '<b>Chapter 19:</b> (2:50)<br /> End 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; } }

// 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;
	}
}

// function called when DOM is loaded
function initPage() {
	if (arguments.callee.done) return; // quit if this function has already been called
	arguments.callee.done = true; // flag this function so called only oce
	// do stuff
	initNavLinks();
	initImages();
}

// for Mozilla based browsers
if (document.addEventListener) {
   document.addEventListener("DOMContentLoaded", initPage, null);
}

// for Internet Explorer
/*@cc_on @*/
/*@if (@_jscript)
   document.write('<script type="text/javascript" defer src=../js/ie_dom_loaded.js></script>');
/*@end @*/

/* for other browsers */
window.onload = initPage;