(function($){
  $.fn.lavaLamp = function (o) {
    o = $.extend({
        fx:"linear",
        speed:500,
        click:function () {},
        stall_before_reset:2000
      },
      o||{}
    );
    return this.each(function () {
      var timer = null;
      var b = $(this);
      var $li = $(">li",this);
      $li
        .find('ul')
        .css({
            'display':'block',
            'z-index':4
          })
        .each(function () {
            $(this)
              .data('extend_top', $(this.parentNode.parentNode).height())
              .data('base_top', $(this.parentNode.parentNode).height() - $(this).height() -3)
              .css({'top':$(this).data('base_top')});
          });
      var $back = $('<li class="back"><div class="left"></div></li>').appendTo(b);
      var $curtain = $('<li class="curtain"><span></span></li>').appendTo(b);
      var curr = $("li.current",this)[0] || $($li[0]).addClass("current")[0];
      var sub_shown = null;

      $li.bind('mouseenter', function () { enter(this) });
      $(this).bind('mouseleave', function(){ leave(curr) });
      $li.click(function (e) { return o.click.apply(this,[e,this]) });
      setCurr(curr);

      function setCurr (a) {
        $back.css({
            "left":a.offsetLeft-evo.conf.floater_buffer.left+"px",
            "width":a.offsetWidth+evo.conf.floater_buffer.width+"px"
          });
        curr=a;
        move(a);
      };

      function enter (a) {
        if (timer) clearTimeout(timer);
        move(a);
      }

      function leave (a) {
        if (timer) clearTimeout(timer);
        timer = setTimeout(function (a) {
            return function () { move(a) };
          }(a), o.stall_before_reset);
      }

      function move (a) {
        $back
//           .each(function () { $(this).dequeue() })
          .stop(true)
          .animate({
              width:a.offsetWidth+evo.conf.floater_buffer.width,
              left:a.offsetLeft-evo.conf.floater_buffer.left
            }, o.speed, o.fx)
        if (sub_shown) {
          sub_shown
            .stop(true)
            .css({'z-index':3})
            .animate({top:sub_shown.data('base_top')});
        }
        sub_shown = $('ul', a);
        sub_shown
          .stop(true)
          .css({'z-index':4})
          .animate({top:sub_shown.data('extend_top')});
      }
    })
  }
})(jQuery);
