/*
* jwSlider v1 - http://www.jeffrey-way.com
* Last Updated: October 29, 2009

* This is a very simple and convenient slider/fader to transition between images or content. it offers only fade and slide transitions to keep the script
* as lightweight as possible.

* Developed by Jeffrey Way
* http://www.jeffrey-way.com
* jeffrey@effrey-way.com
*/


// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});


(function($) {
	
	$.fn.jwSlider = function(options) {

		// set default options
		var defaults = {
			speed : 1000,
			pause : 2000,
			transition : 'fade'
		},

		// Take the options that the user selects, and merge them with defaults.
		options = $.extend(defaults, options);
		
		// Needed to fix a tiny bug. If the pause is less than speed, it'll cause a flickr.
		// This will check for that, and if it is smaller, it increases it to just about the options.speed.
		if(options.pause <= options.speed) options.pause = options.speed + 100;
	
		// for each item in the wrapped set
		return this.each(function() {
		
			// cache "this."
			var $this = $(this);
			
			// Wrap "this" in a div with a class of "slider-wrap."
			$this.wrap('<div class="slider-wrap" />');
			
			// Set the width to a really high number. Adjusting the "left" css values, so need to set positioning.
			$this.css({
				'width' : '99999px',
				'position' : 'relative',
				'padding' : 0
			});

			// If the user chose the "slide" transition...
			if(options.transition === 'slide') {
				$this.children().css({
					'float' : 'left',
					'list-style' : 'none'
				});			
			}
						
			// If the user instead chose the "slide" transition, call the slide function.
			if(options.transition === 'slide') slide();	
			
			$featuredTotal = $this.children().length;
			$featuredWidth = $this.parent().width();
			$current = 0;
			
			$playing = true;
			$moving = false;
			
			$('#controls a.pause').click(function () {
				if($playing == true) {
					clearInterval($sliderInterval);
					$playing = false;
					$('#controls a.pause').text("Play");
				} else {
					$playing = true;
					$('#controls a.pause').text("Pause");
					slide();
				}
			});
			
			$('#controls a.next').click(function () {
				$playing = true;
				$('#controls a.pause').text("Pause");
				clearInterval($sliderInterval);
				if($moving == false){
					$moving = true;
					if($current == ($featuredTotal-1)) {
						$current = -1;
					}
					$new = $current+1;
					// Animate to the left the width of the image/div
					$this.animate({'left' : '-' + ($new*$featuredWidth)}, options.speed, 'easeInOutExpo', function() {
						$current++;
						slide();
						$moving = false;
					})
				}
			});
			
			$('#controls a.prev').click(function () {
				$playing = true;
				$('#controls a.pause').text("Pause");
				clearInterval($sliderInterval);
				if($moving == false){
					$moving = true;
					if($current == 0) {
						$current = $featuredTotal;
					}
					$last = $current-1;
					$this.animate({'left' : '-' + ($last*$featuredWidth)}, options.speed, 'easeInOutExpo', function() {
						$current = $last;
						slide();
						$moving = false;
					})
				}
			});
			
			function slide() {
				$sliderInterval = setInterval(function() {						   
					if($current == ($featuredTotal-1)) {
						$current = -1;
					}
					$new = $current+1;
					// Animate to the left the width of the image/div
					$this.animate({'left' : '-' + ($new*$featuredWidth)}, options.speed, 'easeInOutExpo', function() {
						$current++;
					})
				}, options.pause);
			} // end slide		

		}); // end each		
	
	} // End plugin. Go eat cake.
	
})(jQuery);
