(function($) {
    $.fn.subnav_useHash = function() {
		// setup initial vars
		var LinkHash = location.hash;
		var clicked = "";
		var currentItem = "";
		var previousItem = "";
		ua = navigator.userAgent.toLowerCase();
		
		/* Give unique id to each item (subnav_#)
		     - ID used to track current vs previous items
		   Check for initial url hash value and
		     - setNav as needed
		*/
		$(this).each(function (itemCount) {
			$(this).attr('id','subnav_' + itemCount);
			if(LinkHash == $(this).attr('href')) {
				$(this).parent().addClass('subon');
				previousItem = $(this);
			}
		});
		
		// create click event | call setNav on click
        $(this).click(function(event) {
			setNav(this);
		});
		
		// FUNCTION: setNav
		function setNav(itemClicked) {
			// setup current vs previous items
			currentItem = '#' + $(itemClicked).attr('id');
			if (clicked !== "") {
				previousItem = clicked;
			}

			// Set subon class to clicked items
			clicked = '#' + $(itemClicked).attr('id');
			$(clicked).parent().addClass('subon');
			if(previousItem !== "") {
				$(previousItem).parent().removeClass('subon');
			}
			
			var initPathName = location.pathname,
			    finalPathName = initPathName,
			    hashVal = $(itemClicked).attr('href'),
			    homeFlag = false;
			if ( initPathName.indexOf( '/Home' ) !== -1 ) {
			    homeFlag = true;
			    finalPathName = initPathName.replace( '/Home', '' );
			}
			var newURL = finalPathName + hashVal;
			location.href = newURL;
			if ( homeFlag !== true ) { location.reload(); }
		}
    };
})(jQuery);
