/**	 
 *
 * Usage:
 *
 *	 // Init element with slideshow, using the default options.
 *	 $(selector).slideshow({assets: asset_object}, height: 420);
 *
 */

(function($) {

	/*
		Constants
	*/
	
	PLUGIN_NAME = 'slideshow';
	current_image_index = 1;
	prev_element = null;
	next_element = null;
	assets = null;
	var opts;

	/*
		Private methods
	*/

	function rotateSwitch($this, duration) {
		slideshow_play = setInterval(function(){
			rotate($this);
		}, duration);
	}

	function rotate($this) {
		if (!current_image_index || current_image_index >= $this.children('.slide').length) {
			current_image_index = 0;
		} else if (current_image_index < 0) {
			current_image_index = $this.children('.slide').length -1;
		}
	    next_element = $this.children('.slide').eq(current_image_index);
		$this.find("a.jump").removeClass('active');
		$this.find("a.jump[rel='" + current_image_index + "']").addClass('active');
		transitionSlideshow($this, opts.effect, opts.duration);
		clearInterval(slideshow_play);
		rotateSwitch($this, opts.pause);
		prev_element = next_element;
		current_image_index++;
	}
	
	function start($this) {
		prev_element = $this.children('.slide').eq(0).show();
		$this.find("a.jump[rel='0']").addClass('active');
		rotateSwitch($this, opts.pause);
	}

	function transitionSlideshow($this, effect, speed) {
		prev_element.stop();
		next_element.stop();
		var width = $this.width();
		switch(effect) {
			case "slide-left":
				prev_element.animate({left: -width - 20}, speed, function() {
					$(this).hide();
				});
				next_element.show().css('left', width + 20).animate({left: 0}, speed );
				break;
			case "slide-right":
				prev_element.animate({left: width + 20}, speed, function() {
					$(this).hide();
				});
				next_element.show().css('left', -width - 20).animate({left: 0}, speed );
				break;
			case "fade":
				prev_element.fadeOut();
				next_element.css('left', 0).hide().fadeIn(speed);
		}
	}

	function attachEvents($this) {
		$this.find("a.prev").click(function() {
			current_image_index = current_image_index -2;
			clearInterval(slideshow_play);
			rotateSwitch($this, 0);
			return false;
		})

		$this.find("a.next").click(function() {
			clearInterval(slideshow_play);
			rotateSwitch($this, 0);
			return false;
		})

		$this.find("a.jump").click(function() {
			if(current_image_index -1 != parseInt($(this).attr('rel'))) {
				current_image_index = $(this).attr('rel');
				clearInterval(slideshow_play);
				rotateSwitch($this, 0);
			};
			return false;
		})
	}

	/*
		Public methods
	*/

	var publicMethods = {
		init: function(options) {return this.each(function() {

			opts  = $.extend({}, $.fn['slideshow'].defaults, options);
			var $this = $(this);
			WIDTH = $this.width();

			if ($this.children().length <= 1) return false; // Single image doesn't constitute a slideshow

			$this.children().hide().addClass('slide');
			
			$this.append($("<div>").addClass('slideshow-controls'));
			$this.find(".slideshow-controls").css({
				'z-index': 1,
				'position': 'absolute',
				'bottom': 10
			});
			$this.find(".slideshow-controls").append("<a class='prev' href='#'>Prev</a>");
			
			for (i = 0; i < $this.children('.slide').length; i++) {
				$this.find(".slideshow-controls").append("<a class='jump' href='#' rel='" + i + "'>" + (i+1) + "</a>");
			}
			
			$this.find(".slideshow-controls").append("<a class='next' href='#'>Next</a>");
			$this.find(".slideshow-controls").css({
				'left': '50%',
				'margin-left': -($this.find(".slideshow-controls").width() / 2)
			});
			
			var data = $this.data(PLUGIN_NAME);
			if (!data) {
				$this.data(PLUGIN_NAME, {
					opts : opts
				});
			};
			
			attachEvents($this);
			start($this);
			
		})}

	};

	/*
		Initialization
	*/
	
	$.fn[PLUGIN_NAME] = function(method) {
		if (publicMethods[method]) {
			return publicMethods[method].apply(
				this,
				Array.prototype.slice.call(arguments, 1)
			);
		}
		else if (typeof method == 'object' || !method) {
			return publicMethods.init.apply(this, arguments);
		}
		else {
			$.error('Method ' + method + ' does not exist on jQuery.' + PLUGIN_NAME);
		}
	};

	/*
		Default options
	*/
	
	$.fn[PLUGIN_NAME].defaults = {
		effect: 'slide-left',
		duration: 1000,
		pause: 6000
	};
})(jQuery);
