/*** Javascript utility functions ***/

var gCachedImg = new Array();

/* generate html with image roll over effect
 *  sImgName   - (required) name of the image
 *  sAltText   - (optional) Alt text to display when image not available
 *  sImgOnSrc  - (required) path of the image to display when mouse over
 *  sImgOffSrc - (required) path of the default display image or mouse out
 *  sScriptCmd - (optional) javascript command to perform when mouse clicked
 *  sLink      - (optional) Url to go to when mouse clicked
 *  sTarget    - (optional) target location to open the url on
 */
function createRollOverImage(sImgName, sAltText, sImgOnSrc, sImgOffSrc, sScriptCmd, sLink, sTarget) {
	var oImgOn, oImgOff, sWidth, sHeight;
	var html, href, onClick, onMouseOver, onMouseOut;
		
	// preload images
	oImgOn  = preloadImg(sImgOnSrc);
	oImgOff = preloadImg(sImgOffSrc);
		
	// get Image properties
	sWidth  = (oImgOff.width)?  " width=\""  + oImgOff.width  + "\"" : "";
	sHeight = (oImgOff.height)? " height=\"" + oImgOff.height + "\"" : "";
	sAlt    = (sAltText)?       " alt=\""    + sAltText       + "\"" : "";
			
	// determine tag attributes based on parameters passed
	href = "";
	if ( sLink ) {
		href = " href=\"" + sLink + "\"";
		href += (sTarget)? " target=\"" + sTarget + "\"" : "";
	}
	onClick = ( sScriptCmd )? " onClick=\"" + sScriptCmd + "\"" : "";
	onMouseOver = ( sImgOnSrc  )? " onMouseOver=\"hoverImage('" + sImgName + "', 1);\"" : "";
	onMouseOut  = ( sImgOffSrc )? " onMouseOut =\"hoverImage('" + sImgName + "', 0);\"" : "";
		
	// put them together
	html  = "<a " + href + onMouseOver + onMouseOut + onClick + ">";
	html += "<image name=\"" + sImgName + "\" border=\"0\"" + sWidth + sHeight + sAlt + ">";
	html += "</a>";

	// first write the html
	document.writeln(html);
		
	// now set preloaded image sources
	var oImg = document.images[sImgName];
		
	if ( oImg ) {
		oImg.src = oImgOff.src;
		oImg.imageOff = oImgOff;
		oImg.imageOn = oImgOn;
	}
}

// flip flop images
function hoverImage(sName, nState) {
	var oImg = document.images[sName];

	if ( oImg ) {
		oImg.src = (nState)? oImg.imageOn.src : oImg.imageOff.src;
	}
}

// preload image
function preloadImg(sSrc) {

	var oImg = null;
	
	for (var i=0; i<gCachedImg.length; i++) {
		if (gCachedImg[i] && gCachedImg[i].path == sSrc) {
			oImg = gCachedImg[i];
		}
	}
	
	if ( !oImg ) {
		oImg = new Image();
		oImg.src = sSrc;
		oImg.path = sSrc;
		gCachedImg[gCachedImg.length] = oImg;
	}

	return oImg;
}
