// JavaScript (jQuery) 'Selected' Document

var selectedItem = function() {
	// get url string
	var location = new String(window.location);
	
	// get path because i use root in links
	var pathname = window.location.pathname;
	
	// get page/file name
	var pageName = new String(location.substring(location.lastIndexOf('/')+1));

	// check if pagename is blank, this assumes that it's the home page
	if(pageName == '' || pageName == '/') {
		pageName = 'index.php';		
	}
	
	// alert(pathname);
	if (pathname == "/"){
	  pathname = '/index.php';	
	}
	
	$('.navbar li a[href$="' + pathname + '"]').addClass('selected');
	
	$('li a.selected').each(function(){
	  // alert($(this).parents('.navbar li a:eq(1)').text());
	  // alert($(this).parent().parent().parent('li').text());
	  // add class to only the first parent element li a
	  $(this).parent().parent().parent('li').find('a:eq(0)').addClass('selected');
	});
	
    /**** use for subdirectories 
	// jquery : find nav item and apply selected class
	// iterate through each UL first child LI elements
	$('ul > li').each(function() {
		// if a link item href matches current page/file name
		if($(this).find('a[href='+pageName+']').length > 0) {
			// apply selected class to first anchor link child of list item.
			$(this).find('a:eq(0)').addClass("selected");
			$(this).addClass("selected");
		}
		// check sub directory for link
		if($(this).find('a[href=/'+pageName+']').length > 0) {
			// apply selected class to first anchor link child of list item.
			$(this).find('a:eq(0)').addClass("selected");
			$(this).addClass("selected");
		}
	}); 
	****/
}

var ddMenu = function (){
  // keep 'selected' ul item for dd menu items
  // use 'active' because we don't want to remove the 'selected' from parent ul
  $('.mainnav ul li ul').hover(
    function(){
    // add 'selected' class to dd menu ul
    $(this).parent().children().addClass('active');},
    function(){ 
    // remove it on mouseout of ddmenu ul
    $(this).parent().children('a').removeClass('active');
  });
}

$(document).ready(function() {
	selectedItem();
	ddMenu();
});
