window.onload = menu_init;


/***********************************************************************\
		Globale variabelen
\***********************************************************************/
var mouse_out_time = 500;
var speed_out = 25;
var speed_in = 10;
var steps = 10;
var end_opac = 0.9;

function menu_init()
{
	window.m_timeout = null;
	window.m_temp = null;
	window.m_last = null;
	window.m_lastover = null;
	window.m_lastob = null;
	window.m_counter = null;
	window.m_lastcntr = null;
	window.m_lastob_out = null;
	window.m_lastcntr_out = null;
	window.m_counter_out = null;
	window.m_fade_out_array = new Array();
	
	window.top_ul = document.getElementById('nav');
	
	var lis = top_ul.getElementsByTagName('li');
	for(var i =0;i < lis.length;i++)
	{
		lis[i].onmouseover = m_over;
		lis[i].onmouseout = m_out_set_timeout;
		
		var uls = lis[i].getElementsByTagName('ul');
		if(uls.length > 0)
		{
			m_init_ob(uls[0]);
			uls[0].style.zIndex = '2';
		}
	}
}

function m_out_set_timeout(e)
{
	var the_e = (e) ? e : window.event;

	var reltg = (the_e.relatedTarget) ? the_e.relatedTarget : the_e.toElement;
	if(reltg)
	{
		var under = m_ancestor(reltg,this);
		if(under === false && reltg != this)
		{
			window.m_last = this;
			window.m_timeout = setTimeout(function(){m_out()},mouse_out_time);
		}
	}
}

function m_out()
{
	if(window.m_last == null)return;
	
	var uls = window.m_last.getElementsByTagName('ul');
	for(var i = 0;i < uls.length;i++)
	{
		m_effect_out(uls[i]);
		window.m_last.className = '';
	}
	window.m_lastover = null;
	window.m_last = null;
}

function m_over(e)
{
	var the_e = (e) ? e : window.event;

	the_e.cancelBubble = true;
	
	if(the_e.stopPropagation)
		the_e.stopPropagation();

	//if(m_last == this)
	clearTimeout(window.m_timeout);
	//for(var i = 0;i < window.m_fade_out_array.length;i++)
	//{
	//	clearTimeout(window.m_fade_out_array[i]);
	//}
	
	if((window.m_last) && (window.m_last != this) && (m_ancestor(this,window.m_last) == false))
	{
		window.m_temp = '';
		m_out();
	}
	else
		window.m_last = null;

	var reltg = (the_e.relatedTarget) ? the_e.relatedTarget : the_e.fromElement;
	var ob = this.getElementsByTagName('ul')[0];
	var under = m_ancestor(reltg,this);
	if(ob && under == false)
	{
		if(window.m_lastover != ob)
		{
			m_effect_over(ob);
			window.m_lastover = ob;
		}
	}
}

function m_set_opac(ob,level)
{
	if(ob)
	{
		ob.style.opacity = level/100;
		ob.style.filter = "alpha(opacity="+level+")";
	}
}
function m_increase_opacity(ob)
{
		if(m_lastob == ob && m_lastcntr == m_counter)
		{
			var opac = Math.round((m_counter/steps) * end_opac * 100);
			m_set_opac(ob,opac);
			m_counter = (m_counter <= steps) ? m_counter + 1 : 1;
			m_lastob = ob;
			m_lastcntr = m_counter;
		}
}

function m_decrease_opacity(ob)
{
		if(m_lastob_out == ob && m_lastcntr_out == m_counter_out)
		{
			var opac = Math.round((100 - ((m_counter_out/steps)* 100)) * end_opac );
			m_set_opac(ob,opac);
			m_counter_out = (m_counter_out <= steps) ? m_counter_out + 1 : 1;
			m_lastob_out = ob;
			m_lastcntr_out = m_counter_out;
		}
}

function m_effect_over(ob)
{
	ob.style.display = 'block';
	ob.style.zIndex = '10';
	window.m_lastob = ob;
	window.m_lastcntr = 1;
	window.m_counter = 1;
	for(var i = 1;i <= steps;i++)
	{
		setTimeout(function(){m_increase_opacity(ob)},i * speed_in);
	}
}

function m_effect_out(ob)
{
	ob.style.zIndex = '2';
	window.m_lastob_out = ob;
	window.m_lastcntr_out = 1;
	window.m_counter_out = 1;
	for(var i = 1;i <= steps;i++)
	{
		window.m_fade_out_array[i - 1] = setTimeout(function(){m_decrease_opacity(ob)},i * speed_out);
	}
	window.m_fade_out_array[i] = setTimeout(function(){ob.style.display = 'none';m_set_opac(ob,0)},steps * speed_out);
}

function m_init_ob(ob)
{
	ob.style.display = 'none';
	m_set_opac(ob,0);
}

function m_ancestor(child, parent)
{
	if(child == null)return false;//Saves checking elsewhere
	if(navigator.userAgent.indexOf('Gecko') != -1 && navigator.userAgent.indexOf('Opera') == -1)
	{
		var allc = parent.getElementsByTagName('*');
		for(var i= 0;i<allc.length;i++)
		{
			if(allc[i] == child)
			{
				return true;
			}
		}
	}
	else
	{
		for(; child.parentNode; child = child.parentNode)
		{
			if(child.parentNode === parent) return true;
		}
	}
	return false;
}

function top_li(ob)
{
	for(;ob.parentNode != window.top_ul; ob = ob.parentNode);
	
	return ob;
}

function debug(str)
{
	var x = document.getElementById('debug');
	x.innerHTML = str + '<br />' + x.innerHTML;
}


