//------------------------------------------------------------------------------------
// Image Rollover Code
//------------------------------------------------------------------------------------
/*
File: swapScript.js
Author: Sean O'Neill (sean@marsworks.com)
Company: MARSWorks Inc.
Date: October 9, 2003
Purpose: code to create rollover images in site navigation
Usage:
	NEW USAGE
	name the image and the anchor the same, then call the constructor for each image
		QUICK USAGE:
			arrImages.push(new objRollover(strImageName));
			assumes strImageName to be the image and anchor names, and both actual images to be named strImageName1 and strImageName2
			and uses the default path and default file extension
		DETAILED USAGE:
			arrImages.push(new objRollover(strImageName, strImagePath, strImageFilename1, strImageFilename2, strFileExtension));
			specify all variables...can be useful for other rollover groups like side navigation or in page rollovers

	OLD USAGE
	in the <a> tag of a navigation include, include these event handlers
	onMouseOver="swapImage('XXX');"
	onMouseOut="turnOff();"
	
	XXX should be the image "name" and the files should be called XXX1.gif and XXX2.gif
	
Variables to customize:
	imgRoot - make this the absolute path to the images to rollover
	arrImages - a string array of the image names

Update (v002): April 7, 2004
	tried to make the script actually "sniff" out the images and create the event handlers itself
	the update was successful...however the system doesn't work for Netscape 4.x...it doesn't seem to be able to
	dynamically add event handlers after loading for some reason
	
Update (v003): April 7, 2004
	added a new object that could take arguments for name, path, and three images for the three main states
	this object defaults to operating with only one argument to make it easy, but allows for multiple sets
	of rollovers to be added on a page with ease
	
Update (v004): April 7, 2004
	tried to put the preloading right into the object constructor...
*/

// variable setup - don't customize
var currentImage = null;
var swap = (document.images)?true:false;
var isLoaded = false;
var charLFlag = (document.location.href.indexOf('/fr/') == -1)?'':'-f';
// variables to customize
var strDefaultPath = "/images/buttons/"; // strDefaultPath is the path to the images for rollovers


var arrImages = new Array();

// create an object to hold the image name, its path and filenames
function objRollover(strImageName, strImagePath, strImageFilename1, strImageFilename2) {
	this.imageName = strImageName;
	this.imagePath = (strImagePath)?strImagePath:strDefaultPath;
	this.image1 = new Image();
	this.image1.src = this.imagePath + ((strImageFilename1)?strImageFilename1:this.imageName + '1' + '_' + charLanguage + '.' + ((strFileExtension)?strFileExtension:strDefaultFileExtension));
	this.image2 = new Image();
	this.image2.src = this.imagePath + ((strImageFilename2)?strImageFilename2:this.imageName + '2' + '_' + charLanguage + '.' + ((strFileExtension)?strFileExtension:strDefaultFileExtension))

	// add the event handlers to the anchor
	// try adding to the parentNode
	var loopLimit = 10;
	var loopCounter = 0;
	var nodeImgParent = document.images[strImageName].parentNode;
	if (nodeImgParent) {
		while (nodeImgParent.tagName.toLowerCase() != 'a' && loopLimit < loopCounter) {
			loopCounter++;
			nodeImgParent = nodeImgParent.parentNode;
		}
		if (nodeImgParent.tagName.toLowerCase() == 'a') {
			nodeImgParent.onmouseover = swapImage;
			nodeImgParent.onmouseout = turnOff;
			nodeImgParent.ptrArrImages = this;
		}
	}
}

var arrMenuOffset = new Array();

if (charLFlag == '') {
	arrMenuOffset['menu_accommodations'] = 0;
	arrMenuOffset['menu_dining'] = -60;
	arrMenuOffset['menu_reservations'] = -30;
	arrMenuOffset['menu_meetings'] = -90;
	arrMenuOffset['menu_spa'] = -105;
	arrMenuOffset['menu_golf'] = -175;
	arrMenuOffset['menu_rec'] = -50;
	arrMenuOffset['menu_about'] = -562;
	arrMenuOffset['menu_reach'] = -284;
}
else {
	arrMenuOffset['menu_accommodations'] = 0;
	arrMenuOffset['menu_dining'] = -60;
	arrMenuOffset['menu_reservations'] = -30;
	arrMenuOffset['menu_meetings'] = -50;
	arrMenuOffset['menu_spa'] = -105;
	arrMenuOffset['menu_golf'] = -175;
	arrMenuOffset['menu_rec'] = -175;
	arrMenuOffset['menu_about'] = -562;
	arrMenuOffset['menu_reach'] = -238;
}

function swapImage() {
	if (swap && isLoaded) {
		document.images[this.ptrArrImages.imageName].src = this.ptrArrImages.image2.src;
		currentImage = this.ptrArrImages;
		
		// added in to include popup menu of the same name (without the roll_)
		if (this.ptrArrImages.imageName.indexOf('roll_menu_') != -1) {
			//alert(document.images[this.ptrArrImages.imageName].src);
			popup(this.ptrArrImages.imageName.split('roll_')[1], this.ptrArrImages.imageName, 0, arrMenuOffset[this.ptrArrImages.imageName.split('roll_')[1]]);
		}
	}
}

function turnOff() {
	if (swap && isLoaded && currentImage) {
		document.images[currentImage.imageName].src = currentImage.image1.src;
		currentImage = null;
		
		// added in to include popup menu of the same name (without the roll_)
		if (this.ptrArrImages.imageName.indexOf('roll_menu_') != -1) {
			popdown();
		}
	}
}

function initSwap() {
	isLoaded = true;

	if (swap) {
		// try preprocessing the page and looking for images named "roll_anything"
		for (var i = 0; i < document.images.length; i++) {
			if (document.images[i].name.indexOf('roll_') != -1) {
				// we have an image with this name...create the object accordingly
				strImagePath = document.images[i].src;
				strImageFilename1 = strImagePath.substring(strImagePath.lastIndexOf('/') + 1, strImagePath.length);
				strImageFilename2 = strImageFilename1;
				if (strImageFilename2.indexOf('1') != -1) {
					strImageFilename2 = strImageFilename2.replace(/([^1])1(.*)/, "$12$2");
				}
				else {
					strImageFilename2 = strImageFilename2.replace(/([^2])2(.*)/, "$11$2");
				}
	
				strImagePath = strImagePath.substring(0, strImagePath.lastIndexOf('/') + 1);
				arrImages.push(new objRollover(document.images[i].name, strImagePath, strImageFilename1, strImageFilename2));
			}
		}
		
	}
}

// try to set the onLoad event handler
//window.onload = initSwap;