/**
 * 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.1.0
 *
 *
 *
 * Modified by Daniel Ruiz to handle location hash 
 */


(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="slide"><div class="left"></div></li>').appendTo(me),
            $li = $("li", this), 
            curr = findCurr();


        $li.click(function(e) {
        	$('li.current', me).not( $(this) ).removeClass('current');
        	//
            move( this );
            return o.click.apply(this, [e, this]);
        });

		addCurrentClass( curr );
        setCurr(curr);
        
        function findCurr() {
            var el;
            
            if( window.location.hash != "" ) {
                var hash =window.location.hash,
                    hash = hash.replace( /^#/, '' );
                
                el = $( 'li#page-' + hash, me )[0];
            }
            
            if( !el || el === undefined ) {
                el = $( 'li.current', me )[0] || $( $li[0] ).addClass( 'current' )[0];
            }
            
            return el;
        };
        
        function addCurrentClass( el ) {
            $( 'li.current', me ).not( $( el ) ).removeClass( 'current' );
            
            /*
$( el ).addClass( 'current' ).find( 'img' ).css( 'opacity', 1 );
            
            $( 'li', me ).not( el ).find( 'img' ).css( 'opacity', 0.3 );
*/
			$( el ).addClass( 'current' ).css( 'opacity', {'opacity':1, 'filter':''} );
            
            $( 'li', me ).not( el ).not( $('li.slide') ).css( {'opacity':0.3, 'filter':''} );
        };

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

        function move(el) {
            $back.each(function() {
                $.dequeue(this, "fx"); }
            ).animate({
                width: el.offsetWidth,
                left: el.offsetLeft
            }, o.speed, o.fx, function( slider ) {
                setCurr( el );
                addCurrentClass( el );
            });
        };
    });
};
})(jQuery);

