var timerID = 0;
var tStart  = null;
var obj_help = null;
var previouscursor='default';
var debugmode = 0;
var x;
var y;

var help_x = 25;
var help_y = 25;
	
function printDebug(msg)
{
	if (debugmode) window.status = msg;
}


function UpdateTimer()
{
	
	if (timerID)
	{
		clearTimeout(timerID);
		clockID  = 0;
	}

	if (!tStart) tStart = new Date();
	
	var   tDate = new Date();
	var   tDiff = tDate.getTime() - tStart.getTime();
	
	tDate.setTime(tDiff);
	
	printDebug('TIMER - '+tDate.getSeconds()+'s');

	if (tDate.getSeconds()==timerdelay && obj_help!=null)
	{
		previouscursor = obj_help.style.cursor;
		obj_help.style.cursor='help';
		
		if (autodisplay)
		{
			if (obj_help.tagName && obj_help.id != 'tooltip')
			{
				if (previousid != 0) HideTip();
				previousid = obj_help.id;
				ShowTip(obj_help.id);
			}
		}
	}

	timerID = setTimeout("UpdateTimer()", 1000);
}

function Start()
{
	tStart   = new Date();
	UpdateTimer()
}

function Stop()
{
	if(timerID)
	{
	clearTimeout(timerID);
	timerID  = 0;
	}

	tStart = null;
}

function Reset()
{
	Stop();
	
	if (obj_help != null)
	{
		if (obj_help.style.cursor=='help')
		{
			obj_help.style.cursor = previouscursor;
			obj_help = null;
		}
	}
	tStart = null;
}



var previousid=0;
var info='';

if (navigator.appName.indexOf("Netscape") != -1) 
{
	document.captureEvents(Event.MOUSEOVER);
	document.captureEvents(Event.MOUSEOUT);
	document.captureEvents(Event.MOUSEDOWN);
	document.captureEvents(Event.MOUSEMOVE);
	document.onmouseover = overNS;
	document.onmouseout  = outNS;
	document.onmousedown = clicNS;
	document.onmousemove = moveNS;

}
if (navigator.appName.indexOf("Microsoft") != -1) 
{
	document.onmousemove = moveIE;
	document.onmousedown = clicIE;
	document.onmouseout  = outIE;
	document.onmouseover = overIE;
}

function clicCommon(nav, button, src)
{
	if ((button == 2 && nav == 'ie') || (button == 3 && nav == 'ns') ) // left button
	{  
		if (src.tagName && src.id != 'tooltip')
		{
			if (src.id == previousid || src.id == '') // same id
			{
				HideTip();
				previousid = 0;
			}
			else
			{
				if (previousid != 0) HideTip();
				previousid = src.id;
				ShowTip(src.id,x,y);
			}
				return false;
		}
	}
	else
	{
		if (src.tagName && src.id != 'tooltip')
		{
			if (previousid != 0) HideTip();
			previousid = 0;
		}
	}
}

function ClickTip(src_id)
{
	
	if (src_id != 'tooltip')
	{
		if (src_id == previousid || src_id == '') // same id
		{
			HideTip();
			previousid = 0;
		}
		else
		{
			if (previousid != 0) HideTip();
			previousid = src_id;
			ShowTip(src_id,x,y);
		}
	}
}

function ShowTip(fArg)
{
	
	var tooltipOBJ = (document.getElementById) ? document.getElementById('msg') : eval("document.all['msg']");
	if (tooltipOBJ != null) {
		var tooltipLft = (document.body.offsetWidth?document.body.offsetWidth:document.body.style.pixelWidth) - (tooltipOBJ.offsetWidth?tooltipOBJ.offsetWidth:(tooltipOBJ.style.pixelWidth?tooltipOBJ.style.pixelWidth:200)) - 30;
		var tooltipTop = 10;
		if (navigator.appName == 'Netscape') {
			if (parseFloat(navigator.appVersion) >= 5) { tooltipTop = (document.body.scrollTop>=0?document.body.scrollTop+10:y+10); }
			tooltipOBJ.style.left = tooltipLft; tooltipOBJ.style.top = tooltipTop;
		}
		else {
			tooltipTop = y+help_y;
			tooltipLft = x+help_x;
			/*
			tooltipTop = (document.body.scrollTop>=0?document.body.scrollTop+10:y+10);
			if ((x > tooltipLft) && (y < (tooltipOBJ.scrollHeight?tooltipOBJ.scrollHeight:tooltipOBJ.style.pixelHeight) + 10)) {
				tooltipTop = (document.body.scrollTop?document.body.scrollTop:document.body.offsetTop) + y + 20;
			}
			*/
			tooltipOBJ.style.pixelLeft = tooltipLft; tooltipOBJ.style.pixelTop = tooltipTop;
		}
		tooltipOBJ.style.visibility = "visible";
		tooltipOBJ.style.filter = "alpha(opacity=90)";
		tooltipmsg = '';
		
		for (i=0;i<dims_help.length;i++)
		{
			if (dims_help[i][0] == fArg) tooltipmsg = dims_help[i][1];
		}
		
		tooltipOBJ.innerHTML = header+tooltipmsg+footer;
	}
}

function HideTip()
{

	var tooltipOBJ = (document.getElementById) ? document.getElementById('msg') : eval("document.all['msg']");
	if (tooltipOBJ != null) {
		tooltipOBJ.style.pixelLeft = 0; 
		tooltipOBJ.style.pixelTop = 0;
		tooltipOBJ.style.visibility = "hidden";
	}
}

/**
* INTERNET EXPLORER functions
**/

function clicIE()  
{ 
	// try to find the ID parent element (IE specific)

	Reset();

	obj = event.srcElement;
	
	while(obj.id == '' && obj.parent)
	{
		obj = obj.parent;
	}
	
	clicCommon('ie', event.button, obj);
} 

function overIE()  
{ 
	// try to find the ID parent element (IE specific)
	obj = event.srcElement;
	
	while(obj.id == '' && obj.parent)
	{
		obj = obj.parent;
	}

	if (obj.id != '' && obj.id != 'msg' && obj.id != 'tooltip')
	{
		// search if available help for current element
		help_found = false;
		for (i=0;i<dims_help.length;i++)
		{
			if (dims_help[i][0] == obj.id) help_found = true;
		}
		
		if (help_found) // if found start timer
		{
			obj_help = obj;
			Start(); // start timer
			printDebug('IE MOUSEOVER - ' + obj.id + ' => START TIMER');
		}
	}

} 

function outIE()  
{ 
	// try to find the ID parent element (IE specific)
	obj = event.srcElement;
	
	while(obj.id == '' && obj.parent)
	{
		obj = obj.parent;
	}

	if (obj.id != '' && obj.id != 'msg' && obj.id != 'tooltip')
	{
		printDebug('IE MOUSEOUT - ' + obj.id + ' => RESET TIMER');
	}
	previousid = 0;
	HideTip();
	Reset();

} 

function moveIE()
{
	x = event.clientX;
	y = event.clientY;

	obj = event.srcElement;
	
	var tooltipOBJ = (document.getElementById) ? document.getElementById('msg') : eval("document.all['msg']");
	if (tooltipOBJ != null)
	{
		if (tooltipOBJ.style.visibility == "visible")
		{
			tooltipOBJ.style.pixelLeft = x+help_x;
			tooltipOBJ.style.pixelTop = y+help_y;
		}
	}		
	
	//printDebug('IE x,y - ' + x + ',' + y);
	return true;
}

/**
* NETSCAPE functions
**/

function clicNS(ev)  
{ 
	Reset();

	clicCommon('ns', ev.which, ev.target);
} 

function overNS(ev)  
{ 
	if (ev.target.id != null && ev.target.id != '' && ev.target.id != 'msg' && ev.target.id != 'tooltip')
	{
		
		obj_help = ev.target;
		Start(); // start timer
		printDebug('NS MOUSEOVER - ' + ev.target.id + ' => START TIMER');
	}
} 

function outNS(ev)
{
	Reset();
	printDebug('NS MOUSEOUT - ' + ev.target.id + ' => RESET TIMER');
}


function moveNS(ev)
{
	x = ev.pageX;
	y = ev.pageY;
	printDebug('NS x,y - ' + x + ',' + y);
	return true;
}