/* General purpose slideshow script*/
Slideshow = (function($) {
			
	SlideshowDefaultOptions = {
		transition: 1000,
		pause: 5000,
		slidesSelector: 'li',
		slidesNavSelector: 'a',
		slidesNavLinkSelector: 'a'
	}
	
	//constructor function
	function Slideshow(element,options) {
		this.$container = $(element);
		if(!this.$container) return;
		
		this.options = SlideshowDefaultOptions;
		if (typeof(options) != "object") options = {};
        $.extend(this.options, options);
        
		this.$slides = this.$container.find(this.options.slidesSelector);
		this.$slides.hide('hide');
		
		if(this.options.navigation) {
			this.$nav = $(this.options.navigation);
			this.addNavigation();
		}
		if(this.options.previous) this.previousButton();
		if(this.options.next) this.nextButton();
		this.length = this.$slides.size();
		this.count = 0;
		this.start();
	}
	
	Slideshow.prototype = {
		
		increment: function() {
			var count = arguments[0] || this.count;
			count = (count + 1) % this.length;
			return count;
		},
		
		decrement: function() {
			var count = arguments[0] || this.count;
			count = (count <= 0) ? (this.length-1) : (count - 1 ) ;
			return count;
		},
		
		start: function() {
			current = this.$slides.get(this.count);
			var slideshow = this;
			$(current).fadeIn(this.options.transition,function(){slideshow.activeButton();});
			this.timeout = window.setTimeout(function(){ slideshow.tween(); },this.options.pause);
		},
		
		pause: function() {
			return function() {
				
			}
		},
		
		tween: function() {
			window.clearTimeout(this.timeout);
			current = this.$slides.get(this.count);
			this.count = this.increment();
			next = this.$slides.get(this.count);
			var slideshow = this;
			if(!this.stop && next != current)
			{
				$(current).fadeOut(this.options.transition,function(){slideshow.activeButton();});
				$(next).fadeIn(this.options.transition);
			}
			this.timeout = window.setTimeout(function(){ slideshow.tween(); },this.options.pause);
		},
		
		show: function(count) {
			this.stop = true;
			this.count = count;
			window.clearTimeout(this.timeout);
			this.$slides.stop(true,true);
			this.activeButton(count);
			this.$slides.each(function(index) {
				$(this).hide();
			});
			current = this.$slides.get(count);
			$(current).show();
			var slideshow = this;
			this.timeout = window.setTimeout(function(){ slideshow.stop = false; slideshow.tween(); },this.options.pause);
		},
		
		addNavigation: function() {
			var slideshow = this;
			this.$nav.find(this.options.slidesNavLinkSelector).each(function(index) {
				$(this).click(function(event){
					slideshow.show(index);
				});
			});
		},
		
		activeButton: function() {
			if(!this.$nav) return;			
			var count = arguments[0] || this.count;
			var buttons = this.$nav.find(this.options.slidesNavSelector);
			$(buttons).removeClass('active');
			current = $(buttons).get(count);
			$(current).addClass('active');
		},
		
		previousButton: function() {
			var slideshow = this;
			$(this.options.previous).click(function(event) {
				var previous = slideshow.decrement();
				slideshow.show(previous);
			});
		},
		
		nextButton: function() {
			var slideshow = this;
			$(this.options.next).click(function(event) {
				var next = slideshow.increment();
				slideshow.show(next);
			});
		}
	
	};

	$.fn.slideshow = function(options) {
		return this.each(function() { new Slideshow(this,options); });
	}

})(jQuery);
