/*
	MultipleStateButton class
	
	last updated 2007-11-24
*/

function MultipleStateButton(buttonLinkElement,stateNames,rolloverSuffix,actionFunction,deactivatedSuffix,beginDeactivated) {

	// member variables
	this.buttonLinkElement = buttonLinkElement;
	this.stateNames = stateNames;
	this.frame1Suffix = (rolloverSuffix ? rolloverSuffix : "_f2");
	this.frame2Suffix = (deactivatedSuffix ? deactivatedSuffix : null);
	this.actionFunction = actionFunction;
	this.imageFrames = [];

	// assign this object to a new property of the link element
	this.buttonLinkElement.multipleStateButton = this;
	
	// methods
	this.getImageName = function() {
		if (this.usesImage) {
			var src = this.imageElement.src;
			var lastSlash = src.lastIndexOf("/");
			lastSlash = (lastSlash>=0 ? lastSlash+1 : 0);
			var lastDot = src.lastIndexOf(".");
			lastDot = (lastDot>=0 ? lastDot : src.length);
			return(src.substring(lastSlash,lastDot));
		} else return(null);
	}
	this.getImageExtension = function() {
		if (this.usesImage) {
			var src = this.imageElement.src;
			var lastDot = src.lastIndexOf(".");
			lastDot = (lastDot>=0 ? lastDot+1 : 0);
			return(src.substring(lastDot,src.length));
		} else return(null);
	}
	this.setState = function(newState) {
		if (this.usesImage && newState) {
			if (this.imageName && this.imageExtension) {
				this.imageName = newState;
				this.preloadImages();
				this.imageElement.src = this.imageFrames[0].src;
			}
		}
	}
	this.activate = function() {
		this.buttonLinkElement.disabled = false;
		if (this.usesImage) {
			if (this.imageName && this.imageExtension) {
				if (this.imageFrames[0] && this.imageFrames[0].complete) {
					this.imageElement.src = this.imageFrames[0].src;
				}
			}
		}
	}
	this.deactivate = function() {
		this.buttonLinkElement.disabled = true;
		if (this.usesImage) {
			if (this.imageName && this.imageExtension && this.imageFrames[2]) {
				this.imageElement.src = this.imageFrames[2].src;
			}
		}
	}
	this.preloadImages = function() {
		this.imageFrames[0] = new Image();
		this.imageFrames[0].src = this.imagePath+this.imageName+"."+this.imageExtension;
		this.imageFrames[1] = new Image();
		this.imageFrames[1].src = this.imagePath+this.imageName+this.frame1Suffix+"."+this.imageExtension;
		if (this.frame2Suffix) {
			this.imageFrames[2] = new Image();
			this.imageFrames[2].src = this.imagePath+this.imageName+this.frame2Suffix+"."+this.imageExtension;
		}
	}
	this.clearRectangle = function() {
		this.buttonLinkElement.blur();
	}

	// constructor

	// find child image inside of link
	this.usesImage = false;
	if (buttonLinkElement.children.length) {
		var childElement = buttonLinkElement.children[0];
		if (childElement.tagName.toLowerCase()=="img") {
			this.usesImage = true;
			this.imageElement = childElement;
		}
	} else {
		this.usesImage = true;
		this.imageElement = buttonLinkElement;
	}
	if (this.usesImage) {
		this.imagePath = this.imageElement.src;
		var lastSlash = this.imagePath.lastIndexOf("/");
		if (lastSlash>0) this.imagePath = this.imagePath.substring(0,lastSlash+1);
		this.imageName = this.getImageName();
		this.imageExtension = this.getImageExtension();
		this.preloadImages();
	}
	if (beginDeactivated) this.deactivate();
	
	// initialize event handlers for button link
	if (actionFunction) {
		this.buttonLinkElement.onclick = function() {
			// note: 'this' is now the button's surrounding link element
			if (this.multipleStateButton && !this.disabled) {
				var button = this.multipleStateButton;
				button.clearRectangle();
				if (button.actionFunction) {
					// use return value of actionFunction to set the appropriate button state
					button.setState(button.actionFunction());
					return(false);
				} else return(true);
			}
		}
	}
	this.buttonLinkElement.onmouseover = function() {
		// note: context for 'this' is now the button's surrounding link element
		if (this.multipleStateButton && !this.disabled) {
			var button = this.multipleStateButton;
			if (button.usesImage) {
				if (button.imageName && button.imageExtension) {
					if (button.imageFrames[1] && button.imageFrames[1].complete) {
						button.imageElement.src = button.imageFrames[1].src;
					}
				}
			}
		}
	}
	this.buttonLinkElement.onmouseout = function() {
		// note: context for 'this' is now the button's surrounding link element
		if (this.multipleStateButton && !this.disabled) {
			var button = this.multipleStateButton;
			if (button.usesImage) {
				if (button.imageName && button.imageExtension) {
					if (button.imageFrames[0] && button.imageFrames[0].complete) {
						button.imageElement.src = button.imageFrames[0].src
					}
				}
			}
		}
	}
	
}
