/* Needs the following styles defined:
#tooltip {
	padding: 2px;
	background-color: #FFFFDB;
	z-index: 6;
}
#tooltip_shadow {
	padding: 2px;
	background-color: #ECECEC;
	z-index: 5;
}
*/

/* Position of the tooltip relative to the mouse in pixels */
var tts_offsetx = 0;
var tts_offsety = 21;

/* Defauly width of tooltip */
var tts_default_width = 'auto';

/* Width of tooltip shadow (0 for disable) */
var tts_shadow_width = 1;

/* Sets approx maximum tooltip length in characters before truncation occurs (at nearest space)
   0 for no maximum */
var tts_maxtiplen = 0;
/* Truncation string if longer */
var tts_truncstr = '&nbsp; <i>[more..]</i>';

/* If the tooltip needs to appear in the correct place when called from outside an onmouseover event then this needs to be true */
var tts_alwaysReadMousePos = false;



/* END CONFIG */



var tts_tooltip = false;
var tts_tooltip_shadow = false;


function tooltip(tip, width) {
	width = (width == null) ? tts_default_width : width;
	if (!document.getElementById('tooltip')) {
		tts_newElement('tooltip');
		if (tts_shadow_width>0) {
			tts_newElement('tooltip_shadow');
		}
	}
	tts_tooltip = document.getElementById('tooltip');
	if (tts_shadow_width>0) {
		tts_tooltip_shadow = document.getElementById('tooltip_shadow');
	}
	
	if (tts_maxtiplen > 0 && tip.length > tts_maxtiplen+20) {
		for (i = tts_maxtiplen; tip.charAt(i) != ' ' && i < tip.length; i++) {}
		tts_tooltip.innerHTML = tip.substr(0,i)+tts_truncstr;
		if (tts_shadow_width>0) {
			tts_tooltip_shadow.innerHTML = tip.substr(0,i)+tts_truncstr;
		}
	} else {
		tts_tooltip.innerHTML = tip;
		if (tts_shadow_width>0) {
			tts_tooltip_shadow.innerHTML = tip;
		}
	}
	
	if (tip != '') {
		tts_tooltip.style.display = 'block';
		if (tts_shadow_width>0) {
			tts_tooltip_shadow.style.display = 'block';
		}
	}
	if (width == 'auto') {
		tts_tooltip.style.width = 'auto';
		if (tts_shadow_width>0) {
			tts_tooltip_shadow.style.width = 'auto';
		}
	} else {
		tts_tooltip.style.width = width+'px';
		if (tts_shadow_width>0) {
			tts_tooltip_shadow.style.width = width+'px';
		}
	}
	document.onmousemove = tts_positionTooltip;
}

function tooltip_hide() {
	if (!tts_tooltip) {
		return;
	}
	tts_tooltip.style.display = 'none';
	if (tts_shadow_width>0) {
		tts_tooltip_shadow.style.display = 'none';
	}
	
	if (!tts_alwaysReadMousePos) {
		document.onmousemove = null;
	}
}



function tts_newElement(newid) {
	if (document.createElement) {
		var el = document.createElement('div');
		el.id = newid;
		with(el.style) {
			display = 'none';
			position = 'absolute';
		}
		el.innerHTML = '&nbsp;';
		document.body.appendChild(el);
	}
}

function tts_positionTooltip(e) {
	if(!tts_tooltip) {
		return;
	}
	
	var posx = 0;
	var posy = 0;
	if (!e) {
		e = window.event;
	}
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
		
	} else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft
		       + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
		       + document.documentElement.scrollTop;
	}
	
	tts_tooltip.style.left = (posx+tts_offsetx) + 'px';
	tts_tooltip.style.top = (posy+tts_offsety) + 'px';
	if (tts_shadow_width>0) {
		tts_tooltip_shadow.style.left = (posx+tts_offsetx+tts_shadow_width) + 'px';
		tts_tooltip_shadow.style.top = (posy+tts_offsety+tts_shadow_width) + 'px';
	}
}

if (tts_alwaysReadMousePos) {
	tooltip('&nbsp;');
	tooltip_hide();
}
