// library to do popup layers
// by Thomas M. Farrell

//sniff browser version
function Is() {
	var agent = navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.ns  = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
	this.ns4 = (this.ns && (this.major >= 4));
	this.ie   = (agent.indexOf("msie") != -1);
	this.ie4  = (this.ie && (this.major >= 4));
	} //end function

//create object for sniffer
var is = new Is()

//this sets up some browser-specific code so we can abstract the layers.
var doc = "";
var sty = "";
var htm = "";
if(is.ns4) {
	doc = "document";
	sty = "";
	htm = ".document"
	}//end if
else if(is.ie4) {
	doc = "document.all";
	sty = ".style";
	htm = ""
	}//end else

//the following array will hold abstract references to the layers. 
var layers = new Array();

//the following function sets up the abstract reference to a particular layer.
function makeAbstract(layerName) {
	var foo = null;
	foo = eval(doc + '["' + layerName + '"]' + sty);
	return foo;
	} //end function

//the following function sets up an abstract reference to the *content* of a layer.
function makeContentAbstract(layerName) {
	var foo = null;
	foo = eval(doc + '["' + layerName + '"]' + htm);
	return foo;
	} //end function

//the following function allows you to replace the content of a layer.
function layerContent(layerContentHandle,newContent) {
	if(is.ns4) {
		layerContentHandle.write(newContent);
		layerContentHandle.close();
		} 
	else {
		layerContentHandle.innerHTML = newContent;
		}
	} //end function
	
//the following function makes the specified layer appear.
function showLayer(foo) {
	foo.visibility = "visible";
	} //end function

//the following function makes all layers in the layers array appear
function showAllLayers() {
	showAllLayersSelect(layers);
	} //end function

//the following function shows all layers in the specified array
function showAllLayersSelect(theLayers) {
	for (var x=0; x<theLayers.length; x++) {
		showLayer(theLayers[x]);
		} //end for loop
	} //end function

//the following function makes the specified layer disappear.
function hideLayer(foo) {
	foo.visibility = "hidden";
	} //end function

//the following function makes all layers in the layers array disappear.
function hideAllLayers() {
	hideAllLayersSelect(layers);
	} //end function

//the following functions makes all layers in the specified array disappear.
function hideAllLayersSelect(theLayers) {
	for (var x=0; x<theLayers.length; x++) {
		hideLayer(theLayers[x]);
		} //end for loop
	} //end function

//the following function positions a layer to a specified X and Y.
function positionLayer(theLayer,x,y) {
	theLayer.top = y;
	theLayer.left = x;
	} //end function

//the following function sizes a layer to a specified width and height.
function sizeLayer(theLayer,w,h) {
	theLayer.width = w;
	theLayer.height = h;
	} //end fucntion

//the following function takes the name of a layer and adds it to the end of the layers array.
function collectLayer(layerName) {
	layers[layers.length] = makeAbstract(layerName);
	} //end function

//the following function takes a general name for a set of layers and the number of the largest
//and puts them all in the layers array. Note: they must be numbered starting at zero!
//the prefix is what comes before the number on all of the layers. 
//the suffix is what comes after the number on all of the layers.
function collectAllLayers(prefix,suffix,maxNumber) {
	for (var x=0; x<=maxNumber; x++) {
		collectLayer(prefix + x + suffix);
		} //end for loop
	} //end function

//the following function takes a general name for a set of layers and the number of the largest
//and puts them all in an array and returns it. Note: they must be numbered starting at zero!
//the prefix is what comes before the number on all of the layers. 
//the suffix is what comes after the number on all of the layers.
function returnAllLayers(prefix,suffix,maxNumber) {
	var tempLayers = [];
	for (var x=0; x<=maxNumber; x++) {
		tempLayers[x] = makeAbstract(prefix + x + suffix);
		} //end for loop
	return tempLayers;
	} //end function

//the following function makes only the numbered layer visible of the array.
function oneLayer(foo) {
	if (layers[foo]!=null) {
		hideAllLayers();
		showLayer(layers[foo]);
		} //end if
	} //end function

//the following function makes only the numbered layer from the specified array
//of layers visible.
function oneLayerSelect(theLayers,layerNumber) {
	if (theLayers[layerNumber]!=null) {
		hideAllLayersSelect(theLayers);
		showLayer(theLayers[layerNumber]);
		} //end if
	} //end function

//the following variables are for maintaining the previous and next buttons.
var currentScreen = 0;
var maxScreen = 0;

//the following should be set to true if there is a separator line between the
//previous and next buttons that you want to appear only when both are visible.
var prevNextSeparator = false;
	
//the following makes only the "next" or "previous" layers appear as appropriate.
function manageControls() {
	var prev = makeAbstract("pr");
	var nex = makeAbstract("ne");
	if (currentScreen==0) {
		hideLayer(prev);
		} //end if
	else {
		showLayer(prev);
		} //end else
	if (currentScreen==maxScreen) {
		hideLayer(nex);
		} //end if
	else {
		showLayer(nex);
		} //end else
	if (prevNextSeparator) {
		if ((currentScreen<maxScreen) && (currentScreen>0)) {
			showLayer(makeAbstract("controlsLine"));
			} //end if
		else {
			hideLayer(makeAbstract("controlsLine"));
			} //end else
		} //end if
	} //end function

//the following makes the next screen appear.
function nextScreen() {
	currentScreen += 1;
	oneLayer(currentScreen);
	manageControls();
	} //end function

//the following makes the previous screen appear.
function previousScreen() {
	currentScreen -= 1;
	oneLayer(currentScreen);
	manageControls();
	} //end function

//The following function makes only the "next" or "previous" layers for a 
//given set appear as appropriate.
//
//This function is not fully optimized for speed and is a good candidate for 
//revision at a later date.
function manageControlsSelect(prefix,showLine) {
	var prev = makeAbstract(prefix + "Pr");
	var nex = makeAbstract(prefix + "Ne");
	if (getCurrentScreenSelect(prefix)==0) {
		hideLayer(prev);
		} //end if
	else {
		showLayer(prev);
		} //end else
	if (getCurrentScreenSelect(prefix)==getMaxScreenSelect(prefix)) {
		hideLayer(nex);
		} //end if
	else {
		showLayer(nex);
		} //end else
	if (showLine) {
		if ((getCurrentScreenSelect(prefix)<getMaxScreenSelect(prefix)) && (getCurrentScreenSelect(prefix)>0)) {
			showLayer(makeAbstract(prefix + "ControlsLine"));
			} //end if
		else {
			hideLayer(makeAbstract(prefix + "ControlsLine"));
			} //end else
		} //end if
	} //end function

//the following makes the next screen of the specified set appear.
//
//This function is not fully optimized for speed and is a good candidate for 
//revision at a later date.
function nextScreenSelect(prefix,showLine) {
	incrementCurrentScreenSelect(prefix);
	//you need to put in the other parameter for onelayerselect.
	oneLayerSelect(returnAllLayers(prefix,"",getMaxScreenSelect(prefix)),getCurrentScreenSelect(prefix));
	manageControlsSelect(prefix,showLine);
	} //end function

//the following makes the previous screen of the specified set appear.
//
//This function is not fully optimized for speed and is a good candidate for 
//revision at a later date.
function previousScreenSelect(prefix,showLine) {
	decrementCurrentScreenSelect(prefix);
	oneLayerSelect(returnAllLayers(prefix,"",getMaxScreenSelect(prefix)),getCurrentScreenSelect(prefix));
	manageControlsSelect(prefix,showLine);
	} //end function

// the following variables are used internally by the manageControlsSelect()
// function and should be ignored by the developer.
//
// currentScreenArray contains all the currentScreen values for the various sets of screens.
// maxScreenArray contains all the maxScreen values for the various sets of screens.
// If you don't know what that means, please leave the arrays alone.
var currentScreenArray = [];
var maxScreenArray = [];

// The following function is used internally by the nextScreenSelect() and
// previousScreenSelect() functions and is not intended for use by the developer.
//
// The function returns the currentScreen valyue for a given set of screens.
function getCurrentScreenSelect(prefix) {
	for (var x=0; x<currentScreenArray.length; x++) {
		if (currentScreenArray[x][0]==prefix) {
			return currentScreenArray[x][1];
			} //end if
		} //end for loop
	return 0;
	} //end function

// The following function sets the currentScreen valyue for a given set of screens.
// Make sure to initialize it to zero before you call manageControlsSelect() !!!
function setCurrentScreenSelect(prefix,value) {
	var didit = false;
	for (var x=0; x<currentScreenArray.length; x++) {
		if (currentScreenArray[x][0]==prefix) {
			currentScreenArray[x][1] = value;
			didit = true;
			} //end if
		} //end for loop
	if (!didit) {
		var tempArray = []
		tempArray[0] = prefix;
		tempArray[1] = value;
		currentScreenArray[currentScreenArray.length] = tempArray;
		} //end if
	} //end function

// the following function is used internally by the manageControlsSelect()
// function and should be ignored by the developer.
//
// the function returns the maxScreen value for a given set of screens.
function getMaxScreenSelect(prefix) {
	for (var x=0; x<currentScreenArray.length; x++) {
		if (currentScreenArray[x][0]==prefix) {
			return maxScreenArray[x][1];
			} //end if
		} //end for loop
	return 0;
	} //end function

// The following function sets the maxScreen value for a given set of screens.
// Its parameters are the string prefix for the set and the integer value.
function setMaxScreenSelect(prefix,value) {
	var didit = false;
	for (var x=0; x<maxScreenArray.length; x++) {
		if (maxScreenArray[x][0]==prefix) {
			maxScreenArray[x][1] = value;
			didit = true;
			} //end if
		} //end for loop
	if (!didit) {
		var tempArray = []
		tempArray[0] = prefix;
		tempArray[1] = value;
		maxScreenArray[maxScreenArray.length] = tempArray;
		} //end if
	} //end function

// The following function increments the current screen for a given set.
// It is not intended for use by the developer.
function incrementCurrentScreenSelect(prefix) {
	var tempvar = getCurrentScreenSelect(prefix);
	setCurrentScreenSelect(prefix,tempvar + 1);
	} //end function

// The following function decrements the current screen for a given set.
// It is not intended for use by the developer.
function decrementCurrentScreenSelect(prefix) {
	var tempvar = getCurrentScreenSelect(prefix);
	setCurrentScreenSelect(prefix,tempvar - 1);
	} //end function
