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>Angel of Mercy or Medical Monster?</b> (2:08)<br />An introduction to Dr. Walter Freeman\'s story.';
			break;
		case 'ch_02':
			strNavText = '<b>Ellen Ionesco\'s Story</b> (4:14)<br />The first patient to receive a transorbital or "ice pick" lobotomy is a 29-year-old woman.';
			break;
		case 'ch_03':
			strNavText = '<b>Warehoused Patients and Experimental Treatments</b> (4:04)<br />Desperate conditions in mental hospitals lead to experimental treatments for mental illness.';
			break;
		case 'ch_04':
			strNavText = '<b>Dr. Freeman\'s Research</b> (6:29)<br />Convinced that mental illness has physical causes, Freeman performs frontal lobotomies in an attempt to help patients.';
			break;
		case 'ch_05':
			strNavText = '<b>Refining the Procedure</b> (6:40)<br />Ignoring side effects and failures, Freeman continues to perform and improve his procedure. ';
			break;
		case 'ch_06':
			strNavText = '<b>Production Line Lobotomies</b> (4:33)<br />To perform lobotomies on a mass scale for poor patients in state hospitals, Freeman develops a technique to enter the frontal lobe area through the eye socket.';
			break;
		case 'ch_07':
			strNavText = '<b>Training Others</b> (3:04)<br />Often surrounded by press, Freeman performs many lobotomies using his new procedure.';
			break;
		case 'ch_08':
			strNavText = '<b>Beulah Jones\' Story</b> (3:10)<br />Family members tell the story of a victim of a lobotomy.';
			break;
		case 'ch_09':
			strNavText = '<b>The West Virginia Lobotomy Project</b> (4:41)<br />Freeman\'s crusade takes him to West Virginia where he performs lobotomies on many of the state hospitals\' poorest and most vulnerable mental patients.';
			break;
		case 'ch_10':
			strNavText = '<b>Fighting Critics</b> (4:03)<br />When lobotomy is judged harshly, Freeman challenges critics to find an alternative treatment for serious mental illness.';
			break;
		case 'ch_11':
			strNavText = '<b>Howard Dully\'s Story</b> (5:11)<br />Freeman lobotomizes a twelve-year-old boy whose stepmother wanted to improve his behavior.';
			break;
		case 'ch_12':
			strNavText = '<b>Freeman\'s Final Journey</b> (3:39)<br />Criss-crossing the country Freeman searches out his former patients in an effort to prove that lobotomy was not a medical disaster.';
			break;
		case 'ch_13':
			strNavText = '<b>Credits</b> (2:52)';
			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;