var IE = document.all ? true : false;
var tooltipIsActive = false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

function getMouseXY(e) {

	var width = 0;
	if( document.documentElement && document.documentElement.clientWidth ) {
		documentWidth = document.documentElement.clientWidth;
	}
	else if( document.body && document.body.clientWidth ) {
		documentWidth = document.body.clientWidth;
	}
	else if( window.innerWidth ) {
		documentWidth = window.innerWidth - 18;
	}
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		cursorX = e.pageX - 262;
		cursorY = e.pageY + 10;
	} else if ((e.clientX || e.clientY) && navigator.appVersion.indexOf('MSIE 7') == -1) {
		cursorX = e.clientX + document.body.scrollLeft - document.body.clientLeft - 262;
		cursorY = e.clientY + document.body.scrollTop  - document.body.clientTop + 10;
	} else {
		cursorX = e.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft - 265;
		cursorY = e.clientY + document.documentElement.scrollTop  - document.documentElement.clientTop;
	}
	if(cursorX < 10) cursorX = 10;
	//if(cursorY > (getBodyHeight() - document.getElementById('tooltip').offsetHeight)) cursorY -= document.getElementById('tooltip').offsetHeight;
	if(tooltipIsActive) {
		document.getElementById('tooltip').style.left = cursorX + 'px';
		document.getElementById('tooltip').style.top = cursorY + 'px';
 	}
 	
}


function Tooltip(element, id, movieID, title, text)
{
	this.dataLoaded = false;
	this.title = '';
	this.text  = '<div style="text-align: center;"><img src="/i/loading.gif" alt="" /></div>';
	this.movieID = movieID;
	this.id = id;
	
	if (movieID == 0)
	{
	this.title = title;
	this.text  = text;
	this.dataLoaded = true;
	}
	
	// Attach
	if (movieID == 0)
	{
		eval('document.getElementById(\'' + element + '\').onmouseover = function() { tooltips[' + id + '].showTooltip(); }');
		eval('document.getElementById(\'' + element + '\').onmouseout = function() { tooltips[' + id + '].hideTooltip(); }');
	}
	else
	{
		// Zrobione przy tworzeniu obiektu
	}
	
	this.getData = function()
	{	   	
	   advAJAX.get({
		    url: '/getdata,id,' + movieID,
		    unique: false,
		    mimeType: 'text/javascript',
		    onSuccess : function(obj)
		    {
		    	eval( obj.responseText );
		    	tooltips[id].dataLoaded = true;
		    	if(tooltipIsActive) tooltips[id].showTooltip();
		    }
		});
    }
	
	this.makeVisible = function() {
		document.getElementById('tooltip').style.visibility = 'visible';
	}

	this.showTooltip = function() {
			 
		document.getElementById('tooltip').innerHTML = '<h4>' + this.title + '</h4>' + this.text;
		document.getElementById('tooltip').style.left = window.cursorX + 'px';
		document.getElementById('tooltip').style.top = window.cursorY + 'px';
		tooltipIsActive = true;
		
		this.timeout2 = window.setTimeout(this.makeVisible, 500);
		
		if ( !this.dataLoaded )
		{
		    this.timeout = window.setTimeout(this.getData, 500);
		}
	}
	this.hideTooltip = function() {
		document.getElementById('tooltip').style.visibility = 'hidden';
		tooltipIsActive = false;
		window.clearTimeout(this.timeout);
		window.clearTimeout(this.timeout2);
	}
}

