//Config
var slideshowDelay = 5000;
var slideshowEffectDuration = 500;

//Global Variables
var slideshows = new Array();


/* ================================================
	slideshowInit()
	Handles slideshow inititalization
================================================ */

function slideshowInit() {
	$('.slides').each(function(index){
		slideshows[index] = new Array();
		slideshows[index]['position'] = 0;
		slideshows[index]['delay'] = slideshowDelay;
		slideshows[index]['effectDuration'] = slideshowEffectDuration;
		slideshows[index]['slideWidth'] = $(this).children('li').width();
		slideshows[index]['slideCount'] = $(this).children('li').size();
		
		$(this).attr('id','slideshow_'+index);
		
		if($(this).parent().siblings('.slidenav').length > 0) {
			$(this).parent().siblings('.slidenav').attr('id','slidenav_'+index);
			$('#slidenav_'+index+' .indicator').each(function(slideIndex){
				$(this).addClass('i_'+slideIndex);
				$(this).click(function(){ slideshowNavigate(index,'target',slideIndex); });
			});
		}
		
		if($(this).parent().siblings('.slidenav').children('.previous').length > 0) {
			$(this).parent().siblings('.slidenav').children('.previous').click(function(){slideshowNavigate(index,'previous');});
		}
		
		if($(this).parent().siblings('.slidenav').children('.next').length > 0) {
			$(this).parent().siblings('.slidenav').children('.next').click(function(){slideshowNavigate(index,'next')});
		}
		
		if($(this).hasClass('fading')) {
			slideshows[index]['type'] = 'fading';
			$(this).children('li').each(function(subindex){
				$(this).hide();
				$(this).css('position','absolute');
				$(this).attr('id','slide_'+subindex);
			});
			$(this).children('li:eq(0)').show();
		//} else if($(this).hasClass('sliding')) {
		} else {
			slideshows[index]['type'] = 'sliding';
			$(this).width(slideshows[index]['slideCount'] * slideshows[index]['slideWidth']);
			$(this).children('li').each(function(subindex){
				$(this).css('float','left');
				$(this).attr('id','slide_'+subindex);
			});
		}
		
		if(!$(this).hasClass('manual')) slideshowPlay(index);
	});
}



/* ================================================
	slideshowPlay(index)
	Begins slideshow auto-rotation
	* index - slideshow ID from the slideshows[] array
================================================ */

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



/* ================================================
	slideshowPause()
	Stops slideshow auto-rotation
	* index - slideshow ID from the slideshows[] array
================================================ */

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



/* ================================================
	slideshowAutoRotate()
	Handles slideshow auto-rotation timer
	* index - slideshow ID from the slideshows[] array
================================================ */

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



/* ================================================
	slideshowAutoRotate()
	Navigates between slides
	* index - slideshow ID from the slideshows[] array
	* direction - can be 'next', 'previous', or 'target'
	* target - if direction is 'target', this specifies the slide ID to jump to
================================================ */

function slideshowNavigate(index,direction,target) {
	slideshowPause(index);
	var slideshowID = '#slideshow_'+index;
	var targetSlide = '#slide_';
	var currentSlide = '#slide_'+slideshows[index]['position'];
	var slideCount = slideshows[index]['slideCount'] - 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;
		if(slideshows[index]['type']=='fading') {
			$(currentSlide).show();
			$(targetSlide).show();
			$(currentSlide).css({zIndex:800,display:'block'});
			$(targetSlide).css({zIndex:1,display:'block'});
			$(currentSlide).stop().fadeOut(slideshows[index]['effectDuration']);
		} else if(slideshows[index]['type']=='sliding') {
			$(slideshowID).stop().animate({marginLeft: targetSlideID*slideshows[index]['slideWidth']*-1},slideshows[index]['effectDuration']);
		}
		if($('#slidenav_'+index).length > 0) {
			$('#slidenav_'+index+' .indicator').removeClass('selected');
			$('#slidenav_'+index+' .i_'+targetSlideID).addClass('selected');
		}
	}
}

