/* Copyright (c) 2010 Tin @ Klik - http://www.klikhome.co.uk/
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * jKlikFlow
 * Version: 1.0 (May, 2010)
 * Requires: jQuery 1.2+
 * Thanks to Kean Loong Tan for the initial inspiration with jFlow, on which this is based
 */
 
(function($) {

	$.fn.jKlikFlow = function(options) {
		var opts = $.extend({}, $.fn.jKlikFlow.defaults, options);
		var slideStripID="#"+$(this).attr("id");
		var numSlides = $(this).find("div").length;
		var timer;

    /* Duplicate the first and last slides, so that easeBackOut doesn't give a glimpse of white */
		$(slideStripID).find("div:first-child").before($(slideStripID).find("div:last-child").html());
		$(slideStripID).find("div:last-child").after($(slideStripID).find("div:nth-child(2)").html());
		numSlides+=2;
		cur=1; // Start at slide 1 (the original slide 0)

		/* Create a container around the whole slide block to act as the "window" [ jFlowSliderWindow ] */
		$(slideStripID).before('<div id="jFlowSliderWindow"></div>').appendTo("#jFlowSliderWindow");
		
    /* Create a container around each slide [ jFlowSlideContainer ]*/
		$(slideStripID).find("div").each(function(){ $(this).before('<div class="jFlowSlideContainer"></div>').appendTo($(this).prev()); });
		
    var jumpTo = function (i,doAnimate) {
      if (doAnimate) {
        $(slideStripID).animate({ marginLeft: "-" + (i * $(slideStripID).find(":first-child").width() + "px")}, opts.duration, opts.easing);
      } else {
        $(slideStripID).css({ marginLeft: "-" + (i * $(slideStripID).find(":first-child").width() + "px") });
      }
      cur = i;
    }
		
		var resize = function (x){
		  // Deal with the slides outer container
			$("#jFlowSliderWindow").css({
				position:"relative",
				width: opts.width,
				height: opts.height,
				overflow: "hidden"
			});
		
		  // Deal with the slides inner container
			$(slideStripID).css({
				position:"relative",
				width: $("#jFlowSliderWindow").width()*numSlides+"px",
				height: $("#jFlowSliderWindow").height()+"px",
				overflow: "hidden"
			});

      // Deal with each slide		
			$(slideStripID).children().css({
				position:"relative",
				width: $("#jFlowSliderWindow").width()+"px",
				height: $("#jFlowSliderWindow").height()+"px",
				"float":"left"
			});

      // Jump to the current slide			
      jumpTo(cur,false);
		}
		
		resize();
		
		$(window).resize(function(){ resize(); });
		
		$(opts.prev).click(function(){
			if (cur==1) { // At first slide, jump to the last slide (a duplicate of this) before animating backwards
			  jumpTo(numSlides-1,false);
				cur=numSlides-2;
			} else {
				cur--;
		  }
		  jumpTo(cur,true);
			if (opts.auto) doTimer();
		});
		
		$(opts.next).click(function(){
		  if (cur==numSlides - 2) { // At last slide, jump to the first slide (a duplicate of this) before animating forward
		    jumpTo(0,false);
				cur=1;
			} else {
				cur++;
		  }
		  jumpTo(cur,true);
			if (opts.auto) doTimer();
		});

		var doTimer = function (x){
      if (timer!=null) clearInterval(timer);
      timer = setInterval(function() { $(opts.next).click(); }, opts.pauseBetweenSlides + opts.duration);
		}

    // Start the ball rolling...
    $(slideStripID).css({ display: "block" });
    jumpTo(cur,false);
		if (opts.auto) doTimer();

	};
	
	$.fn.jKlikFlow.defaults = {
		easing: "swing",
		duration: 1000,
    pauseBetweenSlides: 2500,
		easing: "easeOutBack",
		width: "100%"
	};
	
})(jQuery);
