/**
 * Copyright (c) 2007 Daylight Studio (http://www.thedaylightstudio.com/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $Id: jquery.slideshow.js 2007-11-02 David McReynolds
 **/

(function($){
	var SlideShow = function(el, o){
		this.options  = {
			delay : 5000,
			transSpeed : 1000,
		 	startSlideIndex: 0,
			buttons : true,
			autorun: true
		};
		$.extend(this.options, o);
		this.el = el;
		this.slides = null;
		this.timer = null;
		this.currentSlideIndex = this.options.startSlideIndex;
		this.slides = null;
		this.animating = false;
		this.loaded = false;
		this.init();
	}
	$.extend(SlideShow.prototype, {
	
		init : function(){
			if (this.options.transSpeed > this.options.delay) this.options.transSpeed = this.options.delay;
		
			$(this.el).css({position:'relative' });
			this.slides = $(this.el).children();
			this.slides.css({position : 'absolute', top : '0px', left:'0px', display: 'block'});
			this.slides.slice(1).hide();
			this.slides.eq(0).show();
			$(this.el).trigger('slideInitiated');
		
			var $this = this;
			$(window).load(function(e){
				$this.loaded = true;
				if ($this.options.buttons){
					$this.createImageButtons($this);
				}
				if ($this.options.autorun){
					$this.start();
				}
				$this.changeSlide($this.options.startSlideIndex);
			});
		},
	
		start : function(){
			this.stop();
			var $this = this;
			var timerCallback = function(){
				$this.changeSlide();
			}
			this.timer = setInterval(timerCallback, this.options.delay);
			$(this.el).trigger('slideStarted');
		},
	
		stop : function(){
			clearInterval(this.timer);
			$(this.el).trigger('slideStopped');
		},
	
		reset : function(){
			this.changeSlide(0);
			$(this.el).trigger('slideReset');
			this.start();
		},
	
		changeSlide : function(newSlideIndex){
			var $this = this;
			if (newSlideIndex == undefined) newSlideIndex = this.getNextSlideIndex();
			this.highlightCurrentButton(newSlideIndex);
			if (newSlideIndex == this.currentSlideIndex) return;
		
			this.slides.eq(newSlideIndex).css('zIndex', this.slides.size() + 1);
			this.slides.eq(this.currentSlideIndex).css('zIndex', this.slides.size());
			this.slides.eq(this.currentSlideIndex).show();
		
			var prev = this.currentSlideIndex;
			this.currentSlideIndex = newSlideIndex;
			this.slides.eq(newSlideIndex).hide();
			this.animating = true;
			$(this.el).trigger('slideChanged');
			this.slides.eq(newSlideIndex).fadeIn(this.options.transSpeed, function(){
				$this.animating = false;
				$this.slides.eq(prev).css('zIndex', 0);
				$($this.el).trigger('slideDoneAnimating');
			});
		},
	
		getNextSlideIndex : function(){
			if (this.currentSlideIndex >= (this.slides.size() - 1)){
				$(this.el).trigger('looped');
				return 0;
			} else {
				return parseInt(this.currentSlideIndex) + 1;
			}
		},
	
		getPrevSlideIndex : function(){
			if (!this.currentSlideIndex) return 0;
			if (this.currentSlideIndex <= 0){
				return this.slides.size() - 1;
			} else {
				return this.currentSlideIndex - 1;
			}
		},
	
		createImageButtons : function($this){
			$($this.el).append('<div class="slideshow-buttons-container"></div>');
			$this.slides.each(function(i){
				$($this.el).find('.slideshow-buttons-container').append('<div index="' + i + '" class="slideshow-btn"></a>');
			});
			var el = $this.el;
			$(this.el).find('.slideshow-btn').click(function(e){
				if (!$this.animating){
					var index = parseInt($(this).attr('index'));
					$this.changeSlide(index);
					$this.start();
				}
			});
		},
	
		highlightCurrentButton : function(btnIndex){
			$(this.el).find('div[index]').removeClass('slideshow-btn-on');
			$(this.el).find('div[index=' + btnIndex + ']').addClass('slideshow-btn-on');
		},
		
		getCurrentSlideIndex : function(){
			return this.currentSlideIndex || 0;
		}
	});

	$.fn.extend({
		resetSlides : function()	{
			return _w.call(this, 'reset');
		},
		stopSlides : function()	{
			return _w.call(this, 'stop');
		},
		startSlides : function() {
			return _w.call(this, 'start');
		},
		changeSlide : function(index) {
			return _w.call(this, 'changeSlide', arguments);
		},
		currentSlideIndex : function(){
			var c = _getController(this[0]);
			var index = c.getCurrentSlideIndex();
			return index;
		},
		nextSlideIndex : function(index) {
			var c = _getController(this[0]);
			var index = c.getNextSlideIndex();
			return index;
		},
		
		prevSlideIndex : function(index) {
			var c = _getController(this[0]);
			var index = c.getPrevSlideIndex();
			return index;
		}
	});

	function _w(method, args){
		return this.each(
			function(i) {
				var c = _getController(this);
				if (c) {
					if (args){
						return c[method].apply(c, args);
					} else {
						return c[method]();
					}
				}
			}
		);
	}
	function _getController(el)
	{
		if (el._ssId) return $.event._ssCache[el._ssId];
		return false;
	};

	jQuery.fn.slideShow = function(o) {
		if (!$.event._ssCache) $.event._ssCache = [];
	
		return this.each(function(i){
			if (!this._ssId) {
				this._ssId = $.event.guid++;
			}
			$.event._ssCache[this._ssId] = new SlideShow(this, o);
		});
	};
})(jQuery);
