// ##########################################################################################################
// # Slideshow                                                                                              #
// # Thanks http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding              #
// ##########################################################################################################

var slideshow;
var numberOfItems;
var numberOfItemsMinusOne;
var itemWidth;
var itemHeight;

var ANIMATION_SPEED = 800;
var CYCLING_SPEED = 3500;

var cycler;
var active = 0;

function previous() {

	clearTimeout(cycler);

	active = active - 1;
	if (active < 0) {
		active = numberOfItems - 1;
	}

	var position = (active*itemWidth*-1);
	$("ul", slideshow).animate({marginLeft: position}, ANIMATION_SPEED);

}

function next(cycling) {

	clearTimeout(cycler);

	active = active + 1;
	if (active >= numberOfItems) {
		active = 0;
	}

	var position = (active*itemWidth*-1);
	$("ul", slideshow).animate({marginLeft: position}, ANIMATION_SPEED);

	if (cycling) {
		cycler = setTimeout(function(){next(true);}, CYCLING_SPEED);
	}

}

function initSlideshow(divId) {
	slideshow = $("#" + divId);
	numberOfItems = $("li", slideshow).length;
	itemWidth = slideshow.width();
	itemHeight = slideshow.height();
	$("ul", slideshow).css('width', numberOfItems * itemWidth);
	$("li", slideshow).css('float', 'left');
	cycler = setTimeout(function(){next(true);}, CYCLING_SPEED-ANIMATION_SPEED);
}

// ##########################################################################################################
