﻿//Sets the active state by first looking for a custom active state call from the page itself.

//If not set, then it looks for a self-referencing link and sets the active state on it.

//Main nav active state runs off which sub-nav is loaded. 



function activeStates(customActive) {
    var SNClass, section, slashLoc, currentUrl, poundLoc, subLink, navLink;



    //needed if a page is in a section but doesn't use default sub-nav

    if (customActive) {

        $("#nav").find("#" + customActive).addClass("active");

        $("body").addClass(customActive);

        return;

    }



    //find and clean URL

    slashLoc = document.URL.lastIndexOf("/");

    currentUrl = document.URL.slice(slashLoc + 1);



    poundLoc = currentUrl.indexOf("#");

    if (poundLoc !== -1) {

        currentUrl = currentUrl.slice(0, poundLoc);

    }



    //setting 'section'

    SNClass = $("#sub-nav ul").attr("class");

    window.console && console.log("test","'"+SNClass+"'");

    if (SNClass) {

        section = SNClass;

    } else {

        navLink = $("#nav").find("a[href=" + currentUrl + "]");

        if (navLink.length !== 0) {

            section = navLink.attr("id");

        }

    }



    //Set main-nav active state

    $("#nav").find("#nav" + section).addClass("active");

    $("body").addClass(section);



    //Set sub-nav active state and expand ul

    subLink = $("#sub-nav").find("a[href='" + currentUrl + "']");

    $(subLink).addClass("active").parents("ul").addClass("expand").each(function () {

        $(this).parent("li").addClass("open");

    });

    $(subLink).siblings("ul").addClass("expand").each(function () {

        $(this).parent("li").addClass("open");

    });



}
