(function($) {
$.fn.lightLamp = 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 = $("li.current", this)[0] || $($li[0]).addClass("current")[0];

        $li.not(".back").hover(function() {
            move(this);
        }, noop);

        $(this).hover(noop, function() {
        	
        	if($(".submain_menu:visible").length == 0)
        		move(curr);
        });
        
     
        $("div [rel|='mainmenulink']").hover(function(){
        	$(this).attr('hovered', 'hovered');
        	 move('#'+$(this).attr('id'));	    	
	    },function(){
	    	$(this).removeAttr('hovered');
			
	    	$(this).fadeOut();    	
	    	move(curr);
	    	
	    });

        
	    $("[id|='mainmenulink']").click(function(){
	    	 var rel = $("div [rel='"+$(this).attr('id')+"']");

	    	 if(rel.length == 0)
	    		 return true;
	    	 
	   		 if(rel.is(':visible'))
	   			hide_submenu($(this));
	   		 else
	   			show_submenu($(this));
	   		 
	   		 return false;
	   });        
        
/*        
	    $("[id|='mainmenulink']").hoverIntent({    
	   		 interval: 300,
	   	     over: show_submenu, // function = onMouseOver callback (REQUIRED)    
	   	     timeout: 100, // number = milliseconds delay before onMouseOut    
	   	     out: hide_submenu // function = onMouseOut callback (REQUIRED)    
	   });
*/	    

        $li.click(function(e) {
            setCurr(this);
            return o.click.apply(this, [e, this]);
        });

        setCurr(curr);

        function setCurr(el) {
        	var longueur = $(el).innerWidth() / 2;
            $back.css({ "left": el.offsetLeft - 90 + longueur +"px", "width": "151px" });
            curr = el;
        };

        function move(el) {
        	var longueur = $(el).innerWidth() / 2;
            $back.each(function() {
                $.dequeue(this, "fx"); }
            ).animate({
                width: "151",
                left: el.offsetLeft - 90 + longueur
            }, o.speed, o.fx);
        };

		function show_submenu(obj)
		{
			// animation
			$("div [rel='"+obj.attr('id')+"']").fadeIn();
		};
		
		
		function hide_submenu(obj)
		{	
			// animation
			$("div [rel='"+obj.attr('id')+"']").fadeOut();
			move(curr);
		}
	    

    });
};
})(jQuery);

