﻿$().ready(function(){
    
    var area = window.location.pathname.match(/[\w-]+/i);
    var regex = new RegExp("^/?" + area); // WD: We make "/" optional (?) because ie does not include the forward slash in an href's .pathname
    var $navigationAll = $("#menuArea > ul > li");
    var $navigationCurrent = null;
       
    // Private: Set's the passed navigation item to selected
    function setActive($active)
    {
        $navigationAll
            .children("a")
            .removeClass("selected")
            .end()
            .children(".subnav-holder")
            .hide();
        
        if ($active)
            $active
                .children("a")
                .addClass("selected")
                .end()
                .children(".subnav-holder")
                .show();
    };

    // Get the navigation item that represents the current page
    $("> a", $navigationAll).each(function() {
        if (regex.test(this.pathname)) {
            $navigationCurrent = $(this).parent();
            return false; // End loop early
        };
    });

    //Bind the mouseover and mouseout event's of each item in the navigation
    $navigationAll
        .mouseover(function() { setActive($(this)); })
        .mouseout(function() { setActive($navigationCurrent); });
    
    // Set the current navigation item to selected
    setActive($navigationCurrent);
    
});
