/* 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.1 (May 22, 2008)
 * Requires: jQuery 1.2+
 */

//modified by LandRover 30th May 08

(function(jQuery) {

	jQuery.fn.jFlow = function(options) {
		var opts = jQuery.extend({}, jQuery.fn.jFlow.defaults, options);
		var cur = 0;
		var timer;
		var selected_class = "jFlowSelected";
		var maxi = jQuery(".jFlowControl").length;
		jQuery(this).find(".jFlowControl").each(function(i){
			jQuery(this).click(function(){
				dotimer();
				
				jQuery(".jFlowControl").removeClass(selected_class);
				jQuery(this).addClass(selected_class);
				//alert(cur);
				//alert(i);
				var dur = Math.abs(cur-i);
				jQuery(opts.slides).animate({
					marginLeft: "-" + (i * jQuery(opts.slides).find(":first-child").width() + "px")
				}, opts.duration*(dur));
				cur = i;
			});
		});	

		jQuery(opts.slides).before('<div id="jFlowSlide"></div>').appendTo("#jFlowSlide");

		jQuery(opts.slides).find("div").each(function(){
			jQuery(this).before('<div class="jFlowSlideContainer"></div>').appendTo(jQuery(this).prev());
		});

		//initialize the controller
		jQuery(".jFlowControl").eq(cur).addClass(selected_class);

		var resize = function (x){
			jQuery("#jFlowSlide").css({
				position: "relative",
				width: opts.width,
				height: opts.height,
				overflow: "hidden"
			});

			jQuery(opts.slides).css({
				position:"relative",
				width: jQuery("#jFlowSlide").width()*jQuery(".jFlowControl").length+"px",
				height: jQuery("#jFlowSlide").height()+"px",
				overflow: "hidden"
			});

			jQuery(opts.slides).children().css({
				position: "relative",
				width: jQuery("#jFlowSlide").width()+"px",
				height: jQuery("#jFlowSlide").height()+"px",
				"float":"left"
			});

			jQuery(opts.slides).css({
				marginLeft: "-" + (cur * jQuery(opts.slides).find(":first-child").width() + "px")
			});
		}

		resize();

		jQuery(window).resize(function(){
			resize();
		});

		jQuery(".jFlowPrev").click(function(){
			dotimer();
			doprev();
		});
		
		var doprev = function (x){
			if (cur > 0)
				cur--;
			else
				cur = maxi -1;

			jQuery(".jFlowControl").removeClass(selected_class);
			jQuery(opts.slides).animate({
				marginLeft: "-" + (cur * jQuery(opts.slides).find(":first-child").width() + "px")
			}, opts.duration);
			jQuery(".jFlowControl").eq(cur).addClass(selected_class);
		}

		jQuery(".jFlowNext").click(function(){
			donext();
			dotimer();
		});

		var donext = function (x){
			if (cur < maxi - 1)
				cur++;
			else
				cur = 0;

			jQuery(".jFlowControl").removeClass(selected_class);
			jQuery(opts.slides).animate({
				marginLeft: "-" + (cur * jQuery(opts.slides).find(":first-child").width() + "px")
			}, opts.duration);
			jQuery(".jFlowControl").eq(cur).addClass(selected_class);
		}

		var dotimer = function (x){
			if(timer != null) 
			    clearInterval(timer);
			    
        		timer = setInterval(function() {
	                	donext();
    	        	}, 6000);
    	        	
    	        }

		dotimer();
	};

	jQuery.fn.jFlow.defaults = {
		easing: "fade",
		duration: 600,
		width: "100%"
	};

})(jQuery);