// system part
configs=new Array();
conf=new Array();
menu=new Array();
path=new Array();
function createMenu(name,levels) {
	eval("configs['"+name+"']=new Array();");
	eval("menu['"+name+"']=new Array();");
}
//.system part



// global configs
conf['hidedelay']=20/*0*/;
conf['showdelay']=/*20*/0;
//.global configs

// menubuilder
function showmenu(menu_name,obj) {
	if(!menu_name || !menu[menu_name]) {
		return true;
	}
//	doPath(menu_name,obj);
//	if(obj.getAttribute('id')=='a'+menu_name) {
//		if(!configs[menu_name]['class']) {
//			configs[menu_name]['class']=obj.getAttribute('class');
//		}
//		obj.setAttribute('class','h');
//	}
	if(obj.getAttribute('fixed')==null) {
		var test=getBounds(obj);
		if(obj.getAttribute('level')==1) {
			document.getElementById(menu_name).style.left=test['left']-0;
			document.getElementById(menu_name).style.top=test['top']+2+test['height'];
		} else {
			document.getElementById(menu_name).style.left=obj.offsetLeft+0+obj.clientWidth;
			document.getElementById(menu_name).style.top=test['offsetTop']-2;
		}
		obj.setAttribute('fixed',1);
	}
	clearTimeout(configs[menu_name]['timeout']);
	configs[menu_name]['timeout']=setTimeout("doPath('"+menu_name+"',document.getElementById('"+obj.id+"'));document.getElementById('"+menu_name+"').style.display='inline';",conf['showdelay']);
	return true;
}

function hidemenu(menu_name) {
	if(!menu_name || !menu[menu_name]) {alert(menu_name);
		return false;
	}
	clearTimeout(configs[menu_name]['timeout']);
	configs[menu_name]['timeout']=setTimeout("document.getElementById('"+menu_name+"').style.display='none';",conf['hidedelay']);
	if(path[document.getElementById('a'+menu_name).getAttribute('level')]) {
		clearTimeout(path[document.getElementById('a'+menu_name).getAttribute('level')].getAttribute('timeout'));
		path[document.getElementById('a'+menu_name).getAttribute('level')].setAttribute('timeout',setTimeout("deletePath(document.getElementById('a"+menu_name+"').getAttribute('level'));",conf['hidedelay']));
	}
	if(configs[menu_name]['parent']) {
		hidemenu(configs[menu_name]['parent']);
	}
	configs[menu_name]['parent']=false;
	return true;
}

function liveme(menu_name) {
	if(menu_name && !menu[menu_name]) {
		createMenu(menu_name,0);
	}
	if(!menu_name || !menu[menu_name]) {
		return false;
	}
	if(path[document.getElementById('a'+menu_name).getAttribute('level')]) {
		clearTimeout(path[document.getElementById('a'+menu_name).getAttribute('level')].getAttribute('timeout'));
	}
	clearTimeout(configs[menu_name]['timeout']);
	if(configs[menu_name]['parent']) {
		liveme(configs[menu_name]['parent']);
	}
	return true;
}

function createparent(name,parent) {
	if(name && !configs[name]) {
		createMenu(name,0);
	}
	if(parent && !configs[parent]) {
		createMenu(parent,0);
	}
	if(name && parent) {
		configs[name]['parent']=parent;
		return true;
	}
	return false;
}

function getBounds(element) {
	var left = element.offsetLeft;
	var top = element.offsetTop;
	var lastLeft = new Array;
	var lastTop = new Array;
	for (var parent = element.offsetParent; parent; parent = parent.offsetParent) {
		left += parent.offsetLeft;
		top += parent.offsetTop;
		if(parent.offsetParent) {
			lastLeft[lastLeft.length]=parent.offsetLeft;
			lastTop[lastTop.length]=parent.offsetTop;
		}
	}
	var offsetLeft = left;
	var offsetTop = top;
	for(var i=lastLeft.length-1;i>(lastLeft.length-element.getAttribute('level'));i--) {
		offsetLeft-=lastLeft[i];
	}
	for(var i=lastTop.length-1;i>(lastTop.length-element.getAttribute('level'));i--) {
		offsetTop-=lastTop[i];
	}
	return {offsetLeft: offsetLeft, offsetTop: offsetTop,left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
}

function doPath(menu_name, obj) {
	var lvl=obj.getAttribute('level');
	for(var i in path) {
		if(i>lvl) {
			deletePath(i);
		} else {
			clearTimeout(path[i].getAttribute('timeout'));
		}
	}
	if(path[lvl]) {
		path[lvl].className=path[lvl].className.replace(/ .*$/,'');
	}
	path[lvl]=obj;
	showPath();
}

function deletePath(lvl) {
	if(!path[lvl]) return false;
	path[lvl].className=path[lvl].className.replace(/ .*$/,'');
	delete path[lvl];
}

function showPath() {
	for(var i in path) {
		var cls=path[i].className.replace(/ .*$/,'');
		cls=cls+' '+cls+'-l';
		path[i].className=cls;
	}
}