// Function to split ul into multiple smaller lists
// list = the dom object for the ul
// maxLength = maximum list items in each list
function split_list(list, maxLength){
        maxLength = maxLength - 1;
        var li_num = $(list).find("li").length;
        if(li_num > maxLength){
                var new_list = $("<ul></ul>");
                $(list).after(new_list);
                new_list.append($(list).find("li:gt(" + maxLength + ")"));
                if(new_list.find("li").length > maxLength + 1){
                        split_list(new_list, maxLength + 1);
                }
        }
}

// Wait until the document has loaded
$(document).ready(function() {

// =============== Navigation =================

    // Split navigation menus to avoid
    // navigation going below the fold
    $("ul#nav .child ul").each( function(k,v) {
            split_list(v, 8);
    });
    
    // Overide the default hover behavior and
    // add animation to drop-down menus
    $('ul#nav li.parent').hoverIntent({    
      sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
      interval: 20, // number = milliseconds for onMouseOver polling interval    
      timeout: 0, // number = milliseconds delay before onMouseOut    
      over: function () {
        $('.child', this).slideToggle(100);
      },
      out: function () {
        $('.child', this).slideToggle(100);
      }
    }).find('.child').hide();

    // Overide the default hover behavior and
    // add animation to related-links menus
    $('.related_links').hoverIntent({    
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
        interval: 20, // number = milliseconds for onMouseOver polling interval    
        timeout: 100, // number = milliseconds delay before onMouseOut    
        over: function () {
            $('ul',this).slideToggle(100);
        },
        out: function () {
            $('ul',this).slideToggle(100);
        }
    }).find('ul').hide();
    
    // Move current sub-menu (third-level) to the top of the list
    $("#secondary_nav > li.current").insertAfter("#secondary_nav > li.root");

// =============== Mobile =================

    // Menu open/closing functionality for mobile devices
    $('#mobile_nav .menuToggle a').click(function () {
        $('#secondary_nav').toggle();
        $(this).toggleClass('open');
    });
    
    // Set default state
    $('#mobile_nav #top_menu').show();
    $('#mobile_nav #secondary_nav').hide();

// =============== Slideshows =================

    $('#primary_content .slideshow').each(function() {
        var total = $(this).children().length;
        var rand = Math.floor(Math.random()*total);

        $(this).nivoSlider({
                startSlide:rand,
        	    effect: 'sliceUpDown',
        	    pauseTime: 6000,
        	    slices: 10,
        	    animSpeed: 300,
        	    keyboardNav: false,
        	    directionNav:true,
        	    captionOpacity:0.0
        	});
    });
    $('#tertiary_content .slideshow').each(function() {
        var total = $(this).children().length;
        var rand = Math.floor(Math.random()*total);

        $(this).nivoSlider({
//                startSlide:rand,
        	    effect: 'fade',
        	    pauseTime: 4000,
        	    slices: 10,
        	    animSpeed: 300,
        	    keyboardNav: false,
        	    directionNav:false,
        	    captionOpacity:0.0
        	});
    });
// ========================================
    
    // Call Program Directory Plugin
    $('.filterBox').filterBox();
    
    $('table.filter').each(function() {
        obj = $(this);
        obj.wrap('<div class="tableFilter" />');
        obj.parent().prepend('<fieldset><p><label>Search:</label><input type="text" placeholder="keywords" class="q"/></p></fieldset>');
        obj.tableFilter({
            selectOptionLabel: 'All',
            additionalFilterTriggers: [$('input.q',obj.parent())]
        });
    });
    

});