//Config
slideshowDelay = 5000;
slideshowEffectDuration = 500;

//Globally Accessed Variables
var slideshows = new Array();

$(document).ready(function() {
	slideshowInit();
});

function slideshowInit() {
	$('.slideshow').each(function(index){
		$(this).attr('id','slideshow_'+index);
		$(this).children('li').each(function(subindex){
			$(this).hide();
			$(this).attr('id','slide_'+subindex);
		});
		$(this).children('li:eq(0)').show();
		slideshows[index] = new Array();
		slideshows[index]['position'] = 0;
		slideshows[index]['delay'] = slideshowDelay;
		slideshows[index]['effectDuration'] = slideshowEffectDuration;
		slideshowPlay(index);
	});
}

function slideshowPlay(index) {
	slideshows[index]['timeout']=setTimeout("slideshowAutoRotate("+index+")",slideshows[index]['delay']);
}

function slideshowPause(index) {
	clearTimeout(slideshows[index]['timeout']);
}


function slideshowAutoRotate(index) {
	slideshowNavigate(index,'next');
	slideshowPlay(index);
}

function slideshowNavigate(index,direction,target) {
	slideshowPause(index);
	var slideshowID = '#slideshow_'+index;
	var targetSlide = '#slide_';
	var currentSlide = '#slide_'+slideshows[index]['position'];
	var slideCount = $(slideshowID+" li").size() - 1;
	if (direction=='previous'){
		if(slideshows[index]['position']==0) targetSlideID = slideCount;
		else targetSlideID = slideshows[index]['position'] - 1;
	} else if (direction=='next') {
		if(slideshows[index]['position']==slideCount) targetSlideID = 0;
		else targetSlideID = slideshows[index]['position'] + 1;
	} else if (direction=='target') {
		targetSlideID = target;
	}
	if (slideshows[index]['position'] != targetSlideID) {
		slideshows[index]['position'] = targetSlideID;
		targetSlide = targetSlide + targetSlideID;
		$(currentSlide).show();
		$(targetSlide).show();
		$(currentSlide).css({zIndex:800,display:'block'});
		$(targetSlide).css({zIndex:1,display:'block'});
		$(currentSlide).fadeOut(slideshows[index]['effectDuration']);
	}
}
