/*
 * Get the target name to position the top navigation marker
 * This is completely dependent on the url, if the url changes, this will not work
 * Search is a level down, but doesn't have a marker, look for search specifically
 */
function displayNavMarker() {
	var loc = location.href;
	var level = getPageLevel(loc);
	
	hideMarkers();
	
	if(level >= 1 && loc.indexOf("/search/") == -1) {
		level = (level > 2) ? 2 : level;
		var section = getTarget(loc, level);
		
		var mark = document.getElementById("mark-" + section);
		mark.className = "visible";
	}
}

/*
 * Get the page level from the url by counting slashes
 */
function getPageLevel(loc) {
	var myloc = loc.substring(7, loc.length);
	var levels = myloc.split("/");
	
	return (levels.length - 2);
}

/*
 * Get the target name and turn it into the key format (alphanumeric and lowercase)
 */
function getTarget(loc, level) {
	var myloc = loc.substring(7, loc.length);
	var pages = myloc.split("/");
	var target = pages[level];
	target = target.toLowerCase();
	target = target.replace(/[^a-z0-9]+/g,'');
	
	return target;
}

/*
 * Hide all markers
 */
function hideMarkers() {
	var markers = Array("mark-ourwork", "mark-projects", "mark-countries", "mark-issues", "mark-news", "mark-mediafeed", "mark-publications", "mark-aboutrwi", "mark-contactus", "mark-partners", "mark-grants");

	for(var i = 0; i < markers.length; i++) {
		var mark = document.getElementById(markers[i]);
		mark.className = "hidden";
	}
}

/*
 * Hide/show default search text
 */
var siteSearchText = "";
function searchFieldFocus(f) {
    siteSearchText = (siteSearchText == '') ? f.value : siteSearchText;
    if (f.value == siteSearchText) {
        f.value = ''; 
        f.style.color = "#333";
    }
}

function searchFieldBlur(f) {
    if (f.value == '') {
        f.value = siteSearchText;
        f.style.color = "#999";
    }
}
