// expects js/getWindowDims.js to have been parsed

var cursorOffset_x = 10; // tooltip's pixel distance to the right (or left) of the cursor
var cursorOffset_y = 10; // tooltip's pixel distance below the cursor

var tipEl, tipTextEl;
var tipElLeft, tipTextElLeft;
var windowDims;

function tooltipInit() { // put IDs of elements in here
	if (document.getElementById) {
		tipEl = document.getElementById('tooltip');
		tipTextEl = document.getElementById('tooltip_text');
		tipElLeft = document.getElementById('tooltip_left');
		tipTextElLeft = document.getElementById('tooltip_left_text');
		// preload tooltip BG images
		var preloadBgs = new Array(
			new Image(), new Image(), new Image(), new Image(),
			new Image(), new Image(), new Image(), new Image()
		);
		preloadBgs[0].src = 'images/tooltip/bottomleft.gif';
		preloadBgs[1].src = 'images/tooltip/bottomright.gif';
		preloadBgs[2].src = 'images/tooltip/topleft.gif';
		preloadBgs[3].src = 'images/tooltip/topright.gif';
		preloadBgs[4].src = 'images/tooltip/left/bottomleft.gif';
		preloadBgs[5].src = 'images/tooltip/left/bottomright.gif';
		preloadBgs[6].src = 'images/tooltip/left/topleft.gif';
		preloadBgs[7].src = 'images/tooltip/left/topright.gif';
	}
}

function tooltipOn(html) {
	if (document.getElementById && tipEl && tipTextEl) {
		tipTextEl.innerHTML = html;
		window.document.onmousemove = tipElFollow;

		// just for IE, otherwise mousemove doesn't fire
		// until it moves AGAIN
		if (window.event) tipElFollow();
	}
}
function tipElFollow(e) {
	if (!e) e = window.event;
	tipEl.style.left = (getMouseX(e) + cursorOffset_x) + 'px';
	tipEl.style.top = (getMouseY(e) + cursorOffset_y) + 'px';
	tipEl.style.display = 'block';
}
function tooltipOff() {
	if (document.getElementById && tipEl && tipTextEl) {
		tipEl.style.display = 'none';
		window.document.onmousemove = null;
		tipTextEl.innerHTML = '';
	}
}

function tooltipOnLeft(html) {
	if (document.getElementById && tipElLeft && tipTextElLeft) {
		windowDims = getWindowDims(); // refresh window dimensions
		tipTextElLeft.innerHTML = html;
		window.document.onmousemove = tipElLeftFollow;

		// just for IE, otherwise mousemove doesn't fire
		// until it moves AGAIN
		if (window.event) tipElLeftFollow();
	}
}
function tipElLeftFollow(e) {
	if (!e) e = window.event;
	tipElLeft.style.right = (windowDims[0] - getMouseX(e) + cursorOffset_x) + 'px';
	tipElLeft.style.top = (getMouseY(e) + cursorOffset_y) + 'px';
	tipElLeft.style.display = 'block';
}
function tooltipOffLeft() {
	if (document.getElementById && tipElLeft && tipTextElLeft) {
		tipElLeft.style.display = 'none';
		window.document.onmousemove = null;
		tipTextElLeft.innerHTML = '';
	}
}

function getMouseX(e) {
	if (e.pageX) return e.pageX;
	else if (e.clientX) { // if IE
		var scrollAdd;
		if (document.documentElement && document.documentElement.scrollLeft) { // if in strict mode
			scrollAdd = document.documentElement.scrollLeft;
		} else {
			scrollAdd = document.body.scrollLeft;
		}
		return e.clientX + scrollAdd;
	}
}
function getMouseY(e) {
	if (e.pageY) return e.pageY;
	else if (e.clientY) { // if IE
		var scrollAdd;
		if (document.documentElement && document.documentElement.scrollTop) { // if in strict mode
			scrollAdd = document.documentElement.scrollTop;
		} else {
			scrollAdd = document.body.scrollTop;
		}
		return e.clientY + scrollAdd;
	}
}