/* 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+
*/
(function (A) {
    A.fn.jFlow = function (D) {
        var E = A.extend({}, A.fn.jFlow.defaults, D);
        var F = 0;
        var B = A(".jFlowControl").length;
        var timeout= E.timeout;
        var timercontrol=null;
        var timerreset=false;

        A("div.slide", E.slides).clone(true).appendTo(E.slides);
        
        if(E.transition=="fade")
        	A(E.slides+" > .jFlowSlideContainer:not(:first-child)").hide();

        var timerslide=function(){ 
            var doreset=false;

        	if(timerreset){
        		timeout=E.timeout/2;
        		timerreset=false;
        	}
            //console.log("F: "+F + " == B: "+ B + " - 1");
            if( F == B - 1  && E.transition == "slide"){
                //console.log('hit');
                doreset=true; }
	        if (F < B - 1) {

                F++;
            } else {
                F=0;
            }
            
            A(".jFlowControl").removeClass("jFlowSelected");
            var nextSlide=(F<B?F+1:0);
            if(E.transition=="fade"){
				A(E.slides+" > .jFlowSlideContainer").stop(true,true);
				A(E.slides+" > .jFlowSlideContainer").eq(F-1).fadeOut(function(){ A(E.slides+" > .jFlowSlideContainer").eq(nextSlide-1).fadeIn() });
			}
            else {
	            A(E.slides).animate({
	                marginLeft: "-" + ((doreset?B:F) * A(E.slides).find(":first-child").width() + "px")
	            }, E.duration, function(){
                    if(doreset)
                    {
                        A(E.slides).css('marginLeft',"0px");
                    }
                });
        	}
            A(".jFlowControl").eq(F).addClass("jFlowSelected") 
        };

        timercontrol= setInterval(timerslide,timeout);

        A(this).find(".jFlowControl").each(function (G) {
            A(this).click(function () {
                A(".jFlowControl").removeClass("jFlowSelected");
                A(this).addClass("jFlowSelected");
                var I = G + B;
                var H = (G<F?Math.abs(F - I):Math.abs(F - G));
	            if(E.transition=="fade"){
					A(E.slides+" > .jFlowSlideContainer").stop(true,true);
					A(E.slides+" > .jFlowSlideContainer").eq(F).fadeOut(function(){ A(E.slides+" > .jFlowSlideContainer").eq(G).fadeIn() });
				}
	            else {
	                A(E.slides).animate({
	                    marginLeft: "-" + ((G<F?I:G) * A(E.slides).find(":first-child").width() + "px")
	                }, E.duration * (H),function(G,F){ return function(){
                        if(G<F)
                            A(E.slides).css('marginLeft',"-" + (G * A(E.slides).find(":first-child").width() + "px"));
                    }}(G,F));
			    }
                F = G;
                timeout=E.timeout*2;
                clearInterval(timercontrol);
                timerreset=true;
                timercontrol=setInterval(timerslide,timeout);
            })
        });
        A(E.slides).before('<div id="jFlowSlide"></div>').appendTo("#jFlowSlide");
        A(E.slides).find("div").each(function () {
            A(this).before('<div class="jFlowSlideContainer"></div>').appendTo(A(this).prev())
        });
        A(".jFlowControl").eq(F).addClass("jFlowSelected");
        var C = function (G) {
            A("#jFlowSlide").css({
                position: "relative",
                width: E.width,
                height: E.height,
                overflow: "hidden"
            });
            A(E.slides).css({
                position: "relative",
                width: A("#jFlowSlide").width() * A(".jFlowControl").length * 2 + "px",
                height: A("#jFlowSlide").height() + "px",
                overflow: "hidden"
            });
            A(E.slides).children().css({
                position: "relative",
                width: A("#jFlowSlide").width() + "px",
                height: A("#jFlowSlide").height() + "px",
                "float": "left"
            });
            A(E.slides).css({
                marginLeft: "-" + (F * A(E.slides).find(":first-child").width() + "px")
            })
        };
        C();
        A(window).resize(function () {
            C()
        });
        A(".jFlowPrev").click(function () {
            if (F > 0) {
                F--
            } else {
                F = B - 1
            }
            A(".jFlowControl").removeClass("jFlowSelected");
            A(E.slides).animate({
                marginLeft: "-" + (F * A(E.slides).find(":first-child").width() + "px")
            }, E.duration);
            A(".jFlowControl").eq(F).addClass("jFlowSelected")
        });
        A(".jFlowNext").click(function () {
            if (F < B - 1) {
                F++
            } else {
                F = 0
            }
            A(".jFlowControl").removeClass("jFlowSelected");
            A(E.slides).animate({
                marginLeft: "-" + (F * A(E.slides).find(":first-child").width() + "px")
            }, E.duration);
            A(".jFlowControl").eq(F).addClass("jFlowSelected")
        })
    };
    A.fn.jFlow.defaults = {
        easing: "swing",
        duration: 400,
        width: "100%",
        timeout: 4000,
        transition: "slide"
    }
})(jQuery);
