;(function($){
    $.fn.superfish = function(op){

        var sf = $.fn.superfish,
            c = sf.c,
            $arrow = $('<span class="'+c.arrowClass+'"></span>'),
            over = function(e){
                var $$ = $(this), menu = getMenu($$);
                clearTimeout(menu.sfTimer);
                $$.showSuperfishUl().siblings().hideSuperfishUl();
            },
            out = function(e){
                var $$ = $(this), menu = getMenu($$), o = sf.op;
                var close = function(){
                    o.retainPath=($.inArray($$[0],o.$path)>-1);
                    $$.hideSuperfishUl();
                    if (o.$path.length && $$.parents('li.'+o.hoverClass).length<1){
                        o.onIdle.call();
                        $.proxy(over,o.$path,e)();
                    }
                };
                if (e.type !== 'mouseleave' && e.type !== 'focusout'){
                    close();
                } else {
                    clearTimeout(menu.sfTimer);
                    menu.sfTimer=setTimeout(close,o.delay);
                }
            },
            getMenu = function($child){
                if ($child.hasClass(c.menuClass)){
                    $.error('Superfish requires you to update to a version of hoverIntent that supports event-delegation, such as this one: https://github.com/joeldbirch/onHoverIntent');
                }
                var menu = $child.closest('.'+c.menuClass)[0];
                sf.op = sf.o[menu.serial];
                return menu;
            },
            applyHandlers = function($menu){
                var targets = 'li:has(ul)';
                if (!sf.op.useClick){
                    if ($.fn.hoverIntent && !sf.op.disableHI){
                        $menu.hoverIntent(over, out, targets);
                    } else {
                        $menu.on('mouseenter', targets, over);
                        $menu.on('mouseleave', targets, out);
                    }
                }
                $menu.on('focusin', targets, over);
                $menu.on('focusout', targets, out);
                $menu.on('click', 'a', clickHandler);
            },
            clickHandler = function(e){
                var $a = $(this);
                //in plone we have span wrapping a
                var $submenu = $a.parent().next('ul');
                var follow = $a.data('follow');
                if ( $submenu.length && (sf.op.useClick || !follow) ){
                    e.preventDefault();
                    if (!$submenu.is(':visible')){
                        $.proxy(over,$(this).parent().parent(),e)();
                    } else if (sf.op.useClick && follow) {
                        $.proxy(out,$(this).parent().parent(),e)();
                    }
                }
            },
            addArrows = function($li,o){
                if (o.autoArrows) {
                    //$li.children('a').each(function() {
                    $li.find('> span a').each(function() {
                        addArrow( $(this) );
                    });
                }
            },
            addArrow = function($a){
                $a.addClass(c.anchorClass).append($arrow.clone());
            };

        return this.addClass(c.menuClass).each(function() {
            var s = this.serial = sf.o.length;
            var o = $.extend({},sf.defaults,op);
            var $$ = $(this);
            var $liHasUl = $$.find('li:has(ul)');
            o.$path = $$.find('li.'+o.pathClass).slice(0,o.pathLevels).each(function(){
                $(this).addClass(o.hoverClass+' '+c.bcClass)
                    .filter('li:has(ul)').removeClass(o.pathClass);
            });
            sf.o[s] = sf.op = o;

            addArrows($liHasUl,o);
            applyHandlers($$);

            $liHasUl.not('.'+c.bcClass).hideSuperfishUl();

            o.onInit.call(this);

        });
    };

    var sf = $.fn.superfish;
    sf.o = [];
    sf.op = {};

    sf.c = {
        bcClass     : 'sf-breadcrumb',
        menuClass   : 'sf-js-enabled',
        anchorClass : 'sf-with-ul',
        arrowClass  : 'sf-sub-indicator'
    };
    sf.defaults = {
        hoverClass  : 'sfHover',
        pathClass   : 'overideThisToUse',
        pathLevels  : 1,
        delay       : 0,
        animation   : {opacity:'show'},
        animationOut: {opacity:'hide'},
        speed       : 'normal',
        speedOut : 'fast',
        autoArrows  : true,
        disableHI   : false,        // true disables hoverIntent detection
        useClick : true,
        onInit      : function(){}, // callback functions
        onBeforeShow: function(){},
        onShow      : function(){},
        onHide      : function(){},
        onIdle      : function(){}
    };

    $.fn.extend({
        hideSuperfishUl : function(){
            var o = sf.op,
                $$ = this,
                not = (o.retainPath===true) ? o.$path : '';
            o.retainPath = false;
            var $ul = $('li.'+o.hoverClass,this).add(this).not(not)
                    .find('>ul');
            // Do not hide if it is still animating ( showing for example )
            if ($ul.filter(':animated').length<1) {
                $ul.stop().animate(o.animationOut,o.speedOut,function(){
                        $ul = $(this);
                        $ul.css('visibility','hidden').parent().removeClass(o.hoverClass);
                        o.onHide.call($ul);
                        //$$.children('a').data('follow', false);
                        $$.find('> span a').data('follow', false);
                    });
            }
            return this;
        },
        showSuperfishUl : function(){
            var o = sf.op,
                $$ = this,
                $ul = this.addClass(o.hoverClass)
                    .find('>ul:hidden').css('visibility','visible');
            o.onBeforeShow.call($ul);
            $ul.stop().animate(o.animation,o.speed,function(){
                o.onShow.call($ul);
                //$$.children('a').data('follow', true);
                $$.find('> span a').data('follow', true);
            });
            //Position the <ul> tags below the menu
            if ($ul.length > 0){
                var maxHeight = 0;
                $ul.each(function() {
                    var top = $($ul.parent().closest('ul')).height();
                    $(this).css('top', top);
                    if(maxHeight < $(this).height()) {
                        maxHeight = $(this).height();
                    }
                });
                //Fixed height depending on the maxHeight <ul>
                $ul.each(function() {
                    $(this).height(maxHeight);
                });
            }
            return this;
        }
    });

})(jQuery);
