CSSRule('.dMenu',{padding:'2px',cursor:'pointer',border:'1px solid #aaa',fontFamily:'tahoma',fontSize:'.7em'});
CSSRule('.dMenu div',{position:'relative',color:'black',padding:'2px 16px 2px 3px',backgroundColor:'white',whiteSpace:'nowrap'});
CSSRule('.dMenu .sel',{color:'white',backgroundColor:'#3B46BF'});
CSSRule('.dMenu span',{position:'absolute',right:'5px',fontSize:'xx-small',paddingTop:'1px'});
if (!window.menuObject)
	menuObject = {};
if (!window.menuObject2)
	menuObject2 = {};

function showMenu(t,str,e){
	t=$(t);
	var o = {};
	if (!menuObject2[str])
		if (menuObject[str])
			menuObject2[str] = createMenu(str,t);
		else
			return;

	var m = menuObject2[str];
	m.linkElm = t;
	
	if (!t.hasClassName('menuItem') && $(t.parentNode).hasClassName('menuItem')){
		t=$(t.parentNode);
	}
	if (t.hasClassName('menuItem')){
		m.instigator = t;
		m.parentMenu = t.parentNode;
	}
	if (m.parentMenu){
		m.parentMenu.subMenu = m;
		if (!m.parentMenu.horizontal)
			o = {placement:'right'};
	}
	if (m.placement)
		o = {placement:m.placement};
		
	m.show();
	if (m.placement == 'pointer' && e){
		x = e.x || e.clientX
		y = e.y || e.clientY
		o = {placement:'pointer',x:x,y:y}
		m.position(t,o);
	}
	else if (m.placement != 'inline')
		m.position(t,o);
	return m;
}

menuMethods = {
	closeSubMenu : function(){
		var m = this;
		var a = [];
		while (m.subMenu){
			if (a.indexOf(m)>-1){
				alert('error : circular reference')
				return;
			}
			a.push(m)
			if (m.subMenu.selectedItem){
				m.subMenu.selectedItem.removeClassName('sel');
				m.subMenu.selectedItem = null;
			}
			m.subMenu.close()
			m = m.subMenu;
			m.parentMenu.subMenu = null;
		}
	}
	,onmouseover : function(e){
		var ev = (e) ? e : event;
		var t = $(ev.target) || $(ev.srcElement)
		if (t.className == 'menuItem'){
			var m = t.parentNode;
			m.selectItem(t);
		}
	}
	,onclick : function(e){
		var ev = (e) ? e : event;
		var t = $(ev.target) || $(ev.srcElement)
		if (!t.hasClassName('menuItem'))
			t=t.parentNode;
			
		if (t.hasClassName('menuItem') && !t.disabld){
			var m = t.parentNode;
			if (typeof(t.fx)=='function')
				t.fx();
			else {
				if (m.autoOpen && m.subMenu){
					m.subMenu.close();
					m.autoOpen = false;
				}
				else{
					showMenu(t,t.fx);
					if (m.horizontal)
						m.autoOpen = true;
				}
			}
		}
	}

	,onmouseenter	:	function(){
		if (this.timeout)
			clearTimeout(this.timeout)
		if (this.instigator && this.parentMenu.selectedItem != this.instigator)
			this.parentMenu.selectItem(this.instigator);
	}
	,selectItem	:	function(t){
		var m = this;
		if (m.selectedItem){
			if (m.selectedItem == t)
				return;
			if (m.subMenu && m.subMenu.instigator == m.selectedItem && !m.autoOpen)
				m.subMenu.timeout = setTimeout(function(){m.closeSubMenu()},700);
			if (m.selectedItem.timeout)
				clearTimeout(m.selectedItem.timeout)
			m.selectedItem.removeClassName('sel')
		}
		t.addClassName('sel')
		m.selectedItem = t;
		if (m.subMenu && m.subMenu.instigator == t && m.subMenu.timeout)
			clearTimeout(m.subMenu.timeout);
			
		if (m.autoOpen){
			m.closeSubMenu()
			if (!t.disabld && typeof(t.fx)=='string')
				showMenu(t,t.fx);
		}
		else if (!m.horizontal && !t.disabld && typeof(t.fx)=='string')
			t.timeout = setTimeout(function(){showMenu(t,t.fx)},700)
	}
}

function createMenu(str,t){
	var optObj = {}
	optObj.shadowOpacity = menuObject[str].shadowOpacity || 24;
	optObj.shadowWidth = (menuObject[str].shadowWidth == undefined) ? 3 : menuObject[str].shadowWidth

	var box = createBox(optObj)
	box.placement = menuObject[str].placement || 'jj';
	//if (menuObject[str].shadowWidth != undefined){
	//	if (menuObject[str].shadowWidth >0)
	//		box.addShadow(menuObject[str].shadowWidth,shadowOpacity);
	//}
	//else
	//	box.addShadow(3,shadowOpacity);


	if (menuObject[str].placement == 'inline'){
		t.appendChild(box.parentNode);
		box.style.position = box.parentNode.style.position = 'relative'
	}

	box.addClassName(menuObject[str].className || 'dMenu');
	box.dMenu = true;
	
	Object.extend(box,menuMethods);
	box.style.padding = '';
	
	var d,h,arrow;
	var a;
	
	box.horizontal = !!menuObject[str].horizontal;
	var hideArrows = !!menuObject[str].hideArrows;
	
	arrow = (box.horizontal) ? '&#9660' : '&#9658';
	var left = parseInt(box.getStyle('paddingLeft'));
	var item;
	for (var i=0;i<menuObject[str].items.length;i++){
		item = menuObject[str].items[i];
		if (!item.length){
			if (!box.horizontal){
				box.appendChild(document.createElement('hr')).style.width='1px'
				var followhr = true;
			}
		}
		else{
			d = $(document.createElement('div'));
			d.className = 'menuItem';
			try{
				d.disabld = !eval(item[2]);
			}
			catch(err){
				d.disabld = true;
			}
			if (d.disabld)
				d.style.color = 'lightgrey'
			a=[item[0]]
			
			if (typeof(item[1])=='string' && !hideArrows)
				a.push('<span>'+arrow+'</span>');
			d.fx = item[1];
			d.innerHTML = a.join('');
			box.appendChild(d)
			if(followhr){
				d.style.marginTop='-7px'
				followhr = false;
			}
			if(box.horizontal){
				d.style.position = 'absolute';
				d.style.left = left+(parseInt(d.getStyle('marginLeft') || 0))+'px';
				left += d.offsetWidth+(parseInt(d.getStyle('marginLeft') || 0))+(parseInt(d.getStyle('marginRight') || 0));
			}
		}
	}
	var hrs = box.getElementsByTagName('hr');
	var width = box.offsetWidth-12;
	for (var i=0;i<hrs.length;i++){
		hrs[i].style.width = width+'px';
		hrs[i].style.marginTop = '-4px'
	}
	if (box.horizontal){
		box.style.width = (left-parseInt(box.getStyle('paddingLeft')))+'px'
		box.style.height = d.offsetHeight+'px';
	}
	return box;
}


function closeMenu(e){
	var t=e.srcElement || e.target;
	var elm = t;
	while (t && !$(t).dMenu && t.tagName != 'BODY'){
		t = $(t.parentNode);
	}
	var ea = [];
	if (t && t.selectedItem && t.selectedItem.subMenu){
		ea.push(t.selectedItem.subMenu)
	}
	
	while (t && t.dMenu){
		ea.push(t)
		if (t.parentMenu){
			if (t == t.parentMenu){
				alert('error:circular reference')
				return;
			}
			t=t.instigator.parentNode
		}
		else
			t=null
	}
	var mo = menuObject2;
	var moi;
	for(var i in mo){
		moi = mo[i];
		if (mo.hasOwnProperty(i)){
			if (ea.indexOf(moi) == -1 && elm != moi.linkElm){
				if (moi.placement != 'inline'){
					moi.close();
				}
				moi.autoOpen = false;
				if (moi.selectedItem){
					moi.selectedItem.removeClassName('sel')
					moi.selectedItem = null;
				}
			}
		}
	}
}

Event.observe(window,'load',function(){
	Event.observe(document.body,'click',closeMenu)
})
