(function($){

    // Grab the overlay to speed all subsequent jQuery actions
    var overlay = '';

    // A jQuery function used to excerpt text,
    // in this case the press releases in the nav
    $.fn.excerpt = function( maxLength, fill) {
        return this.each( function (i,t) {
            var txt = $(t).text();
            $.log('TXT ('+ txt.length+'): '+txt);

            if (txt.length > maxLength) {
                if (!fill) fill = '...';
                var chopPos = maxLength - fill.length + 1;
                $.log('CHOPPING '+chopPos);
                txt = txt.toString().substr(0,chopPos) + '' +fill;
                $.log('AFTER CHOP: '+txt);
                $(t).text(txt);
                $.log('NEW STRING:  '+$(t).text());
            }
        });
    };

    // My own modest little logging function
    $.fn.logjq = function (prepend) {
        return this.each(function (i,t) {
           console.log(prepend+' '+$(t).html());
        })
    };


    $.fn.initBetawaveNav = function (navObj) {

        var nav          = this;
        var navCurrent   = navObj['current'];
        var showTertiary = navObj['showTertiary'];

        // Remove the 'nojs' class signifying that Javascript is in the hizzouse.
        $(nav).removeClass('nojs');

        // Add the 'current' class to the current page's top
        // nav element so that it's opened
        if (navCurrent != '') $(navCurrent).addClass("current");

        if (( typeof(showTertiary) !== 'undefined') &&
                    ( showTertiary === true))  {

// Moved to the bottom, to execute on hover, not page load.
            // Add a div.background into the #releases and #case-studies list tabs.
            // This causes the modal overlay to have a tabbed effect
            $("#releases-nav > a, #case-studies-nav > a", nav)
                .append( $("<div>")
                    .attr('class', 'background')
                    .css({
                        // width:$(this).width(), 
                        // height:$(this).height()
                        width: '100%', 
                        height: '100%'
                    })
                );

            // Excerpt all press release titles on press release page
            $("#press #releases", nav)
                .find("li:not(:last-child) a span.title")
                .excerpt(110)
            .end()
                .find("li:last-child a:not(.archivelink)")
                .excerpt(100);
            $.log("Press release link texts excerpted");

        } else {
            
            $("#releases-nav,#case-studies-nav", nav)
                .removeClass('parent')
                .find(">ul")
                .remove();
        }        

        $(this).superfish({ 
            pathClass:      'current',
            delay:          800, 
            pathLevels:     2,
            dropShadows:    false, 
            onBeforeShow:   cbBeforeNavShow,
            onHide:         cbOnNavHide
        }); 

        return this;
    }

    // Replace the Betawave logo with a flash version that wiggles
    $.fn.initFlashyLogo = function () {

        var sel = this;

        if (typeof(sel) === 'undefined') sel = "#logo";

        // FIXME: Shouldn't be hard-coded
        flashLogo = '';

        $(sel)
            .find("> a").flash({ src: flashLogo },{ update: false } ) // Be silent
        .end() 
            .addClass('flash');
        return this;
    }


    /*  cbBeforeNavShow()
     *  A Suckerfish callback handler called before a section is revealed
     */
    function cbBeforeNavShow() {
        console.log("Currently showing #"+$(this).attr('id'));  // DEBUG

        // The nav is active once a menu is shown
        $(nav).removeClass('inactive');

        overlay = (overlay != '') ? overlay : $(".background");

        // Reveal the overlay if this is the #releases-nav ul
        thisId = $(this).attr('id');
        if ((thisId == 'releases') || (thisId == 'case-studies'))
            $(overlay).stop().show().animate({opacity: 0.75});
    }


    /*  cbOnNavHide()
     *  A Suckerfish callback handler called after a section of the menu is hidden
     */
    function cbOnNavHide() {
        console.log("Currently hiding #"+$(this).attr('id'));   // DEBUG

        // Check to see whether the nav is inactive to change
        // to bottom border state of primary nav
        if (0 == $(nav).find(".sfHover,.sf-breadcrumb").size())
            $(nav).addClass('inactive')
        else
            $(nav).removeClass('inactive')

        overlay = (overlay != '') ? overlay : $(".background");

        // Hide the overlay if we're hiding the #releases tab
        thisId = $(this).attr('id');
        if ((thisId == 'releases') || (thisId == 'case-studies'))
            $(overlay).stop().animate({opacity:0}).hide();
    }


})(jQuery);