/*
 * jQuery JavaScript Library v1.3.2
 * http://jquery.com/
 *
 * Copyright (c) 2009 John Resig
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 *
 * Date: 2009-02-19 17:34:21 -0500 (Thu, 19 Feb 2009)
 * Revision: 6246
 */
(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.
            className: "selected", // this sets the default name of the class to use
            homeHref: "/" // this sets the href for the home/default/index page
        });

        var curUrl = window.location.href;
        curUrl = curUrl.substring(curUrl.lastIndexOf('/'));
        if (curUrl.indexOf('#') != -1)
            curUrl = curUrl.substring(0, curUrl.lastIndexOf('#'));
        
        if (curUrl == "/") curUrl = "/Default.aspx";
        var navEle = $("> li > a[href$=" + curUrl + "]", this); //lets look at the top level anchors for a hit

        if (navEle.size() > 0) {

            navEle.addClass(options.className);

        }
        else { //we didn't find a hit, we have to loop  through

            $("li", this).each(function () {
                var cur = $(this), // current  LI
                    i = $("a", cur), // anchor in LI
                    href = i.attr("href"); // href of anchor


                $("ul > li > a", cur).each(function () { // now loop through the child nodes
                    var url = $(this).attr("href");

                    url = url.substring(url.lastIndexOf('/'));

                    if (CheckURL(url)) { //if we have a match...
                        AddClass(i); //add the class to the parent anchor
                        return false; // exit the each statement
                    }
                });
            });

        }

        function CheckURL(a) {

            if (curUrl.toLowerCase() == a.toLowerCase()) {

                return true;
            }

            return false;
        }

        function AddClass(a) {
            a.first().addClass(options.className);
        }
    }

})(jQuery);
