/**
 * Oregon Chapter Slideshow
 * 
 * Supports manually controlled image display on the Oregon Chapter main page (index.asp).
 * Copyright 2009 James Maskus All rights reserved
 *
 * @author James Maskus
 */

// Slideshow image object cache (global)
var imageObj = new Array();

/**
 * Caches all of the slideshow images on document ready for quick swapping
 * Note: (imageMap) must be declared in the html source file
 */
function cacheImages(path) {
   var i = 0;
	 
	 for (var key in imageMap) {
			imageObj[i] = new Image();
			imageObj[i].src = path + key;
			imageObj[i].alt = imageMap[key];
			i++;
	 }
}

/**
 * Slideshow image swapping function uses callback for timing control
 */
function showPict(e,node) {
	 var evt = (e) ? e : event;
   var speed = "normal";
	 var i = parseInt(node.firstChild.data);
	 
	 // easter egg for last image
	 if (evt.altKey || evt.metaKey) {
		 evt.stopPropagation();          // stop browser modifer key side effects 
		 i = getSize(imageMap);
   }
	 
   // fade out the existing image
   jQuery('#photo').fadeOut(speed, function () { showPictCallback(--i, speed); } );
}

/**
 * Slideshow callback function sets the image source & caption text
 * then fades in the new image
 */
function showPictCallback(i, speed) {
   var delay = 200;		// pause between fadeOut/fadeIn

   // set the new img source & caption text
   jQuery('#photo > img').attr( { src: imageObj[i].src } );
   jQuery('#photo > p').text( imageObj[i].alt );
	 
	 // fade in the new image
   setTimeout(("jQuery('#photo').fadeIn('"+speed+"')"),delay);
}

/**
 * Generates a ranged random number within low/high inclusive
 *
 * @returns random value as an integer
 */
function rangedRandom(low,high) {
   var randValue = ( Math.floor( Math.random() * (high - low + 1) ) + low );

   return randValue;
}

/**
 * Utility function gets the matching key for a given index of an associative array object
 * Note: imageMap is declared in the source html file
 *
 * @returns the item key
 */
function getKey(i) {
	 var counter = 1;
	 var key = "";
	 
	 // 
   for (key in imageMap) {
	    if ( i == counter ) { break; }
			counter++;
	 }
	 
   return key;
}

/**
 * Utility function gets the size of an associative array object
 *
 * @returns the size as an integer
 */
function getSize(map) {
   var i = 0;

   for (var k in map) { i++; }

   return i;
}
