/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * jFlow
 * Version: 1.0 (May 13, 2008)
 * Requires: jQuery 1.2+
 */
 
(function($ef) {

	$ef.fn.jFlow = function(options) {
		var opts = $ef.extend({}, $ef.fn.jFlow.defaults, options);
		var cur = 0;
		var stopRotate = 0; // added to allow automatic rotation of slides
		var maxi = $ef(".jFlowControl").length;
		$ef(this).find(".jFlowControl").each(function(i){
			$ef(this).click(function(){
				stopRotate = 1;
				$ef(".jFlowControl").removeClass("jFlowSelected");
				$ef(this).addClass("jFlowSelected");
				var dur = Math.abs(cur-i+1);
				$ef(opts.slides).animate({
					marginLeft: "-" + (i * $ef(opts.slides).find(":first-child").width() + "px")
				}, opts.duration*(dur));
				cur = i;
			});
		});	
		
		$ef(opts.slides).before('<div id="jFlowSlide"></div>').appendTo("#jFlowSlide");
		
		$ef(opts.slides).find("div").each(function(){
			$ef(this).before('<div class="jFlowSlideContainer"></div>').appendTo($ef(this).prev());
		});
		
		//initialize the controller
		$ef(".jFlowControl").eq(cur).addClass("jFlowSelected");
		
		var resize = function (x){
			$ef("#jFlowSlide").css({
				position:"relative",
				width: opts.width,
				height: opts.height,
				overflow: "hidden"
			});
		
			$ef(opts.slides).css({
				position:"relative",
				width: $ef("#jFlowSlide").width()*$ef(".jFlowControl").length+"px",
				height: $ef("#jFlowSlide").height()+"px",
				overflow: "hidden"
			});
		
			$ef(opts.slides).children().css({
				position:"relative",
				width: $ef("#jFlowSlide").width()+"px",
				height: $ef("#jFlowSlide").height()+"px",
				"float":"left"
			});
			
			$ef(opts.slides).css({
				marginLeft: "-" + (cur * $ef(opts.slides).find(":first-child").width() + "px")
			});
		}
		
		resize();
		
		$ef(window).resize(function(){
			resize();						  
		});
		
		$ef(".jFlowPrev").click(function(){
			if (cur > 0)
				cur--;
			else
				cur = maxi -1;
			
			$ef(".jFlowControl").removeClass("jFlowSelected");
			$ef(opts.slides).animate({
				marginLeft: "-" + (cur * $ef(opts.slides).find(":first-child").width() + "px")
			}, opts.duration);
			$ef(".jFlowControl").eq(cur).addClass("jFlowSelected");
		});
		
		$ef(".jFlowNext").click(function(){
			if (cur < maxi - 1)
				cur++;
			else
				cur = 0;
				
			$ef(".jFlowControl").removeClass("jFlowSelected");
			$ef(opts.slides).animate({
				marginLeft: "-" + (cur * $ef(opts.slides).find(":first-child").width() + "px")
			}, opts.duration);
			$ef(".jFlowControl").eq(cur).addClass("jFlowSelected");
		});
		// added to allow automatic rotation of slides
		if (opts.autoFlow == "true") {			
			$ef(document).ready(function() {
				$ef(this).click(function(){
				stopRotate = 1;
				$ef(".jFlowControl").removeClass("jFlowSelected");
				$ef(this).addClass("jFlowSelected");
				var dur = Math.abs(cur-i+1);
				$ef(opts.slides).animate({
					marginLeft: "-" + (i * $ef(opts.slides).find(":first-child").width() + "px")
				}, opts.duration*(dur));
				cur = i;
			});
				$ef.timer(8000, function (timer) {
					if (stopRotate == 0) {
						if (cur < maxi - 1)
							cur++;
						else
							cur = 0;

						$ef(".jFlowControl").removeClass("jFlowSelected");
						$ef(opts.slides).animate({
							marginLeft: "-" + (cur * $ef(opts.slides).find(":first-child").width() + "px")
						}, opts.duration);
						$ef(".jFlowControl").eq(cur).addClass("jFlowSelected");
					} else {
						timer.stop();
					}
				});
			});
		}
		// end auto rotate
	};
	
	$ef.fn.jFlow.defaults = {
		easing: "swing",
		duration: 400,
		width: "100%",
		autoFlow: "true" // added to allow automatic rotation of slides
	};
	
})(jQuery);
