﻿//closure wrap ensures no conflict with any other potential libraries
(function($) {
    $(document).ready(function() {

        var p = $.url.parse(document.URL).path;
        var titleLi = null;
        var whiteBoxMenu = null;
        // also process 'white box' menu
        if ($('div.linkswhy .current').length > 0) {
            var elem1 = $('div.linkswhy .current').parentsUntil('div.linkswhy').filter('li').clone();
            elem1.children('ul').remove();
            elem1.each(function() {
                var innerEmText = $(this).children('a').text(); $(this).children('a').text(innerEmText);
            });
            var l = $('div.activityBox div.title p a').clone();
            var innerLinkText = l.text();
            l.text(innerLinkText);
            //var titleLi = $('<li></li>').append(l);
            //var bArray = jQuery.makeArray(elem1);
            //var moreBreadcrumb = bArray.concat(jQuery.makeArray(titleLi));
            //moreBreadcrumb.reverse();
            //$('#breadcrumb li.last').before($(moreBreadcrumb));
            whiteBoxMenu = elem1;
            titleLi = $('<li></li>').append(l);
        }
        //$.fn.reverse = [].reverse;
        //$('#breadcrumb li.last').before(elem1.reverse());


        // hide paths that do not lead to current menu item in the left menu
        // 'clickable' submenu will render slidedown animation useless since click on submenu
        // will load another page, not slidedown animation
        var elem = $('#secondNav .current').parentsUntil('#secondNav').filter('li').clone();
        elem.children('ul').remove();
        elem.each(function() {
            var innerEmText = $(this).children('a').text();
            $(this).children('a').text(innerEmText);
        });
        // add topmost item (landing)
        var newRoot = null;
        var newText = null;
        var i = 0;
        var newRootText = $('#secondNav>li:first').children().first().text();
        $('#secondNav li').each(function() {
            if (i == 1) {
                newText = $(this).children().first().text();
            }
            i = i + 1;
        });

        var setBreadcrumb = jQuery.makeArray(elem);

        if ($('#secondNav>li:first').attr('class') == "current") {
            //newRoot = $('<li><a href="">' + newRootText + '</a></li>');

        }
        else {

            var newRootLink = $('#secondNav>li:first').children().first().attr('href');
            //if (newRootText.value != 'undefined')
            if (newRootLink)
                newRoot = $('<li><a href="' + newRootLink + '">' + newRootText + '</a></li>');
            //setBreadcrumb = jQuery.makeArray(elem).concat(jQuery.makeArray(newRoot));

        }
        // setBreadcrumb.reverse();
        //$.fn.reverse = [].reverse;
        //$('#breadcrumb li.last').before(elem.reverse());
        //jQuery.makeArray(newRoot).concat(jQuery.makeArray(titleLi)).concat(whiteBoxMenu).concat(setBreadcrumb);
        var allBreadcrumb = setBreadcrumb.concat(jQuery.makeArray(whiteBoxMenu)).concat(jQuery.makeArray(titleLi)).concat(jQuery.makeArray(newRoot));
        allBreadcrumb.reverse();
        //allBreadcrumb[0].attr('id', 'sectionBreadcrumb');

        //$('#breadcrumb li.last').before($(setBreadcrumb));
        $('#breadcrumb li.last').before($(allBreadcrumb));
        if ($('#columnOne .current:first>span').length > 0) {
            //$('#breadcrumb .last a').text($('#columnOne .current:first>span').text());
        }


        // banner link
        var bannerLink;
        if ($('#breadcrumb li').length > 2) {
            var found = false;
            $('#breadcrumb li').each(function(i) {
                if (i > 0 && !$(this).hasClass('prefix') && !found) {
                    bannerLink = $(this).children('a').attr('href');
                    found = true;
                }
            });
        }

        
        if (bannerLink == null || bannerLink == '') {
            bannerLink = '#';
        }
        var banner = $('#bannerLink');
        if (banner != null) {
            banner.attr('href', bannerLink);
            //alert('set ' + bannerLink);
        } else {
            //alert("no banner?");
        }
        


        ///hightlight menu && banner text
        var titleText = null;
        if ($('#breadcrumb li').length > 2) {
            $('#breadcrumb li').each(function(i) {
                if (i == 1) {
                    titleText = $(this).children('a').text();
                }
            });
        }
        else {
            titleText = $('#sectionHeader').children().first().text();
        }
        
        //program highlight
        var indexPro = null;
        if ($('.mba.twoColumn').length > 0) {
            if ($('#secondNav>li:first').attr('class') == "current") {
                titleText = $('#secondNav>li:first').children().first().text();
            }
            else {
            }

        }
        if ($('.mbaEsec.twoColumn').length > 0) {
            if ($('#secondNav>li:first').attr('class') == "current") {
                titleText = $('#secondNav>li:first').children().first().text();
            }
        }
        if ($('.execEd.twoColumn').length > 0) {
            if ($('#secondNav>li:first').attr('class') == "current") {
                titleText = $('#secondNav>li:first').children().first().text();
            }

        }

        //        $('#sectionNav li').each(function(i) {
        //            if (i == indexPro) {
        //                $(this).children().first().addClass("active1");
        //            }
        //        });
        var indexUti = null;
        if ($('.facultyResearch.twoColumn').length > 0) {
           titleText = $('#hiddenMenuTitle').text();
          
        }

        //        $('#navigation li').each(function(i) {
        //            if (indexUti == i) {
        //                $(this).children().first().addClass("active2");
        //            }

        //        });

        //nav menu
        var liN = $('#navigation li');
        liN.each(function() {
            var text = $(this).children('a').text();
            if (text == titleText) {
                $(this).children().first().addClass("active2");
            }
            else if (((text == "About") && ((titleText == "About") || (titleText == "About Darden"))) 
		|| (((text == "News & Media") || (text == "Media")) && ((titleText == "News and Media") || (titleText == "News And Media") || (titleText == "News & Media") || (titleText == "Media"))) 
		|| ((text == "Recruit & Partner") && ((titleText == "Recruit and Partner") || (titleText == "Recruit And Partner"))) 
		|| (((text == "Faculty Research") || (text == "Faculty & Research")) && ((titleText == "Faculty and Research") || (titleText == "Faculty Research") || (titleText == "Faculty And Research")))) {
                $(this).children().first().addClass("active2");
            }
        });
        //program menu
        var liP = $('#sectionNav li');
        liP.each(function() {
            var text = $(this).children('a').text();
            if (text == titleText) {
                $(this).children().first().addClass("active1");
            }
        });

        /// special case: modify last breadcrumb item in case page name is 'Home'
        if (($("ul#breadcrumb>li.last").first().text().trim() == "Home") || ($("ul#breadcrumb>li.last").first().text().trim() == "home")) {
            var newLastBreadcrumbText = $('#secondNav .current:first>span').text();
            //var newLastBreadcrumbText = $('#columnOne>#hiddenMenuTitle').text();
            //var newLastBreadcrumbText = $("#secondNav li.current").parents('li').first().children("a").first().text().trim();
            if (newLastBreadcrumbText != "") {
                $("ul#breadcrumb>li.last>a").text(newLastBreadcrumbText);
            }
        }
        else {
            /// use menu name instead of page title for empty menu
            if ($("#secondNav").length > 0) {
                var menuTitle = $("#secondNav li.current:first").children().first().text().trim();
                if (menuTitle != "")
                    $("#breadcrumb li.last:last a").text(menuTitle);
            }
        }
    });
})(jQuery)
