var context_menu_div	= 'DIV_CONTEXT_MENU';
var context_menu_is_ie	= true;
var context_menu_lst	= new Array();
var context_menu_load	= false;

function context_menu_item_init()
{
	context_menu_lst	= new Array();
	
	return true;
}

function context_menu_item_add(nom, act)
{
	if (!nom) nom = '';
	if (!act) act = null;
	context_menu_lst.push(new Array(nom, act));	

	return true;
}

function context_menu_draw()
{
	if (!context_menu_load) return false;

	var obj  = document.getElementById(context_menu_div);
	var html = '';
	var fnc  = null;
	var act  = '';
	var scr  = ' onmouseover="this.style.background=\'#AAAAAA\';" onmouseout="this.style.background=\'\';" ';
	
	for(var k=0; k<context_menu_lst.length; k++)
	{
		fnc = context_menu_lst[k];
		if (fnc[0]=='')
		{
			html+= "<hr >\n";			
		}
		else if (fnc[1]==null)
		{
			html+= '<div style="color:#888888; width:99%;" '+scr+' >'+fnc[0]+"</div>\n";
		}
		else
		{
			html+= '<div style="cursor: pointer; width:99%; " '+scr+' onclick="'+fnc[1]+'">'+fnc[0]+"</div>\n";
		}
	}
	
	obj.innerHTML  = html;

	return true;
}

function context_menu_show(evt)
{
	if (!context_menu_load) return false;

	var obj = document.getElementById(context_menu_div);
	
	if (context_menu_is_ie)
	{
		var pos_x = event.clientX;
		var pos_y = event.clientY;
	}
	else
	{
		var pos_x = evt.clientX;
		var pos_y = evt.clientY;	
	}
	

	var scr_w = document.body.clientWidth;
	var scr_h = document.body.clientHeight; 

	if (!scr_w) scr_w = 1024;
	if (!scr_h) scr_h = 530;

	obj.style.display = 'block';
	var obj_w = obj.clientWidth;
	var obj_h = obj.clientHeight;
	
	if (pos_x + obj_w > scr_w - 10) pos_x-= obj_w;
	if (pos_y + obj_h > scr_h - 10) pos_y-= obj_h;
	
	if (pos_x<0) pos_x = 0;
	if (pos_y<0) pos_y = 0;

	if (pos_x>0) pos_x+= 'px';
	if (pos_y>0) pos_y+= 'px';

	obj.style.left = pos_x;
	obj.style.top  = pos_y;
		
	return false;
}

function context_menu_hide(evt)
{
	if (!context_menu_load) return false;

	var obj = document.getElementById(context_menu_div);
	
	obj.style.display = 'none';

	return true;
}

function context_menu_init()
{
	if (!document.getElementById(context_menu_div))
	{
		setTimeout('context_menu_init();', 500);
		return false;	
	}

	context_menu_is_ie = (navigator.userAgent.toLowerCase().indexOf('msie')>-1);
	
	addEvent(document.body, 'click', context_menu_hide);
	
	context_menu_load = true;

	return true;
}

context_menu_init();