function	 sdebug (str, color) 
{
}

var	pm_mi_prefix		= 'MI_';
var	pm_mb_prefix		= 'MB_';

// ---------------------------

function	 rel_pos_x ( obj )
{
	var pos = 0;
	if (obj)
	{
		pos = obj.offsetLeft;
		while (obj.offsetParent != null)
		{
			obj = obj.offsetParent;
			pos += obj.offsetLeft;
			if (obj.tagName == "BODY") break;
		};
	}
	return pos;
}

function	 rel_pos_y ( obj )
{
	var pos = 0;
	if (obj)
	{
		pos = obj.offsetTop;
		while (obj.offsetParent != null)
		{
			obj = obj.offsetParent;
			pos += obj.offsetTop;
			if (obj.tagName == "BODY") break;
		};                      
	}
	return pos;
}

function	 find_mi (id)
{
	ret = document.getElementById (pm_mi_prefix + id);
	return ret;
}

function	 find_mb (id)
{
	ret = document.getElementById (pm_mb_prefix + id);
	return ret;
}


// +++++++++++++++++++++++++++++++++++++++++++

// CLASS: pm_menu
function	 pm_menu (id, parent)
{
	if (id == undefined) id = 0;
	if (parent == undefined) parent = 0;
	this.id = id;
	this.id_parent = parent;
	this.opens = null;
	this.sub = new Array ();
	this.timers = new Array ();
	this.max_id = 0;
}

function 	register_menu (id, parent)
{
	if (id)
	{
		g_menu.sub[id] = new pm_menu (id, parent);
		sdebug ('register: id=' + id + ', pid = '+parent, 'cyan');
		if (id > g_menu.max_id)
		{
			g_menu.max_id = id;
			sdebug ('max: ' + g_menu.max_id, 'navy');
		}
	};
}

function	 get_sub (id)
{
	ret = g_menu.sub[id];
	if (ret == undefined)
		ret = null;
	return ret;
}

function	 MI_over (id)
{
	sdebug ('MI over: '+id, 'green');
	
	var sub = get_sub (id);
	hide_process ();
	if (sub)
	{
		show_menu (id);
		current_menu = sub;
	};
	
	MI_over_draw(id);
}

function 	MI_out (id)
{
	hide_process ();
	
	MI_out_draw(id);
}

function 	MB_over (id)
{
	sdebug ('MB over: '+id, 'blue');
	
	stop_hide_list (id);
}



function 	MB_out (id)
{
	sdebug ('MB out: '+id, 'blue');
	
	var sub = get_sub (id);
	if (sub)
	{
		hide_process (id);
	};
}

function	 stop_hide_list (id)
{
	var sub = get_sub (id);
	if (sub)
	{	
		sdebug ('stop hide list: ' + id);
		
		stop_hide (id);
		if (sub.id_parent)
			stop_hide_list (sub.id_parent);
	};
}


function	 hide_process (id)
{               
	sdebug ('hide process: ' + id);
	for (var i = 0; i <= g_menu.max_id; i++)
		start_hide (i);
}

function 	build_parents_list (id, parents)
{
	var	sub = get_sub (id);
	if (sub)
	{
		if (sub.id_parent)
		{
			parents [sub.id_parent] = sub.id_parent;
			build_parents_list (sub.id_parent, parents);
		}
	};
}

function 	start_hide (id)
{
	var	sub = get_sub (id);
	if (sub)
	{
		sdebug ('start hide: ' + id, 'red');
		
		sdebug('STOP BEFORE START: '+id+'/'+g_menu.timers[id], '#369');
		stop_hide (id); 
		
		g_menu.timers [id] = setTimeout ("hide_menu ("+id+")", hide_time);
	}
}

function 	stop_hide (id)
{
	if (id == 11) sdebug ('STOP id/timer: ' + id + '/'+ g_menu.timers[id], 'magenta');
	clearTimeout(g_menu.timers[id]);
	g_menu.timers[id] = null;
}

function	 stop_all_hide ()
{
	var s = '';
	for (var i = 0; i <= g_menu.max_id; i++)
	//if (g_menu.timers [i] != undefined)
	{
		s += i + ', ';
		stop_hide (i);
	}
	sdebug ('clear hides: ' + s, 'red');
}

function	 show_menu (id)
{
	var	obj = find_mb (id);
	if (obj)
	{
		sdebug ('show: ' + id, 'red');
		
		stop_hide (id);
		set_coo (obj, id);
		obj.style.display = 'block';
		
	};
}

function 	hide_menu (id)
{
	var	obj = find_mb (id);
	if (obj)
	{
		sdebug ('hide: ' + id, 'red');
		
		stop_hide (id);
		obj.style.display = 'none';
		
	};
}

function	 set_coo (obj, id)
{
	if (obj && id)
	{
		var sub = get_sub (id);
		var psub = get_sub (sub.id_parent);
		
		var MI = find_mi (id);
		rx = rel_pos_x (MI);
		ry = rel_pos_y (MI);
		
		x = rx + (MI.clientWidth / 4 * 3);
		y = ry + 1;
		
		obj.style.left = x+'px';
		obj.style.top = y+'px';
	}
}

//{{{ DONE: MI_out_draw
function 	MI_out_draw(id)
{
	var MI = find_mi(id);
	if(MI)
	{
		var MI_pre = document.getElementById(pm_mi_prefix + id + '_PRE');
		var MI_pre_l = document.getElementById(pm_mi_prefix + id + '_PRE_L');
		
		var MI_post = document.getElementById(pm_mi_prefix + id + '_POST');
		var MI_post_f = document.getElementById(pm_mi_prefix + id + '_POST_F');
		var MI_post_l = document.getElementById(pm_mi_prefix + id + '_POST_L');
		
		if(MI.getAttribute('level2'))
		{
			MI.style.backgroundColor = '#C0C0C0';
			if(MI_pre) MI_pre.style.backgroundColor = '#C0C0C0';
			if(MI_post) MI_post.style.backgroundColor = '#C0C0C0';
			if(MI_post_f) MI_post_f.style.backgroundImage = 'url(/i/sm-corn-rt.gif)';
			if(MI_post_l) MI_post_l.style.backgroundImage = 'url(/i/sm-corn-rb.gif)';
			if(MI_pre_l) MI_pre_l.style.backgroundImage = 'url(/i/sm-corn-lb.gif)';
		}
		else
		if(MI.getAttribute('levels'))
		{
			MI.style.backgroundColor = '#A5A5A5';
			if(MI_pre) MI_pre.style.backgroundColor = '#A5A5A5';
			if(MI_post) MI_post.style.backgroundColor = '#A5A5A5';
			if(MI_post_f) MI_post_f.style.backgroundImage = 'url(/i/sm2-corn-rt.gif)';
			if(MI_post_l) MI_post_l.style.backgroundImage = 'url(/i/sm2-corn-rb.gif)';
			if(MI_pre_l) MI_pre_l.style.backgroundImage = 'url(/i/sm2-corn-lb.gif)';
		};
	};
}
//}}}

//{{{ DONE: MI_over_draw
function 	MI_over_draw(id)
{
	var MI = find_mi(id);
	if(MI)
	{
		var MI_pre = document.getElementById(pm_mi_prefix + id + '_PRE');
		var MI_pre_l = document.getElementById(pm_mi_prefix + id + '_PRE_L');
		
		var MI_post = document.getElementById(pm_mi_prefix + id + '_POST');
		var MI_post_f = document.getElementById(pm_mi_prefix + id + '_POST_F');
		var MI_post_l = document.getElementById(pm_mi_prefix + id + '_POST_L');
		
		if(MI.getAttribute('level2'))
		{
			MI.style.backgroundColor = '#B2B2B2';
			if(MI_pre) MI_pre.style.backgroundColor = '#B2B2B2';
			if(MI_post) MI_post.style.backgroundColor = '#B2B2B2';
			if(MI_post_f) MI_post_f.style.backgroundImage = 'url(/i/sm-corn-rt-a.gif)';
			if(MI_post_l) MI_post_l.style.backgroundImage = 'url(/i/sm-corn-rb-a.gif)';
			if(MI_pre_l) MI_pre_l.style.backgroundImage = 'url(/i/sm-corn-lb-a.gif)';
		}
		else
		if(MI.getAttribute('levels'))
		{
			MI.style.backgroundColor = '#909090';
			if(MI_pre) MI_pre.style.backgroundColor = '#909090';
			if(MI_post) MI_post.style.backgroundColor = '#909090';
			if(MI_post_f) MI_post_f.style.backgroundImage = 'url(/i/sm2-corn-rt-a.gif)';
			if(MI_post_l) MI_post_l.style.backgroundImage = 'url(/i/sm2-corn-rb-a.gif)';
			if(MI_pre_l) MI_pre_l.style.backgroundImage = 'url(/i/sm2-corn-lb-a.gif)';
		};
	};
}
//}}}

// ---------------------------

var		hide_time		= 100;
var		g_menu			= new pm_menu ();

