/*
	BiiOJS - Javascript Library
	Copyright (C) 2005  Fabrizio Filieri
	fabrizio.filieri@biiomutimedia.com
	http://www.biiomultimedia.com   
	
	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

function BiiOTooltip(idcontainer, id)
{
	this.id = id;
	this.idcontainer = idcontainer;
	this.width = 200;
	this.height = 30;

	this.idTimer = 0;

	this.cssstyle = "";

	this.sleep = 200;
	this.zIndex = 24
}

BiiOTooltip.prototype.create = function()
{
	// print hidden layer (tooltip)
	if (this.cssstyle == "")
		document.writeln('<div id="'+this.id+'" style="position:absolute; left:0px; top:0px; z-index:'+this.zIndex+'; visibility:hidden; width:200px; border:1px solid #909090; background:#FFFFFF;"></div>');
	else
		document.writeln('<div id="'+this.id+'" class="'+this.cssstyle+'" style="position:absolute; left:0px; top:0px; z-index:'+this.zIndex+'; visibility:hidden;"></div>');
	
	// register event
	var element = document.getElementById(this.idcontainer);
	var obj = this;
	if (element.addEventListener) // W3C event model
	{
		element.addEventListener("mouseover", function(evt) {BiiOTooltip_showTooltip(evt, obj);}, false);
		element.addEventListener("mouseout", function(evt) {BiiOTooltip_hideTooltip(evt, obj);}, false);
		element.addEventListener("mousemove", function(evt) {BiiOTooltip_moveTooltip(evt, obj);}, false);
	}
	else if (element.attachEvent) // IE event model
	{
		element.attachEvent("onmouseover", function() {BiiOTooltip_showTooltip(null, obj);});
		element.attachEvent("onmouseout", function() {BiiOTooltip_hideTooltip(null, obj);});
		element.attachEvent("onmousemove", function() {BiiOTooltip_moveTooltip(null, obj);});
	}


	document.getElementById(this.id).innerHTML = this.code;
}

function ShowTooltip(id)
{
	document.getElementById(id).style.visibility='visible';
}

function BiiOTooltip_showTooltip(e, obj)
{
	if(!e) e = window.event;
	var tooltip = document.getElementById(obj.id);
	// posizione del mouse
	var x = e.clientX + document.body.scrollLeft;
	var y = e.clientY + document.body.scrollTop;
	var pageHeight = document.body.clientHeight;
	var pageWidth = document.body.clientWidth;
	
	// spostamento verticale
	if ((y+tooltip.clientHeight + 20) > pageHeight)
	{
		y = pageHeight - tooltip.clientHeight - 20;
	}
	
	// riflessione orizzontale
	if ((x+tooltip.clientWidth + 30) > pageWidth)
	{
		x = x - tooltip.clientWidth - 30;
	}
	
	document.getElementById(obj.id).style.left = x + 20;
	document.getElementById(obj.id).style.top = y;
	obj.idTimer = window.setTimeout('ShowTooltip(\''+obj.id+'\')', obj.sleep);
}

function BiiOTooltip_hideTooltip(e, obj)
{
	var tooltip = document.getElementById(obj.id);
	tooltip.style.visibility='hidden';
	tooltip.style.top = 0;
	tooltip.style.left= 0;
	window.clearTimeout(obj.idTimer);
}

function BiiOTooltip_moveTooltip(e, obj)
{
	if(!e) e = window.event;
	var tooltip = document.getElementById(obj.id);
	// posizione del mouse
	var x = e.clientX + document.body.scrollLeft;
	var y = e.clientY + document.body.scrollTop;
	var pageHeight = document.body.clientHeight;
	var pageWidth = document.body.clientWidth;
	
	// spostamento verticale
	if ((y+tooltip.clientHeight+20) > pageHeight)
	{
		y = pageHeight - tooltip.clientHeight - 20;
	}
	
	// riflessione orizzontale
	if ((x+tooltip.clientWidth+30) > pageWidth)
	{
		x = x - tooltip.clientWidth - 30;
	}
	
	document.getElementById(obj.id).style.left = x + 20;
	document.getElementById(obj.id).style.top = y;
}
