/*-----------------------------------
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));
}

/*-----------------------------------
global variable initialization
-----------------------------------*/
var is = new Is();

var swapArray = new Array();  //global that holds swap images info
var selectedImage = "";


/*------------------------------------------------
Pop up window function for the links to mapmuseum
------------------------------------------------*/

function pop(URL, name) {
  bWindow = window.open(URL, name,"status=0,menubar=0,width=600,height=542");
}

/*-----------------------------------
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);
	//selectedImage = top.selectedImage;
	//onClickSwap(selectedImage);
}

//================================================                  
//= Determines layer ID depending on the browser
//================================================
function getLayerRef(layerID) {
	if (is.NN) {
		return document.layers[layerID];
	} else {
		return document.all[layerID];
	}
}

/*---------------------------------------
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;
			layerArray[layRef] = new Object();
			layerArray[layRef].layerRef = str.layers[layRef];
			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)[0]; // grab the extension
	var imgRef = imgObj.name;	 
	var divChar = srcString.lastIndexOf('/') + 1;    //finds the last '/' and records it's location
	var filePath = srcString.substring(0, divChar);
	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].layerRef = imgObj;
}

/*---------------------------------------
Called from the <a href> tag      
Swap image function for rollovers 
---------------------------------------*/
function swap(imgName, onoff) {
	if (swapArray[imgName] != null) {
		swapArray[imgName].layerRef.src = swapArray[imgName][onoff].src;
	}
}


function onClickSwap(imgName) {
	if(selectedImage != imgName) {
		swap(imgName, 'on');
		if(selectedImage != '') {
			swap(selectedImage, 'off');
		}
		selectedImage = imgName;
		return true;
	} else {
		swap(imgName, 'on');
		return false;
	}
}

// called from onMouseOver and onMouseOut
function mouseSwap(imgName, onoff) {
	if(selectedImage != imgName) {
		swap(imgName, onoff);
	}
}
