﻿$(document).ready(function () {

    datastore("pageName", window.location.pathname);

    if ($(".dropdown").length > 0) {
        // intualize the drop down menu 
        JsLoader.JquerySuperFish(function () {
            if ($("head").find("#superfishCss").length > 0) {
                $("head").append("<link id=\"superfishCss\" rel=\"Stylesheet\" href=\"/Areas/CMS/Content/NavigationCSS/superfish.css\" />");
            }

            setTimeout(function () {
                $(".dropdown").superfish();

            }, 100);

            $(".dropdown").each(function () {
                $(this).find("a[href='" + window.location.pathname + "']").each(function () {
                    if ($(this).parents('ul:first').hasClass('dropdown'))   // is this a navhead
                        $(this).addClass("active");
                    else   // dropdown item - then select the respective navhead
                        $(this).parents('li').find('a.navhead').addClass('active');
                });
            });
        });
    }

    if ($(".treeviewnav").length > 0) {
        // intualize the True view 
        JsLoader.JQTreeView(function () {
            if ($("body").find("#TreeViewCss").length > 0) {
                $("body").append("<link id=\"TreeViewCss\" rel=\"Stylesheet\" href=\"/Areas/CMS/Content/jquery.treeview.css\" />");
            }
            try {
                $(".treeviewnav").treeview({ collapsed: true,
                    unique: true,
                    persist: "location"
                });
                $(".treeviewnav").each(function () { SetActiveNavigationItem(this); });
            }
            catch (err) { }
        });
    }
    if ($(".accordionnav").length > 0) {
        // intualize the Accordion 
        $(".accordionnav").each(function () {
            var accordionEventType = "click";
            if ($(this).parent().find(".OverrideHyperlink").val() == "False") {
                accordionEventType = "mousedown";
            } else {
                accordionEventType = "click";
            }

            $(this).accordion({
                active: false,
                header: '.navhead',
                navigation: true,
                event: accordionEventType,
                fillSpace: false,
                animated: 'easeslide'
            });

            SetActiveNavigationItem(this);
        });
        var selectedAccordion = null
        $(".accordionnav").each(function () {
            selectedAccordion = this;
            var uls = $(this).find(".ui-accordion-content").filter("ul");
            var selectedUL = $(this).find("a").filter(".active").parents("ul").filter(".ui-accordion-content");
            var n = 0;

            $.each(uls, function (i, item) {
                if (item == selectedUL[0]) {
                    $(selectedAccordion).accordion("activate", n);
                }

                n += 1;
            });

        });


    }

}, 1000);


function SetActiveNavigationItem(selector) {
    $(selector).find("a[href='" + window.location.pathname + "']").addClass("active");


}
