// JavaScript Document
var g_interval = 0;
var g_showInterval = 0;

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " " || ch == "\n" || ch == "\r") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " " || ch == "\n" || ch == "\r") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   /*
   while (retValue.indexOf("  ") != -1) 
   { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   */
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function showMenu(id_menu){
	var my_menu = document.getElementById(id_menu);
	if(my_menu.style.display=="none" || my_menu.style.display==""){
		my_menu.style.display="block";
	} else { 
		my_menu.style.display="none";
	}
}

function onMouseOver(event) {	
	clearInterval(g_interval);
	var element = Event.findElement(event, 'li').identify();
	element = element.substr(0, element.length - 3);
	$(element + '-id').observe('mouseout', leftEarly);
	showAfter(element);	
}

function showNow(element) {
	menuHideAll();	
	clearInterval(g_showInterval);	
	$(element + '-submenu-id').style.display="block";
	$(element + '-id').addClassName('menu-over');
	//$(element + '-image-id').src = "../images/arrow_hover.gif";	
	$(element + '-id').observe('mouseout', onMouseOut);
}

function onMouseOut(event) {
	var element = Event.findElement(event, 'li').identify();
	element = element.substr(0, element.length - 3);
	hideAfter(element);
}

function showAfter(element) {
	clearInterval(g_showInterval);	
	g_showInterval = setInterval("showNow('"+element+"')", 100);
}

function leftEarly(element) {
	Event.stopObserving(element, 'mouseout', leftEarly);	
	clearInterval(g_showInterval);
}

function hideAfter(element) {
	g_interval = setInterval("hideNow('"+element+"')", 350);
}

function menuHideAll() {
	for(var i = 1; i < g_menu_items; i++) {
		hideNow('menu-' + i);
	}
}

function hideNow(el) {
	$(el + "-submenu-id").style.display = "none";
	$(el + "-id").removeClassName('menu-over');	
	//$(el + "-image-id").src = "../images/arrow.gif";	
	clearInterval(g_interval);
}

function ListContains(list, value) {
	var currentEntries = list.split(",");
	
	for(var i = 0; i < currentEntries.length; i++) {
		if(value == trim(currentEntries[i])) {
			return true;
		}		
	}
	return false;
}

function ListFind(list, value) {
	var currentEntries = list.split(",");
	
	for(var i = 0; i < currentEntries.length; i++) {
		if(value == trim(currentEntries[i])) {
			return i;
		}		
	}
	return -1;
}

function ListAt(list, value) {
	var currentEntries = list.split(",");
	return currentEntries[value];
}

function ListAdd(list, value, shouldSort) {
	var currentEntries = list.split(",");
	
	/*
	for(var i = 0; i < currentEntries.length; i++) {
		if(value == trim(currentEntries[i])) {
			return list;
		}		
	}
	*/
	
	if(list.length != 0) { list += ","; }
	list += value + ',';
	
	if(shouldSort) {		
		var sort_array = list.split(",");
		sort_array.sort();
		list = "";
		for(var i = 0; i < sort_array.length; i++) {
			if(sort_array[i] != '') {
				list += sort_array[i] + ',';
			}
		}
	}

	return list.substring(0, list.length - 1);
}

function ListRemoveAt(list, index) {
	var currentEntries = list.split(",");
	var newList = "";

	for(var i = 0; i < currentEntries.length; i++) {
		if(index != i) {
			newList += currentEntries[i] + ","			
		}
	}
	
	return newList.substring(0, newList.length - 1);
}

function ListRemove(list, value) {
	var currentEntries = list.split(",");

	var newList = "";

	for(var i = 0; i < currentEntries.length; i++) {
		if(value != currentEntries[i]) {
			newList += currentEntries[i] + ","			
		}
	}
	
	return newList.substring(0, newList.length - 1);
}


