var Tooltips = {
	tooltips : [],
	tooltipsCount : 0,
	activeTooltip : false,
	tooltipTimer : false,
	URL : URLRoot+'/?module=tooltips&section=default&page=ajax_get_tooltip',
	run : function(e) {
		if (this.tooltipTimer) {
			window.clearTimeout(this.tooltipTimer);
		}
		var trg =  Event.element(e);

		if (trg.getAttribute('tooltip')) {
			if (trg.getAttribute('tooltip_id')) {
				id = trg.getAttribute('tooltip_id');
				var tt = this.tooltips[id];
			} else {//new tooltip
				var tt = new Tooltip(trg);
			}
			if (!tt) {
				return false;
			}
			tt.e = e;
			tt.canceled = false;
			if (Tooltips.lastShowedTooltip) {
				Tooltips.lastShowedTooltip.hide();
			}
			this.tooltipTimer = window.setTimeout(function(){tt.show()}, tt.timer);
		}
		
	},
	cancelTooltip : function(node) {
		var tt = Tooltips.tooltips[node.getAttribute('tooltip_id')];
		if (tt.autoHide) {
			tt.hide();
		}
		tt.canceled = true;
	}
}

var Tooltip = function(node) {
	this.id = Tooltips.tooltipsCount++;
	Tooltips.tooltips[this.id] = this;
	this.loaded = false;
	var data = node.getAttribute('tooltip');
	try {
		tooltipData = eval('[{'+data+'}]');
	} catch(er) {
		tooltipData = data;
	}
	
	if (typeof tooltipData[0] != 'object') {
		tooltipData = data;
	} else {
		tooltipData = tooltipData[0];
	}
	this.timer = tooltipData.timer ? tooltipData.timer : 300;
	this.autoHide = tooltipData.autoHide != undefined ? tooltipData.autoHide : true;
	this.follow = tooltipData.follow != undefined ? tooltipData.follow : true;
	this.more = tooltipData.more != undefined ? tooltipData.more : false;
	this.width = tooltipData.width != undefined ? tooltipData.width : false;
	
	
	this.data = tooltipData;
	if (tooltipData.id) {//server side
		this.dataid = tooltipData.id;
		this.type = 'ajax';
	} else {
		this.type = 'local';
		this.contents = this.data.text;
	}
	node.setAttribute('tooltip_id', this.id);
	
	Event.observe(node, 'mouseout', function() {Tooltips.cancelTooltip(node);}, true);
	Event.observe(node, 'click', function() {Tooltips.cancelTooltip(node);}, true);
}

Tooltip.prototype.getDataFromServer = function() {
	var pars = 'tooltipId='+this.id;
	pars += '&tooltipDataId='+encodeURIComponent(this.dataid);
	var myAjax = new Ajax.Request(
		Tooltips.URL, 
		{
			method: 'get', 
			parameters: pars,
			tooltipId: this.id,
			onComplete : function(originalRequest, json, ajaxRequest){
				tt = Tooltips.tooltips[ajaxRequest.options.tooltipId];
				tt.loaded = true;
				tt.contents = originalRequest.responseText;
				
				tt.show();
			}
		}
	);
}
Tooltip.prototype.show = function() {
	var e = this.e;
	if (this.canceled) {
		return false;
	}
	if (!this.div) {
		var container = Builder.node('DIV', {className: 'tooltip'});
		this.div = container;
		$('theBODY').appendChild(this.div);
	}
	
	if (this.type == 'ajax' && !this.loaded) {
		this.getDataFromServer();
	} else {
		if (this.contents.length == 0) {//empty tooltip
			return false;
		}
		
		if (!this.contentDIV) {
			var content = Builder.node('DIV', {className: 'content'});
			this.contentDIV = content;
			this.contentDIV.innerHTML = this.contents;
			if (this.width) {
				this.contentDIV.style.width = this.width+'px';
			}
			this.div.appendChild(content);
			var moreInfoBar = Builder.node('DIV', {className: 'moreInfoBar', style:'display:none'});
			if (this.width) {
				moreInfoBar.style.width = this.width+'px';
			}
			
			moreInfoBar.innerHTML = 'Press F1 for more info.';
			this.moreInfoBar = moreInfoBar;
			this.div.appendChild(moreInfoBar);
			this.div.setAttribute('tooltip_id', this.id);
			Event.observe(this.div, 'mouseout', function(){
				var tt = Tooltips.tooltips[this.getAttribute('tooltip_id')];
				if (tt.autoHide) {
					tt.hide();
				}
			}, true);
		}
		
		var y = currentMousePosY+10;
		var x = currentMousePosX+10;
		this.div.style.top = y+'px';
		this.div.style.left = x+'px';
		
		if (this.more) {
			this.moreInfoBar.show();
		}
		this.div.hide();
		this.div.toTop();
		Effect.Appear(this.div, {duration: 0.5});
		Tooltips.lastShowedTooltip = this;
	}
	Tooltips.activeTooltip = this;
}
Tooltip.prototype.hide = function() {
	if (this.div) {
		//Effect.Fade(this.div, {duration: 1.0});
		this.div.hide();
	}
	Tooltips.activeTooltip = false;
}

Tooltip.prototype.showMore = function() {
	openHelp(this.more);
}

var lastHoveredElement;
Event.observe(document, 'mousemove', function(e) {
	if (Tooltips.activeTooltip) {
		tt = Tooltips.activeTooltip;
		if (tt.follow) {
			tt.div.toMousePos();
		}
	}
	var trg = Event.element(e);
	if (trg == lastHoveredElement) {//the mouse is not moving
		return false;
	}
	lastHoveredElement = trg;
	Tooltips.run(e);
}, true);




/* HELP */
function openHelp(item) {
	var url = URLRoot+'?module=surveys&section=help';
	if (item) {
		url += '&item='+encodeURIComponent(item);
	}
	popup(url, 'helpWindow', 500, 500, 'yes');
}


Event.observe(window, 'load', function (e) {
	Event.observe(window, 'keydown', function (e) {
		var key = e.keyCode;
		if (key == 112) {//F1
			if (Tooltips.activeTooltip.more) {
				Tooltips.activeTooltip.showMore();
			} else {
				openHelp();
			}
			Event.stop(e);
			return false;
		}
	});
});