/**
 * Slider - A menu plugin for jQuery with hover effect.
 * Version 1.0.0, tested with jQuery v1.4.2
 *  
 * Copyright (c) 2010 by Silverpeak Software (www.silverpeak.ch)
 * Licensed under the MIT and GPL licences:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
 * The Slider plugin is an adaptation of the cool and 
 * powerful LavaLamp menu plugin originally released 
 * in 2007 by Ganeshji Marwaha.
 * http://gmarwaha.com/blog/?p=7 
**/

(function($) {
$.fn.slider = function(o) {
    o = $.extend({ fx: "linear", speed: 500, click: function(){} }, o || {});

    return this.each(function() {
        var me = $(this), noop = function(){},
            $back = $('<li class="back"><div class="left"></div></li>').appendTo(me),
            $li = $("li", this),
            curr = null;

        $back.css({ "opacity": "0.0" });
        $li.not(".back").hover(function() {
            if (curr == null) {
              setCurr(this);
            }
            move(this);
        }, noop);

        $(this).hover(noop, function() {
           setCurr(null);
           move(null);
        });

        function setCurr(el) {
            if (el != null)
            {
              $back.stop();
              $back.css({ "left": el.offsetLeft+"px", "width": el.offsetWidth+"px" });
              $back.animate({ opacity: 1.0 });
            }
            curr = el;
        };

        function move(el) {
            if (el == null) {
              $back.each(function() {
                  $(this).dequeue(); }
              ).animate({
                opacity : 0.0
              });
            }
            else {
              $back.each(function() {
                  $(this).dequeue(); }
              ).animate({
                  width: el.offsetWidth,
                  left: el.offsetLeft
              }, o.speed, o.fx);
            }
        };

    });
};
})(jQuery);

