// 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';	
	}
	// if path url ends with same name as list item anchor, add selected class to it
	$('.navbar li a[href$="' + pathname + '"]').addClass('selected');
	
	// for each 'selected' anchor
	$('li a.selected').each(function(){
	  // add class to only the first parent element li a in the menu
	  $(this).parent().parent().parent('li').find('a:eq(0)').addClass('selected');
	  /* traverse up from this li a to parent (li) to its parent (ul) to its parent that is an li element, 
	  find the anchor (a) and add the class 'selected' */
	});
}

var ddMenu = function (){
  // keep 'selected' ul item for drop down (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');
  });
}

// preserve semantic/valid html, but add thumbnail for thumbnail pop up
var thumbnail = function (){
  // add rel="thumbnail" to img.thumbnail anchor
  $('img.thumbnail').parent().attr('rel','thumbnail');
}

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