/*
*    jQuery Slideshow Plugin
*    Transitions between slides of arbitrary content
*
*    Copyright 2010 Illuminati Karate. All rights reserved.
*
*	 Modified 7.1.2010
*/
(function($)
{
	$.fn.slideshow = function(options)
	{
		// save reference to this object
		var target = this;

		// Set the options.
		var options = $.extend({}, $.fn.slideshow.defaults, options);

		// initialize variables
		var slides = $(this).children('.slide_group');
		var curSlide = 0;
		var totalSlides = slides.size();
		var slideInterval = false;

		if (totalSlides == 0)
		{
			return false;
		}

		// initialize timer
		if(options.automatic)
		{
			slideInterval = window.setInterval(function () { nextSlide(); }, options.interval);
		}

		var disableInterval = function()
		{
			if (typeof(slideInterval) != 'undefined')
			{
				window.clearInterval(slideInterval);
			}
		}
		
		// listen for left clicks
		$('a#left_arrow').live('click', function () 
		{
			// stop the automatic transitions once the user has interactied with the pagination
			if (options.freezeOnClick)
			{
				disableInterval();
			}
			$(slides).stop(true,true);
			prevSlide();
			return false;
		});
		
		// listen for right clicks
		$('a#right_arrow').live('click', function () 
		{
			// stop the automatic transitions once the user has interactied with the pagination
			if (options.freezeOnClick)
			{
				disableInterval();
			}
			$(slides).stop(true,true);
			nextSlide();
			return false;
		});
		
		var prevSlide = function ()
		{		
			if ( curSlide > 0 )
			{
				gotoSlide = curSlide - 1;
			}
			else
			{
				gotoSlide = 2;
			}
			//TBD: do this some other way than hardcoding
			if (curSlide == 2 && gotoSlide == 1){
				slides.eq(2).animate(
					{ left: '1024px' }, {
						duration: 'medium'
					}).end();			
				slides.eq(1).animate(
					{ left: '0px' }, {
						duration: 'medium'
					}).end();
				slides.eq(0).animate(
					{ left: '-1024px' }, {
						duration: 'medium'
					}).end();	
			} else if (curSlide == 1 && gotoSlide == 0){
				slides.eq(2).animate(
					{ left: '2048px' }, {
						duration: 'medium'
					}).end();	
				slides.eq(1).animate(
					{ left: '1024px' }, {
						duration: 'medium'
					}).end();			
				slides.eq(0).animate(
					{ left: '0' }, {
						duration: 'medium'
					}).end();	
			} else if (curSlide == 0 && gotoSlide == 2){
				slides.eq(0).animate(
					{ left: '-2048px' }, {
						duration: 'medium'
					}).end();
				slides.eq(1).animate(
					{ left: '-1024px' }, {
						duration: 'medium'
					}).end();			
				slides.eq(2).animate(
					{ left: '0' }, {
						duration: 'medium'
					}).end();	
			} 
			curSlide = gotoSlide;
			return;
		};

		var nextSlide = function ()
		{			
			if ( curSlide < (totalSlides - 1) )
			{
				gotoSlide = curSlide + 1;
			}
			else
			{
				gotoSlide = 0;
			}	
			//TBD: do this some other way than hardcoding
			if(curSlide == 0 && gotoSlide == 1){
				slides.eq(0).animate(
					{ left: '-1024px' }, {
						duration: 'medium'
					}).end();			
				slides.eq(1).animate(
					{ left: '0' }, {
						duration: 'medium'
					}).end();	
				slides.eq(2).animate(
					{ left: '1024px' }, {
						duration: 'medium'
					}).end();	
			} else if (curSlide == 1 && gotoSlide == 2){
				slides.eq(0).animate(
					{ left: '-2048px' }, {
						duration: 'medium'
					}).end();	
				slides.eq(1).animate(
					{ left: '-1024px' }, {
						duration: 'medium'
					}).end();			
				slides.eq(2).animate(
					{ left: '0' }, {
						duration: 'medium'
					}).end();		
			} else if (curSlide == 2 && gotoSlide == 0){
				slides.eq(2).animate(
					{ left: '2048px' }, {
						duration: 'medium'
					}).end();	
				slides.eq(1).animate(
					{ left: '1024px' }, {
						duration: 'medium'
					}).end();		 
				slides.eq(0).animate(
					{ left: '0' }, {
						duration: 'medium'
					}).end();	
			}
			curSlide = gotoSlide;
			return;
		};

		return $(this);
	};
	// Public defaults.
	$.fn.slideshow.defaults = {
		'interval' : 2000,
		'automatic': true,
		'freezeOnClick' : true
	};
	// Public functions.
	$.fn.slideshow.publicFunction = function()
	{
		return;
	};
})(jQuery);
