/* 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 Andrew J Wright : 2 Nov 2010
//modified by LandRover 30th May 08

(function($) {

    $.fn.jFlow = function(options) {
        var opts = $.extend({}, $.fn.jFlow.defaults, options);
        var cur = 1;
        var timer;
		var active = false;
        //var selected_class = "jFlowSelected";
        var maxi = $(".jFlowControl").length;
		/*
        $(this).find(".jFlowControl").each(function(i) {
            $(this).click(function() {
                dotimer();

                $(".jFlowControl").removeClass(selected_class);
                $(this).addClass(selected_class);

                var dur = Math.abs(cur - i);
				_moveToIndex(i);

                cur = i;
            });
        });
		*/
		var container = $(opts.slides);
		// clone the first feature item and tack it on the end of the group
		var slides = container.children();
		var firstSlide = slides.eq(0);
		var lastSlide = slides.eq(slides.length-1);
		firstSlide.clone().insertAfter(lastSlide);
		lastSlide.clone().insertBefore(firstSlide);


		// Create outer-most container
		container.before('<div id="jFlowSlide"></div>').appendTo("#jFlowSlide");

		// create containers around each item
		container.find("div").each(function() {
            $(this).before('<div class="jFlowSlideContainer"></div>').appendTo($(this).prev());
        });

		var firstSlideWidth = container.children().eq(0).width();
        //initialize the controller
        //$(".jFlowControl").eq(cur).addClass(selected_class);


		var _moveToIndex = function(){
			active=true;
			container.animate({
				left: "-" + (cur * firstSlideWidth + "px")
			},
			2000, 
			opts.easing, 
			function(){
				if (cur<1) {
					cur = maxi+cur;
					container.css({
						left: "-" + (cur * firstSlideWidth + "px")
					});
				} else if (cur>maxi){
					cur = 1;
					container.css({ left: "-" + firstSlideWidth + "px" });
				}
				active = false;
			});
		};


        var resize = function(x) {
			// resize the container that 'frames' the slider and clips the images
            $("#jFlowSlide").css({
                position: "relative",
                width: opts.width,
                height: opts.height,
                overflow: "hidden"
            });
			
			// resize the container that holds the individual features
			var _w = $("#jFlowSlide").width();
			var _h = $("#jFlowSlide").height();
            $(opts.slides).css({
                position: "relative",
                width: _w * ($(".jFlowControl").length+2) + "px",
                height: _h + "px",
                overflow: "hidden"
            });

			// ensure that the child blocks are the right size and floated properly
            $(opts.slides).children().css({
                position: "relative",
                width: _w + "px",
                height: _h + "px",
                "float": "left"
            });
			_moveToIndex();
        }

        resize();

        $(window).resize(function() {
            resize();
        });
		/* //////////////// */
        var doprev = function(x) {
            //$(".jFlowControl").removeClass(selected_class);
			if(!active){
				cur--;
				_moveToIndex();
				dotimer();
			}
            //$(".jFlowControl").eq(cur).addClass(selected_class);
        }
		/* //////////////// */
		var donext = function(x) {
            //$(".jFlowControl").removeClass(selected_class);
			if(!active){
				cur++;
				_moveToIndex();
				dotimer();
			}
            //$(".jFlowControl").eq(cur).addClass(selected_class);
        }
		/* //////////////// */
        var dotimer = function(x) {
            if (timer != null)
                clearInterval(timer);

            timer = setInterval(function() {
                donext();
            }, 10000);

        }
        dotimer();
		/* //////////////// */
        $(".jFlowPrev").click(function(e) {
            e.preventDefault();
            doprev();
        });
		/* //////////////// */
        $(".jFlowNext").click(function(e) {
            e.preventDefault();
            donext();
        });
    };

    $.fn.jFlow.defaults = {
        easing: "swing",
        duration: 800,
        width: "100%"
    };

})(jQuery);
