// Copyright (c) 20011 REDF (jhulbert AT redf DOT com || http://www.redf.com)
// Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
// and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
(function($) {
	
	var opts,
		curr = 0,
		prev = -1,
		currPanel = null,
		prevPanel = null,
		timer = null;
	
	var methods = {
		
		init: function(options) {
			opts = $.extend({
				sliderWrap:"#slider-wrap",
				slider:"#slider",
				sliderPanel:".slider-panel",
				prevBtn:".arrow#left",
				nextBtn:".arrow#right",
				thumbClass:".thumb",
				thumbMarker:"#thumb-marker",
				delay:7,
				transition:.7
			}, options);
			
			methods.setup();
		},
		
		setup: function() {
			$(opts.sliderPanel).filter(":gt(0)").css({
				"left":$(opts.sliderWrap).width()
			});
			
			$(opts.nextBtn).bind("click", function(evt) {
				if(timer != null) clearInterval(timer);
				if($(opts.sliderPanel).filter(":animated").length == 0) {
					if(prev == -1) {
						prev = 0;
						curr += 1;
					} else if(curr < $(opts.sliderPanel).length - 1) {
						prev = curr;
						curr+=1;
					} else {
						prev = $(opts.sliderPanel).length - 1;
						curr = 0;
					}
					methods.slideNext();
					methods.moveThumbMarker();
				}
			});
			
			$(opts.prevBtn).bind("click", function(evt) {
				if(timer != null) clearInterval(timer);
				if($(opts.sliderPanel).filter(":animated").length == 0) {
					if(curr > 0) {
						prev = curr;
						curr-=1;
					} else {
						prev = 0;
						curr = $(opts.sliderPanel).length - 1;
					}
					methods.slidePrev();
					methods.moveThumbMarker();
				}
			});
			
			$(opts.thumbClass).bind("click", function(evt) {
				if(timer != null) clearInterval(timer);
				var thumbIndex = $(evt.currentTarget).index(opts.thumbClass);
				if($(opts.sliderPanel).filter(":animated").length == 0 && thumbIndex != curr) {
					prev = curr;
					curr = thumbIndex;
					methods.slideNext();
					methods.moveThumbMarker();
				}
			});
			
			timer = setInterval(function() {
				if($(opts.sliderPanel).filter(":animated").length == 0) {
					if(prev == -1) {
						prev = 0;
						curr += 1;
					} else if(curr < $(opts.sliderPanel).length - 1) {
						prev = curr;
						curr+=1;
					} else {
						prev = $(opts.sliderPanel).length - 1;
						curr = 0;
					}
					methods.slideNext();
					methods.moveThumbMarker();
				}
			}, opts.delay * 1000);
			
			//methods.slideNext();
		},
		
		slideNext:function() {
			currPanel = $(opts.sliderPanel).eq(curr);
			prevPanel = $(opts.sliderPanel).eq(prev);
			currPanel.css({"left":$(opts.slider).width()});
			
			currPanel.animate({
				left:-((currPanel.width() - $(opts.slider).width()) / 2),
				opacity:1
			}, { queue:false, duration:opts.transition * 1000, easing:"easeOutExpo" });
			prevPanel.animate({
				left:-$(opts.sliderWrap).width(),
				opacity:0
			}, { queue:false, duration:opts.transition * 1000, easing:"easeOutExpo" });
		},
		
		slidePrev:function() {
			currPanel = $(opts.sliderPanel).eq(curr);
			prevPanel = $(opts.sliderPanel).eq(prev);
			currPanel.css({"left":-$(opts.slider).width()});
			
			currPanel.animate({
				left:-((currPanel.width() - $(opts.slider).width()) / 2),
				opacity:1
			}, { queue:false, duration:opts.transition * 1000, easing:"easeOutExpo" });
			prevPanel.animate({
				left:$(opts.sliderWrap).width(),
				opacity:0
			}, { queue:false, duration:opts.transition * 1000, easing:"easeOutExpo"} );
		},
		
		moveThumbMarker:function() {
			currThumb = $(opts.thumbClass).eq(curr);
			$(opts.thumbMarker).animate({
				left:$(currThumb).position().left
			}, { queue:false, duration:opts.transition * 1000, easing:"easeOutExpo"} );
		}
	}
	
	$.fn.imageSlider = function(_method) {
		if(methods[_method]) return methods[_method].apply(this, Array.prototype.slice.call(arguments, 1));
		else if(typeof _method === "object" || !_method) return methods.init.apply(this, arguments);
		else throw "The method '" + _method + "' does not exist in jquery.imageSlider";
	}
	
})(jQuery);
