

function Tooltips (tags)
{
	this.__construct = function (tags)
	{
		//this.tag = tag;
		
		for (var i=0; i<tags.length; i++)
		{
			this.Deploy(tags[i]);
		}
	}
	
	this.Deploy = function (tag)
	{//alert('DDD');
		var anchors = document.getElementsByTagName(tag);
		for (var i=0; i<anchors.length; i++)
		{//alert(anchors[i].hasAttribute);
			if (anchors[i].getAttribute('title') != undefined)
			{//alert('new_t');
				anchors[i].tooltip = new Tooltip(anchors[i]);
			}
		}
	}
	
	this.__construct(tags);
}

function Tooltip (owner)
{
	this.__construct = function (owner)
	{
		this.owner = owner;
		this.Deploy();
	}
	
	this.Deploy = function ()
	{
		this.obj = document.createElement('div');
		this.obj.className = 'tooltip';
		this.content = this.owner.getAttribute('title');
		if (this.content.search(/^\@/) == -1)
		{
			this.obj.className += ' text_tooltip';
		}
		$(this.owner); // IE
		this.owner.parentNode.insertBefore(this.obj,this.owner);
		this.owner.removeAttribute('title');
		this.owner.on('mouseover',function (e) { obj = (e != undefined) ? e.target : event.srcElement; while (obj.parentNode && !obj.tooltip) {obj = obj.parentNode} obj.tooltip.Show(); if (e) e.preventDefault(); });
		this.owner.on('mousemove',function (e) { obj = (e != undefined) ? e.target : event.srcElement; while (obj.parentNode && !obj.tooltip) {obj = obj.parentNode} obj.tooltip.Move(e); if (e) e.preventDefault(); });
		this.owner.on('mouseout',function (e) { obj = (e != undefined) ? e.target : event.srcElement; while (obj.parentNode && !obj.tooltip) {obj = obj.parentNode} obj.tooltip.Hide(); if (e) e.preventDefault(); });
	}
	
	this.Load = function (uri)
	{
		this.api = new DataSource('raw',uri,new Callback(this,'LoadReply'));
		this.api.Get();
	}
	
	this.LoadReply = function (data)
	{
		this.content = data;
		this.obj.innerHTML = this.content;
	}
	
	this.Show = function ()
	{
		if (this.content)
		{
			if (this.content.search(/^\@/) != -1)
			{
				this.Load(this.content.replace(/^\@/,''));
				this.content = '';
			}
			else if (this.content.search(/^\:/) != -1)
			{
				this.obj.innerHTML = decodeURIComponent(this.content.substr(1));
				this.content = '';
			}
			else
			{
				this.obj.innerHTML = this.content; //this.obj.appendChild(document.createTextNode(this.content));
				this.content = '';
			}
		}
		this.obj.style.display = 'block';
		this.Move();
	}
	
	this.Move = function (e)
	{
		var e = e || window.event || {};
		
		var cursor_space = 10;
		
		var screen_w = window.innerWidth || document.body.offsetWidth;//document.body.offsetWidth;
		var screen_h = window.innerHeight || document.body.offsetHeight;//document.body.offsetHeight;
		
		var object_w = this.obj.offsetWidth;
		var object_h = this.obj.offsetHeight;
		
		var screen_x = e.clientX;
		var screen_y = e.clientY;
		
		var doc_x = e.pageX ? e.pageX : e.clientX + document.body.scrollLeft; 
		var doc_y = e.pageY ? e.pageY : e.clientY + document.body.scrollTop;
		//alert(screen_w+','+object_w);
		//alert('screen_y='+screen_y+'; object_h='+object_h+'; screen_h='+screen_h);
		var top = (screen_y+object_h <= screen_h) ? doc_y+cursor_space : doc_y-(object_h-(screen_h-screen_y)); //doc_y+cursor_space : doc_y-object_h-cursor_space;
		var left = (screen_x+object_w <= screen_w) ? doc_x+cursor_space : doc_x-object_w-cursor_space;
		//alert(top+','+left);
		this.obj.style.left = left+'px';
		this.obj.style.top = top+'px';
	}
	
	this.Hide = function ()
	{
		this.obj.style.display = 'none';
	}
	
	this.__construct(owner);
}