var relPath = '../';

// include other js files
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 archives section
var info_over_discon_src = 'images/icon_text_info.gif';
var site_over_discon_src = 'images/icon_text_web.gif';
var shop_over_discon_src = 'images/icon_text_video.gif';
var tguide_over_discon_src = 'images/icon_text_teach.gif';
var promo_over_discon_src = 'images/icon_text_promo.gif';
var default_discon_src = 'images/icon_text_off.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();

	// archives section
	initArchives();
}

// start archive functions
function initArchives(){
	if(!document.getElementById('archives')) return;
	try {
		var regex_roll = new RegExp('\\b'+ncClsRoll+'\\b');
		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; }

	// preload extra for archives
	loadImg('images/icon_text_info', 'gif');
	loadImg('images/icon_text_web', 'gif');
	loadImg('images/icon_text_video', 'gif');
	loadImg('images/icon_text_teach', 'gif');
	loadImg('images/icon_text_promo', 'gif');

	var aryImages = document.getElementsByTagName('img');
	for (var i = 0; i < aryImages.length; i++) {
		var thisImg = aryImages[i];
		// this is used for archive icon images
		if( thisImg.id.match(regex_info) || thisImg.id.match(regex_site) || thisImg.id.match(regex_shop) || thisImg.id.match(regex_tguide) || thisImg.id.match(regex_promo)  ){
			thisImg.siteId = thisImg.id.substr(thisImg.id.lastIndexOf('_') + 1, thisImg.id.length);			
		}
		if(thisImg.id.match(regex_info)){
			thisImg.onclick = function() { return more_info_click(this); }
			// hide more info by default
			manClsNames('add', document.getElementById('more_info_' + thisImg.siteId), 'hide');
		}
		
		// this is used for single image roll overs and archive icons
		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);
			thisImg.roll_idx = loadImg(imgFile+ncImgOver, imgType);
			thisImg.onmouseover = function() { img_mouseover(this); }
			thisImg.onmouseout = function() { img_mouseout(this); }
		}
	} // end for

}

function img_mouseover(imgObj){
	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_');

		if(imgObj.id.match(regex_info)) swapImg('amex_roll_' + imgObj.siteId, info_over_discon_src);
		if(imgObj.id.match(regex_site)) swapImg('amex_roll_' + imgObj.siteId, site_over_discon_src);
		if(imgObj.id.match(regex_shop)) swapImg('amex_roll_' + imgObj.siteId, shop_over_discon_src);
		if(imgObj.id.match(regex_tguide)) swapImg('amex_roll_' + imgObj.siteId, tguide_over_discon_src);
		if(imgObj.id.match(regex_promo)) swapImg('amex_roll_' + imgObj.siteId, promo_over_discon_src);
		swapImg( imgObj.id, aryLoadImg[imgObj.roll_idx].src );
	}catch(e){ }
}

function img_mouseout(imgObj){
	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_');

		if( imgObj.id.match(regex_info) || imgObj.id.match(regex_site) || imgObj.id.match(regex_shop) || imgObj.id.match(regex_tguide) || imgObj.id.match(regex_promo)  ){
			swapImg('amex_roll_' + imgObj.siteId, default_discon_src);
		}
		var regex_ncImgOver = new RegExp(ncImgOver+'\.');
		swapImg( imgObj.id, imgObj.src.replace(regex_ncImgOver, '.') );

		if( imgObj.id.match(regex_info) ) correct_icon(imgObj);
	}catch(e){ }
}

function more_info_click(imgObj){
	try {
		manClsNames('swap', document.getElementById('more_info_' + imgObj.siteId), 'hide', 'show');
		correct_icon(imgObj);
		return false;
	}catch(e){ alert(debug(e)); }
}

function correct_icon(imgObj){ // for "more info" icon
	if(manClsNames('check', document.getElementById('more_info_' + imgObj.siteId), 'show')){
		swapImg( imgObj.id, aryLoadImg[imgObj.roll_idx].src );
	}else{
		var regex_ncImgOver = new RegExp(ncImgOver+'\.');
		swapImg( imgObj.id, imgObj.src.replace(regex_ncImgOver, '.') );
	}
}
// end archive functions

// 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';
	}
	return strOut;
}

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

// **********************************
// below originally part of images.js
// **********************************

// utility functions and Array
var aryLoadImg = new Array();
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 swapInputImg(imgId, imgSrc){ if(document.getElementById){ document.getElementById(imgId).src = imgSrc; } }

function initMaps() {
	if ( !document.getElementsByTagName || !document.getElementById ) return;

	var imgPath = relPath + 'images/';
	
	//var NavAt = ( ( document.getElementById('sched') || document.getElementById('sched_upcoming') ) ? ( 'nav_schedule' ) : ( 'nav_off' ) );
	var NavAt = document.getElementById('nav') ? document.getElementById('nav').src : imgPath+'nav_off.gif' ;

	var areas = document.getElementsByTagName('area');
	for (var i = 0; i < areas.length; i++) {
		var area = areas[i];
		// these are used for a single img roll over from nav map and back to top map
		if(area.className.match( /(\broll_nav\b)/ )){ //class="roll_nav"
			var imgFile = imgPath + 'nav_' + area.id.substr(4);
			var imgType = area.id.substr(0,3);
			area.roll_idx = loadImg(imgFile, imgType);
			area.onmouseover = function(){
				try {
					swapImg( 'nav', aryLoadImg[this.roll_idx].src );
				}catch(e){ }
			};
			area.onmouseout = function(){
				try {
					//swapImg( 'nav', imgPath + NavAt + '.' + imgType );
					swapImg( 'nav', NavAt );
				}catch(e){ }
			};
		}
		if(area.className.match( /(\broll_b2t\b)/ )){ //class="roll_b2t"
			var imgFile = imgPath + area.id.substr(4);
			var imgType = area.id.substr(0,3);
			area.roll_idx = loadImg(imgFile, imgType);
			area.onmouseover = function(){
				try {
					swapImg( 'ddot_footer', aryLoadImg[this.roll_idx].src );
				}catch(e){ }
			};
			area.onmouseout = function(){
				try {
					var regex_ncImgOver = new RegExp(ncImgOver+'\.');
					swapImg( 'ddot_footer', aryLoadImg[this.roll_idx].src.replace(regex_ncImgOver, '.') );
				}catch(e){ }
			};
		}
		// this is used for a double disconnected roll over from archive map
		if(area.className.match( /(\broll_archive\b)/ )){ //class="roll_archive"
			var imgFile = imgPath + 'hp_themes_' + area.id.substr(4);
			var imgFile2 = imgPath + 'hp_themes_top_' + area.id.substr(4);
			var imgType = area.id.substr(0,3);
			area.roll_idx = loadImg(imgFile, imgType);
			area.roll_idx2 = loadImg(imgFile2, imgType);

			area.onmouseover = function(){
				try {
					swapImg( 'arc', aryLoadImg[this.roll_idx].src );
					swapImg( 'arc_top', aryLoadImg[this.roll_idx2].src );
				}catch(e){ }
			};
			area.onmouseout = function(){
				try {
					swapImg( 'arc', imgPath + 'hp_themes_off.gif' );
					swapImg( 'arc_top', imgPath + 'hp_themes_top_off.gif' );
				}catch(e){ }
			};
		}
	}
}
