/* 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;
		var timer;
		var selected_class = "jFlowSelected";
		var maxi = $(".jFlowControl").length;
		$(this).find(".jFlowControl").each(function(i){
			$(this).click(function(){
				if(timer != null) 
					clearInterval(timer);
				dox(i);
			});
		});	

		$(opts.slides).before('<div id="jFlowSlide"></div>').appendTo("#jFlowSlide");

		$(opts.slides).find("div").each(function(){
			$(this).before('<div class="jFlowSlideContainer"></div>').appendTo($(this).prev());
		});

		//initialize the controller
		$(".jFlowControl").eq(cur).addClass(selected_class);

		var resize = function (x){
			$("#jFlowSlide").css({
				position: "relative",
				width: opts.width,
				height: opts.height,
				overflow: "hidden"
			});

			$(opts.slides).css({
				position:"relative",
				width: $("#jFlowSlide").width()*$(".jFlowControl").length+"px",
				height: $("#jFlowSlide").height()+"px",
				overflow: "hidden"
			});

			$(opts.slides).children().css({
				position: "absolute",
				width: $("#jFlowSlide").width()+"px",
				height: $("#jFlowSlide").height()+"px"
			});
			
			$(opts.slides).children(':eq(0)').css({
				opacity: 1,
				top: '0'
			});
			$(opts.slides).children(':gt(0)').css({
				opacity: 0,
				top: opts.height
			});
		}
		
		resize();
		
		$(window).resize(function(){
		});

		$(".jFlowPrev").click(function(){
		});
		
		var doprev = function (x){
		}

		$(".jFlowNext").click(function(){
		});

		var donext = function (x){
			$(".jFlowControl").removeClass(selected_class);
			
			if (cur < maxi - 1) {
				dox(cur+1);
			} else {
				dox(0);
			}
			
			$(".jFlowControl").eq(cur).addClass(selected_class);
		}

		var dox = function (x){
			$(".jFlowControl").removeClass(selected_class);
			$(opts.slides).children().stop();
			if (cur < x) {
				// fade in x
				$(opts.slides).children().eq(x).css({
					top: '0'
				}).animate({
					opacity: 1
				}, opts.duration, opts.easing, function(){
					// turn on all between when done
					$(opts.slides).children().slice(cur-1,x+1).css({
						opacity: 1,
						top: '0'
					});
				});
			} else if (cur > x) {
				// turn off all between immediately
				$(opts.slides).children().css({
					opacity: 0,
					top: opts.height
				});
				$(opts.slides).children().eq(cur).css({
					opacity: 1,
					top: '0'
				})
				$(opts.slides).children().eq(x).css({
					opacity: 1,
					top: '0'
				})
				// fade out current
				$(opts.slides).children().eq(cur).animate({
					opacity: 0
				}, opts.duration, opts.easing, function(){
					$(this).css({
						top: opts.height
					});
				});
			}
			// set current to x
			cur = x;
			$(".jFlowControl").eq(cur).addClass(selected_class);
		}
		
		// var showx = function(x,anim=0){
		// 	// retarget
		// 	x = $(opts.slides).eq(x);
		// 	
		// 	// initial state
		// 	$(x).css({
		// 		opacity: 0,
		// 		top: '0px'
		// 	})
		// 	
		// 	if (anim) {
		// 		$(x).animate({
		// 			opacity: 1,
		// 		})
		// 	} else {
		// 		$(x).css({
		// 			opacity: 1,
		// 		})
		// 	}
		// }
		// 
		// var hidex = function(x,anim=0){
		// 	// retarget
		// 	x = $(opts.slides).eq(x);
		// 	
		// 	// initial state
		// 	$(x).css({
		// 		opacity: 1,
		// 		top: '0px'
		// 	})
		// 	
		// 	if (anim) {
		// 		$(x).animate({
		// 			opacity: 0,
		// 		}, opts.duration, opts.easing, function() {
		// 			$(this).css({
		// 				top: opts.height,
		// 			})
		// 		})
		// 	} else {
		// 		$(x).css({
		// 			opacity: 0,
		// 			top: opts.height,
		// 		})
		// 	}
		// }
		
		var dotimer = function(){
			if(timer != null) {
				clearInterval(timer);
				timer = setInterval(function() {
					donext();
				}, opts.pause);
			}
		}
		
		var starttimer = function (){
			timer = true;
			dotimer();
		}
		
		starttimer();
	};

	$.fn.jFlow.defaults = {
		easing: "swing",
		duration: 1000,
		pause: 2000,
		width: "100%",
		hidden: {
			opacity: '0'
		},
		shown: {
			opacity: '1'
		}
	};

})(jQuery);