var Tooltip = Class.create();
Tooltip.prototype = {
	showTimeout: 0.55,			//tooltip to show timeout
	minDelta: 5,				//minimum viewport delta
	xOffsetLeft: -5,			//horisonatl offset from cursor
	xOffsetRight: 5,			//horisonatl offset from cursor
	yOffsetTop: 1,				//vertical offset from cursor
	yOffsetBottom: 13,			//vertical offset from cursor
	
	tooltip: null,				//tooltip element
	tooltipEventElement: null,	//tooltip generator
	tooltipMouseMoveObserver: null, //tooltip mouse move observer
	tooltipVisible: false,		//show status
	tooltipShowTimeout: null,	//show timeout
	
	initialize: function (element)
	{
		this.element = element;
		this.tooltip = this.createTooltipElement(element);
		this.tooltipMouseMoveObserver = this.onMouseMove.bind(this);
		this.tooltipMouseOutObserver = this.onMouseOut.bind(this);
	},
	
	set: function(event, entries)
	{
		this.tooltipX = Event.pointerX(event);
		this.tooltipY = Event.pointerY(event);
		this.tooltipEventElement = Event.element(event);

		//hack observe mouse move on tooltip actor
		if ( !Element.hasClassName(this.tooltipEventElement, "tootlipObserved") ) {
			Element.addClassName(this.tooltipEventElement, "tootlipObserved");
			Event.observe(this.tooltipEventElement, "mousemove", this.tooltipMouseMoveObserver);
			Event.observe(this.tooltipEventElement, "mouseout", this.tooltipMouseOutObserver);
		}
		
		var data = '';
		$A(entries).each(function(entry) {
		    switch (entry.type) {
		      case 'string':
				data += entry.string + '<br>';
		      	break;
		    }
		}, this)
		Element.update(this.tooltip, data);
		this.show();
	},
	
	onMouseMove: function(event)
	{
		this.tooltipX = Event.pointerX(event);
		this.tooltipY = Event.pointerY(event);
		
		if ( this.tooltipVisible ) {
			this.showAction(this.tooltip);
		}
	},

	onMouseOut: function(event)
	{
		this.unset();
	},

	unset: function ()
	{
		this.hide();
	},

	showTimed: function()
	{
		if ( false === this.tooltipShowTimeout ) {
			this.finishShowTimed(); //show new message
			return;
		} else
		if ( null !== this.tooltipShowTimeout ) {
			this.cancelShowTimed();
		}
		
		this.tooltipShowTimeout = new PeriodicalExecuter(this.finishShowTimed.bind(this), this.showTimeout);
	},

	cancelShowTimed: function()
	{
		if	(this.tooltipShowTimeout) {
			this.tooltipShowTimeout.stop();
			this.tooltipShowTimeout = null;
		}
	},

	finishShowTimed: function()
	{
		this.cancelShowTimed();
		this.tooltipShowTimeout = false;
		this.showAction(this.tooltip);
	},

	show: function()
	{
		this.showTimed();
	},
	
	getShowPosition: function()
	{
		var x = this.tooltipX;
		var y = this.tooltipY;		

		var tDim = Element.getDimensions(this.tooltip);
		var vDim = document.viewport.getDimensions();
		var vScr = document.viewport.getScrollOffsets();
		
		var freeRight = vDim.width + vScr.left - this.tooltipX;
		var freeLeft = this.tooltipX - vScr.left;
		var freeTop = this.tooltipY - vScr.top;
		var freeBottom = vDim.height + vScr.top - this.tooltipY;
		
		//apply better x
		if( freeRight > tDim.width + this.minDelta + this.xOffsetRight) {
			x = this.tooltipX + this.xOffsetRight;
		} else
		if( freeLeft > tDim.width + this.minDelta + this.xOffsetLeft) {
			x = this.tooltipX - tDim.width - this.xOffsetLeft;
		} else {
			x = this.tooltipX - (tDim.width + this.xOffsetLeft) / 2;
		}
		
		//apply better y
		if (freeBottom > tDim.height + this.minDelta + this.yOffsetBottom) {
			y = this.tooltipY + this.yOffsetBottom;
		} else
		if( freeTop > tDim.height + this.minDelta + this.yOffsetTop) {
			y = this.tooltipY - tDim.height - this.yOffsetTop;
		} else {
			y = this.tooltipY - (tDim.height + this.yOffsetTop) / 2;
		}
		
		return { x: x, y: y };
	},

	showAction: function (tooltip)
	{
		var position = this.getShowPosition();
		//change position
		Element.setStyle(tooltip, {
			position: 'absolute',
			top: position.y + 'px',
			left: position.x + 'px'
		});
		
		//show
		Element.show(tooltip);
		
		//first show of toltip?
		var firstShow = !this.tooltipVisible;		
		this.tooltipVisible = true;
		if ( firstShow ) { //update position with correct tooltip size
			this.showAction(tooltip);
		}
	},

	hide: function()
	{
		this.cancelShowTimed();
		this.tooltipShowTimeout = null,
		this.hideAction(this.tooltip);
	},
	
	hideAction: function (tooltip)
	{
		this.tooltipVisible = false;
		Element.hide(tooltip);
	},
	
	createTooltipElement: function(element)
	{
		tooltip = document.createElement('div');
		Element.addClassName(tooltip, 'tooltip');
		this.hideAction(tooltip);
		document.body.appendChild(tooltip);
		return tooltip;
	}
}

var tooltip;
Event.observe(window, "load", function() { tooltip = new Tooltip(document); });

//TODO remove this ... 

function mouse_move()
{
	//legacy conhelp nessages
}

function SetConHelp()
{
	//legacy conhelp nessages
}