/**
 * LavaLamp - A menu plugin for jQuery with cool hover effects.
 * @requires jQuery v1.1.3.1 or above
 *
 * http://gmarwaha.com/blog/?p=7
 *
 * Copyright (c) 2007 Ganeshji Marwaha (gmarwaha.com)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Version: 0.2.0
 * Requires Jquery 1.2.1 from version 0.2.0 onwards. 
 * For jquery 1.1.x, use version 0.1.0 of lavalamp
 */

/**
 * Creates a menu with an unordered list of menu-items. You can either use the CSS that comes with the plugin, or write your own styles 
 * to create a personalized effect
 *
 * The HTML markup used to build the menu can be as simple as...
 *
 *       <ul class="lavaLamp">
 *           <li><a href="#">Home</a></li>
 *           <li><a href="#">Plant a tree</a></li>
 *           <li><a href="#">Travel</a></li>
 *           <li><a href="#">Ride an elephant</a></li>
 *       </ul>
 *
 * Once you have included the style sheet that comes with the plugin, you will have to include 
 * a reference to jquery library, easing plugin(optional) and the LavaLamp(this) plugin.
 *
 * Use the following snippet to initialize the menu.
 *   $(function() { $(".lavaLamp").lavaLamp({ fx: "backout", speed: 700}) });
 *
 * Thats it. Now you should have a working lavalamp menu. 
 *
 * @param an options object - You can specify all the options shown below as an options object param.
 *
 * @option fx - default is "linear"
 * @example
 * $(".lavaLamp").lavaLamp({ fx: "backout" });
 * @desc Creates a menu with "backout" easing effect. You need to include the easing plugin for this to work.
 *
 * @option speed - default is 500 ms
 * @example
 * $(".lavaLamp").lavaLamp({ speed: 500 });
 * @desc Creates a menu with an animation speed of 500 ms.
 *
 * @option click - no defaults
 * @example
 * $(".lavaLamp").lavaLamp({ click: function(event, menuItem) { return false; } });
 * @desc You can supply a callback to be executed when the menu item is clicked. 
 * The event object and the menu-item that was clicked will be passed in as arguments.
 */
(function($) {
$.fn.lavaLamp = 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-menu-item") /*|| $("li.current", this)[0] || $($li[0]).addClass("current")[0]*/;
            $li = $("li", this),   curr=$("li.current-page-parent",this)[0]||$("li.current-menu-parent",this)[0]||$("li.current-menu-item",this)[0]||$($li[0]).addClass("current-menu-item")[0];
        	$subli = $("#menu-site").find("li ul li");
        	$("ul.sub-menu").each(function(){
        		var myWidth=$(this).css("width");
        		var myHeight=$(this).css("height");
        		$(this).data("myWidth",myWidth);
        		$(this).data("myHeight",myHeight);
        		$(this).width(0);
        		$(this).height(0);
        		$(this).css("overflow","hidden");
        	});
        	//$subliParent=$subli.parent().parent();
        $li.not(".back").hover(function() {
        	var mySub=$(this).find("ul.sub-menu");
        	if(mySub.length>0) {
        		theWidth=mySub.data("myWidth");
        		theHeight=mySub.data("myHeight");
        		mySub.css("visibility","visible");
        		mySub.stop().animate({width:theWidth,height:theHeight},{queue:false,duration:500});
        	}
            move(this);
        }, /*noop*/function (){
        	var mySub2=$(this).find("ul.sub-menu");
        	if(mySub2.length>0) {
        		mySub2.stop().animate({width:0,height:0},{queue:false,duration:500},function(){
        			$(this).css("visibility","hidden");
        		});
        		
        	}
        });
        $("#menu-site").find("ul.sub-menu").hover(function() {
        	$(this).css("visibility","visible");
        },function (){
        	$(this).css("visibility","hidden");
        });
        
        $subli.hover(function() {
        	var myparent= $(this).parent().parent();
           move(myparent[0]);
        }, noop);
        $(this).hover(noop, function() {
            move(curr);
        });

        $li.click(function(e) {
            setCurr(this);
            return o.click.apply(this, [e, this]);
        });

        setCurr(curr);
        	$("#menu-marques li.current-menu-item,#menu-marques li.current-menu-parent").find("ul.sub-menu").each(function(){
        		//$(this).css("visibility","visible");
        		marquesWidth=$(this).data("myWidth");
        		marquesHeight=$(this).data("myHeight");
        		var menuOpen=$(this).find('li.current-menu-item');
        		$("#menu-site li").removeClass('current-menu-item');
        		$("#menu-site li.current-category-ancestor").addClass('current-menu-item');
        		if(menuOpen.length){
        			var the_parent= menuOpen.parent();
        			the_parent.width(marquesWidth).height(marquesHeight);
        			the_parent.css("visibility","visible");
        		}else{
        			$(this).width(marquesWidth).height(marquesHeight);
        			$(this).css("visibility","visible");
        		}
        		var newCurrent = $("#menu-site li.current-category-ancestor")[0];
        		setCurr(newCurrent);
        	});
        	$("#menu-marques li.current-post-ancestor").find("ul.sub-menu").each(function(){
        		//$(this).css("visibility","visible");
        		marquesWidth=$(this).data("myWidth");
        		marquesHeight=$(this).data("myHeight");
        		var menuOpen=$(this).find('li.current-menu-parent');
        		$("#menu-site li").removeClass('current-menu-item');
        		$("#menu-site li.current-category-ancestor").addClass('current-menu-item');
        		if(menuOpen.length){
        			var the_parent= menuOpen.parent();
        			the_parent.width(marquesWidth).height(marquesHeight);
        			the_parent.css("visibility","visible");
        		}else{
        			$(this).width(marquesWidth).height(marquesHeight);
        			$(this).css("visibility","visible");
        		}
        		var newCurrent = $("#menu-site li.current-category-ancestor")[0];
        		setCurr(newCurrent);
        	});
        function setCurr(el) {
        	var l=el.offsetLeft;
        	var w=el.offsetWidth;
        	if(l==0) w=0;
            $back.css({ "left": l+"px", "width": w+"px" });
            curr = el;
        };

        function move(el) {
        	var l=el.offsetLeft;
        	var w=el.offsetWidth;
        	if(l==0) {w=0;l=$li[0].offsetLeft;}
            $back.each(function() {
                $(this).dequeue(); }
            ).animate({
                width: w,
                left: l
            }, o.speed, o.fx);
        };

    });
};
})(jQuery);

