var AHSI = {}; // AHSI namespace


/*----------------------------
	=Homepage image rotator
----------------------------*/
AHSI.ImageRotatorClass = function() {
	this.imageOut = -1;	// the index of the image fading out
	this.imageIn = 0;	// the index of the image fading in
	this.interval = 3000;	// time between updates in milliseconds
	this.fadeSpeed = 1000;	// milliseconds of how long the fades take
	this.images = [];	// image array gets loaded dynamically in init()
	
	this.init = function() {
		// if homepageVideoFO flash object exists, add click event handler to rotating images
		if (AHSI.homepageVideoFO) {
			// build array of all logo images
			this.images.push(jQuery("#homepagetout .featured").children("a")[0]);
			imgs = jQuery("#homepagetout .logos").children("img");
			for (var i = 0; i < imgs.length; i++) {
				this.images.push(imgs[i]);
			}
			
			// make video link
			vid = jQuery("#homepagetout .video img");
			vid.attr('style', 'cursor:pointer');
			vid.click(function() {
				AHSI.imageRotator.loadVideoPlayer();
			});
			/*
			for (var i = 0; i < this.images.length; i++) {
				img = jQuery(this.images[i]);
				if (img) {
					img.attr('style', 'cursor:pointer');
					img.click(function() {
						AHSI.imageRotator.loadVideoPlayer();
					});
				}
			}
			*/
		}
		
		// start timer for first image fade
		setTimeout("AHSI.imageRotator.update()", this.interval);
	};
	
	// update the image rotator
	this.update = function() {
		// get next ids for the fading images
		this.imageOut = this.getNextImage(this.imageOut);
		this.imageIn = this.getNextImage(this.imageOut);
		
		// fade in and out the images
		jQuery(this.images[this.imageOut]).fadeOut(this.fadeSpeed);
		jQuery(this.images[this.imageIn]).fadeIn(this.fadeSpeed);

		// set timer for next iteration
		setTimeout("AHSI.imageRotator.update()", this.interval);
	};
	
	this.getNextImage = function(num) {
		if (num >= this.images.length - 1)
			return 0;
		else
			return (num + 1);
	};
	
	this.loadVideoPlayer = function() {
		if (UFO && AHSI.homepageVideoFO) {
			UFO.create(AHSI.homepageVideoFO, "homepagetout");
		}
	};
};


/*----------------------------
	=Document Ready Actions
----------------------------*/
jQuery(document).ready(function() {
	AHSI.imageRotator = new AHSI.ImageRotatorClass();
	AHSI.imageRotator.init();
});
