/* 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($) {

	$.fn.jFlow = function(options) {
		var opts = $.extend({}, $.fn.jFlow.defaults, options);
		var cur = 0;
		eval("var " + opts.timer_obj +";");
		var timer;
		var selected_class = "jFlowSelected";
		var maxi = $("."+ opts.jflowObj).length;
		
		var jSlide = opts.jSlide;
		var jSlideContainer = opts.jSlideContainer;
		
		//var maxi = 3; // force number of slides
		$(this).find("." + opts.jflowObj).each(function(i){
			$(this).click(function(){
				dotimer();
				
				var dur = Math.abs(cur-i);
				$(opts.slides).animate({
					marginLeft: "-" + (i * $(opts.slides).find(":first-child").width() + "px")
				}, opts.duration*(dur));
				cur = i;
			});
		});	

		$(opts.slides).before('<div id="'+jSlide+'"></div>').appendTo("#" + jSlide);

		$(opts.slides).find("div").each(function(){
			$(this).before('<div class="'+jSlideContainer+'"></div>').appendTo($(this).prev());
		});

		//initialize the controller
		$("." + opts.jflowObj).eq(cur).addClass(selected_class);

		var resize = function (x){
			$("#" + jSlide).css({
				position: "relative",
				width: opts.width,
				height: opts.height,
				overflow: "hidden"
			});

			$(opts.slides).css({
				position:"relative",
				width: $("#" + jSlide).width()*$("." + opts.jflowObj).length+"px",
				height: $("#" + jSlide).height()+"px",
				overflow: "hidden"
			});

			$(opts.slides).children().css({
				position: "relative",
				width: $("#" + jSlide).width()+"px",
				height: $("#" + jSlide).height()+"px",
				"float":"left"
			});

			$(opts.slides).css({
				marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")
			});
		}

		resize();

		$(window).resize(function(){
			resize();
		});

		$(".jFlowPrev").click(function(){
			if(opts.jMove == true) {
				dotimer();
				doprev();
			}
			
		});
		
		var doprev = function (x){
			if (cur > 0)
				cur--;
			else
				cur = maxi -1;

			$("."+ opts.jflowObj).removeClass(selected_class);
			$(opts.slides).animate({
				marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")
			}, opts.duration);
			$("."+ opts.jflowObj).eq(cur).addClass(selected_class);
		}

		$(".jFlowNext").click(function(){
			if(opts.jMove == true) {
				donext();
				dotimer();
			}
			
		});

		var donext = function (x){
			if (cur < maxi - 1)
				cur++;
			else
				cur = 0;

			$("."+ opts.jflowObj).removeClass(selected_class);
			$(opts.slides).animate({
				marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")
			}, opts.duration);
			$("."+ opts.jflowObj).eq(cur).addClass(selected_class);
		}

		var dotimer = function (x){
			if(opts.timer_obj != null) 
			    clearInterval(opts.timer_obj);
			    
        		opts.timer_obj = setInterval(function() {
	                	donext();
    	        	}, opts.auto_interval);
    	        	
    	        }

		dotimer();
	};

	$.fn.jFlow.defaults = {
		easing: "swing",
		duration: 400,
		width: "100%"
	};

})(jQuery);