(function(window){

if(window.addEventListener) window.addEventListener('load',init,false);
else if(window.attachEvent) window.attachEvent('onload',init);
function init(){
	var ary = document.getElementsByTagName('a');
	var i,l = ary.length;
	for(i=0;i<l;i++){
		var classes = ary[i].getAttribute('class') || ary[i].getAttribute('className');
		if(classes)
		{
			var classAry = classes.split(/\s+/);
			var i2,l2 = classAry.length;
			for(i2 = 0; i2 < l2;i2++)
			{
				if(classAry[i2] == "tooltip") {
					new ToolTip(ary[i]);
					break;
				}
			}
		}
	}
}

function ToolTip()
{
	this.initialize.apply(this,arguments);
}
var p = ToolTip.prototype;
	p.element = null;
	p.tipElement = null;
	p.initialize = function(element)
	{
		this.element = element;
		this.tipElement = document.createElement('div');
		this.tipElement.innerHTML = this.element.getAttribute('title').replace('||','<br />');
		this.tipElement.setAttribute('class','tooltipballoon');
		this.tipElement.setAttribute('className','tooltipballoon');
		this.tipElement.style.position = "absolute";
		this.tipElement.style.display = 'none';
		document.body.appendChild(this.tipElement);
		var that = this;
		if(this.element.addEventListener) {
			if(typeof this.element.ontouchend !== 'undefined'){
				this.element.addEventListener('touchend',function(e){that.onMouse.call(that,e);},false);
				this.element.addEventListener('touchstart',function(e){that.onMouse.call(that,e);},false);
			}
			else {
				this.element.addEventListener('mouseover',function(e){that.onMouse.call(that,e);},false);
				this.element.addEventListener('mouseout',function(e){that.onMouse.call(that,e);},false);
			}
			this.element.addEventListener('mousemove',function(e){that.onMouse.call(that,e);},false);
		}
		else if(this.element.attachEvent) {
			this.element.attachEvent('onmouseover',function(e){that.onMouse.call(that,e);});
			this.element.attachEvent('onmouseout',function(e){that.onMouse.call(that,e);});
			this.element.attachEvent('onmousemove',function(e){that.onMouse.call(that,e);});
		}
	}
	p.onMouse = function(e){
		if(e.type == 'mouseover' || e.type == 'touchstart' )
		{
			this.tipElement.style.display = 'block';
		}
		else if(e.type == 'mouseout' || e.type == 'touchend')
		{
			this.tipElement.style.display = 'none';
		}
		if(e.type == 'mouseover' || e.type == 'mouseout' || e.type == 'mousemove' || e.type == 'touchstart' || e.type == 'touchend')
		{
			this.tipElement.style.left = getPointer(e).x - this.tipElement.offsetWidth/2  + "px";
			this.tipElement.style.top = getPointer(e).y - this.tipElement.offsetHeight - 20 + "px";
		}
	}
	function getPointer(e){
			var pointer = {x:0,y:0};
			if(e && e.touches && e.touches[0]){
				pointer.x = e.touches[0].pageX;
				pointer.y = e.touches[0].pageY;
			}
			else if(e && e.pageX ){
				pointer.x = e.pageX;
				pointer.y = e.pageY;
			}
			else if(typeof event != 'undefined' && event.clientX){
				pointer.x = event.clientX;
				pointer.y = event.clientY;
			}
			return pointer;
		}
window.ToolTip = ToolTip;
}(window));

