﻿(function ($) {
    $.fn.SelectedNav = function (options) {

        options = $.extend({
            position: 0 // this sets the position of the 'home' nav option. If no matches are found, we highlight the anchor at this index.
        });

        var curUrl = window.location.href;

        this.each(function () {

            var cur = $(this), // current  LI
                i = $("a", cur), // anchor in LI
                href = i.attr("href"); // href of anchor

                $(".indentmenu > ul > li > a", cur).each(function () { // loop to check for child nodes for
                    if (CheckURL($(this))) { //if we have a match...
                        AddClass(i); //add the class
                        return false; // exit the each statement
                    }

                });
                if (CheckURL(i)) { //if we still haven't got a match check the normal non child nodes
                    AddClass(i);
                    return false;
                }
        });

        if ($("a.current").size() == 0) { // if we STILL don't have a match, add it to the position as specified by the options (eg home page)
            $(".indentmenu > ul > li > a").first().addClass("current");
        }

        function CheckURL(a) {
            if (curUrl.indexOf(a.attr("href")) > 0) {
                return true;
            }

            return false;
        }

        function AddClass(a) {
            a.first().addClass("current");

        }
    }
})(jQuery);


$(document).ready(function () {
    $(".indentmenu > ul > li").SelectedNav();
});
