/*-----------------------------------
Determines browser
-----------------------------------*/
function Is() {
    var agent = navigator.userAgent.toLowerCase();
    this.major = parseInt(navigator.appVersion);
    this.NN  = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
    this.IE   = (agent.indexOf("msie") != -1);
    this.WIN = (agent.indexOf("win") != -1);
    this.IE5 = (this.IE && (agent.indexOf('5') != -1));
}

/*-----------------------------------
Inserts stylesheet reference depending on platform
-----------------------------------*/
function insertStyle() {
	if (is.WIN) {
		document.write('<link rel="stylesheet" href="../external/mapgallery.css">');
	} else {
		document.write('<link rel="stylesheet" href="../external/mapgalleryMAC.css">');
	}
}

/*-----------------------------------
global variable initialization
-----------------------------------*/
var is = new Is();

var swapArray = new Array();  //global that holds swap images info
var whichButton = null;

/*-----------------------------------
Called from the onLoad() inside the body tag 
Creates arrays for all images and references 
to the layers involved in the navigation     
-----------------------------------*/
function initialize() {
	parseLayers(document);
}

/*---------------------------------------
Called from initialize()
Automatically parse every layer in document,
determining which have swappable images, 
and (NS only) create references to every 
layer in the document
---------------------------------------*/
function parseLayers(str) {
	for (var i=0; i < str.images.length; i++) {
		if (str.images[i].name != "") {
			createImageObjects(str.images[i]);
		}
	}
	if (is.NN) {
		for (var i=0; i < str.layers.length; i++) {
		    var layRef = str.layers[i].name;
			parseLayers(str.layers[i].document);
		}
	}
}

/*---------------------------------------
Called from parseLayers()
Preloads and creates object references for swappable images
including _on state, _off state, and DOM image object path
---------------------------------------*/
function createImageObjects(imgObj) {
	var ftypeExp = /\.[^\.]*$/;   // regular expression used to split the filename string
	var srcString = imgObj.src;
	var extString = srcString.match(ftypeExp); // grab the extension
	var imgRef = imgObj.name;	 
	var divChar = srcString.lastIndexOf('/') + 1;    //finds the last '/' and records it's location
	var filePath = srcString.substring(divChar, 0);
	swapArray[imgRef] = new Object();
	swapArray[imgRef].on = new Image();
	swapArray[imgRef].on.src = filePath + imgRef + "_on" + extString;
	swapArray[imgRef].off = new Image();
	swapArray[imgRef].off.src = filePath + imgRef + "_off" + extString;
	swapArray[imgRef].press = new Image();
	swapArray[imgRef].press.src = filePath + imgRef + "_press" + extString;
	swapArray[imgRef].layerRef = imgObj;
}

/*---------------------------------------
Called from the <a href> tag      
Swap image function for rollovers 
---------------------------------------*/
function swap(imgName, onoff) {
	if (whichButton == imgName) {
		if (onoff == 'on') {
			swapArray[imgName].layerRef.src = swapArray[imgName][onoff].src;
		} else {
			swapArray[imgName].layerRef.src = swapArray[imgName]['press'].src;
		}
	} else {
		swapArray[imgName].layerRef.src = swapArray[imgName][onoff].src;
	}
}

function goButton(headURL, contentURL, imgName) {
	if (whichButton != null) {
		swapArray[whichButton].layerRef.src = swapArray[whichButton]['off'].src;
	}
	swap(imgName, 'press');
	parent.header.location.href = headURL;
	parent.content.location.href = contentURL;
	whichButton = imgName;
}