/**
 * Buttons hover and click effects.
 * There are three functions here (near the end)
 * that are to be used on buttons.
 * hover() -  onmouseover.
 * down() - onmousedown.
 * up() - onmouseout.
 */

/*
 * Change imagePath as required below, or it can be overriden in a page.
 */
if (window.buttonImagePath == null) {
	var buttonImagePath = '/images/zurich_layout/';
}

/**
 * Show an error alert only on localhost.
 */
function showError(anError, optionalMsg) {
	if (window.location.hostname == 'localhost') {
    	var wholeMessage = optionalMsg ? optionalMsg : '';
    	wholeMessage += (anError.message ? anError.message : anError);
		alert(wholeMessage);
	}	
}

/**
 * Gets the current background image for an object
 * taking into account whether it was set with javascript Style object
 * or via CSS.
 */
function getBackgroundImage(obj) {
	try {
		var bgImage = obj.style.backgroundImage;
		if (bgImage && bgImage.length > 0) {
			return bgImage;
		} else {
			if (window.getComputedStyle) {
				// Mozilla etc.
				return window.getComputedStyle(obj, null).getPropertyValue('background-image');
			} else {
				// IE
				return obj.currentStyle.backgroundImage;
			}
		}
	} catch (anError) {
		showError(anError);
	}
}
/**
 * Replace the backgroundImage of an object with another one.
 * It is assumed that the current background image
 * has an _[0-9][0-9]* pattern in its bas filename.
 * The new image name will have this string appended to it
 * along with the filename extension of the current image.
 */
function replaceImage(obj, imageName) {
	try {
		var bgImage = getBackgroundImage(obj);
		if (bgImage) {
			var pathBits = bgImage.split('/');
			var basename = pathBits[pathBits.length - 1];
			var numberRe = /_[0-9][0-9]*/;
			var match = numberRe.exec(basename);
			if (match) {
				var number = match[0];
				var newImage = 'url(' + buttonImagePath + imageName + number + '.gif)';
				obj.style.backgroundImage = newImage;
			}
		}
	} catch (anError) {
		showError(anError);
	}
}

function hover(obj) {
	replaceImage(obj, 'buttonActivated');
}

function down(obj) {
	replaceImage(obj, 'buttonSelected');
}

function out(obj) {
	replaceImage(obj, 'button');
}