/*
    ------------------------------------
    Table of Contents
    ------------------------------------

    + Equal Heights
    + Site Navigation
    + Feature Panel Carousel
    + CMI Search
*/
$(document).ready(function() {
    /*
        Equal Heights
    */
    function equalHeight(group) {
        tallest = 0;
        group.each(function() {
            thisHeight = $(this).height();
            if(thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    }

    // Balance heights of selected elements
    equalHeight($('.balance_height'));
    
    /*
        Site Navigation
    */
    // Get menu mapping value
    var currPage = $("#menu_mapping").attr("class");

    // Match against site navigation values
    if (currPage != "") {
        // Match against first-level menu items
        $("#site_nav > ul > li").each(function(i) {
            var firstLevelItem = $(this).attr("class").split("-")[0];

            if ($(this).attr("class") == currPage ||  firstLevelItem == currPage) {
                // Apply active class to first-level menu item
                $(this).addClass('active');
            }
        });
        // Match against second-level menu items
        $("#site_nav ul > li >  ul > li").each(function(i) {
            if ($(this).attr("class") == currPage) {
                // Apply active class to second-level menu item
                $(this).addClass('active');
                // Apply active class to first-level menu item
                $(this).parent().parent().addClass('active');
            }
        });
        // Match against global menu items
        $("#global_nav ul > li").each(function(i) {
            if ($(this).attr("class") == currPage) {
                // Apply active class to global menu item
                $(this).addClass('active');
            }
        });
    }
    
    function megaHoverOver() {
        $(this).closest('ul').find('li.active ul').hide();
        $(this).find('ul').show();
    }

    function megaHoverOut() {
        $(this).find('ul').hide();
    }

    var hoverConfig = {
         //sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
         //interval: 100, // number = milliseconds for onMouseOver polling interval
         over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
         timeout: 500, // number = milliseconds delay before onMouseOut
         out: megaHoverOut // function = onMouseOut callback (REQUIRED)
    };
    
    // Apply hoverIntent to top-level site navigation
    $("#site_nav > ul > li").hoverIntent(hoverConfig);
    $("#site_nav > ul").bind('mouseleave', function() {
        setTimeout(function() {
            if (!$('#site_nav > ul > li ul:visible').length) {
                $('#site_nav li.active ul').show();
            }
        }, 500);
    });

    /*
        Feature Panel Carousel
    */
    // Initialize scrollable
    $(".scrollable").scrollable({easing: 'linear', speed: 750, circular: true}).autoscroll(5000); // 5 second scroll interval

    /*
        CMI Search
    */
    // Apply styling to CMI Search button
    $("#medicine_search :submit").addClass("search_button");
});

