// for preloading images and rollovers
var ImgPreLoad = new Array();
var ImgPath = 'images/nav/';
var ShipImgPath = 'images/toolkits/ships/images/';
var RollOverNameConv = '_h';
var RollOutNameConv = '';
var RollOutCurrentNameConv = '_at';

// default glossary highlight state
var highlights_on = true;
var galera_table_on = true;
var galera_current_step = 1;  // 0 is the intro step
var galera_ImgPath = 'images/toolkits/math/galera/animation/';

// for flash detection
hasFlash = 'check_it';

// for flash page launch
whichPage = 'index';


// assign on onload function to the document to dynamically setup roll overs etc
// this allows your page content to be javascript free - if not supported it's not run
window.onload=function(){
	// this checks to see if there is an element with an id of sub_dir (used on body) to set the correct image path
	if(document.getElementById){
		( document.getElementById('sub_dir') ? (ImgPath = '../images/nav/') : (ImgPath = 'images/nav/') );
		( document.getElementById('hide_show_hilight') ? ( show('hide_show_hilight') ) : false );
		// these are for the galera page
		( document.getElementById('galera_activity') ? ( show('galera_activity') ) : false ); // the step through
		( document.getElementById('step_prev') ? (document.getElementById('step_prev').innerHTML = '') : false );  // previous link in activity
		( document.getElementById('hide_show_table') ? ( show('hide_show_table') ) : false ); // link to show the table
		( document.getElementById('galera_table') ? ( hide('galera_table') ) : false ); // the table
		galera_table_on = false;
		// this is for the ships toolkit
		if( document.getElementById('ship_Map') ){ // the image map
			dynAreas = document.getElementById('ship_Map').getElementsByTagName('area');
			for (var i = 0; i < dynAreas.length; i++) {
				var curArea = dynAreas[i];
				if (curArea.getAttribute('href')){
					if(curArea.className.match( /(\bship_roll\b)/ )){ //class="ship_roll"
						curArea.onmouseover = function(){
							ship_on( this.id.substr(4), this.id.substr(0,3) );
							show( 'num_'+this.id.substr(6) );
							document.getElementById('ship_text').innerHTML = document.getElementById('descr_'+this.id.substr(6) ).innerHTML;
						};
						numArea = document.getElementById('num_'+curArea.id.substr(6) );
						numArea.onmouseout = function(){
							ship_off( 's_'+this.id.substr(4), 'jpg' );
							hide( 'num_'+this.id.substr(4) );
							document.getElementById('ship_text').innerHTML = 'Click on the parts of the galley to learn more.';
						};
					}
				}
			}
		}
	}
	initLinks();
	// add init for the hightlighter
	// to store and read from a cookie
};

// "preload" is used for preloading the rollover images
function preload(i, imgName,imgType){
	//alert('preload[' + imgName + RollOverNameConv + '].[' + imgType + '] ImgPreLoad[' + i + ']');
	ImgPreLoad[i] = new Image();
	ImgPreLoad[i].src = ImgPath + imgName + RollOverNameConv + '.' + imgType ;
}
// special preload for the ship toolkit
function ship_preload(i, imgName,imgType){
	//alert('preload[' + imgName + RollOverNameConv + '].[' + imgType + '] ImgPreLoad[' + i + ']');
	ImgPreLoad[i] = new Image();
	ImgPreLoad[i].src = ShipImgPath + imgName + RollOverNameConv + '.' + imgType ;
}

// "on" is used to swap an img to it's roll over state
function on(imgName,imgType){ ((document.images) ? ( document.images[imgName].src = ImgPath + imgName + RollOverNameConv + '.' + imgType ) : false); }

// "off" is used to swap an img to it's off state
function off(imgName,imgType){ ((document.images) ? ( document.images[imgName].src = ImgPath + imgName + RollOutNameConv + '.' + imgType ) : false); }

// "at" is used to swap an img to it's at state (for the current section)
function at(imgName,imgType){ ((document.images) ? ( document.images[imgName].src = ImgPath + imgName + RollOutCurrentNameConv + '.' + imgType ) : false); }

// "ship_on" is used to swap an img to it's roll over state
function ship_on(imgName,imgType){ ((document.images) ? ( document.images[imgName].src = ShipImgPath + imgName + RollOverNameConv + '.' + imgType ) : false); }

// "ship_off" is used to swap an img to it's off state
function ship_off(imgName,imgType){ ((document.images) ? ( document.images[imgName].src = ShipImgPath + imgName + RollOutNameConv + '.' + imgType ) : false); }


function initLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName('a');
	for (var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute('href')){  // ignores named anchors
			// this is used for single image roll overs
			if(anchor.className.match( /(\broll\b)/ )){ //class="roll"
				preload( i, anchor.name.substr(4), anchor.name.substr(0,3) ) ;
				anchor.onmouseover = function(){
					on( this.name.substr(4), this.name.substr(0,3) );
				};
				anchor.onmouseout = function(){
					off( this.name.substr(4), this.name.substr(0,3) );
				};
			}
			// this is used for multiple image roll overs and text changing in the ships toolkit only
			if(anchor.className.match( /(\bship_roll\b)/ )){ //class="ship_roll"
				ship_preload( i, anchor.name.substr(4), anchor.name.substr(0,3) ) ;
				anchor.onmouseover = function(){
					ship_on( this.name.substr(4), this.name.substr(0,3) );
					show( 'num_'+this.name.substr(6) );
					document.getElementById('ship_text').innerHTML = document.getElementById('descr_'+this.name.substr(6) ).innerHTML;
				};
				anchor.onmouseout = function(){
					ship_off( this.name.substr(4), this.name.substr(0,3) );
					hide( 'num_'+this.name.substr(6) );
					document.getElementById('ship_text').innerHTML = 'Click on the parts of the galley to learn more.';
				};
			}
			// this is used for single image roll overs "at" the current section
			if(anchor.className.match( /(\bat\b)/ )){ //class="at"
				anchor.onmouseover = function(){
					on(this.name.substr(4),this.name.substr(0,3));
				};
				anchor.onmouseout = function(){
					at(this.name.substr(4),this.name.substr(0,3));
				};
			}
			// this is used for image roll overs with a disconnected roll over
			if(anchor.className.match('double_roll')){
				preload( anchors.length + i + 1, anchor.name.substr(4) + '1', anchor.name.substr(0,3) ) ;
				preload( anchors.length + i + 2, anchor.name.substr(4) + '2', anchor.name.substr(0,3) ) ;
				anchor.onmouseover = function(){
					on( this.name.substr(4) + '1', this.name.substr(0,3) );
					on( this.name.substr(4) + '2', this.name.substr(0,3) );
				};
				anchor.onmouseout = function(){
					off( this.name.substr(4) + '1', this.name.substr(0,3) );
					off( this.name.substr(4) + '2', this.name.substr(0,3) );
				};
			}
			// this is used for taking action based on flash install meeting required version
			// this expects that the flash detect script are included seperately
			if(anchor.className.match('swf_action')){
				if(hasFlash == 'check_it'){
					hasFlash = DetectFlashVer(6, 65);
				}
				if(hasFlash){
					switch(anchor.name){
						case 'journey':
							anchor.onclick = function(){
								popMap('journey/journey_pop.html');
								return false;
							};
							break;	
						case 'jpg_icon_map':
							anchor.onclick = function(){
								popMap('journey/journey_pop.html');
								return false;
							};
							break;
						case 'manuscript':
							anchor.onclick = function(){
								popViewer('manuscript/manuscript_pop.html');
								return false;
							};
							break;
						case 'jpg_icon_viewer':
							anchor.onclick = function(){
								popViewer('manuscript/manuscript_pop.html');
								return false;
							};
							break;
						case 'navkit':
							document.getElementById('swf_txt').innerHTML = 'Click to play animation.';
							anchor.onclick = function(){
								popNavKit('navkit_pop.html');
								return false;
							};
							break;
					}
					//hide('get_flash');
				}else{
					if(anchor.name == 'navkit'){
						anchor.onclick = function(){
							popImage(this.href, this.title);
							return false;
						};
					}else{
						anchor.onclick = function(){
							show('really_get_flash');
							return false;
						};
					}
				}
			}
			//page_launch is used to open specific pages in the flash manuscript viewer
			if(anchor.className.match('page_launch')){
				if(hasFlash == 'check_it'){
					hasFlash = DetectFlashVer(6, 65);
				}
				if(hasFlash){
					anchor.onclick = function(){
						popViewer('manuscript/' + this.name + '/index.html');
						return false;
					};
				}
			}
			//page_launch is used to open a specific map in the flash journey map
			if(anchor.className.match('map_launch')){
				if(hasFlash == 'check_it'){
					hasFlash = DetectFlashVer(6, 65);
				}
				if(hasFlash){
					anchor.onclick = function(){
						popMap('journey/' + this.name + '/index.html');
						return false;
					};
				}
			}
			// this is used to open a popup for a single image
			// see also galera_step function below
			if(anchor.className.match( /(\bpop\b)/ )){ //class="pop"
				anchor.onclick = function(){
					popImage(this.href, this.title);
					return false;
				};
			}
			// this is used to open a specifically-sized popup
			// see also galera_step function below
			if(anchor.className.match( /\bpop_\d+_\d+\b/ )){ //class="pop_###_###"
				anchor.onclick = function(){
					var dim = this.className.match( /\bpop_(\d+)_(\d+)\b/ )
					popWithSize(this.href, parseInt(dim[1]), parseInt(dim[2]));
					return false;
				};
			}
			// this is used to open a popup for a single glossary term
			if(anchor.className.match( /(\bglossary_on\b|\bglossary_off\b)/ )){ //class="glossary"
				anchor.onclick = function(){
					popTerm(this.href);
					return false;
				};
			}
			// this is used to highlight and un-highlight glossary links
			if(anchor.id.match( /(\bhighlighter\b)/ )){ //id="highlighter"
				anchor.onclick = function(){
					toggle_highlight(this);
					return false;
				};
			}
			// this is used to hide and show galera table
			if(anchor.id.match( /(\btabletoggle\b)/ )){ //id="tabletoggle"
				anchor.onclick = function(){
					toggle_table(this);
					return false;
				};
			}
			// this is used for the galera step through
			if(anchor.id.match( /(\bstep)/ )){ //id="step
				anchor.onclick = function(){
					galera_step(this);
					return false;
				};
			}
			// this is used to launch new browser windows for external links
			if(anchor.getAttribute("rel") == 'external'){
				anchor.target = '_blank';
			}
		}
	}
}

// these functions allow you to show and hide divs
function show(elemID){ ( ( document.getElementById ) ? ( document.getElementById(elemID).style.display = 'block' ) : false );}
function hide(elemID){ ( ( document.getElementById ) ? ( document.getElementById(elemID).style.display = 'none' ) : false );}

// this function allows you to highlight and un-highlight glossary links		
function toggle_highlight(href_link){
	if (!document.getElementsByTagName || !document.getElementById) return;
	
	var anchors = document.getElementsByTagName('a');
	
	for (var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if(anchor.className.match("glossary")){ // match both glossary_on and glossary_off
			( highlights_on ) ? ( anchor.className = "glossary_off" ) : ( anchor.className = "glossary_on" );
		}
	}
	// change link text
	( highlights_on ) ? ( href_link.innerHTML = 'Show Glossary Highlights' ) : ( href_link.innerHTML = 'Hide Glossary Highlights' );
	// toggle hiding boolean
	highlights_on = !highlights_on;

	return false;
}

// this function hides and shows the galera divison
function toggle_table(href_link){
	// change link text and do action
	if ( galera_table_on ) {
		href_link.innerHTML = 'Show all steps in table form';
		hide('galera_table');
	}else{
		href_link.innerHTML = 'Hide all steps in table form';
		show('galera_table');
	}
	// toggle hiding boolean
	galera_table_on = !galera_table_on;

	return false;
}

function galera_step(href_link){
//alert(href_link);
	if (!document.getElementById || !document.images) return;
	
	var arySteps = new Array();
	arySteps[0] = 'step_prev';
	arySteps[1] = 'step_intro';
	arySteps[2] = 'step_a';
	arySteps[3] = 'step_b';
	arySteps[4] = 'step_c';
	arySteps[5] = 'step_d';
	arySteps[6] = 'step_e';
	arySteps[7] = 'step_f';
	arySteps[8] = 'step_g';
	arySteps[9] = 'step_h';
	arySteps[10] = 'step_i';
	arySteps[11] = 'step_j';
	arySteps[12] = 'step_k';
	arySteps[13] = 'step_l';
	arySteps[14] = 'step_next';
	
	// clear current underline
	document.getElementById(arySteps[galera_current_step]).className = '' ;

	document.getElementById('step_prev').innerHTML = '&laquo;';
	document.getElementById('step_next').innerHTML = '&raquo;';


	for (var j = 0; j < arySteps.length; j++) {
		if( href_link.id == arySteps[j] ){
			switch(j){
				case 0:
					if(galera_current_step == 1 || galera_current_step == 2){
						document.getElementById('step_prev').innerHTML = '';
						galera_current_step = 1;
						document.images['step_image'].src = galera_ImgPath + 'i_default.jpg';
					} else if(galera_current_step > 2 && galera_current_step <= 11){
						galera_current_step = galera_current_step - 1;
						document.images['step_image'].src = galera_ImgPath + 'i_0' + (galera_current_step-1) + '.jpg';
					} else if(galera_current_step > 11 && galera_current_step <= 13){
						galera_current_step = galera_current_step - 1;
						document.images['step_image'].src = galera_ImgPath + 'i_' + (galera_current_step-1) + '.jpg';
					}
					break;

				case 1:
					document.getElementById('step_prev').innerHTML = '';
					galera_current_step = 1;
					document.images['step_image'].src = galera_ImgPath + 'i_default.jpg';
					break;

				case 14:
					if(galera_current_step == 0){
						document.getElementById('step_prev').innerHTML = '';
						galera_current_step = 1;
						document.images['step_image'].src = galera_ImgPath + 'i_default.jpg';
					} else if(galera_current_step > 0 && galera_current_step < 10){
						galera_current_step = galera_current_step + 1;
						document.images['step_image'].src = galera_ImgPath + 'i_0' + (galera_current_step-1) + '.jpg';
					} else if(galera_current_step >= 10 && galera_current_step < 12){
						galera_current_step = galera_current_step + 1;
						document.images['step_image'].src = galera_ImgPath + 'i_' + (galera_current_step-1) + '.jpg';
					} else if(galera_current_step == 12 || galera_current_step == 13){
						document.getElementById('step_next').innerHTML = '';
						galera_current_step = 13;
						document.images['step_image'].src = galera_ImgPath + 'i_' + (galera_current_step-1) + '.jpg';
					}
					break;

				default:
					galera_current_step = j ;
					if( j < 11 ){
						document.images['step_image'].src = galera_ImgPath + 'i_0' + (j-1) + '.jpg';
					} else {
						document.images['step_image'].src = galera_ImgPath + 'i_' + (j-1) + '.jpg';
					}
					if( j == 1 ){ document.getElementById('step_prev').innerHTML = ''; }
					if( j == 13 ){ document.getElementById('step_next').innerHTML = ''; }
					break;
			}
			break;//for loop
		}
	}

	// update the activity text and link style
	document.getElementById('step_descr').innerHTML = document.getElementById('descr_' + arySteps[galera_current_step].substr(5) ).innerHTML;
	if(galera_current_step ==1){
		document.getElementById('step_descr').innerHTML += '<p class="step_text">To follow Michael through the steps, click on the letters below.</p>';
	}
	document.getElementById(arySteps[galera_current_step]).className = 'step_active' ;

	// make sure that any links that get written into the activity get properly initialized (copied sections from initLinks)
	dynAnchors = document.getElementById('step_descr').getElementsByTagName('a');
	for (var i = 0; i < dynAnchors.length; i++) {
		var anchor = dynAnchors[i];
		if (anchor.getAttribute('href')){
			if(anchor.className.match( /(\bpop\b)/ )){ //class="pop"
				anchor.onclick = function(){
					popImage(this.href, this.title);
					return false;
				};
			}
			if(anchor.className.match( /\bpop_\d+_\d+\b/ )){ //class="pop_###_###"
				anchor.onclick = function(){
					var dim = this.className.match( /\bpop_(\d+)_(\d+)\b/ )
					popWithSize(this.href, parseInt(dim[1]), parseInt(dim[2]));
					return false;
				};
			}

		}
	}
	return false;
}

// dynamically write in a swf
function writeSWF(divTarget, swfName, swfWidth, swfHeight){
	if(document.getElementById){
		targetDiv = document.getElementById(divTarget);
		if(targetDiv){
			targetDiv.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0" width="' + swfWidth + '" height="' + swfHeight + '" border="0">' +
				'<param name="movie" value="' + swfName + '">' + 
				'<param name="menu" value="false">' +
				'<param name="quality" value="best">' +
				'<param name="bgcolor" value="black">' +
				'<embed src="' + swfName + '" menu="false" quality="best" bgcolor="black" width="' + swfWidth + '" height="' + swfHeight + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" border="0">' +
				'</embed>' +
				'</object>';
		}
	}
}

/* from rhodes js */
var ng = navigator;
var ua = ng.userAgent;
var nav = (ng.appName == 'Netscape');
var saf = (ng.appVersion.indexOf('Safari') != -1); // from pfaxwin.js 
var ie4 = (ng.appVersion.indexOf('MSIE 4') != -1);
var ie = (ng.appVersion.indexOf('MSIE') != -1);
var mac = (ua.indexOf('Mac') != -1);
var aol = (ua.indexOf('AOL') != -1);
var hnd;

function popMap(page, noret) {
	// content is 766 x 565
    if (mac) { w=nav?766:(ie?750:766); h=nav?(saf?569:565):(ie?549:565); } else { w=766; h=aol?565:565; }
    x=aol?40:25; y=aol?12:37;
    mapPop = window.open(page,'mapPop','width='+w+',height='+h+',screenx='+x+',screeny='+y+',left='+x+',top='+y);	
    if ( mapPop.focus ) { mapPop.focus(); }
    if ( noret == null ) { return false; }
}

function popViewer(page, noret) {
	// content is 765 x 565
    if (mac) { w=nav?765:(ie?749:765); h=nav?(saf?569:565):(ie?549:565); } else { w=765; h=aol?565:565; }
    x=aol?40:25; y=aol?12:37;
    viewerPop = window.open(page,'viewerPop','width='+w+',height='+h+',screenx='+x+',screeny='+y+',left='+x+',top='+y);	
    if ( viewerPop.focus ) { viewerPop.focus(); }
    if ( noret == null ) { return false; }
}

function popNavKit(page, noret) {
	// content is 428 x 485
    if (mac) { w=nav?428:(ie?412:428); h=nav?(saf?489:485):(ie?469:485); } else { w=428; h=aol?485:485; }
    x=aol?40:25; y=aol?12:37;
    navkitPop = window.open(page,'navkitPop','width='+w+',height='+h+',screenx='+x+',screeny='+y+',left='+x+',top='+y);	
    if ( navkitPop.focus ) { navkitPop.focus(); }
    if ( noret == null ) { return false; }
}

function popTerm(page, noret){
	var w = 310;
	var h = 200;
	var x = 10;
	var y = 10;
	termPop = window.open(page,'termPop','scrollbars=1,resizable=1,width='+w+',height='+h+',screenx='+x+',screeny='+y+',left='+x+',top='+y);
    if( termPop.focus ) { termPop.focus(); }
    if (noret == null) { return false; }
}

function popWithSize(winURL, winWidth, winHeight){
	// for popping static pop-ups of given size
	winWidth += 16; winHeight += 5; // make sure no scrollbars appear needlessly
	//alert("popWithSize: width=" + winWidth + ", height=" + winHeight);
	var popWin = window.open(winURL, '_blank', 'scrollbars=1,resizable=1,left=10,top=10,width=' + winWidth + ',height=' + winHeight);
	if ( !popWin ) {
		return true; // if blocked by pop-up blocker, allow regular HREF to trigger
	}
	// don't need to give focus since we're using _blank
	return false;
}

function popImage(imgURL, imgTitle){
	// content is unknown so size window to fit
	var PositionX = 10;
	var PositionY = 10;
	var defaultWidth  = 600;
	var defaultHeight = 400;
	var bgStyle = '#E9EEEB';	//STEW added bgStyle
	var AutoClose = false;

    var imagePop = window.open('','imagePop','scrollbars=1,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);

	if( !imagePop ) { return true; }	// if a pop-up blocker is triggered, don't fire up an error
		imagePop.document.write('<html><head><title>'+imgTitle+'<\/title><script type="text\/javascript"><!--\n'+
		'function resizeWindow() {\n'+
		'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
		'var oH = document.images[0].height, oW = document.images[0].width;\n'+
		'if( !oH || window.doneAlready ) { return; }\n'+	// if images are disabled, end the script
		'window.doneAlready = true;\n'+		//fixes onload issues with Opera and Safari
		'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
		'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
		'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
		'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
		'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
		'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
		'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ) +3, oH = oH + ( (oH + 200 ) - myH ) +3 );\n'+	// STEW added +3 to both to ensure scrollbars are hidden
		'}\n'+
		'//--><\/script>'+
		'<\/head><body onload="resizeWindow();"'+(AutoClose?' onblur="self.close();"':'')+' style="background: '+bgStyle+';" >'+	//STEW added bgStyle
		(document.layers?('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;">'))+
		'<img src="'+imgURL+'" alt="'+imgTitle+'" title="'+imgTitle+'" onload="resizeWindow();" \/>'+
		(document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');

	imagePop.document.close();

	if( imagePop.focus ) { imagePop.focus(); }

	return false;
}
