function	 sdebug (str, color) 
{
    return;
    document.body.innerHTML = document.body.innerHTML 
        + '<br /><span style="color:' + color + '">' + str + '</span>';
}

var	pm_mi_prefix		= 'MI_';
var	pm_mb_prefix		= 'MB_';

// ---------------------------

function	 rel_pos_x ( obj )
{
    str = "";
	var pos = 0;
	if (obj)
	{
		pos = obj.offsetLeft;
		while (obj.offsetParent != null)
		{
		    str += obj.tagName + ':' + obj.offsetLeft + '\n';
			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;
	};
}

function 	MI_out (id)
{
	hide_process ();
}

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';
		after_show(obj, id);
	};
}

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);
		
		if (psub) // если уже не первое подменю (т.е. сбоку нужно)
		{
			x = rx + MI.clientWidth;
			y = ry ;
			if (x + obj.clientWidth > document.body.clientWidth)
			{
				x = rx - obj.clientWidth-2;
			};
		}
		else
		{
			//obj.style.width = (MI.clientWidth + 2) + 'px';
			y = (ry + MI.clientHeight + 4);
			x = rx;
			if (x + obj.clientWidth > document.body.clientWidth)
				x = document.body.clientWidth - 3 - obj.clientWidth;
		};
		x = x + 'px';
		y = y + 'px';
		obj.style.left = x;
		obj.style.top = y;
	}
}

function after_show(obj, id)
{

}

// ---------------------------

var		hide_time		= 100;
var		g_menu			= new pm_menu ();


