/*******************************************************************************

FILE: mud_ToolTip.js
REQUIRES: prototype.js
AUTHOR: Takashi Okamoto mud(tm) - http://www.mudcorp.com/
VERSION: 1.1 - added support for WIN IE6
DATE: 04/08/2006

--------------------------------------------------------------------------------

This file is part of MudToolTip.

	MudToolTip is free for anyone to use, but this header MUST be
	included, and may not be modified.
	
--------------------------------------------------------------------------------

Usage:

MudToolTip('instance_var', 'tooltip_id')

*******************************************************************************/

var MudToolTip = Class.create();

MudToolTip.OFFSET = new Array(-12, 20);

MudToolTip.prototype = {
	
	/* ------------- CALLBACKS ------------- */
	/* ------------- EDITABLE -------------- */
	
	// runs right before tooltip is displayed
	onStart: function() {
	},
	
	// runs right after tooltip disappears
	onEnd: function() {
	},
	
	/* ------------- DON'T EDIT PAST HERE ------------- */
	
	initialize: function(thisObj, id) {
		this.id = id;
		this.thisObj = thisObj;
		this.pos = new Array(0, 0);
		this.hidden = true;
		this.target = false;
	},
	
	// call onmouseover
	showTip: function(evt) {
		this.onStart();
		evt = (evt) ? evt : event;
		this.setPos(evt);
		this.target = this.getTarget(evt);
		this.target.setAttribute('tiptext', this.target.getAttribute('title'));
		this.target.removeAttribute('title');
		this.hidden = false;
		$(this.id+"-content").innerHTML = this.target.getAttribute('tiptext');
		Element.show(this.id);
	},
	
	// call onmouseout
	hideTip: function() {
		if (this.target) {
			this.target.setAttribute('title', this.target.getAttribute('tiptext'));
			this.target.removeAttribute('tiptext');
			this.hidden = true;
			$(this.id+"-content").innerHTML = "";
			this.target = false;
			Element.hide(this.id);
			this.onEnd();
		}
	},
	
	setPos: function(evt) {
		this.pos = cursorPos(evt);
		$(this.id).style.left = this.pos[0] + MudToolTip.OFFSET[0] + "px";
		$(this.id).style.top = this.pos[1] + MudToolTip.OFFSET[1] + "px";
	},
	
	getTarget: function(evt) {
		var target = (evt.target) ? evt.target : evt.srcElement;
		if (target.className != "tooltip") target = target.parentNode;
		return target;
	}
}

/* ------------- PUBLIC FUNCTIONS ------------- */

function cursorPos(evt) {
	var pos = new Array(0, 0);
	evt = (evt) ? evt : event;
	if (!evt) return false;
	
	if (evt.pageX) {
		pos[0] = evt.pageX;
		pos[1] = evt.pageY;
	}
	else if (evt.clientX) {
		if (document.documentElement.scrollTop){
			pos[0] = evt.clientX + document.documentElement.scrollLeft;
			pos[1] = evt.clientY + document.documentElement.scrollTop;
		}
		else{
			pos[0] = evt.clientX + document.body.scrollLeft;
			pos[1] = evt.clientY + document.body.scrollTop;
		}
	}
	return pos;
}
