;(function($){
	$.fn.lcaAccordion = function(options){

		var acc = $.fn.lcaAccordion,
	    settings = {
            itemSelector: '>div',   // selector for individual menu items within the accordion   
            easing: 'swing',

            speed: {
                base: 400,
                /* if any of the below settings are specified in the options, they override the base speed option
                relaxed: 400,     // duration of relaxation after mouse leaves accordion menu
                compressed: 400,  // duration of compression when mouse enters accordion menu
                active: 400,      // duration of expansion when mouse enters an individual menu item
                inactive: 400,    // duration of contraction when mouse leaves an individual menu item
                */
            },

            animations: [                          // an array specifying elements and properties to be animated
                { selector: ">div",                // selector for the animated element
                  relaxed:    { height: "20px" },  // state of selected element when mouse is not in accordion
                  compressed: { height: "0px" },   // state of selected element when mouse is in accordion
                  active:     { height: "200px" }, // state of selected element when mouse is in the element
                  inactive:   { height: "0px" }    // state of selected element when mouse is not in the element
                }
                // ... you can add multiple elements to be animated ...
            ]
	    },

        // classes to apply to menu items while animations are occuring (used internally)
        statusLabels = {
            active:     'acc-active',
            inactive:   'acc-inactive',
            relaxed:    'acc-relaxed',
            compressed: 'acc-compressed'
        };
    	animationLabel = {
    		activating     : 'acc-activating',
    		inactivating   : 'acc-inactivating',
    		relaxing       : 'acc-relaxing',
    		compressing    : 'acc-compressing',
    	};

        // apply an animationType animations to the given menuItem
        // animationType : ['relaxed'|'compressed'|'active'|'inactive']
        // animationClass : one of the members of animationLabel
        var applyAnimation = function (menuItem, animationType, animationClass) {
            $.each(settings.animations, function(idx, animation) {
                var $$ = menuItem.find(animation.selector);  // find the element to be animated


                // if the mouse leaves the accordion and comes back in before
                // the relaxation animation is finished, we need to allow the
                // expansion animation to continue (ie, filter it out so we
                // don't do the compression animation))
                if ( animationType === 'compressed' ) {
                    // more readable form:
                    // :not(.activating) .relaxing, :not(:animated)
                    // (ie, compress any menu item that's relaxing and not activating and compress it;
                    //  also compress any menu items that are not currently animated)
                    $('#before').text($$.length);
                    $$ = $$.filter([':not(.',animationLabel.activating,') .',animationLabel.relaxing,',:not(:animated)'].join(''));
                    $('#after').text($$.length);

                }

                // remove all animationLabel classes other than the given animationClass
                for ( c in animationLabel ) {
                    if ( animationLabel[c] !== animationClass ) {
                        $$.removeClass(animationLabel[c]);
                    }
                }
                $$.addClass(animationClass);

                $$.stop().animate(
                    animation[animationType],
                    {duration: settings.speed[animationType] ? settings.speed[animationType] : settings.speed.base,
                     complete: function() { $$.removeClass(animationClass); },
                     easing: settings.easing}
                );

            });
        };

			
        // return the jquery object so the lcaAccordion function can be chained
		return this.each(function() {

            if ( options ) { $.extend(settings, options); }

            // apply animations to individual menu item hover events
            $(this).find(settings.itemSelector).hover(function() {
                $(this).addClass(statusLabels.active);
                applyAnimation($(this), 'active', animationLabel.activating);
            }, function () {
                applyAnimation($(this), 'inactive', animationLabel.inactivating);
                $(this).removeClass(statusLabels.active);
            });

            // apply animations for whole accordion
            $(this).hover(function() {
                applyAnimation($(this), 'compressed', animationLabel.compressing);
            }, function() {
                applyAnimation($(this), 'relaxed', animationLabel.relaxing);
            });

		});
	};
})(jQuery);

