(function($) {
    $.fn.extend({
        accordion: function() {
            return this.each(function() {
                //if (!$(this).is(":visible")) return;
                if ($(this).data('accordiated'))
                    return false;
                $.each($(this).find('ul, li>div'), function() {
                    $(this).data('accordiated', true);
                    $(this).hide();
                });
                $.each($(this).find('a:not(.foo)'), function() {

                    if ($(this).parent().children().length > 1) {

                        if ($(this).parent().hasClass("onhover")) {
                            $(this).hover(function(e) {
                                callActivate(e.target);
                                return void (0);
                            }, function(e) { });
                        } else {
                            $(this).click(function(e) {
                                callActivate(e.target);
                                return void (0);
                            });
                        }
                    }
                });

                $.each($(this).find('li').filter('li:has(ul)'), function() {

                    if ($(this).children().length > 1) {

                        if ($(this).hasClass("onhover")) {
                            $(this).hover(function(e) {
                                callActivate(e.target);
                                return void (0);
                            }, function(e) { });
                        } else {
                            $(this).click(function(e) {
                                callActivate(e.target);
                                return void (0);
                            });
                        }
                    }
                });

                var active = false;
                if (location.hash)
                    active = $(this).find('a[href=' + location.hash + ']')[0];
                else if ($(this).find('li.current'))
                    active = $(this).find('li.current a')[0];

                if (active) {
                    activate(active, 'toggle', 'parents');
                    $(active).parents().show();
                }

                function callActivate(el) {
                    if ($(el).is("a")) {
                        if ($(el).parent().hasClass('active')) return;
                        activate(el);
                    }
                    else {
                        if ($(el).hasClass('active')) return;
                        activate($(el).find("a"));
                    }
                }

                function activate(el, effect, parents) {
                    $(el)[(parents || 'parent')]('li').toggleClass('active').siblings().removeClass('active').children('ul, div').slideUp('fast');
                    $(el).siblings('ul, div')[(effect || 'slideToggle')]((!effect) ? 'fast' : null);
                }

            });
        }
    });
})(jQuery);
