/* Slideshow Plugin */
/*
* jQuery Slideshow Plugin v1.0
*
* easy slideshow
*/
(function($){
	// plugin definition
	$.fn.SlideShow = function (options){
		var options = $.extend({}, $.fn.SlideShow.defaults, options);
		return this.each(function (){
			var $this = $(this), position = parseFloat($this.css('left')), isMoving = false;
			if (!options.item && $this.children().length){
				options.item = $this.children()[0].tagName.toLowerCase();
			}
			var current = $(options.item+':first', $this);
			var start = $this.position()['left'];
			
			function MoveNext(){
				// if (isMoving){return;}
				var next = current.next();
				if (next.length){
					current = next;
					Move(-next.position()['left']);
				}
			}
			function MoveLast(){
				// if (isMoving){return;}
				var next = $(options.item+':last', $this);
				if (next.length){
					current = next;
					Move(-next.position()['left']);
				}
			}
			function MovePrev(){
				// if (isMoving){return;}
				var prev = current.prev();
				if (prev.length){
					current = prev;
					Move(-prev.position()['left']);
				}
			}
			function MoveFirst(){
				// if (isMoving){return;}
				var prev = $(options.item+':first', $this);
				if (prev.length){
					current = prev;
					Move(-prev.position()['left']);
				}
			}
			function Move(x){
				// console.log('moving to ', current[0], x);
				// isMoving = true;
				$this.stop().animate({left: x}, options.duration, function (){isMoving = false});
			}
			// buttons
			if (options.nextBtn){
				$(options.nextBtn)
					.click(function (){MoveNext();return false;})
					.dblclick(function (){MoveLast();return false;})
				;
			}
			if (options.prevBtn){
				$(options.prevBtn)
					.click(function (){MovePrev();return false;})
					.dblclick(function (){MoveFirst();return false;})
				;
			}
		});
	};
	// plugin defaults
	$.fn.SlideShow.defaults = {
		duration			: 1000		// seconds
		, nextBtn			: null		// next button, jQuery selector
		, prevBtn			: null		// prev button, jQuery selector
		, item				: null		// items, jQuery selector
	};
})(jQuery);

