$(document).ready(function(){

    (function($) {
        $.fn.easyFader = function(options) {

            var obj = $(this);

            var defaults = {
                fadeIn: 	1000,
                fadeOut: 	1000,
                pause: 		1000,
                type:		"double",
                jump:       false,
                jumpspeed:  500,
                jumpcount:  2,
                jumpdistance: 50
            };

            var options = $.extend(defaults, options);

            function doRun(e, options) {

                var $active = $(e).children('UL').children('LI.active');
                var $next = $active.next().length ? $active.next() : $(e).children('UL').children('LI:first');

                $active.removeClass('active');
                $active.addClass('notactive');
                $next.addClass('active');
                $next.removeClass('notactive');

                switch (options.type) {
                    case "single":
                        $active.animate({opacity: 0.0}, options.fadeOut, function() {
                            $active.hide();
                            $next.animate({opacity: 1.0}, options.fadeIn);
                        });
                        $diff = options.fadeOut + options.fadeIn;
                        break;
                    case "double":
                    case "default":
                        $next.show();
                        $active.show();
                        $next.animate({opacity: 1.0}, options.fadeIn);
                        $active.animate({opacity: 0.0}, options.fadeOut, function() {
                            $active.hide();
                        });
                        $diff = options.fadeIn < options.fadeOut ? options.fadeOut : options.fadeIn;
                        if (jump) {
                            jump($next, options.jumpspeed, options.jumpcount, options.jumpdistance);
                        }
                        break;
                };

                setTimeout(function(){
                    doRun(e, options);
                },$diff+options.pause);

            }

            function jump(e, speed, times, distance) {
                if (times) {
                    oldtop = $(e).offset().top;
                    newtop = oldtop*1+distance*1;
                    newend = oldtop*1-distance*1;

                    n1 = "+"+newtop+"px";

                    while (times) {
                        $(e).animate({'top': '+'+newtop+'px'}, speed).animate({'top': '+'+newend+'px'}, speed).animate({'top': '+'+oldtop+'px'}, speed);
                        times--;
                    }
                    
                }
            }

            $(this).children('UL').children('LI:first').addClass("active");
//            $(this).children('UL').children('LI:active').animate({opacity: 1.0}, 3);
            $(this).children('UL').children('LI:first').animate({opacity: 1.0}, 3);
            $(this).children('UL').children('LI').addClass("notactive");
            $(this).children('UL').children('LI:first').removeClass("notactive");
            $(".notactive").animate({opacity: 0.0}, 3);
            $(".notactive").hide();

            timeout = setTimeout(function(){
                doRun(obj, options);
            },options.pause);

        };
    })(jQuery);
});


