
function Tooltips(data, opts) {
	this._data = data;
	
	var def_opts = { create_tt : Util.create_tooltip };
	this._opts = Util.get_opts(def_opts, opts);

	this._curtt = null;
	this._init_handlers();
}


Tooltips.prototype = {

	_find_child_obj_by_id : function (obj, cid) {
		while (obj && obj.nodeName != 'BODY' && obj.id != cid) {
			obj = obj.parentNode;
		}
		return obj && obj.id == cid ? obj : null;
	},

	_init_handlers : function () {
		var self = this;
	
		var out_handler = function (evt) {
			if (self._curtt) {
				// IE: evt.toElement
				var ro = evt.relatedTarget || evt.toElement;
				if (!self._find_child_obj_by_id(ro, self._curtt.id)) {
					self.remove_tt();
				}
			}
		}	
		
		var over_handler = function (evt) {
			if (self._curtt) return;	

			var o = HTML_AJAX_Util.eventTarget(evt);
			while (!self._data[o.id] && o.nodeName != 'BODY')
				o = o.parentNode;
			
			var id = o.id;
			if (self._data[id]) {
				var text = self._data[id];
				var pos = Util.get_mouse_pos(evt);
				pos = [ pos[0], pos[1] ];
				self._curtt = self._opts.create_tt(pos, text);
				self._curtt.id = "tt_" + id;
				document.body.appendChild(self._curtt);
			}
		}

		HTML_AJAX_Util.registerEvent(document.body, "mouseover", over_handler);
		HTML_AJAX_Util.registerEvent(document.body, "mouseout", out_handler);
	},

	remove_tt : function () {
		if (this._curtt) {
			document.body.removeChild(this._curtt);
			this._curtt = null;
		}
	},

	set_data : function (data) { this._data = data; },
	get_data : function () { return this._data; }
}
