$(document).ready(
	function() {
		menuHover();
		buildBreadcrumbNav();
	}
);


// build breadcrumb nav
function buildBreadcrumbNav() {
	// get the path to the current file, minus protocol+host
	var sURL = window.location.href.replace('http://' + window.location.host, "");
	// based on the current url, locate the active menu item
	var jqCurrentItem = null;
	$('#primary-nav a, #sub-nav a').each(
		function() {
			var jqThis = $(this);
			if( sURL == jqThis.attr('href') ) {
				jqThis.addClass('active');
				jqCurrentItem = jqThis;
			}
		}
	);
	
	// no match was found (homepage, prolly).  return false;
	if( jqCurrentItem == null) {
		return false;	
	}
	
	// determine whether this is a top or sub level item
	// and then select the sub items of this section
	var jqParents = jqCurrentItem.parents().filter('ul');
	var jqMenu = null;
	if( jqParents.size() == 1 ) {
		// create a copy of the UL
		jqMenu = jqCurrentItem.parent().find('ul').clone();
		
		// get the top level item text, which will be used as text in the first list item element
		var sText = jqCurrentItem.text();
		var sUrl = jqCurrentItem.attr('href');
		// top level items are images in primary nav so retrieve title text from alt attribute
		var jqImg = jqCurrentItem.find('img');
		if( jqImg.size() > 0 ) {
			sText = jqImg.attr('alt');
		} 
		
		// append the "title" to the list
		jqMenu.prepend("<li class=\"title\">" + sText + " &raquo</li>");
	} else {
		
		// reference to the UL that contains this UL/LI/A
		var jqParent = jqCurrentItem.parent().parent();
		
		// get the top level item text, which will be used as text in the first list item element
		var jqTopLevelItem = $(jqParent.parent().find('a').get(0));
		var sText = jqTopLevelItem.text();
		var sUrl = jqTopLevelItem.attr('href');
		
		var jqImg = jqParent.parent().find('img');
		if( jqImg.size() > 0 ) {
			sText = jqImg.attr('alt');
		}
		var sTitle = "<li class=\"title\"><a href=\"" + sUrl + "\">" + sText + "</a> &raquo;</li>";
		jqMenu = jqParent.clone().prepend(sTitle);
	}
	jqMenu.attr('id', 'breadcrumb-nav').addClass('adjacent clearfix').prependTo('#content');
}


// attach hover events to menus
function menuHover() {
	$('#home-nav li, .menu li').hover(
		function() { $(this).addClass('hover') },
		function() { $(this).removeClass('hover') }
	);
}

