/***
**	@filename - homepage.js
**	@author   - Mike Pacella, Troy Web Consulting
**  @created  - 1/29/2009
**  @description
**		Contains code for leveraging JQuery to fade in/fade out the sponsors and
**		slideshow images on the homepage.
**
***/

// Times for fading in, showing, and fading out images, respectively
var fadeInTimeMs  = 2000;
var showTimeMs    = 5000;
var fadeOutTimeMs = 1000;

/***
**	SLIDER RELATED VARIABLES 
***/
// array of images held in the slider slideshow (on top)
var aSliderImages = new Array();
// random array of image indices to randomize image order
var aRandomSlider = new Array();
// tracks index of current slider being displayed
var sliderIndex = 0;
// web relative path to directory containing slider images
var sliderImageLocation = '/images/slideshow/';
// web relative path to xml file containing slider images
var sliderXmlUrl = "/images/slideshow/contents.php";

/***
**	JQuery Document Ready Event Listener
***/
$(document).ready( function() {
	// SLIDESHOW CONTAINER population
	if ( $('#slideshowContainer') ) {
		$('#slideshowContainer').hide();
		$.ajax({
			type: "GET",
			url: sliderXmlUrl,
			success: function(txt) { 
				// for each pic element found, we do the following
				aSliderImages = txt.split( ',' );
                                aSliderImages.pop();
				// preload the images for faster loading
				//preloadImages( aSliderImages );
				// get the random array of indices
				aRandomSlider = getRandomArray( aSliderImages.length );
				// call recursive function showSliderImage 
				showSliderImage();
			}
		} ); //end $.ajax for HOME-SLIDER.xml
	}
} ); // end document.ready
	
/***
**	@function showSliderImage()
**
**	@description - shows an image in slideshowContainer using the randomly generated order.
**
***/
function showSliderImage() {
	// sliderIndex will grow to be greater than the array, so simply mod it
	var ii = sliderIndex % aRandomSlider.length;
	sliderIndex++;
	
       var imgHtml = '<img src="' + sliderImageLocation + aSliderImages[ aRandomSlider[ii] ] + '" />';
	// build the dynamic html into the slider container, fade it in,
	// then wait.......after waiting, call hideSliderImage
	$('#slideshowContainer').html( imgHtml );
	$('#slideshowContainer').fadeIn( fadeInTimeMs, function() {
		setTimeout( hideSliderImage, showTimeMs );	
	} );
}

/***
**	@function hideSliderImage()
**
**	@description - fades out the slideshow image and recalls showSliderImage
**
***/
function hideSliderImage() {
	$('#slideshowContainer').fadeOut(fadeOutTimeMs, showSliderImage); 
}

/***
**	@function getRandomArray( maxNum )
**
**	@argument - maxNum - required int - represents the size of the random array returned.
**				If you specify 100, a 100 item array with indices from 0 to 99 will be
**				returned.
**
**	@description - Generates an array of random numbers.
**
***/
function getRandomArray( maxNum ) {
	var aNums = new Array();
	var aRandom = new Array();
	
	for ( var ii = 0; ii < maxNum; ii++ ) {
		aNums[ii] = ii;
	}
	
	while ( aNums.length > 0 ) {
		var ran = Math.floor( Math.random() * aNums.length );
		aRandom.push( aNums[ ran ] );
		aNums.splice( ran, 1 );
	}
	return aRandom;
}

/***
**	@function preloadImages()
**
**	@argument - aImages - required array of structs with img key.  Array to preload.
**
**	@description - Preloads images.
**
***/
function preloadImages ( aImages ) {
	if (document.images)
    {
      preload_image_object = new Image();
      for(var i=0; i < aImages.length; i++) 
         preload_image_object.src = aImages[i].img;
    }	
}

