/* 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



$J = jQuery.noConflict();

(function($J) {





	$J.fn.jFlow = function(options) {


		var opts = $J.extend({}, $J.fn.jFlow.defaults, options);


		var cur = 0;


		var timer;


		var selected_class = "jFlowSelected";


		var maxi = $J(".jFlowControl").length;


		$J(this).find(".jFlowControl").each(function(i){


			$J(this).click(function(){


				dotimer();


				


				$J(".jFlowControl").removeClass(selected_class);


				$J(this).addClass(selected_class);


				//alert(cur);


				//alert(i);


				var dur = Math.abs(cur-i);


				$J(opts.slides).animate({


					marginLeft: "-" + (i * $J(opts.slides).find(":first-child").width() + "px")


				}, opts.duration*(dur));


				cur = i;


			});


		});	





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





		$J(opts.slides).find("div").each(function(){


			$J(this).before('<div class="jFlowSlideContainer"></div>').appendTo($J(this).prev());


		});





		//initialize the controller


		$J(".jFlowControl").eq(cur).addClass(selected_class);





		var resize = function (x){


			$J("#jFlowSlide").css({


				position: "relative",


				width: opts.width,


				/*height: opts.height,*/


				overflow: "hidden"


			});





			$J(opts.slides).css({


				position:"relative",


				width: $J("#jFlowSlide").width()*$J(".jFlowControl").length+"px",


				/*height: $J("#jFlowSlide").height()+"px",*/


				overflow: "hidden"


			});





			$J(opts.slides).children().css({


				position: "relative",


				width: $J("#jFlowSlide").width()+"px",


				/*height: $J("#jFlowSlide").height()+"px",*/


				"float":"left"


			});





			$J(opts.slides).css({


				marginLeft: "-" + (cur * $J(opts.slides).find(":first-child").width() + "px")


			});


		}





		resize();





		$J(window).resize(function(){


			resize();


		});





		$J(".jFlowPrev").click(function(){


			dotimer();


			doprev();


		});


		


		var doprev = function (x){


			if (cur > 0)


				cur--;


			else


				cur = maxi -1;





			$J(".jFlowControl").removeClass(selected_class);


			$J(opts.slides).animate({


				marginLeft: "-" + (cur * $J(opts.slides).find(":first-child").width() + "px")


			}, opts.duration);


			$J(".jFlowControl").eq(cur).addClass(selected_class);


		}





		$J(".jFlowNext").click(function(){


			donext();


			dotimer();


		});





		var donext = function (x){


			if (cur < maxi - 1)


				cur++;


			else


				cur = 0;





			$J(".jFlowControl").removeClass(selected_class);


			$J(opts.slides).animate({


				marginLeft: "-" + (cur * $J(opts.slides).find(":first-child").width() + "px")


			}, opts.duration);


			$J(".jFlowControl").eq(cur).addClass(selected_class);


		}





		var dotimer = function (x){


			if(timer != null) 


			    clearInterval(timer);


			    


        		timer = setInterval(function() {


	                	donext();


    	        	}, 5000);


    	        	


    	        }





		dotimer();


	};





	$J.fn.jFlow.defaults = {


		easing: "swing",


		duration: 400,


		width: "100%"


	};





})(jQuery);