/* 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

// opacity changes on slide modified by Arthur Gapusan 11 Aug 09

(function($) {

		
	$.fn.jFlow = function(options) {
		var opts = $.extend({}, $.fn.jFlow.defaults, options);
		var cur = 0;
		
		var enable_opacity_change = opts.enable_opacity_change; /* opacity change on slide, false to disable */
		var opacity_delay = opts.duration / 2; /* the delay of opacity is one half of the delay of slide transition */
	
		var timer;
		var selected_class = "jFlowSelected";
		var maxi = $(".jFlowControl").length;
		$(this).find(".jFlowControl").each(function(i){
			$(this).click(function(){
			

				var selectedAlready = false;
				if ($(this).is('.jFlowSelected')) {
					selectedAlready = true;
				}
				
				$(".jFlowControl").removeClass(selected_class);
				$(this).addClass(selected_class);
				//alert(cur);
				//alert(i);
				
				var dur = Math.abs(cur-i);
				
				clearInterval(timer);
        		timer = setInterval(function() {
	                	dotimer();
    	        	}, opts.duration*(dur));
				
			
				if (!selectedAlready) {
					if (enable_opacity_change) { 
						$('#slideshow-content').animate({opacity: 0.1},  opacity_delay);
						$('#slideshow-content').animate({opacity: 0.1},  (opts.duration*(dur)) - (opacity_delay * 2));
					}
				}
				$(opts.slides).animate({
					marginLeft: "-" + (i * $(opts.slides).find(":first-child").width() + "px")
				}, opts.duration*(dur));
				
				if (!selectedAlready) {
					if (enable_opacity_change) { 
						$('#slideshow-content').animate({opacity: 1}, opacity_delay);
					}
				}
				cur = 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: "relative",
				width: $("#jFlowSlide").width()+"px",
				height: $("#jFlowSlide").height()+"px",
				"float":"left"
			});

			$(opts.slides).css({
				marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")
			});
		}

		resize();

		$(window).resize(function(){
			resize();
		});

		$(".jFlowPrev").click(function(){
			dotimer();
			doprev();
		});
		
		var doprev = function (x){
			if (cur > 0)
				cur--;
			else
				cur = maxi -1;
	
			$(".jFlowControl").removeClass(selected_class);
			
			if (enable_opacity_change) { 
				$('#slideshow-content').animate({opacity: 0.1},opacity_delay);
			}
			
			$(opts.slides).animate({
				marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")
			}, opts.duration);
			$(".jFlowControl").eq(cur).addClass(selected_class);
			
			if (enable_opacity_change) { 
				$('#slideshow-content').animate({opacity: 1},opacity_delay);
			}
		}

		$(".jFlowNext").click(function(){
			donext();
			dotimer();
		});

		var donext = function (x){
			if (cur < maxi - 1)
				cur++;
			else
				cur = 0;

			$(".jFlowControl").removeClass(selected_class);
			if (enable_opacity_change) { 
				$('#slideshow-content').animate({opacity: 0.1}, opacity_delay);
			}	
			$(opts.slides).animate({
				marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")
			}, opts.duration);
			$(".jFlowControl").eq(cur).addClass(selected_class);
			if (enable_opacity_change) { 
				$('#slideshow-content').animate({opacity: 1}, opacity_delay);
			}
		}

		var dotimer = function (x){
			if(timer != null) 
			    clearInterval(timer);
			    
        		timer = setInterval(function() {
	                	donext();
    	        	}, opts.timeout);
    	        	
    	        }

		dotimer();
	};

	$.fn.jFlow.defaults = {
		easing: 'easeout',
		duration: 400,
		width: "100%"
	};

})(jQuery);
