var firstGo = false;

if (typeof addEvent != 'function')
{
 var addEvent = function(o, t, f, l)
 {
  var d = 'addEventListener', n = 'on' + t, rO = o, rT = t, rF = f, rL = l;
  if (o[d] && !l) return o[d](t, f, false);
  if (!o._evts) o._evts = {};
  if (!o._evts[t])
  {
   o._evts[t] = o[n] ? { b: o[n] } : {};
   o[n] = new Function('e',
    'var r = true, o = this, a = o._evts["' + t + '"], i; for (i in a) {' +
     'o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null;' +
     '} return r');
   if (t != 'unload') addEvent(window, 'unload', function() {
    removeEvent(rO, rT, rF, rL);
   });
  }
  if (!f._i) f._i = addEvent._i++;
  o._evts[t][f._i] = f;
 };
 addEvent._i = 1;
 var removeEvent = function(o, t, f, l)
 {
  var d = 'removeEventListener';
  if (o[d] && !l) return o[d](t, f, false);
  if (o._evts && o._evts[t] && f._i) delete o._evts[t][f._i];
 };
}


function ddMenu(n){
	this.m=n;
}
var currentItem = null;
var filterActive = false;

ddMenu.prototype.init=function(p){
	var root=document.getElementById(p);

	var links = root.getElementsByTagName('a');

	for (var i = 0; i < links.length; i++)
	{
		links[i].onclick = toggleMenu;
	}
	
	jQuery(document).bind("click", 
        function(e){ 
			if(!filterActive && currentItem!= null){
				toggleVisible(currentItem, false);
				opened = null;
			}
			filterActive = false;
        }
	);	

}


function toggleMenu(){
	var subList = this.parentNode.getElementsByTagName('ul');
	var oldOpen = opened;

	if(subList.length > 0)
	{
		if(firstGo)
		{
			closeAll(subList[0]);
		}
		if(!active && activeItem != null){
			toggleVisible(activeItem, false);
			activeItem = null;
		}

		if(opened != null){
			toggleVisible(opened, true);
			if(active){
				activeItem = opened;
				active = false;
			}
		}
		
		if(opened != subList[0] || opened == null){
			opened = subList[0];
			toggleVisible(subList[0], true);
			currentItem = subList[0];
		}else{
			opened = null;
			toggleVisible(subList[0], false);
		}

		if((oldOpen != null) && opened != null){
			if(oldOpen != opened.parentNode.parentNode && oldOpen.parentNode.parentNode != opened){
				toggleVisible(oldOpen, false);
			}

			if(oldOpen.parentNode.parentNode != opened.parentNode.parentNode && oldOpen.parentNode.parentNode != opened){
				toggleVisible(oldOpen.parentNode.parentNode, false);
			}
			return false;
		}else{
			if(oldOpen != null){
				if(oldOpen.parentNode.parentNode.id != menuName){
					opened = oldOpen.parentNode.parentNode;
				}
			}
			return false;
		}
	}else{
		return true;
	}
}

function closeAll(selected)
{
	firstGo = false;

	var root=document.getElementById(menuName);

	var uls = root.getElementsByTagName('ul');
	
	if(opened == null)
	{
		var links = root.getElementsByTagName('a');
		for (var i = 0; i < links.length; i++)
		{
			links[i].className = '';
		}
	}
	
	for (var i = 0; i < uls.length; i++)
	{	
		if(uls[i].className == 'active'){
			if((uls[i].parentNode.parentNode.id != menuName) && (uls[i].parentNode.parentNode != selected.parentNode.parentNode))
			{	toggleVisible(uls[i], false);
				toggleVisible(uls[i].parentNode.parentNode, false);
			}else{
				if(uls[i].parentNode.parentNode == selected.parentNode.parentNode)
					toggleVisible(uls[i], false);
			}
		}
	}
}

function toggleVisible(field, visible){
	var link = field.parentNode.getElementsByTagName('a');

	if(visible){
		field.style.display = 'block';
		link[0].className = 'highlighted';
	}else{
		field.style.display = '';
		field.className = '';
		link[0].className = '';
	}
}

