(function($) {

    $.fn.jFlow = function(options) {
        var opts = $.extend({}, $.fn.jFlow.defaults, options),
			randNum = Math.floor(Math.random() * 11),
			jFC = $(opts.slide).find(opts.controller),
			jFS = opts.slideWrapper,
			jSel = opts.selectedWrapper,
			jFSlides,
			inProgress = false;


        var cur = 0;
        var timer;
        var maxi = $(jFC).length;
        // sliding function
        var slide = function(dur, i) {

            inProgress = true;

            var currSlide = $(jFSlides[cur]),
				nextSlide = $(jFSlides[i]),
				slideWidth = currSlide.width();

            $(opts.slides).children().css({
                overflow: "hidden"
            });
            $(opts.slides + " iframe").hide().addClass("temp_hide");
            nextSlide.css({ left: slideWidth + "px", top: 0 });

            currSlide.animate({
                left: "-" + slideWidth + "px"
            },
				opts.duration,
				opts.easing,
				function() {
				    $(".temp_hide").show();
				}
			);

            nextSlide.animate({
                left: 0
            },
				opts.duration,
				opts.easing,
				function() {
				    inProgress = false;
				}
			);


        }
        jFC.each(function(i) {
            $(this).click(function() {
                if (cur === i || inProgress) return;
                dotimer();
                if ($(opts.slides).is(":not(:animated)")) {
                    $(jFC).removeClass(jSel);
                    $(this).addClass(jSel);
                    var dur = Math.abs(cur - i);
                    slide(dur, i);
                    cur = i;
                }
            });
        });

        $(opts.slides).before('<div id="' + jFS.substring(1, jFS.length) + '"></div>').appendTo(jFS);

        $(opts.slides).find("div").each(function() {
            $(this).before('<div class="jFlowSlideContainer"></div>').appendTo($(this).prev());
        });
        jFSlides = $(opts.slides).children()

        //initialize the controller
        $(jFC).eq(cur).addClass(jSel);

        var resize = function(x) {
            $(jFS).css({
                position: "relative",
                width: opts.width,
                height: opts.height,
                overflow: "hidden"
            });
            //opts.slides or #mySlides container
            $(opts.slides).css({
                position: "relative",
                width: $(jFS).width() * $(jFC).length + "px",
                height: $(jFS).height() + "px",
                overflow: "hidden"
            });
            // jFlowSlideContainer			
            jFSlides.each(function(i, el) {
                $(el).css({
                    position: "absolute",
                    width: $(jFS).width() + "px",
                    height: $(jFS).height() + "px",
                    left: 0,
                    top: (cur == i) ? 0 : $(jFS).width() + "px",
                    overflow: "hidden"
                });
            });

        }

        // sets initial size
        resize();

        // resets size
        $(window).resize(function() {
            resize();
        });

        $(opts.prev).click(function() {

            doprev(); //dotimer();

        });

        $(opts.next).click(function() {
            //dotimer();
            donext();

        });

        var doprev = function(x) {
            if ($(opts.slides).is(":not(:animated)")) {
                //                var dur = 1;
                //                if (cur > 0)
                //                    cur--;
                //                else {
                //                    cur = maxi - 1;
                //                    dur = cur;
                //                }
                //$(jFC).removeClass(jSel);
                //$(jFC).eq(cur).addClass(jSel);
                //slide(dur, cur);
                var setPrev = cur - 1;
                if (setPrev < 0)
                    setPrev = maxi - 1;
                $(jFC[setPrev]).trigger("click");
            }
        }

        var donext = function(x) {
            if ($(opts.slides).is(":not(:animated)")) {
                //                var dur = 1;
                //                if (cur < maxi - 1)
                //                    cur++;
                //                else {
                //                    cur = 0;
                //                    dur = maxi - 1;
                //                }
                //                $(jFC).removeClass(jSel);
                //                //$(jFS).fadeOut("fast");
                //                slide(dur, cur);
                //                //$(jFS).fadeIn("fast");
                //                $(jFC).eq(cur).addClass(jSel);
                var setNext = cur + 1;
                if (setNext > maxi-1)
                    setNext = 0;
               
                $(jFC[setNext]).trigger("click");
            }
        }

        var dotimer = function(x) {
            if ((opts.auto) == true) {
                if (timer != null)
                    clearInterval(timer);

                timer = setInterval(function() {
                    var next = (cur + 1 > jFSlides.length - 1) ? 0 : cur + 1;
                    $(jFC[next]).trigger("click");
                }, 9000);
            }
        }

        dotimer();
    };

    $.fn.jFlow.defaults = {
        controller: ".jFlowControl", // must be class, use . sign
        slideWrapper: "#jFlowSlide", // must be id, use # sign
        selectedWrapper: "jFlowSelected",  // just pure text, no sign
        slideSelector: ".slide-wrapper",
        auto: false,
        easing: "swing",
        duration: 18000,
        width: "100%",
        prev: ".jFlowPrev", // must be class, use . sign
        next: ".jFlowNext" // must be class, use . sign
    };

})(jQuery);

