//fLibrary

// fCore: core functions of F Library

function fGetElementById(id)
{
	return(document.getElementById(id));
}

function fGetFirstChild(parent, childTag)
{
	var children = parent.childNodes;
	for(var i=0;i<children.length;i++) {
		if(children[i].tagName) {
			if(children[i].tagName.toLowerCase() == childTag.toLowerCase())
				return(children[i]);
		}
	}
	return(null);
}

function fLTrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		s = s.substring(j, i);
	}
	return(s.toString());
}

function fRTrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		s = s.substring(0, i+1);
	}
	return(s.toString());
}

function fTrim(str)
{
	return(fLTrim(fRTrim(str)));
}

// fMenu: part of F Library
//Dependencies: {xCore}

function fMenu(ul, selClass)
{
	try {
		selClass = fTrim(selClass);
		var menu, ret = false;
		if(typeof(ul) == 'string') {
			ul = fTrim(ul);
			menu = fGetElementById(ul);
		}
		else {
			menu = ul;
		}
		if(menu.hasChildNodes()) {
			var children = menu.childNodes;
			for(var i=0;i<children.length;i++) {
				var menuItem = fGetFirstChild(children[i], 'a');
				var subMenu = fGetFirstChild(children[i], 'ul');
				var inPath = false;
				if(subMenu) {
					inPath = fMenu(subMenu, selClass);
					menuItem.onclick = fMenuToggle;
					if(!inPath && !fMenuMatchClass(children[i], selClass)) {
						subMenu.style.display = 'none';
					}
				}
				if(inPath || fMenuMatchClass(children[i], selClass))
					ret = true;
			}
		}
		return(ret);
	}
	catch(e) {
		alert(e.message);
	}
}

function fMenuMatchClass(node, className)
{
	var ret = false;
	if(node.className) {
		if(fTrim(node.className) == fTrim(className))
			ret = true;
	}
	return(ret);
}

function fMenuToggle()
{
	if(this) {
		var parent = this.parentNode;
		if(parent) {
			var subMenu = fGetFirstChild(parent, 'ul');
			if(subMenu) {
				if(subMenu.style.display != 'none')
					subMenu.style.display = 'none';
				else
					subMenu.style.display = 'block';
			}
		}
	}
	return(true);
}
