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> (2:49)<br /> "Teaser" introduction for <i>Summer of Love</i> on <i>American Experience</i>.';
			break;
		case 'ch_02':
			strNavText = '<b>Chapter 2:</b> (7:53)<br /> Disillusioned members of the Baby Boom generation embrace a utopian vision.';
			break;
		case 'ch_03':
			strNavText = '<b>Chapter 3:</b> (4:25)<br /> A mind altering new drug becomes popular with San Francisco\'s hippies.';
			break;
		case 'ch_04':
			strNavText = '<b>Chapter 4:</b> (7:34)<br /> National news reports put the hippie movement in the national spotlight. Young people travel to San Francisco from across the country.';
			break;
		case 'ch_05':
			strNavText = '<b>Chapter 5:</b> (7:35)<br /> San Francisco residents and local authorities react to the growing number of hippies in the city.';
			break;
		case 'ch_06':
			strNavText = '<b>Chapter 6:</b> (9:17)<br /> San Francisco hippies create the Council for the Summer of Love in response to concerns about a massive influx of young people to the city.';
			break;
		case 'ch_07':
			strNavText = '<b>Chapter 7:</b> (9:46)<br /> Young teenage runaways struggle with drugs, disease and life on the streets of San Francisco.';
			break;
		case 'ch_08':
			strNavText = '<b>Chapter 8:</b> (3:28)<br /> On October 6, 1967, a group of hippies close the curtain on the Summer of Love with "The Death of the Hippie."';
			break;
		case 'ch_09':
			strNavText = '<b>Chapter 9:</b> (2:51)<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;