/* Author: 
   The Atom Group

   INDEX
   1.) Mega Menu
   2.) Left Nav
   3.) Sub Nav Grouped Display
   4.) Print

*/


jQuery(function ($) {


    // 1.) Mega Menu
    //
    // Show/hide mega menu on main nav hover
    $("#main-nav > ul > li").hover(
        function () {
            $(this).addClass("hover").next("li").addClass("next").end().children("ul").stop(true, true).fadeIn(180);
            Cufon.refresh('#main-nav > ul > li > a');
        },
        function () {
            $(this).removeClass("hover").next("li").removeClass("next").end().children("ul").stop(true, true).fadeOut(180);
            Cufon.refresh('#main-nav > ul > li > a');
        }
    );

    //
    // Organize each mega menu into vertical columns
    var $panes = $("#main-nav > ul > li > ul");

    $panes.each(function (index) {
        // get the current pane
        var $curPane = $($panes.eq(index));

        // show so we can measure
        $curPane.show();

        var $categories = $curPane.children("li.CMSListMenuLI, li.CMSListMenuHighlightedLI");

        // Only show top 6 children lis
        $categories.each(function () {
            var $subItems = $(this).find("li");
            $subItems.hide().slice(0, 6).show();

            if ($subItems.length > 6) {
                // need to add a "more" link

                // get the header url
                var headerUrl = $(this).children("a").attr("href");

                $(this).children("ul").append("<li class='more'><a href='" + headerUrl + "'>MORE ></a></li>");
            }
        });

        //$categories = $categories.filter(":visible");

        // get total cat height
        var totalCategoryHeight = 0;
        $categories.each(function (index) { totalCategoryHeight += $(this).outerHeight(); });

        // reduce the category height... cloodgy fix.
        totalCategoryHeight = totalCategoryHeight * 0.5;

        // get column height
        var maxColumnHeight = totalCategoryHeight / 4;

        // If children li's exist in the category li's
        if ($curPane.find("li > ul > li").length > 0) {
            // Create 4 column lis
            $curPane.prepend('<li class="column1 column"><ul></ul></li><li class="column2 column"><ul></ul></li><li class="column3 column"><ul></ul></li><li class="column4 column"><ul></ul></li>');

            var columnIndex = 1;
            $categories.each(function (index) {

                // get current category
                var $curCategory = $categories.eq(index);

                // get current column
                var $curColumn = $curPane.find(".column" + columnIndex).children("ul");

                // append category to column
                $curCategory.appendTo($curColumn);

                // get column height
                if ($curColumn.height() > maxColumnHeight) {

                    if (columnIndex < 4) {
                        columnIndex = columnIndex + 1;
                    }
                }
            });

            // set column heights
            var greatestLiHeight = 0;
            $curPane.children("li").each(function () {
                greatestLiHeight = Math.max(greatestLiHeight, $(this).height());
            });
            $curPane.children("li").height(greatestLiHeight);

        }
        else {
            // Add class that gives shorter width to mega menu and proper spacing to headings
            $(this).addClass("no-cols");
        }

        // highlight top anchor if is selected path
        var isActive = $curPane.find("li.CMSListMenuHighlightedLI").length > 0;
        if (isActive) {
            $curPane.siblings("a").addClass("CMSListMenuHighlightedLI");
        }

        $curPane.hide();
    });

    // 2.) Left Nav
    //
    var $activeItem = $("#left-nav .CMSListMenuHighlightedLI");
    // Show children ul's when page is active
    $activeItem.children("ul").show();
    // Show parente ul's when page is active and add active class to all parent li's
    $activeItem.parents("ul").show().parents("li").addClass("CMSListMenuHighlightedLI");
    // Show active carrot
    $("#left-nav > ul > li.CMSListMenuHighlightedLI > a").append('<span class="carrot">Active Carrot</span>');



    // 3.) Sub Nav Grouped Display
    //
    function SplitList(sourceUl, ulCount, requiredLiCount) {

        var ul, items;
        var container = $(sourceUl).parent();

        // get all lis
        var $lis = container.find("li");

        if ($lis.length < requiredLiCount) {
            return null; //abort
        }

        // determine how many li per ul
        var liPerUl = Math.ceil($lis.length / ulCount);

        // Loop through all the LI's
        $lis.each(function (i, li) {
            // For the start of every third item
            if (i % liPerUl === 0) {
                // If there are items to add dump them into the UL
                if (items && items.length) {
                    ul.append(items);
                    container.prepend(ul);
                }

                items = [];

                // Create a new UL 
                ul = $("<ul/>");
            }

            items.push(li);
        });
    }
    // Sub Lvl 1 sub nav
    SplitList($(".sub-nav ul"), 2, 3);
    // Sub Lvl 1 & 2 sub sub nav
    $(".practice-area-sub-links ul").each(function (i, ul) {
        SplitList(ul, 2, 5);
    })


    // 4.) Print
    //
    $("a.print").bind("click", function () {
        window.print();
        return false;
    });

    // 5.) Promo Video "Learn More" Link -- Starts Video
    //
    $(".promo a.video-learn-more").live("click", function () {
        // mimic a click on the play-video anchor
        $(this).closest(".promo").find("a.promoVideo").click();
    });

});


















