
//// Adds a limit counter box to the page, linked to the specified field. The maxlength
//// attribute of the field is used as the limit.
//function addLimitCounter(fieldId) {
//	var field = document.getElementById(fieldId);
//	var limit = field.getAttribute("maxlength");
//	var remaining = limit - field.value.length;
//	if (field.addEventListener) {
//		field.addEventListener("keyup", checkFieldLimit, false);
//	} else {
//		field.onkeyup = checkFieldLimit;
//	}
//	document.write("<input type=\"text\" size=\"2\" id=\"limitCounter_" + fieldId + "\" value=\"" + remaining + "\" tabindex=\"1000\"/>");
//	document.write("<input type=\"hidden\" size=\"2\" id=\"limitValue_" + fieldId + "\" value=\"" + limit + "\"/>");
//}
//
//// Called on an on key up event from a field with a limit counter. This updates the limit
//// counter field, and if required, trims the input back to the maximum length. In practice
//// the trimming should not be needed because the maxlength attribute should handle it.
//function checkFieldLimit() {
//	var field = this;
//	var fieldLimitCounter = document.getElementById("limitCounter_" + this.getAttribute("id"));
//	var fieldLimitValue = document.getElementById("limitValue_" + this.getAttribute("id"));
//	var limit = fieldLimitValue.value;
//	if (field.value.length > limit) {
//		var croppedValue = field.value.substring(0, limit);
//		field.value = croppedValue;
//	}
//	fieldLimitCounter.value = limit - field.value.length;
//}
//
//// Mapping of source fields to destination fields for automatic URL-safe conversions.
//var urlSafeMappings = new Array();
//
//// Associate two fields, one which will contain a plain text version, the other will
//// automatically be updated with a URL safe version.
//function addAutoUrlSafe(sourceId, destId) {
//	var sourceField = document.getElementById(sourceId);
//	var destField = document.getElementById(destId);
//	if (sourceField.addEventListener) {
//		sourceField.addEventListener("keyup", updateUrlSafe, false);
//	} else {
//		sourceField.onkeyup = updateUrlSafe;
//	}
//	urlSafeMappings[sourceField] = destField;
//	document.write("<input type=\"checkbox\" id=\"autoUrlSafe_" + sourceId + "\" checked=\"checked\"/> <label for=\"autoUrlSafe_" + sourceId + "\">Auto</label>");
//}
//
//// Update the URL safe version.
//function updateUrlSafe() {
//	var sourceField = this;
//	var destField = urlSafeMappings[sourceField];
//	var limit = destField.getAttribute("maxlength");
//	
//	if (document.getElementById("autoUrlSafe_" + sourceField.getAttribute("id")).checked) {
//	
//		var value = sourceField.value;
//		value = value.toLowerCase();
//		value = value.replace(/ /g, "-");
//		value = value.replace(/[^-\w\d]/g, "");
//		value = value.replace(/-{2,}/g, "-");
//		if (value.length > limit) {
//			value = value.substring(0, limit);
//		}
//		if (value.substring(0, 1) == "-") {
//			value = value.substring(1);
//		}
//		if (value.substring(value.length - 1, value.length) == "-") {
//			value = value.substring(0, value.length - 1);
//		}
//		
//	
//		destField.value = value;
//		
//	}
//
//}
//
//// Sets up a select/create combination. If the field is blank or has a value that matches
//// one of the options, the field is replaced with a select, with the corresponding option
//// selected if appropriate. Otherwise the input is left in place. A toggle link is created.
//function selectCreateInit(fieldId, options, optionsVarName) {
//	
//	var fieldContainer = document.getElementById(fieldId + "-sc-field-container");
//	var linkContainer = document.getElementById(fieldId + "-sc-link-container");
//	var field = document.getElementById(fieldId);
//
//	var link = document.createElement("a");
//	link.setAttribute("id", fieldId + "-sc-link");
//	link.appendChild(document.createTextNode("Add"));
//	link.className = "inline-add";
//	link.setAttribute("class", "inline-add");
//	link.setAttribute("style", "margin-left: 5px");
//	link.setAttribute("href", "javascript:selectCreateToggle(\"" + fieldId + "\", " + optionsVarName + ")");
//	
//	linkContainer.appendChild(link);
//	
//	var selectedOptionNum = -1;
//	
//	for (var i = 0; i < options.length; i++) {
//		if (options[i].value == field.value) {
//			selectedOptionNum = i;
//		}
//	}
//	
//	if (selectedOptionNum > -1) {
//		selectCreateToggle(fieldId, options);
//		var field = document.getElementById(fieldId);
//		field.options[selectedOptionNum].selected = true;
//	} else if (field.value.length == 0) {
//		selectCreateToggle(fieldId, options);
//	}
//	
//}
//
//// Toggles a select/create combination between the drop down and free text fields.
//function selectCreateToggle(fieldId, options) {
//
//	var fieldContainer = document.getElementById(fieldId + "-sc-field-container");
//	var linkContainer = document.getElementById(fieldId + "-sc-link-container");
//	var field = document.getElementById(fieldId);
//	var link = document.getElementById(fieldId + "-sc-link");
//	
//	if (field.nodeName.toLowerCase() == "select") {
//	
//		var input = document.createElement("input");
//		input.setAttribute("name", fieldId);
//		input.setAttribute("id", fieldId);
//		
//		fieldContainer.removeChild(field);
//		fieldContainer.appendChild(input);
//		
//		input.focus();
//		
//		link.innerHTML = "List";
//		link.className = "inline-list";
//		link.setAttribute("class", "inline-list");
//	
//	} else {
//	
//		var select = document.createElement("select");
//		select.setAttribute("name", fieldId);
//		select.setAttribute("id", fieldId);
//		
//		fieldContainer.removeChild(field);
//		fieldContainer.appendChild(select);
//		
//		for (var i = 0; i < options.length; i++) {
//			select.options[i] = new Option(options[i].text, options[i].value);
//		}
//		
//		select.focus();
//		
//		link.innerHTML = "Add";
//		link.className = "inline-add";
//		link.setAttribute("class", "inline-add");
//	
//	}
//
//}

var interruptHide = false;
var displayedSubmenuParent = null;
var displayedSubmenu = null;
var lastDisplayedSubmenu = null;

// Displayes a submenu. If one is currently displayed, that is hidden first. The position
// and background position of the submenu are set before it is displayed, so it lines up
// with the parent link.
function displaySubmenu(id, parent) {
	if (displayedSubmenu != null && displayedSubmenu.getAttribute("id") != id) {
		instantHideSubmenu();
	}
	if (displayedSubmenu == null) {
		interruptHide = true;
		var xOffset = parent.offsetLeft;
		var yOffset = document.getElementById("content-wrapper").offsetTop;
		var yOffsetDiff = yOffset - parent.offsetTop;
		displayedSubmenu = document.getElementById("sub-menu-" + id);
		displayedSubmenuParent = parent;
		//displayedSubmenuParent.className = "active";
		
		displayedSubmenu.getElementsByTagName("ul")[0].style.backgroundPosition = "0 -" + yOffsetDiff + "px";
		displayedSubmenu.style.left = xOffset + "px";
		displayedSubmenu.style.top = yOffset + "px";
		
		displayedSubmenu.style.display = "block";
	}

}

// For use when the mouse is rolled over a submenu. Prevents the submenu from being hidden
// by the parent link mouseout.
function lockSubmenu() {
	interruptHide = true;
}

// Hides the currently displayed submenu, provided the hide has not been interrupted.
function hideSubmenu() {
	if (!interruptHide) {
		if (displayedSubmenu != null) {
			displayedSubmenu.style.display = "none";
			displayedSubmenu = null;
		}
		if (displayedSubmenuParent != null) {
			//displayedSubmenuParent.className = "";
			displayedSubmenuParent = null;
		}
	}
}

// Prompts the hide of a submenu after a given delay. The hide may be interrupted during
// this period by rolling the mouse over the parent link or its submenu.
function timedHideSubmenu(wait) {
	interruptHide = false;
	setTimeout('hideSubmenu()', wait);
}

// Forces an instant hide of the currently displayed submenu, for use when the mouse is
// rolled over another parent navigation item.
function instantHideSubmenu() {
	interruptHide = false;
	hideSubmenu();
}
