// Filename: map.js
// Author:   Steve Wallgren, ITO Grand Rapids, Michigan
// Purpose:  This script builds the animation controls for
//           the NDFD "map" pages on the Great Lakes Website.

// Var name prefixes have the following meanings
// boo = boolean
// i   = integer
// o   = object
// oa  = object array
// s   = string

// Globals
var oMap;
var iFPS;
var iDelay;
var iLimit;
var oCPrun;
var oCPfirst;
var oCPlast;
var oCPprev;
var oCPnext;
var oCPfaster;
var oCPslower;
var oCPform;
var oControlPanel;
var oaMaps;

function initializeAnimationControls()
{
	// Check to make sure the browser can handle the DOM functions
	// this script will be using. The script will abort if the
	// browser lacks this level of DOM support.
	if(!document.getElementById ||
	   !document.getElementsByTagName ||
	   !document.createElement ||
	   !document.createTextNode)
	{
		return false;
	}
	
	// Set values
	oMap = document.getElementById("map");
	iLimit = iFileCount - 1;
	iFPS = 3;
	iDelay = 1000 / iFPS;
	
    // Build Control Panel
	oControlPanel = document.createElement("DIV");
	oControlPanel.id = "controlPanel";
	
	// Preload imagery
	oaMaps = new Array(iFileCount);
	var iLoop;
	for(iLoop = 0; iLoop < iFileCount; iLoop++)
	{
		oaMaps[iLoop] = document.createElement("IMG");
		oaMaps[iLoop].src = sPath + aFileName[iLoop];
		//oaMaps[iLoop].width = 1;
		//oaMaps[iLoop].height = 1;
		oControlPanel.appendChild(oaMaps[iLoop]);
	}
	
	
	// The form
	oCPform = document.createElement("FORM");
	oCPform.id = "controlform";
	oControlPanel.appendChild(oCPform);
	
	// The buttons
	oCPfirst = document.createElement("INPUT");
	oCPlast = document.createElement("INPUT");
	oCPprev = document.createElement("INPUT");
	oCPnext = document.createElement("INPUT");
	oCPfaster = document.createElement("INPUT");
	oCPslower = document.createElement("INPUT");
	oCPrun = document.createElement("INPUT");
	
	oCPfirst.type = "button";
	oCPlast.type = "button";
	oCPprev.type = "button";
	oCPnext.type = "button";
	oCPfaster.type = "button";
	oCPslower.type = "button";
	oCPrun.type = "button";
	
	// Unique IDs for all so the CSS can control the layout
	oCPfirst.id = "firstBtn";
	oCPlast.id = "lastBtn";
	oCPprev.id = "prevBtn";
	oCPnext.id = "nextBtn";
	oCPfaster.id = "fasterBtn";
	oCPslower.id = "slowerBtn";
	oCPrun.id = "runBtn";
	
	// Label the buttons
	oCPfirst.value = "<<";
	oCPlast.value = ">>";
	oCPprev.value = "<";
	oCPnext.value = ">";
	oCPfaster.value = "+";
	oCPslower.value = "-";
	oCPrun.value = "Start";
	
	// Add event handlers to the buttons
	oCPfirst.onclick = function() {first()};
	oCPlast.onclick = function() {last()};
	oCPprev.onclick = function() {prev()};
	oCPnext.onclick = function() {next()};
	oCPfaster.onclick = function() {faster()};
	oCPslower.onclick = function() {slower()};
	oCPrun.onclick = function() {run()};
	
	// Attach the buttons to the form
	oCPform.appendChild(oCPfirst);
	oCPform.appendChild(oCPprev);
	oCPform.appendChild(oCPrun);
	oCPform.appendChild(oCPnext);
	oCPform.appendChild(oCPlast);
	oCPform.appendChild(oCPslower);
	oCPform.appendChild(oCPfaster);
	
	// Insert Controls before the "Forest Of Links"
	var oContentDiv = document.getElementById("content");
	var oForestOfLinks = document.getElementById("mapChoices");
	oContentDiv.insertBefore(oControlPanel, oForestOfLinks);

}

function first()
{
	iCurrentIndex = 0;
	oMap.src = sPath + aFileName[iCurrentIndex];
}

function last()
{
	iCurrentIndex = iLimit;
	oMap.src = sPath + aFileName[iCurrentIndex];
}

function prev()
{
	if(iCurrentIndex > 0)
	{
		oMap.src = sPath + aFileName[--iCurrentIndex];
	}
}

function next()
{
	if(iCurrentIndex < iLimit)
	{
		oMap.src = sPath + aFileName[++iCurrentIndex];
	}
}

function faster()
{
	if(iFPS < 5)
	{
		iFPS++;
		iDelay = parseInt(1000 / iFPS);
	}
}

function slower()
{
	if(iFPS > 1)
	{
		iFPS--;
		iDelay = parseInt(1000 / iFPS);
	}
}

function run()
{
	if(oCPrun.value == "Stop")
	{
		oCPrun.value = "Start";
		oCPfirst.disabled = false;
		oCPlast.disabled = false;
		oCPprev.disabled = false;
		oCPnext.disabled = false;
	}
	else
	{
		oCPrun.value = "Stop";
		oCPfirst.disabled = true;
		oCPlast.disabled = true;
		oCPprev.disabled = true;
		oCPnext.disabled = true;
	}
	mainloop();
}

function mainloop()
{
	if(oCPrun.value == "Stop")
	{
		iCurrentIndex = (iCurrentIndex >= iLimit) ? 0 : (iCurrentIndex + 1);
		oMap.src = sPath + aFileName[iCurrentIndex];
		window.setTimeout("mainloop()", iDelay);
	}
}

// Use the addLoadEvent function found in menu.js
// to put the initialization routine into the
// onload event handler.
addLoadEvent(initializeAnimationControls);