(function($) {
$.fn.menuLamp = function(options) {
    options = (options == null) ? new Array() : options;

    var const_LO = 4;   //Растяжение в лево и в право на данное количество px

    //для каждого выбранного элемента 
    return this.each(function() {

        var me = $(this);        
        var noop = function(){}; //пустая функция

        $lamp = $("<div class='menu-lamp'></div>")
                .append('<div class="menu-lamp-top"><div class="menu-lamp-top-inner"></div></div>')
                .append('<div class="menu-lamp-centre"></div>')
                .append('<div class="menu-lamp-bottom"><div class="menu-lamp-bottom-inner"></div></div>');
        $lamp.hide();
        $(me).parent().append($lamp);

        $a = $("a", this);
        curr = ($("a.current", this).size() > 0) ? $("a.current", this)[0]: null;
        if(options.current != null) {
           $("a.current", this).removeClass("current");
           if(options.current >= 0)
               curr = $("a", this)[options.current];
        }


        $a.hover(function() {
            move(this);
        }, noop);

        $(this).hover(noop, function() {
            move(curr);
        });

        setCurr(curr);
        move(curr);

        function setCurr(el) {
            if(el != null) {

                $lamp.css("left",(getOffsetLeft(el)-const_LO)+"px")
                     .css("width",(el.offsetWidth+(const_LO*2))+"px")
                     .css("height",el.offsetHeight + "px")
                     .css("top",getOffsetTop(el)+"px");
                $(".menu-lamp-centre",$lamp).css("height",(el.offsetHeight-18)+"px");
            }
            curr = el;
        }

        function getOffsetLeft(el) {
            var offsetLeft = el.offsetLeft;
            if(el.offsetParent != null) {
                offsetLeft += getOffsetLeft(el.offsetParent);
            }
            return offsetLeft;
        }

        function getOffsetTop(el) {
            var offsetTop = el.offsetTop;
            if(el.offsetParent != null) {
                offsetTop += getOffsetTop(el.offsetParent);
            }
            return offsetTop;
        }

        function move(el) {


            $("a",me).each(function() {
                $(this).stop();}
            ).css('color', '#274b8a');

            if(el != null) {

                $(el).each(function() {
                    $(this).stop();}
                ).css('color','#274b8a')
                 .animate({
                    color: '#ffffff'
                }, 500,"backout");
              
                $lamp.show();
                $lamp.each(function() {
                    $(this).stop();}
                ).animate({
                    width: el.offsetWidth+(const_LO*2),
                    left: getOffsetLeft(el)-const_LO,
                    top: getOffsetTop(el),
                    height: el.offsetHeight
                }, 500,"backout");

                var ch = el.offsetHeight - 18;
                $(".menu-lamp-centre",$lamp).each(function() {
                    $(this).stop();}
                ).animate({
                    height: ch
                }, 500,"backout");


            }
            else {
                $(".menu-lamp-centre",$lamp).each(function() {
                    $(this).stop();}
                );

                $lamp.each(function() {
                    $(this).stop();}
                ).hide();
            }

        };

    });
};
})(jQuery);

