// JavaScript Document
/****************************************************************
FUNCTIONS.JS

The main Javascript file for the website. 
Includes code for changing the text that goes with the map for the
current year, as well as code for opening a window to compare 2 maps
and to open the quiz window.

Further documentation (on how the pieces of this code are designed
to work together, and with Javascript) is included in the design
documentation for this website.
*****************************************************************/


/**
 * Function:  startFlashJavascriptIntegrationKit
 * Desc: Initializes the Flash Javascript Integration Kit
 *		 Allow calls to Flash, via the Flash Javascript Integration Kit.
 *		 A call is made in the format:
 *			flashProxy.call("nameOfFlashFunction", argument1, argument2, ...); *		
 */
function startFlashProxy(){
	var uid = new Date().getTime();
	var flashProxy = new FlashProxy(uid, 'flash/JavaScriptFlashGateway.swf');
	}


/**
 * Function: swapActiveLink
 * Desc: Changes which year link appears to be active.
 */
function swapActiveLink(previousMapYear, mapYear, previousScrubberPosition, currentScrubberPosition){   //this is a separate function only so that it can be called from Flash
	previousLink = document.getElementById('navigation').getElementsByTagName('li')[previousScrubberPosition-1];  //get the previous active link
	currentLink = document.getElementById('navigation').getElementsByTagName('li')[currentScrubberPosition-1];  //gets the currently active link
	previousLink.innerHTML='<a href="javascript:swapMapFlash('+previousScrubberPosition+','+previousMapYear+')" title="'+previousMapYear+'" alt="'+previousMapYear+'">'+previousMapYear+'</a>';  //reset to normal
	currentLink.innerHTML='<span>'+mapYear+'<img src="../../historical_maps/js/images/arrow.gif" title="Year" alt="Year"/></span>';  //set to the active link
}


/**
 * Function: swapSwitchToStatic
 * Desc: Changes the text and link associated with the "switch to HTML only" link
 */
function swapSwitchToStatic(mapYear){
	document.getElementById("switch").innerHTML='<a href="../../historical_maps/js/%27+mapYear+%27.asp" title="'+mapYear+'">switch to html-only map</a>';
	}

/**
 * Function: swapYear
 * Desc: Shows the appropriate year in the html.
 */
function swapYear(mapYear){ 
	document.getElementById("yearText").innerHTML=mapYear;
}


/**
 * Function: swapFontScript
 * Desc: Shows the appropriate font script year in the html.
 */
function swapFontScriptYear(fontScriptYear){ 
	document.getElementById("fontScript").innerHTML=fontScriptYear;
}


/**
 * Function: swapNarrative
 * Desc: Shows the appropriate narrative history, for this year, in the html.
 *		 The narrative appears in an Iframe, that loads in a separate document 
 *		 (e.g., "text1700.html")that is called by the function "loadIframe",
 *		 which is found in the "iframe.js" file.
 */
function swapNarrative(mapYear){ 
	loadIframe('contentIframe', 'text'+mapYear+'.asp');
	}


/**
 * Function: swapPhotos
 * Desc: Shows the appropriate photos, for this year, in the html.
 * Only shows the image2 (and its title, alt tag, etc.) if it is in
 * a year that has an image to show (as defined in the "if" statement).
 * This means that these mapYears (with 2 images) needed to be hard-
 * coded in the function, and changed if the developer wants to add
 * or subtract a second image for a year.
 */
function swapPhotos(mapYear){
	document.getElementById("imageRight1").innerHTML='<img src="../../historical_maps/js/images/%27+mapYear+%27image.gif" title="Canada in '+mapYear+'"  alt="Canada in '+mapYear+'" />';
	if (mapYear == ("1825" || "1862" || "1867" || "1912" || "1977" || "1999")){
		document.getElementById("imageRight2").innerHTML='<img src="../../historical_maps/js/images/%27+mapYear+%27image2.gif" title="Canada in '+mapYear+'"  alt="Canada in '+mapYear+'" />';
		}
	else{
		document.getElementById("imageRight2").innerHTML='';  //this resets it to nothing if there is no image, so that the old image doesn't continue to be displayed
		}
	}


/**
 * Function: swapFunFact
 * Desc: Shows the appropriate fun fact, for this year, in the html.
 */
function swapFunFact(yearFunFact){ 
	document.getElementById("fact").innerHTML=yearFunFact;
}


/**
 * Function: swapMapJS
 * Desc: Receives the appropriate information from the maps.swf for this year.
 * Places this information in the content of the html page.  This function is
 * called from Flash.
 */
function swapMapJS(previousMapYear, mapYear, previousScrubberPosition, currentScrubberPosition, fontScriptYear, yearFunFact){
	swapActiveLink(previousMapYear, mapYear, previousScrubberPosition, currentScrubberPosition);
	swapSwitchToStatic(mapYear);
	swapYear(mapYear);
	swapFontScriptYear(fontScriptYear);
	swapNarrative(mapYear);
	swapPhotos(mapYear);
	swapFunFact(yearFunFact);
}


/**
 * Function: swapMapFlash
 * Desc: Lets the maps.swf know that it is being called from javascript
 * (so that it doesn't re-initialize everything).  Lets it know which
 * map year we want to see.  Flash then calls the "swapMapJS" function
 * above, with the appropriate information.
 */
function swapMapFlash(newScrubberPosition,mapYear){
	flashProxy.call('callFromJS',newScrubberPosition,mapYear);
}


/**
 * Function: switchToDynamic
 * Desc: Switches to the dynamic version of the website.  Calls
 * the appropriate year in Flash.
 */
function switchToDynamic(newScrubberPosition,mapYear){
	document.location.href = "maps.asp"
//	swapMapFlash(newScrubberPosition,mapYear);
	}

/**
 * Function: compareMaps
 * Desc: This function is called from Flash.  
 * It opens a new window, to place the compare.swfs in.
 * The flashCall contains FlashVars, which are sent to the .swf
 * to let it know which 2 maps to open.  Note that there is CSS
 * in the body of the javascript, because it needs to change the
 * padding dynamically, based on the user's screen resolution.
 * Two separate Flash objects are embedded, both containing the
 * preloader_compare.swf file, one showing "map1" and one showing
 * "map2".
 * Note that "map1", "map2", "mapFactor", and "compareMovieName" are sent
 * to the Flash movies as FlashVars in the <embed> and <object> tags.
 * The mapFactor adjusts the map size, based on the user's screen
 * resolution, for screen resolutions smaller than 1152x864.
 */
function compareMaps(map1,map2){
	designWidth=1152;  //the screen resolution that this was designed for
	mapFactor=1;  // if the screen resolution is 1152x864 (or bigger), then we'll leave everything the same size they were designed
	flashWidth=500; //the originally designed size of the flash stage
	flashHeight=355; //the originally designed size of the flash stage
	mapPadding=20; //the original designed padding for the div;

	if (screen.width<1152){  //only do this if the height is less than the screen width this was designed for (you don't want to make the maps bigger than this, or they will lose their clarity)
	mapFactor=screen.width/designWidth; //apply this to both height and width to preserve map dimensions //this is sent to the preloader_compare.swf as a variable, through FlashVars
	}
	//specify Flash file dimensions
	flashWidth=flashWidth*mapFactor;
	flashHeight=flashHeight*mapFactor;

	//specify the padding around the map (to over-ride the CSS)
	mapPadding=mapPadding*mapFactor;
	
	windowWidth=(flashWidth+50*mapFactor)*2;  // since there are two maps on the page
	windowHeight=flashHeight+50*mapFactor;
	
	movieName="flash/preloader_compare.swf";
	compareMovieName = "flash/compare.swf"; //this is sent to the preloader_compare.swf as a variable
	
	// open new document 

	var CompareWindow=window.open('','compare_win','width='+windowWidth+',height='+windowHeight+',left=0,top=0,toolbar=No,location=No,scrollbars=No,status=No,resizable=Yes,fullscreen=No,menubar=No,copyhistory=No');
	CompareWindow.focus();
	CompareWindow.document.open();
	
	// Text of the new document
	// Replace your " with ' or \" or your document.write statements will fail
	var str;
	str='<html>';
	str+=	'<head>';
	str+=		'<title>Compare Maps: '+map1+', '+map2+'</title>';
	str+=		'<link rel="stylesheet" type="text/css" href="../../historical_maps/js/css/compare.css" media="screen" title="CG Default" />';
	str+=	'</head>';
	str+=	'<body>';
	str+=		'<div id="wrapper">';
	str+=			'<div style="margin:0; float:left">';
	str+=				'<div style="margin:0; padding:'+mapPadding+'; border:2px;">';
	str+=					'<div style="background-color:black; padding-top:1px; padding-right:2px; padding-bottom:2px; padding-left:1px;">';
	str+=						'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+flashWidth+'" height="'+flashHeight+'">	<param name="movie" value="../../historical_maps/js/%27+movieName+%27">	<param name="quality" value="high">	<param name=FlashVars value="mapYear='+map1+'&mapFactor='+mapFactor+'&movieName='+compareMovieName+'">	<embed src="../../historical_maps/js/%27+movieName+%27" FlashVars="mapYear='+map1+'&mapFactor='+mapFactor+'&movieName='+compareMovieName+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+flashWidth+'" height="'+flashHeight+'" name="compareMaps1" swLiveConnect="true"></embed></object>';
	str+=					'</div>';
	str+=				'</div>';
	str+=			'</div>';
	str+=			'<div style="margin:0; float:right">';
	str+=				'<div style="margin:0; padding:'+mapPadding+'">';
	str+=					'<div style="background-color:black; padding-top:1px; padding-right:2px; padding-bottom:2px; padding-left:1px;">';
	str+=						'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+flashWidth+'" height="'+flashHeight+'">	<param name="movie" value="../../historical_maps/js/%27+movieName+%27">	<param name="quality" value="high">	<param name=FlashVars value="mapYear='+map2+'&mapFactor='+mapFactor+'&movieName='+compareMovieName+'">	<embed src="../../historical_maps/js/%27+movieName+%27" FlashVars="mapYear='+map2+'&mapFactor='+mapFactor+'&movieName='+compareMovieName+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+flashWidth+'" height="'+flashHeight+'" name="compareMaps2" swLiveConnect="true"></embed></object>';
	str+=					'</div>';
	str+=				'</div>';
	str+=			'</div>';
	str+=			'<div class="clear">';
	str+=			'</div>';
	str+=		'</div>';
	str+=	'</body>';
	str+='</html>';
	CompareWindow .document.write(str);
								  
	// close the document
	CompareWindow .document.close(); 
}


/**Function: openQuiz
 * Finds out from Flash what the current year is.
 * Flash then calls the openQuizWindow function,
 * with the appropriate information.
 */
function openQuiz(){
	flashProxy.call('openQuizWindow');
	}


/**
 * Function: openQuizWindow
 * Desc: Opens a new window which already has the appropriate Flash
 * quiz embedded in it.  This function is called from Flash.
 */
function openQuizWindow(quiz_window) {
quizWindow = window.open(quiz_window, "quizWin", "width=595,height=510");
quizWindow.focus();
}

//Quiz Popup

function newWindow(newwindow) {
	mainWindow = window.open(newwindow, "mainWin", "width=610,height=525");
	mainWindow.focus();
}

//navigation javascript check

function navCheck() {
	document.getElementById("1700").href="maps.asp?scrubber=1";
	document.getElementById("1775").href="maps.asp?scrubber=2";
	document.getElementById("1791").href="maps.asp?scrubber=3";
	document.getElementById("1825").href="maps.asp?scrubber=4";
	document.getElementById("1849").href="maps.asp?scrubber=5";
	document.getElementById("1862").href="maps.asp?scrubber=6";
	document.getElementById("1867").href="maps.asp?scrubber=7";
	document.getElementById("1873").href="maps.asp?scrubber=8";
	document.getElementById("1882").href="maps.asp?scrubber=9";
	document.getElementById("1895").href="maps.asp?scrubber=10";
	document.getElementById("1905").href="maps.asp?scrubber=11";
	document.getElementById("1912").href="maps.asp?scrubber=12";
	document.getElementById("1977").href="maps.asp?scrubber=13";
	document.getElementById("1999").href="maps.asp?scrubber=14";
}

function startMap() {
	var whichMap = window.location.search.slice(1);
	var splitMap = whichMap.split("-");
	swapMapFlash(splitMap[0], splitMap[1]);
	alert(splitMap);
}


//quiz javascript check

function quizCheck(whichYear) {
	var javaLink = "javascript:newWindow('quiz/quiz" + whichYear + ".html')";
	var whichLink = "quiz" + whichYear;
	document.getElementById(whichLink).href=javaLink;
	document.getElementById(whichLink).target='';
}

//maps.html javascript check
function navMapsCheck() {
	document.getElementById("1700").href="javascript:swapMapFlash(1,1700)";
	document.getElementById("1775").href="javascript:swapMapFlash(2,1775)";
	document.getElementById("1791").href="javascript:swapMapFlash(3,1791)";
	document.getElementById("1825").href="javascript:swapMapFlash(4,1825)";
	document.getElementById("1849").href="javascript:swapMapFlash(5,1849)";
	document.getElementById("1862").href="javascript:swapMapFlash(6,1862)";
	document.getElementById("1867").href="javascript:swapMapFlash(7,1867)";
	document.getElementById("1873").href="javascript:swapMapFlash(8,1873)";
	document.getElementById("1882").href="javascript:swapMapFlash(9,1882)";
	document.getElementById("1895").href="javascript:swapMapFlash(10,1895)";
	document.getElementById("1905").href="javascript:swapMapFlash(11,1905)";
	document.getElementById("1912").href="javascript:swapMapFlash(12,1912)";
	document.getElementById("1977").href="javascript:swapMapFlash(13,1977)";
	document.getElementById("1999").href="javascript:swapMapFlash(14,1999)";
}

//glossary javascript check
function glossaryCheck(){
	document.getElementById("glossaryNoScript").style.display = "none";
}

//index javascript check
function indexCheck(){
	document.getElementById("noScript").style.display = "none";
	document.getElementById("scriptText").style.display = "none";
//	document.getElementById("scriptText").style.display = "block";
}




