/*
 * 
 * Dependencies: _element.inc.js (HTMLElement.getLeft,HTMLElement.getTop)
 */

function ToolTip() {
	this.debug = false;
	
	this.msie = (navigator.appName == 'Microsoft Internet Explorer') ? true : false;
}

ToolTip.prototype.constructor = ToolTip;

ToolTip.prototype = {
	triggers: new Array(),

	init: function() {
		var ToolTip = this;

		this.time = (typeof(arguments[0]['time'])=='number') ? arguments[0]['time'] : 0.5;
		this.timeout = false;
		
		this.event_in = (arguments[0]['event_in']) ? arguments[0]['event_in']	: 'mouseover';
		this.event_out = (arguments[0]['event_out']) ? arguments[0]['event_out']	: 'mouseout';
		
		this.HTML = new Object();
		this.HTML.source = (typeof(arguments[0]['source'])=='object') ? arguments[0]['source'] : document.getElementById(arguments[0]['source']);
		
		switch(typeof(arguments[0]['target'])) {
			case 'string':
			this.HTML.target = document.getElementById(arguments[0]['target']);
			break;
			
			case 'object':
			this.HTML.target = arguments[0]['target'];
			break;
			
			default:
			this.HTML.target = document.createElement('span');
			if(this.HTML.source.title!='') { this.HTML.target.innerHTML = this.HTML.source.title; }
			else if(this.HTML.source.alt!='') { this.HTML.target.innerHTML = this.HTML.source.alt; }
			document.documentElement.appendChild(this.HTML.target);
			break;
		}
		
		this.addTrigger(this.HTML.source,this.HTML.target);
		
		this.margin = (arguments[0]['margin']) ? arguments[0]['margin'] : 10;
		this.position = (arguments[0]['position']) ? arguments[0]['position'] : 'absolute';
		
		addClassName(this.HTML.target,'tooltip');
		this.HTML.target.style.zIndex = 99;
		this.HTML.target.style.position = this.position;
		
		if(undefined == arguments[0]['align']) {
			this.setTopPos = function() {
				ToolTip.HTML.target.style.top = getTop(ToolTip.HTML.source)+'px';
				return true;
			};
			this.setLeftPos = function() {
				ToolTip.HTML.target.style.left = ((getLeft(ToolTip.HTML.source)+ToolTip.HTML.source.offsetWidth))+'px';
				return true;
			};
		}
		else {
			if(this.position=='absolute') {
				if(arguments[0]['align'].search(/\btop\b/)>-1) {
					this.setTopPos = function() {
						ToolTip.HTML.target.style.top = ((getTop(ToolTip.HTML.source)-ToolTip.HTML.target.offsetHeight-ToolTip.margin))+'px';
						return true;
					};
				}
				if(arguments[0]['align'].search(/\bbottom\b/)>-1) {
					this.setTopPos = function() {
						ToolTip.HTML.target.style.top = ((getTop(ToolTip.HTML.source)+ToolTip.HTML.source.offsetHeight+ToolTip.margin))+'px';
						return true;
					};
				}
				if(arguments[0]['align'].search(/\bmiddle\b/)>-1) {
					this.setTopPos = function() {
						ToolTip.HTML.target.style.top = ((getTop(ToolTip.HTML.source)+(ToolTip.HTML.source.offsetHeight/2)-(ToolTip.HTML.target.offsetHeight/2)))+'px';
						return true;
					};
				}
				if(arguments[0]['align'].search(/\bleft\b/)>-1) {
					this.setLeftPos = function() {
						ToolTip.HTML.target.style.left = ((getLeft(ToolTip.HTML.source)-ToolTip.HTML.target.offsetWidth-ToolTip.margin))+'px';
						return true;
					};
					
				}
				if(arguments[0]['align'].search(/\bright\b/)>-1) {
					this.setLeftPos = function() {
						ToolTip.HTML.target.style.left = ((getLeft(ToolTip.HTML.source)+ToolTip.HTML.source.offsetWidth+ToolTip.margin))+'px';
						return true;
					};
				}
				if(arguments[0]['align'].search(/\bcenter\b/)>-1) {
					this.setLeftPos = function() {
						ToolTip.HTML.target.style.left = ((getLeft(ToolTip.HTML.source)+(ToolTip.HTML.source.offsetWidth/2)-(ToolTip.HTML.target.offsetWidth/2)))+'px';
						return true;
					};
				}
			}
			else {
				if(arguments[0]['align'].search(/\btop\b/)>-1) {
					this.setTopPos = function() {
						ToolTip.HTML.target.style.top = ((-ToolTip.HTML.target.offsetHeight-ToolTip.margin))+'px';
						return true;
					};
				}
				if(arguments[0]['align'].search(/\bbottom\b/)>-1) {
					this.setTopPos = function() {
						ToolTip.HTML.target.style.top = ((ToolTip.HTML.source.offsetHeight+ToolTip.margin))+'px';
						return true;
					};
				}
				if(arguments[0]['align'].search(/\bmiddle\b/)>-1) {
					this.setTopPos = function() {
						ToolTip.HTML.target.style.top = (((ToolTip.HTML.source.offsetHeight/2)-(ToolTip.HTML.target.offsetHeight/2)))+'px';
						return true;
					};
				}
				if(arguments[0]['align'].search(/\bleft\b/)>-1) {
					this.setLeftPos = function() {
						ToolTip.HTML.target.style.left = ((-ToolTip.HTML.target.offsetWidth-ToolTip.margin))+'px';
						return true;
					};
					
				}
				if(arguments[0]['align'].search(/\bright\b/)>-1) {
					this.setLeftPos = function() {
						ToolTip.HTML.target.style.left = ((ToolTip.HTML.source.offsetWidth+ToolTip.margin))+'px';
						return true;
					};
				}
				if(arguments[0]['align'].search(/\bcenter\b/)>-1) {
					this.setLeftPos = function() {
						ToolTip.HTML.target.style.left = ((+(ToolTip.HTML.source.offsetWidth/2)-(ToolTip.HTML.target.offsetWidth/2)))+'px';
						return true;
					};
				}
			}
		}
		
		this.setLeftPos();
		this.setTopPos();
		this.hide(0);
		
		addEvent(window,'resize',function() {
			ToolTip.setLeftPos();
			ToolTip.setTopPos();
		});
		
		if(this.event_in == 'click' || this.event_out == 'click') {
			this.time = 0;
			addEvent(document,'click',function(e) { ToolTip.isClickedOut(e); });
		}
	},
	
	show: function() {
		if(this.timeout) { window.clearTimeout(this.timeout); this.timeout = false; }
		if(this.HTML.target.style.visibility=='hidden'&&this.HTML.target.innerHTML!='') {
			this.setTopPos();
			this.setLeftPos();
			this.HTML.target.style.visibility = 'visible';
		}
	},
	
	hide: function(time) {
		var ToolTip = this;
		if(!this.timeout && this.HTML.target.style.visibility != 'hidden') {
			time = (typeof(time)=='number') ? time : this.time;
			this.timeout = window.setTimeout(
				function() {
					ToolTip.HTML.target.style.visibility = 'hidden';
					ToolTip.timeout = false;
				},
			time*1000);
		}
	},
	
	isClickedOut: function(e) {
		if(!e)	{ e = window.event; elt = e.srcElement; }
		else { elt = e.target; }
		bool = false;
		while(false==bool&&undefined!=elt) {
			for(trig in this.triggers) {
				if(elt==trig) {
					return false;
				}
			}
			//if(elt == this.HTML.source || elt == this.HTML.target) { return false; }
			if(elt.tagName.toLowerCase()=='html') {
				this.hide();
				return true;
			}
			elt = elt.parentNode;
		}
		return true;
	},
	
	addTrigger: function() {
		var ToolTip = this;
		for(i=0;i<arguments.length;i++) {
			//window.alert(arguments[i]+': '+typeof(arguments[i]));
			elt = (typeof(arguments[i])=='object') ? arguments[i] : document.getElementById(arguments[i]);
			if(this.event_in=='click'||this.event_out=='click') {
				addEvent(elt,'click',function() { ToolTip.show(); });
			}
			else {
				addEvent(elt,this.event_in,function() { ToolTip.show(); });
				addEvent(elt,this.event_out,function() { ToolTip.hide(); });
			}
			this.triggers.push(elt);
			//window.alert(elt.tagName+'#'+elt.id+'.'+elt.className);
		}
	},
	
	setDimension: function(width,height) {
		if(width) {
			this.HTML.target.style.width = width+'px';
		}
		if(height) {
			this.HTML.target.style.height = height+'px';
		}
	}
};