var relPath = '../';

// include other js files
document.write('<script type="text/javascript" src="' + relPath + 'j/images.js"></script>');
document.write('<script type="text/javascript" src="' + relPath + 'j/links.js"></script>');
document.write('<script type="text/javascript" src="' + relPath + 'j/popups.js"></script>');


// config vars
// for image rollovers
var ncImgOver = '_over';  // naming convention of img file suffix for the over state
var ncClsRoll = 'roll';   // name of class for single rollover
var ncClsDblRoll = 'dbl_roll';   // name of class for double rollover
// for main image map
var imgNavMapId = 'img_nav';  // id of the image using the nav map
var navMapId = 'nav_map';  // id of the nav map
var navMapAreaActive = '';  // id of the current active area
var ncClsAreaActive = 'active'

// for teachers area search results
var use_iFrame = true;

// teachers area topics
selectedEra = 1;
aryTopics1 = new Array();
aryTopics2 = new Array();
aryTopics3 = new Array();
aryTopics4 = new Array();
aryTopics5 = new Array();
aryTopics6 = new Array();
aryTopics7 = new Array();

// for archives section
var info_over_discon_src = 'icon_text_info.gif';
var site_over_discon_src = 'icon_text_web.gif';
var shop_over_discon_src = 'icon_text_video.gif';
var tguide_over_discon_src = 'icon_text_teach.gif';
var promo_over_discon_src = 'icon_text_promo.gif';

// 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 once
	if (!document.getElementsByTagName) return;
	// do stuff
	initLinks();
	initMaps();
//teachers area start
	if(document.getElementById('teachers_area')){
		if(document.getElementById('teacher_topic')){
			var aryAllTopics = document.getElementById('teacher_topic').getElementsByTagName('option');
			for (var i=0; i<aryAllTopics.length; i++) {
				var thisOption = aryAllTopics[i];
				if( manClsNames('check', thisOption, 'teacher_era_1') ){
					if( thisOption.selected ){
						selectedEra = 1;
						aryTopics1.push('<option value="' + thisOption.value + '" selected="selected">' + thisOption.innerHTML + '</option>');
					}else{
						aryTopics1.push('<option value="' + thisOption.value + '">' + thisOption.innerHTML + '</option>');
					}
				}
				if( manClsNames('check', thisOption, 'teacher_era_2') ){
					if( thisOption.selected ){
						selectedEra = 2;
						aryTopics2.push('<option value="' + thisOption.value + '" selected="selected">' + thisOption.innerHTML + '</option>');
					}else{
						aryTopics2.push('<option value="' + thisOption.value + '">' + thisOption.innerHTML + '</option>');
					}
				}
				if( manClsNames('check', thisOption, 'teacher_era_3') ){
					if( thisOption.selected ){
						selectedEra = 3;
						aryTopics3.push('<option value="' + thisOption.value + '" selected="selected">' + thisOption.innerHTML + '</option>');
					}else{
						aryTopics3.push('<option value="' + thisOption.value + '">' + thisOption.innerHTML + '</option>');
					}
				}
				if( manClsNames('check', thisOption, 'teacher_era_4') ){
					if( thisOption.selected ){
						selectedEra = 4;
						aryTopics4.push('<option value="' + thisOption.value + '" selected="selected">' + thisOption.innerHTML + '</option>');
					}else{
						aryTopics4.push('<option value="' + thisOption.value + '">' + thisOption.innerHTML + '</option>');
					}
				}
				if( manClsNames('check', thisOption, 'teacher_era_5') ){
					if( thisOption.selected ){
						selectedEra = 5;
						aryTopics5.push('<option value="' + thisOption.value + '" selected="selected">' + thisOption.innerHTML + '</option>');
					}else{
						aryTopics5.push('<option value="' + thisOption.value + '">' + thisOption.innerHTML + '</option>');
					}
				}
				if( manClsNames('check', thisOption, 'teacher_era_6') ){
					if( thisOption.selected ){
						selectedEra = 6;
						aryTopics6.push('<option value="' + thisOption.value + '" selected="selected">' + thisOption.innerHTML + '</option>');
					}else{
						aryTopics6.push('<option value="' + thisOption.value + '">' + thisOption.innerHTML + '</option>');
					}
				}
				if( manClsNames('check', thisOption, 'teacher_era_7') ){
					if( thisOption.selected ){
						selectedEra = 7;
						aryTopics7.push('<option value="' + thisOption.value + '" selected="selected">' + thisOption.innerHTML + '</option>');
					}else{
						aryTopics7.push('<option value="' + thisOption.value + '">' + thisOption.innerHTML + '</option>');
					}
				}
			}
			//add era dropdown
			var aryErasLong = new Array();
			aryErasLong[0] = 'U.S. History to 1850';
			aryErasLong[1] = 'Civil War and Reconstruction (1850-1877)';
			aryErasLong[2] = 'The Development of the Industrial U.S. (1860-1910)';
			aryErasLong[3] = 'The Emergence of Modern America (1890-1930)';
			aryErasLong[4] = 'The Great Depression and World War II (1929-1945)';
			aryErasLong[5] = 'Postwar United States (1945 to early 1970s)';
			aryErasLong[6] = 'Contemporary United States (1968 to the present)';
			
			var aryErasShort = new Array();
			aryErasShort[0] = 'U.S. History to 1850';
			aryErasShort[1] = 'Civil War and Reconstruction';
			aryErasShort[2] = 'Industrialization';
			aryErasShort[3] = 'Emergence of Modern America';
			aryErasShort[4] = 'The Depression and WW II';
			aryErasShort[5] = 'Postwar United States';
			aryErasShort[6] = 'Contemporary United States';
			
			var aryEras = aryErasLong;
			if(document.getElementById('cnt_frm_search_feature_sb')){
				aryEras = aryErasShort;
			}
			// create the era drop down add insert it into the form
			//var strSelectEra = '<h3>Choose Era</h3>';
			var strSelectEra = '';
			strSelectEra += '<select id="teacher_era" name="teacher_era">';
			for (var i=0; i<aryEras.length; i++) {
				if( selectedEra == (i+1) ){
					strSelectEra += '<option value="' + (i+1) + '" selected="selected">' + aryEras[i] + '</option>';
				}else{
					strSelectEra += '<option value="' + (i+1) + '">' + aryEras[i] + '</option>';
				}
			}
			strSelectEra += '</select>';
			
			document.getElementById('cnt_teacher_era').innerHTML = strSelectEra;
			document.getElementById('teacher_era').onchange = function (){ changeTopicList(this.value); };
			// adjust the topic box to only show the selected eras topics
			changeTopicList( selectedEra );
		}//end if document.getElementById('teacher_topic')
		
		// SEARCH add show hide functionality and 
		// display counts of the records in each section
		if(document.getElementById('h1_search_title')){
			makeTeacherSearchCollapsable('h4');
		}
		// BROWSE add show hide functionality only
		if(document.getElementById('h1_broswe_title')){
			makeTeacherBrowseCollapsable('h5');
		}
		
	}// end if teachers_area

// archives section start
	if(document.getElementById('archives')){
		// preload extra for archives
		loadImg('icon_text_info', 'gif');
		loadImg('icon_text_web', 'gif');
		loadImg('icon_text_video', 'gif');
		loadImg('icon_text_teach', 'gif');
		loadImg('icon_text_promo', 'gif');
/*
		info_over_discon_src
		site_over_discon_src
		shop_over_discon_src
		tguide_over_discon_src
		promo_over_discon_src
*/
		try {
			var regex_info = new RegExp('\\b'+'amex_info_');
			var regex_site = new RegExp('\\b'+'amex_site_');
			var regex_shop = new RegExp('\\b'+'amex_shop_');
			var regex_tguide = new RegExp('\\b'+'amex_tguide_');
			var regex_promo = new RegExp('\\b'+'amex_promo_');
		}catch(e){ return; }
		
		var aryImages = document.getElementsByTagName('img');

		for (var i = 0; i < aryImages.length; i++) {
			var thisImg = aryImages[i];
			// this is used for single image roll overs
			if(thisImg.id.match(regex_info)){
				if (typeof thisImg.onmouseover != 'function') {
					alert('no function');
					thisImg.onmouseover = function(){ archive_discon_roll(this.id); }
				} else {
					alert('has function');
					var oldFunction = thisImg.onmouseover;
					thisImg.onmouseover = function() { oldFunction(); archive_discon_roll(this.id); }
				}
			} // end if match(regex_info)
		} // end for

	}// archives section end
}

function archive_discon_roll(fromImgId){
	var siteId = fromImgId.substr(fromImgId.lastIndexOf('_'), fromImgId.length);
	var txtSection = fromImgId.substr(5, fromImgId.lastIndexOf('_'));
	alert('fromImgId: ' + 'site id: ' + siteId + '\rwhat: ' + txtSection );
}




// 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=' + relPath + 'j/ie_dom_loaded.js></script>');
/*@end @*/

/* for other browsers */
window.onload = initPage;


// safely handle additional onload events
function addLoadEvent(func) {
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		var oldonload = window.onload;
		window.onload = function() { oldonload(); func(); }
	}
}

// debug is called from catch clauses and parses the error object
function debug(errObj){
	var strOut = 'DEBUG Message:\n';
	for (var name in errObj){
		strOut += name + ': ' + errObj[name] + '\n';
	}
}

// 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;
	}
}

// techaer topic function
function changeTopicList(intEra){
	var aryAllTopics = document.getElementById('teacher_topic').getElementsByTagName('option');

	var aryTopics = eval('aryTopics' + intEra);
	var strSelectTopics = '<select id="teacher_topic" name="teacher_topic">';
	for (var i=0; i<aryTopics.length; i++) {
		strSelectTopics += aryTopics[i] + '\n';
	}
	strSelectTopics += '</select>';
	document.getElementById('cnt_teacher_topic').innerHTML = strSelectTopics;
}

function makeTeacherSearchCollapsable(tagName){
	// check if DOM is available, return if not
	if(!document.createTextNode){ return; }

	// loop over all headlines of the second level
	var heads=document.getElementsByTagName(tagName);
	for(var i=0;i<heads.length;i++){
		// grab the next sibling (the loop is needed because of whitespace issues)
		var tohide=heads[i].nextSibling;
		while(tohide.nodeType!=1){
			tohide=tohide.nextSibling;
		}
		// add hide/show class and relate the sibling to the headline is clickable
		manClsNames('add', tohide, 'show')
//		manClsNames('add', heads[i], 'clickable')

		// store the element to be hidden in an attribute
		heads[i].tohide=tohide;
		// assign class to set the cursor to pointer
		manClsNames('add', heads[i], 'h5_hover');
		
		// set mouse actions
		heads[i].onmouseover=function(){
			if(manClsNames('check', this.tohide, 'h5_open')){
				manClsNames('swap', this, 'h5_open', 'h5_closed');
			}else{
				manClsNames('swap', this, 'h5_closed', 'h5_open');
			}
		};
		heads[i].onmouseout=function(){
			if(manClsNames('check', this.tohide, 'h5_open')){
				manClsNames('swap', this, 'h5_open', 'h5_closed');
			}else{
				manClsNames('swap', this, 'h5_closed', 'h5_open');
			}
		};
		heads[i].onclick=function(){
			// test if the next sibling is already hidden 
			if(manClsNames('check', this.tohide, 'hide')){
				manClsNames('swap', this, 'h5_open', 'h5_closed');
				manClsNames('swap', this.tohide, 'hide', 'show');
			} else {
				manClsNames('swap', this, 'h5_closed', 'h5_open');
				manClsNames('swap', this.tohide, 'show', 'hide');      
			}
		};
		// insert the count of the paragraphs (records) in this hideable section
		aryParas = tohide.getElementsByTagName('p');
		heads[i].innerHTML = heads[i].innerHTML + ' <span class="attribution">' + aryParas.length + ' records found</span>';
	}
}

function makeTeacherBrowseCollapsable(tagName){
	// check if DOM is available, return if not
	if(!document.createTextNode){ return; }

	// loop over all headlines of the second level
	var heads=document.getElementsByTagName(tagName);
	for(var i=0;i<heads.length;i++){
		// grab the next sibling (the loop is needed because of whitespace issues)
		var tohide=heads[i].nextSibling;
		while(tohide.nodeType!=1){
			tohide=tohide.nextSibling;
		}
		// add hide/show class and relate the sibling to the headline is clickable
		manClsNames('add', tohide, 'show')
//		manClsNames('add', heads[i], 'clickable')

		// store the element to be hidden in an attribute
		heads[i].tohide=tohide;
		// assign class to set the cursor to pointer
		manClsNames('add', heads[i], 'h5_hover');

		// set mouse actions
		heads[i].onmouseover=function(){
			if(manClsNames('check', this.tohide, 'h5_open')){
				manClsNames('swap', this, 'h5_open', 'h5_closed');
			}else{
				manClsNames('swap', this, 'h5_closed', 'h5_open');
			}
		};
		heads[i].onmouseout=function(){
			if(manClsNames('check', this.tohide, 'h5_open')){
				manClsNames('swap', this, 'h5_open', 'h5_closed');
			}else{
				manClsNames('swap', this, 'h5_closed', 'h5_open');
			}
		};
		heads[i].onclick=function(){
			// test if the next sibling is already hidden 
			if(manClsNames('check', this.tohide, 'hide')){
				manClsNames('swap', this, 'h5_open', 'h5_closed');
				manClsNames('swap', this.tohide, 'hide', 'show');
			} else {
				manClsNames('swap', this, 'h5_closed', 'h5_open');
				manClsNames('swap', this.tohide, 'show', 'hide');      
			}
		};
	}
}