/**
 * MPI Slideshow Plugin 
 * 
 * @author dan.rades
 * @lastmod 26.02.2008
 * 
 * Pluginul realizeaza un slideshow intre mai multe elemente(slides).
 * 
 * 
 */

(function($) {
	$.fn.mpiSlideshow = function(o) {
		o = $.extend($.fn.mpiSlideshow.defaults, o || {})
		return this.each(function() {
			this.options = o;

			this.slides = $('.slide',$(this));
			this.currentSlideIdx = 0;
			this.interval = 0;

			var _self = this;
			
			$.fn.mpiSlideshow.currentSlideshow = this;

/*
			this.jumpTo = function(i) {
				if (i < 0)	{
					i = 0;
				}

				if (i > (el.slides.length - 1)) {
					i = this.slides.length - 1
				}
				
				stop();

				this.curentSlideIdx = i;

				start();
			};

			

			var start = function() {
				this.interval = setInterval(function() {
					$.fn.mpiSlideshow.run(_self);
				},o.staticTime);
			}
			
			var stop = function() {
				clearInterval(this.interval);
			}


*/			
			
			$('.start',$(this)).click(function() {
				$.fn.mpiSlideshow.run(_self);
				$.fn.mpiSlideshow.start(_self);
			});	
			
			$('.stop',$(this)).click(function() {
				$.fn.mpiSlideshow.stop(_self);
			});						
			
			$('.next',$(this)).click(function() {
				$.fn.mpiSlideshow.run(_self,'next');
			});
			
			$('.prev',$(this)).click(function() {
				$.fn.mpiSlideshow.run(_self,'previous');
			});			
						
			if (this.options.autostart == true) {
				$.fn.mpiSlideshow.start(_self);
			}			

		});
	}

	$.fn.mpiSlideshow.defaults = {		
		speed : 2000,		
		staticTime : 4000,
		autostart : true,
		callback : function() {}
	};

	$.fn.mpiSlideshow.jumpTo = function(i, el) {
		var el = el || $.fn.mpiSlideshow.currentSlideshow;
		$.fn.mpiSlideshow.stop(el);
		if (i < 0 || i > (el.slides.length - 1))	{
			return false;
		}

		var currentSlide  = $(el.slides[i]);
		$(el.slides[el.currentSlideIdx]).fadeOut($.fn.mpiSlideshow.defaults.speed);
		currentSlide.fadeIn($.fn.mpiSlideshow.defaults.speed);	
		el.options.callback(i);		
		
		el.currentSlideIdx = Number(i);

	};

	$.fn.mpiSlideshow.start = function(el) {

		el.interval = setInterval(function() {
			$.fn.mpiSlideshow.run(el);
		},el.options.staticTime);
	};

	$.fn.mpiSlideshow.stop = function(el) {
		clearInterval(el.interval);
	};

	$.fn.mpiSlideshow.run =  function (el, direction, isJump) {
		var direction = direction || 'next';
		var isJump = isJump || false;
		var c = (direction == 'next') ? 1 : -1;
		c = (isJump == false) ? c : 0;

		var idx = c + el.currentSlideIdx;
		
		if (idx < 0) {
			idx = el.slides.length - 1;
		}
		
		idx = (typeof el.slides[idx] != 'undefined') ? idx : 0;	
				
		el.currentSlideIdx = idx;
		var previousIndex = 0;
		if (direction == 'next') {
			previousSlideIdx = (idx == 0) ? el.slides.length - 1 : (idx == el.slides.length) ? 0 : idx - 1;				
		}
		else {
			previousSlideIdx = (idx == el.slides.length - 1) ? 0 : idx + 1;				
		}
		
		var currentSlide  = $(el.slides[idx]);
		$(el.slides[previousSlideIdx]).fadeOut($.fn.mpiSlideshow.defaults.speed);
		currentSlide.fadeIn($.fn.mpiSlideshow.defaults.speed);	
		el.options.callback(idx);
	};
})(jQuery);