/* Copyright 2009 - tumimusic.com */

function declareHelpTips() {
	var img = document.getElementsByTagName('img');
	for (var i = img.length - 1; i >= 0; i--)
	if (img[i].className == 'helptip')
	var x = new HelpTip(img[i]);
}

function HelpTip(img) {

	//variables
	this.img = img;
	this.tip = img.title || 'No tip';
	img.title = '';
	this.offsetX = 0;
	this.offsetY = 0;

	//function to show helptip
	this.show = function (e) {

		//get mouse coordinates
		var mouse = this.getMouse(e);

		//displaying text
		this.div = document.createElement('div');
		document.body.appendChild(this.div);

		//if y is touching the top or bottom
		if(mouse[1]>10 && mouse[1]<450)
			this.div.style.top = (mouse[1] - 4) + "px";

		//if not
		else this.div.style.top = (mouse[1] + 4) + "px";

		//find approx message height to adjust top of the tip
		var messageHeigth = (this.tip.length / 20) * 8 + 10;

		//message width to adjust the left of the tip
		var chunks = this.tip.split('||');
		var mLength = 0;
		for (var i = 0; i < chunks.length; i++) {
			var len = chunks[i].length;
			if (len >= mLength)
				mLength = len;
		}
		var messageWidth = mLength > 36 ? 180 : mLength*5;

		if (mouse[1] + messageHeigth > screen.availHeight - 200) this.div.style.top = mouse[1] - messageHeigth + "px";
		if (mouse[0] < screen.availWidth - 240) this.div.style.left = mouse[0] + 20 + "px";
		else {
			this.div.style.left = mouse[0] - messageWidth + "px";
			this.div.style.top = (mouse[1] + 20) + "px";
		}
		this.div.id = 'helptip_div';
		this.div.innerHTML = this.tip;
		this.div.style.display = 'block';
		this.div.style.maxWidth = '180px';
		this.offsetX = parseInt(this.div.style.left) - mouse[0];
		this.offsetY = parseInt(this.div.style.top) - mouse[1];
	};

	//function to hide the helptip
	this.hide = function () {
		if (this.div) document.body.removeChild(this.div);
	};

	//function to slide the helptip when mouse is moved
	this.slide = function (e) {

		//get mouse coordinates
		var mouse = this.getMouse(e);

		//slide div
		if (this.div) {
			this.div.style.left = (mouse[0] + this.offsetX) + 'px';
			this.div.style.top = (mouse[1] + this.offsetY) + 'px';
		}
	};

	//function to calculcate the coordinates of mouse
	this.getMouse = function (e) {
		e = e || window.event;
		var mouse = new Array(2);
		if(e.pageX||e.pageY) {
			mouse[0] = e.pageX;
			mouse[1] = e.pageY;
		}
		else if(e.clientX||e.clientY) {
			mouse[0] = e.clientX + document.body.scrollLeft+document.documentElement.scrollLeft;
			mouse[1] = e.clientY + document.body.scrollTop+document.documentElement.scrollTop;
		}
		return mouse;
	};

	//event functions
	var helpTip = this;
	this.img.onmouseover = function (event) {
		helpTip.show(event);
	};
	this.img.onmouseout = function () {
		helpTip.hide();
	};
	this.img.onmousemove = function (event) {
		helpTip.slide(event);
	};
}