(function($) {
  return $.fn.sv21slider = function(opt) {
    var beginToEndAnimation, beginToEndAnimationFinished, defaults, endToBeginAnimation, endToBeginAnimationFinished, item, lis, options, steps, timer;
    defaults = {
      startAfter: 500,
      stepTime: 200,
      fadeTime: 420,
      autostart: false,
      sort: false,
      onAnimationStart: function() {},
      onAnimationFinished: function() {}
    };
    lis = $(this).find('li');
    steps = lis.length;
    timer = null;
    options = $.extend(defaults, opt);

    if (options.sort) {
        item = steps - 1;
    } else item = 0;
    
    $(lis).each(function(step) {
      $(this).css({
        position: 'absolute',
        left: 0,
        top: 0
      });
      if (options.sort == false && step > 0) {
        $(this).hide();
      }
      if (options.sort == true && step != (steps-1)) {
          $(this).hide();
      }
    });
    $('#upanimation').live('click', function() {
      $(this).hide();
      window.clearInterval(timer);
      timer = window.setInterval((function() {
        return beginToEndAnimation(steps);
      }), options.stepTime);
      return false;
    });
    $('#downanimation').live('click', function() {
      $(this).hide();
      window.clearInterval(timer);
      timer = window.setInterval((function() {
        return endToBeginAnimation(steps);
      }), options.stepTime);
      return false;
    });
    beginToEndAnimation = function() {
      var next;
      window.clearInterval(timer);
      if (item == (steps - 1)) {
        beginToEndAnimationFinished();
        options.onAnimationFinished();
        return null;
      }
      next = $(lis[item]).next();
      $(lis[item]).css({
        zIndex: '505'
      });
      $(next).css({
        zIndex: '500'
      });
      $(next).show();
      $(lis[item]).fadeOut(options.fadeTime, function() {
        item++;
        return timer = window.setInterval((function() {
          return beginToEndAnimation();
        }), options.stepTime);
      });
    };
    endToBeginAnimation = function() {
      var prev;
      window.clearInterval(timer);
      if (item == 0) {
        endToBeginAnimationFinished();
        options.onAnimationFinished();
        return null;
      }
      prev = $(lis[item]).prev();
      //$('#content').append(+'<br />');
      $(lis[item]).css({
        zIndex: '505'
      });
      $(prev).css({
        zIndex: '500'
      });
      $(prev).show();
      $(lis[item]).fadeOut(options.fadeTime, function() {
        item--;
        return timer = window.setInterval((function() {
          return endToBeginAnimation();
        }), options.stepTime);
      });
    };
    beginToEndAnimationFinished = function() {
      $('#downanimation').show();
    };
    endToBeginAnimationFinished = function() {
      $('#upanimation').show();
    };
    if (options.autostart) {
      return timer = window.setInterval((function() {
        return beginToEndAnimation();
      }), options.startAfter);
    }
  };
})(jQuery);
