dojo.require("dojo.fx");
dojo.require("dojo.fx.easing");

var ti = new Array();
var w = new Array();
var a = new Array();

function m_fi(m)
{
	var handle = dojo.byId(m);
	if (dojo.style(handle, 'display') != 'block')
	{
		dojo.style(handle, 'display', 'block');
		dojo.fadeIn({
			node: handle,
			duration: 150,
		}).play();
	}
}

function m_fo(m)
{
	var handle = dojo.byId(m);
	{
		dojo.fadeOut({
			node: handle,
			duration: 150,
			onEnd: function() {
				dojo.style(handle, 'display', 'none');
			}
		}).play();
	}
}

function g_i(m)
{
	tm = m.split('_');
	return parseInt(tm[0].substr(1));
}

function m_l(m)
{
	if (dojo.byId(m) != null)
	{
		m_fi(m)
		ix = g_i(m);
		ti[ix] = -1;
		w[ix] = 250;
		a[ix] = new Object;
		a[ix] = null;
	}
}

function m_sh(m)
{
	ix = g_i(m.id);
	var ms = 'm'+m.id.substr(1);
	clearTimeout(ti[ix]);
	t = -1;
	if (a[ix] != null)
	{
		var s = '';
		var t = new Array();
		t = a[ix].id.split("_");
		var tn = new Array();
		tn = m.id.split("_");
		for(i=1; i <t.length; i++)
		{
			s += "_"+t[i];
			if ((t[i] != tn[i]) && (dojo.byId('m'+ix+s) != null))
				m_fo('m'+ix+s);
		}
	}
	a[ix] = m;
	var mn = dojo.byId(ms);
	if (mn != null)
	{
		if (ms.split('_').length > 2)
		{
			mn.style.top = m.offsetTop+'px';
			mn.style.left = (m.offsetWidth+m.offsetLeft)+'px';
			m_fi(ms);
		}
		else
		{
			mn.style.top = (m.offsetHeight+m.offsetTop)+'px';
			mn.style.left = m.offsetLeft+'px';
			m_fi(ms);
		}
	}
}

function m_sv(m, rightMenu)
{
	ix = g_i(m.id);
	var ms = 'm'+m.id.substr(1);
	clearTimeout(ti[ix]);
	t = -1;
	if (a[ix] != null)
	{
		var s = '';
		var t = new Array();
		t = a[ix].id.split("_");
		var tn = new Array();
		tn = m.id.split("_");
		for(i=1; i <t.length; i++)
		{
			s += "_"+t[i];
			if ((t[i] != tn[i]) && (dojo.byId('m'+ix+s) != null))
				m_fo('m'+ix+s);
		}
	}
	a[ix] = m;

	var mn = dojo.byId(ms);
	if (mn != null)
	{
		mn.style.top = m.offsetTop+'px';
		if ((typeof(rightMenu) == 'undefined') || (rightMenu == false))
			mn.style.left = (m.offsetLeft+m.offsetWidth)+'px';
		else
			mn.style.left = (m.offsetLeft-m.offsetWidth)+'px';
		m_fi(ms);
	}
}

function c_m(ix)
{
	if (a[ix] != null)
	{
		var s = '';
		var t = new Array();
		var x = new Array();
		t = a[ix].id.split("_");
		for(i=1; i<t.length; i++)
		{
			s += "_"+t[i];
			if(dojo.byId('m'+ix+s) != null)
				x[i] = s;
		}
		for (i=x.length-1; i>0; i--)
		{
			m_fo('m'+ix+x[i]);
		}
		a[ix] = null;
	}
}

function m_c(m)
{
	ix = g_i(m.id);
	ti[ix]=setTimeout('c_m('+ix+')',w[ix]);
}

function createMenu(handle, mType)
{
	var menuElement = null;
	var menuType = 'h'
	if ((typeof(menuType) != 'undefined') && ((mType == 'v-left') || (mType == 'v-right') || (mType == 'h')))
		menuType = mType;

	if (typeof(handle) != 'undefined')
	{
		if (dojo.isString(handle))
		{
			if (dojo.byId(handle) != null)
				menuElement = dojo.byId(handle);
		}
		else
			if (handle != null)
				menuElement = handle;
	}

	if (menuElement != null)
	{
		function browseMenu(submenu, suffix, deep, index, count)
		{
			if (typeof(deep) == 'undefined')
				var deep = 0;
			if (typeof(index) == 'undefined')
				var index = 0;
			if (typeof(count) == 'undefined')
				var count = submenu.children.length;
			if (typeof(suffix) == 'undefined')
			{
				suffix = parseInt(submenu.id.substr(1));
				if (isNaN(suffix))
					suffix = '0';
			}

			var ulWidth = 0;
			var ulTmpWidth = 0;
			if ((index == 0) && (deep == 0))
				ulWidth = submenu.offsetWidth;

			dojo.forEach(submenu.children, function(child){
				if (child.nodeName == 'A')
				{
					child.id = 'a'+suffix+'_'+index;
					child.onmouseover = function(){
						switch(menuType)
						{
							case 'h':
								m_sh(this, false);
								break;
							case 'v-left':
								m_sv(this, false);
								break;
							case 'v-right':
								m_sv(this, true);
								break;
						}
					};
					child.onmouseout =  function(){
						m_c(this);
					};
				}
				else
				{
					if (child.nodeName == 'LI')
					{

						if ((menuType == 'h') && (deep == 0))
						{
							ulTmpWidth += child.offsetWidth;
							if (ulTmpWidth > ulWidth)
								dojo.style(child, 'display', 'none');
						}

						if (index == 0)
							dojo.addClass(child, 'li_first');
						index ++;
						if (index == count)
							dojo.addClass(child, 'li_last');
						browseMenu(child, suffix, deep, index);
					}
					else
						if (child.nodeName == 'UL')
						{
							dojo.addClass(child, 'deep_' + (deep + 1));
							child.id = 'm'+suffix+'_'+index;
							browseMenu(child, suffix+'_'+index, deep + 1, 0, child.children.length);
						}
				}
			});
		}

		switch(menuType)
		{
			case 'h':
				dojo.addClass(menuElement, 'menu-hor-0');
				break;
			case 'v-left':
				dojo.addClass(menuElement, 'menu-lev-0');
				break;
			case 'v-right':
				dojo.addClass(menuElement, 'menu-prav-0');
				break;
		}

		browseMenu(menuElement);
		m_l(menuElement.id);
	}
}


