/**
 * jQuery UI Lavalamp v1.0
 * Adapted from v0.2 by Ganeshji Marwaha (gmarwaha.com)
 * v1.0 by David Baumgold (davidbaumgold.com)
 *
 * This project is dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Required:
 *  jQuery 1.2.1 or higher
 *  jQuery UI 1.6 or higher (ui.core.js)
 * Recommended:
 *  jQuery Easing plugin (http://gsgd.co.uk/sandbox/jquery/easing/)
 *  jQuery UI Tabs
 */
(function($) {
    
$.widget("ui.lavalamp", {
    _init: function () {
        var o = this.options, $ll = this;
        
        // get existing list items
        this.items = $(this.element).children('li');
        // create lava list element
        this.lava = $('<li class="' + o.lavaClass + '"><div></div></li>').appendTo(this.element);
        // select selected element, and store in this.active
        // first check for tabs, use that if found
        if($.ui.tabs && this.element.hasClass('ui-tabs-nav')) {
            this.select(this.items.filter('.ui-tabs-selected'));
        } else { // next, check for the specified active class
            var act = this.items.filter('.'+o.selectedClass);
            if(act.length == 1) {
                this.select(act);
            } else {
                this.select(this.items.eq(o.selected));    
            }
        }
        
        // set up events
        this.items.mouseover(function () {
            $ll.move(this);
        });
        this.element.mouseout(function () {
            $ll.move($ll.active);
        });
        this.items.click(function(e) {
            $ll.select(this);
            return o.click.apply(this, [e, this]);
        });
        
        //jQuery tabs seems to swallow the click event, preventing it from
        //reaching lavalamp, so we will tell the tabs to select the
        //lavalamp directly.
        if($.ui.tabs) {
           $('.ui-tabs-nav a').click(function (event) {
               var $target = $(event.target);
               $target.parents('li').click();
           });
        }
    },
    /* this moves the lava to the list element you are hovering
       over, or back to the active list element when you stop
       hovering. */
    move: function (el) {
        var o = this.options;
        // hack to allow for index selection
        if(typeof el == 'number') {
            var $el = this.items.eq(el);
        } else {
            var $el = $(el);
        }
        this.lava.stop() // first stop previous animation, if any
            .animate({ // smooth animation
                "left": $el.attr('offsetLeft')+"px",
                "width": $el.attr('offsetWidth')+"px"
            }, o.speed, o.fx);
    },
    select: function (el) {
        var o = this.options;
        // hack to allow for index selection
        if(typeof el == 'number') {
            var $el = this.items.eq(el);
        } else {
            var $el = $(el);
        }
        this.lava.stop() // first stop previous animation, if any
            .css({ // jump without animating
                "left": $el.attr('offsetLeft')+"px",
                "width": $el.attr('offsetWidth')+"px"
            });
        this.active = $el;
        o.selected = this.items.index($el);
    },
    destroy: function() {
        var o = this.options;
        // unbind events
        this.items.unbind('mouseover');
        this.element.unbind('mouseout');
        this.items.unbind('click');
        // remove lava
        this.lava.remove();
    }
});

$.extend($.ui.lavalamp, { 
    defaults: {
        fx: 'easeOutBack', //use any easing keyword
        speed: 500,
        selected: 0,
        
        // CSS classes
        lavaClass: 'ui-lavalamp-lava',
        selectedClass: 'ui-lavalamp-selected',
        click: function(){}
    }
});

})(jQuery);