/**
 * 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 : 8000,
		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();
		
		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);
	},
	
	stop : function(){
		clearInterval(this.timer);
	},
	
	reset : function(){
		this.changeSlide(0);
		this.start();
	},
	
	changeSlide : function(newSlideIndex){
		var $this = this;
		if (newSlideIndex == undefined) newSlideIndex = this.nextSlideIndex();
		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');
		});
	},
	
	nextSlideIndex : function(){
		if (this.currentSlideIndex >= (this.slides.size() - 1)){
			$(this.el).trigger('looped');
			return 0;
		} else {
			return parseInt(this.currentSlideIndex) + 1;
		}
	},
	
	prevSlideIndex : 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');
	}
});

$.fn.extend({
	resetSlides : function()	{
		_w.call(this, 'reset');
	},
	stopSlides : function()	{
		_w.call(this, 'stop');
	},
	startSlides : function()	{
		_w.call(this, 'start');
	}
	
	
});

function _w(method){
	var $this = this;
	return this.each(
		function(i) {
			var c = _getController(this);
			if (c) {
				c[method]();
			}
		}
	);
}
function _getController(ele)
{
	if (ele._ssId) return $.event._ssCache[ele._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);