/**
 * Sets up the paragraph slideshow on the homepage
 */

/**
 * The slideshow object
 * @var Object
 */
var HomeSlideshow = {
	/**
	 * The HTML of the slideshow controls
	 */
	_controls_skel : ''
		+'<div id="home-slideshow-controls">'
		+'<a href="#" class="slideshow-prev"><img src="/graphics/slideshow-prev.png" alt="[-]" title="Previous Image" /></a>'
		+'<span class="slideshow-count"></span>'
		+'<a href="#" class="slideshow-next"><img src="/graphics/slideshow-next.png" alt="[+]" title="Next Image" /></a>'
		+'</div>',
	/**
	 * The slideshow images
	 * @var Array
	 */
	images : null,
	/**
	 * A once-used variable that will contain unloaded images that are queued to be loaded
	 * @var Array
	 */
	images_interim : null,
	/**
	 * Marker for the interval to cycle images
	 * @var Integer
	 */
	images_interval : 0,
	/**
	 * The image number
	 * @var Number
	 */
	image_num : 0,
	/**
	 * Shows the next image in the slideshow
	 */
	doNext : function () {
		var next_step = HomeSlideshow.image_num + 1;
		if (next_step >= HomeSlideshow.images.length) {
			next_step = 0;
		}
		HomeSlideshow.selected(next_step);
	},
	/**
	 * Shows the previous image in the slideshow
	 */
	doPrev : function () {
		var prev_step = HomeSlideshow.image_num - 1;
		if (prev_step < 0) {
			prev_step = HomeSlideshow.images.length - 1;
		}
		HomeSlideshow.selected(prev_step);
	},
	/**
	 * Runs when a queued image finally loads
	 */
	onImageLoad : function () {
		/* Check if there are images left to load */
		if (HomeSlideshow.images_interim.length > 0) {
			var next_src = HomeSlideshow.images_interim.shift();
			var next = new Image();
				next.onload = function () {
					if (HomeSlideshow.images && HomeSlideshow.images.length === 1) {
						HomeSlideshow.selected(0);
						HomeSlideshow.images_interval = setInterval(HomeSlideshow.doNext, 5000);
					}
					HomeSlideshow.onImageLoad();
				};
				next.src = next_src;
			HomeSlideshow.images.push(next);
		}
		HomeSlideshow.updateText();
	},
	/**
	 * Sets and gets the image index in the slideshow
	 * @param Number - The new image index
	 */
	selected : function (index) {
		if (index != undefined) {
			HomeSlideshow.image_num = index;
		}
		if (index > -1) {
			var imageElement = HomeSlideshow.images[HomeSlideshow.image_num];
			var imageTag = document.getElementById('home-gallery-image');
				imageTag.src = imageElement.src;
		}
		HomeSlideshow.updateText();
		return HomeSlideshow.image_num;
	},
	/**
	 * Sets up the slideshow
	 */
	setup : function () {
		$('div.home-gallery-wrapper').append(HomeSlideshow._controls_skel);
		$('div#home-slideshow-controls a.slideshow-prev').click(function () {
			clearInterval(HomeSlideshow.images_interval);
			HomeSlideshow.doPrev();
			return false;
		});
		$('div#home-slideshow-controls a.slideshow-next').click(function () {
			clearInterval(HomeSlideshow.images_interval);
			HomeSlideshow.doNext();
			return false;
		});
		HomeSlideshow.images = new Array();
		HomeSlideshow.onImageLoad();
	},
	/**
	 * Updates the text count marker
	 */
	updateText : function () {
		try {
			var img_count = HomeSlideshow.image_num;
				img_count += 1;
			var img_total = HomeSlideshow.images.length;
			$('div#home-slideshow-controls span.slideshow-count').html(img_count + ' of ' + img_total);
		} catch (err) {
			$('div#home-slideshow-controls span.slideshow-count').html('loading...');
		}
	}
}


/* Hook page load */
$(function () {
	try {
		HomeSlideshow.images_interim = homepage_slideshow_images;
		HomeSlideshow.setup();
	} catch (err) {
		return;
	}
});
