/*
* jamSlider v1 - http://www.jamback.se
*/

(function($) {
	$.fn.gifslide = function(options) {
		var defaults = {
			speed : 500,
			pause : 2000,
			index : 200
		},
		options = $.extend(defaults, options);

		return this.each(function() {
			var $this = $(this);
			$this.wrap('<div class="slide-wrapper" />');
			$this.addClass('slide-container');
			$this.parent('.slide-wrapper').append('<a href ="#" class="slide-control prev"></a>');
			$this.parent('.slide-wrapper').append('<a href ="#" class="slide-control next"></a>');

			order = function() {
				for(var i = $this.children().length, y = 0; i > 0; i--, y++) { 		
					$this.children().eq(y).css('zIndex', i + options.index);	
				}
			}
			$this.find('.content').each(function(){$(this).css('bottom',-$(this).outerHeight());});
			
			function playnext() {
				$this.children(':first').stop(true,true).animate({'opacity' : 0}, options.speed, function() {
					$this
					   .children(':first')
					   .css('opacity', 1) // Return opacity back to 1 for next time.
					   .appendTo($this); // move it to the end of the line.
					order();
					$this.find('.content').each(function(){$(this).css('bottom',-$(this).outerHeight());});
					$this.find('.content:first').animate({'bottom' : 0}, options.speed);
				})
			}
			
			function playprev() {
				$this.children(':last')
					.css('opacity',0)
					.prependTo($this);
				order();
				$this.children(':first').stop(true,true).animate({'opacity':1},options.speed, function() {
					$this.find('.content').each(function(){$(this).css('bottom',-$(this).outerHeight());});
					$this.find('.content:first').animate({'bottom' : 0}, options.speed);
					})
			}
			
			slide = function() {
				play=setInterval(function() {
				 playnext()
				}, options.pause);
			} // end slide

			$(this).hover(function() {
				clearInterval(play); //Stop the rotation
			}, function() {
				slide(); //Resume rotation timer
			});	
			
			$('a.slide-control.next').click(function(){
				clearInterval(play);
				playnext();
				$(this).blur();
			});
			
			$('a.slide-control.prev').click(function(){
				clearInterval(play);
				playprev();
				$(this).blur();
			});
			
			order();
			$this.find('.content:first').animate({'bottom' : 0}, options.speed);
			slide();
		}); // end each		
	}
})(jQuery);
