// ======================================================
// GLOBALS AND CONSTANTS
// ======================================================

ZoomPopup = new Object();
ZoomPopup.chrome = 'channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no';
ZoomPopup.chromewidth = 10;
ZoomPopup.chromeheight = 25;
ZoomPopup.width = 750;
ZoomPopup.height = 472;
ZoomPopup.name = 'Zoom';

POPUP_HANDLE = null;

// ======================================================
// globalafterBodyLoads()
// ======================================================

function globalafterBodyLoads()
{
var queryString = location.search.substring(1);
if (queryString.match(/end/))
	{
	while (currentStep < numSteps) { incrementStep(); }
	}
}

// ======================================================
// function LightButton(ImageObject)
// ======================================================
// LightButton and UnlightButton are picked up from eBook programming; 
// they take a name ("grade5"), and swap in or out an image based on the
// name. It presupposes that all rollovers will be in graphics/buttons/
// and be named as follows: NAME.gif, NAME-lit.gif, and (if applicable) 
// NAME-disabled.gif .


function LightButton(ImageObject)
{
if (ImageObject.src.indexOf("-disabled") < 0) 
	{
	document[ImageObject.name].src = "graphics/buttons/" + ImageObject.name + "-lit.gif";
	}
}

function UnlightButton(ImageObject)
{
if (ImageObject.src.indexOf("-disabled") < 0) 
	{
	document[ImageObject.name].src = "graphics/buttons/" + ImageObject.name + ".gif";
	}
}

// ======================================================
// function preloadimages(imagestoload)
// ======================================================
// preloadimages is a standard image-loader, taking URLs (relative to this JS) 
// and loading them. preloadbuttons taken a list of buttons by name, and converts the 
// names into full URLs for preloadimages. Every HTML page has a section for such 
// loading, eg., preloadbuttons("tour-k","tour-2","tour-5")

function preloadimages(imagestoload)
{
if (!preloadimages.arguments.length) { return false; }
imagestoloadArray = new Array();
for (var i=0;i<imagestoload.length; i++)
	{
	imagestoloadArray[i]=new Image();
	imagestoloadArray[i].src=imagestoload[i];
	}
}

// ======================================================
// function preloadbuttons()
// ======================================================
// preloadbuttons is triggered by every page. It loads the "lit" state of every 
// named graphic. For example, if you have a graphic like this:
//   <img src="graphics/buttons/lb-about.gif" name="lb-about" width="56" height="25" 
//   alt="About" onmouseover="LightButton(this)" onmouseout="UnlightButton(this)">
// Load buttons sees the name (lb-about) and loads graphics/buttons/lb-about-lit.gif
// to prepare for a mouse-over. 
	
function preloadbuttons()
{
buttonstopreload = new Array();
var buttonnum = 0;
for (var i=0; i < allImgTags.length; i++)
	{
	if (allImgTags[i].name != "")
		{ 
		buttonstopreload[buttonnum]=new Image();
		buttonstopreload[buttonnum].src = "graphics/buttons/" + allImgTags[i].name + "-lit.gif"
		buttonnum++;
		}
	}
}

// ======================================================
// STEP FUNCTIONS
// ======================================================

// determines whether a given class occurs on the HTML page
function classOnPage(nameOfClass)
{
var regex = "\\b" + nameOfClass + "\\b";
for (var i=0; i < allDivTags.length; i++)
	{
	if (allDivTags[i].className.match(regex)) { return true; }
	}
return false;
}

// counts the number of step classes on the page, eg., class="step1"

//This is the old way of doing it; bring back if step theory changes.
//function howManySteps()
//{
//var stepCount = 0;
//while ( classOnPage( "step"+(stepCount+1) ) ) { stepCount++; }
//return stepCount;
//}

function howManySteps()
{
var highestcount = 0;
var regex = "\\bstep([0-9])\\b";
for (var i=0; i < allDivTags.length; i++)
	{
	var result = allDivTags[i].className.match(regex);
	if (result != null)
		{
		if (result[1] > highestcount) { highestcount = result[1] };
		}
	}
return highestcount;
}

// shows a given step
// remove kludge when (if) you solve the refresh problem
function showStep(stepNum)
{
var regex = "\\b" + "step" + stepNum + "\\b";
var kludge = "\\bcontent\\b";
for (var i=0; i < allDivTags.length; i++)
	{
	if (allDivTags[i].className.match(regex)) { allDivTags[i].style.display = "block"; }
	if (allDivTags[i].className.match(kludge)) { allDivTags[i].style.display = "block"; }
	}
}

// hides a given step
// remove kludge when (if) you solve the refresh problem
function hideStep(stepNum)
{
var regex = "\\b" + "step" + stepNum + "\\b";
var kludge = "\\bcontent\\b";
for (var i=0; i < allDivTags.length; i++)
	{
	if (allDivTags[i].className.match(regex)) { allDivTags[i].style.display = "none"; }
	if (allDivTags[i].className.match(kludge)) { allDivTags[i].style.display = "none"; }
	}
}

// hide all steps
function hideAllSteps()
{
var regex = "\\b" + "step\\d+\\b"; //revise to satisfy just class="steps"?
for (var i=0; i < allDivTags.length; i++)
	{
	if (allDivTags[i].className.match(regex)) { allDivTags[i].style.display = "none"; }
	}
}

// go up a step
function incrementStep()
{
closeAllCallouts(); //remove once bug is fixed
if (currentStep < numSteps)
	{
	hideStep(currentStep);
	currentStep++;
	showStep(currentStep);
	return false;
	}
else
	{
	if (stepsGoRoundRight) // if at end and "stepsGoRoundRight" is true, restart.
		{
		hideAllSteps();
		currentStep = 1;
		showStep(currentStep);
		}
	else // if "stepsGoRoundRight" is false, allow <a href="xxx"> link to happen
		{
		return true;
		}
	}
return false;
}

// go down a step
function decrementStep()
{
closeAllCallouts(); //remove once bug is fixed
if (currentStep > 1)
	{
	hideStep(currentStep);
	currentStep--;
	showStep(currentStep);
	}
else
	{
	if (stepsGoRoundLeft) // if at beginning and "stepsGoRoundLeft" is true, count up until all steps are shown.
		{
		hideAllSteps();
		while (currentStep < numSteps) { incrementStep(); }
		}
	else
		{
		return true; // if "stepsGoRoundLeft" is false, allow <a href="xxx"> link to happen
		}
	}
return false;
}

// ======================================================
// CALLOUT FUNCTIONS
// ======================================================
// Not currently (11/12/04) used, but called-for in specs.

//open callout; takes the callout number (callout 1 has an id of callout-1)
function openCallout(id, closeOthers)
{
if (closeOthers) { closeAllCallouts(); }
var idname = "callout-" + id;
element = document.getElementById(idname);
element.style.display = 'block';
}

//close callout; takes the callout number (callout 1 has an id of callout-1)
function closeCallout(id)
{
var idname = "callout-" + id;
element = document.getElementById(idname);
element.style.display = 'none';
}

//close all callouts
function closeAllCallouts()
{
var id = 1;
while (1)
	{
	var idname = "callout-" + id;
	element = document.getElementById(idname);
	if (element == null) {break;}
	element.style.display = 'none';
	id++;
	}
}

// ======================================================
// POPUP FUNCTIONS
// ======================================================
// 11/12/04 - Using this simple code. If specs are raised, eBook code
// might be better.

function popupResource(PopupObject, URL)
{
var WindowWidth = PopupObject.width + PopupObject.chromewidth;
var WindowHeight = PopupObject.height + PopupObject.chromeheight;

// center window on screen, taking into account additional height for chrome
var left = Math.ceil((screen.availWidth/2) - (WindowWidth/2));
var top = Math.ceil((screen.availHeight/2) - (WindowHeight/2));

var windowProperties = "width=" + WindowWidth + ",height=" + WindowHeight;
windowProperties += ",top=" + top + ",left=" + left;
windowProperties += "," + PopupObject.chrome;

POPUP_HANDLE = window.open(URL, PopupObject.name, windowProperties);
POPUP_HANDLE.focus()
}

function popupZoom(filename)
{
if (filename == "") { filename = window.location.href;}
filename = filename.replace(/^.*\//, ""); //remove folders
filename = filename.replace(/\..*/, ""); //remove .html, etc.

if (filename.match(/-lr/)) 
	{ filename = filename + '.jpg'}
else
	{ filename = filename + '.gif'}

var url = "helpers/zoom.html?" + filename;
popupResource(ZoomPopup, url);
return false;
}
