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>Introduction</b> (2:52)';

			break;

		case 'ch_02':

			strNavText = '<b>A Dangerous Depot</b> (8:48)<br />In the late nineteenth century, railroad tycoon Cornelius Vanderbilt\'s Grand Central is considered by many to be the worst train station in the world.';

			break;

		case 'ch_03':

			strNavText = '<b>Manhattan Bans Steam Locomotives</b> (12:24)<br />In 1902, steam engines are banned after a terrible accident kills 15 passengers in the smoky, dark Park Avenue tunnel. Engineer William Wilgus devises a radical solution.';

			break;

		case 'ch_04':

			strNavText = '<b>Moving Trains Underground</b> (7:14)<br />Construction crews build down sixty feet into Manhattan bedrock. Electric trains will travel safely underground, and the railroad will sell air rights above the tracks to real estate developers.';

			break;

		case 'ch_05':

			strNavText = '<b>Problems with Electric Engines</b> (8:08)<br />After another tragic accident in 1907, the New York district attorney calls for the indictment of high-ranking officials at the New York Central -- chief among them, chief engineer William Wilgus.';

			break;

		case 'ch_06':

			strNavText = '<b>"The Greatest Station in the World"</b> (10:57)<br />At midnight on February 2, 1913, three thousand people rush into Grand Central Terminal for the first time. By day\'s end, over 150,000 New Yorkers, from every corner of the city, have come to gaze at their newest monument.';

			break;

		case 'ch_07':

			strNavText = '<b>Credits</b> (2:05)';

			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;