﻿
(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() {
                dotimer();

                $(".jFlowControl").removeClass(selected_class);
                $(this).addClass(selected_class);
                //alert(cur);
                //alert(i);
                var dur = Math.abs(cur - i);
                $(opts.slides).animate({
                    // cemerson@RM - IE7 fix
                    // marginLeft: "-" + (i * $(opts.slides).find(":first-child").width() + "px")
                    marginLeft: "-" + (i * opts.animationwidth + "px")
                }, opts.duration * (dur));
                cur = i;

                rotatecontent(cur);                
            });
        });

        $(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);
            $(opts.slides).animate({
                // cemerson@RM - IE7 fix
                // marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")
            marginLeft: "-" + (cur * opts.animationwidth + "px")
            }, opts.duration);
            $(".jFlowControl").eq(cur).addClass(selected_class);
        }

        $(".jFlowNext").click(function() {
            donext();
            dotimer();
        });

        $(".pause").click(function() {
            dopause();
        });
        $(".resume").click(function() {
            doresume();
        });

        var donext = function(x) {
            // /*  cemerson@RM */ alert('donext()');
            if (cur < maxi - 1)
                cur++;
            else
                cur = 0;

            $(".jFlowControl").removeClass(selected_class);
            $(opts.slides).animate({
                // cemerson@RM - IE7 fix
                // marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")
                marginLeft: "-" + (cur * opts.animationwidth + "px")                
            }, opts.duration);
            $(".jFlowControl").eq(cur).addClass(selected_class);

            // cemerson@RM
            // www.rmsource.com specific edit
            rotatecontent(cur);

        }

        var dotimer = function(x) {          
            if (timer != null)
                clearInterval(timer);

            timer = setInterval(function() {
                donext();
            }, 8500);

            rotatecontent(cur);

        }

        dotimer();


        var dopause = function(x) {
            if (timer != null)
                clearInterval(timer);

            timer = setInterval(function() {
                donext();
            }, 500000);

        }

        dotimer();

        var doresume = function(x) {
            if (timer != null)
                clearInterval(timer);

            timer = setInterval(function() {
                donext();
            }, 7500);

        }

        dotimer();

        $("div.jFlowSlideContainer").hover(function() {
            dopause();
        }, function() {
            doresume();
        });
    };

    $.fn.jFlow.defaults = {
        easing: "swing",
        duration: 400,
        width: "100%"
    };

})(jQuery);