Tooltip = Class.create();
Tooltip.prototype = {

	initialize: function(target, msg, small, medium, xrel, yrel, xabs, yabs) {

		this.target = target;
		this.msg = msg;
		this.small = small;
		this.medium = medium;
		this.xpos = 0;  
		this.ypos = 0;  

		this.xabs = xabs;
		this.yabs = yabs;

		this.xrel = xrel;
		if(this.xrel==null)
			this.xrel = "right";
		this.yrel = yrel;
		if(this.yrel==null)
			this.yrel = "left";


		this.removeOlderIfExist();
		this.createTip();

		this.position();

		Event.observe($(target), "focus", this.show.bind(this));		
		Event.observe($(target), "blur", this.hide.bind(this));	
	}, 

	hide: function hide(event) {  
		//this.tip.style.display="none";
		Effect.Fade(this.tip, { duration: 1.0, from: 0.85, to: 0});
	},

	show: function show(event) {
		this.position();
		Effect.Appear(this.tip, { duration: 1.0, from: 0, to: 0.85});
		//setTimeout(this.hide.bind(this), 5000);
	},  

	position: function position() {

		var tmp = this.target;

		// Get the offsets for the current object
		var left = tmp.offsetLeft;
		var top = tmp.offsetTop;

		// If the current object has a parent (ie contained in a table, div, etc..)
		if (tmp.offsetParent) {

			// Loop through all the parents and add up their offsets
			// The while loop will end when no more parents exist and a null is returned
			while (tmp = tmp.offsetParent) {
				left += tmp.offsetLeft;
				top += tmp.offsetTop;
			}
		}
		
		this.xpos = left;  
		this.ypos = top;  
		
		if(this.xrel=="left")
			this.xpos -= this.tip.offsetWidth;  
		else if(this.xrel=="right")
			this.xpos += this.target.offsetWidth;  

		if(this.yrel=="top")
			this.ypos -= this.tip.offsetHeight;
		else if (this.yrel=="bottom") {
			this.ypos += this.target.offsetHeight;
			this.ypos += 4;
		}
		if(this.xabs!=null)
			this.xpos = this.xabs;

		if(this.yabs!=null)
			this.ypos = this.yabs;

		this.tip.style.left = this.xpos + "px";  
		this.tip.style.top = this.ypos + "px";  
	},
	
	removeOlderIfExist : function() {
		if(!this.small) {
			if(!this.medium) {
				tmp = document.getElementById(this.target.id + "tip");
			} else {
				tmp = document.getElementById(this.target.id + "mediumTip");
			}
		} else {
			tmp = document.getElementById(this.target.id + "smallTip");
		}
		if (tmp != null) {
			document.getElementById(tmp.id).remove()
		}
	},
	
	createTip : function() {
		var innerLinks = this.target.getElementsByTagName("a");
		for (i=0;i<innerLinks.length;i++) {
			$(innerLinks[i]).removeAttribute("title");
		}
		innerLinks = this.target.getElementsByTagName("span");
		for (i=0;i<innerLinks.length;i++) {
			$(innerLinks[i]).removeAttribute("title");
		}
		this.target.removeAttribute("title");
		this.target.removeAttribute("alt");
		this.tip = document.createElement("div");  
		document.body.appendChild(this.tip);  
		if(!this.small) {
			if(!this.medium) {
				this.tip.setAttribute("id", this.target.id + "tip");  
				this.tip.className = "tip";
			} else {
				this.tip.setAttribute("id", this.target.id + "mediumTip");
				this.tip.className = "mediumTip";
			}
		} else {
			this.tip.setAttribute("id", this.target.id + "smallTip");
			this.tip.className = "smallTip";
		}


		var tiptop = document.createElement("div");  
		this.tip.appendChild(tiptop);  
		if(!this.small) {
			if(!this.medium) {
				tiptop.setAttribute("id", this.target.id + "tiptop");  
				tiptop.className = "tiptop";
			} else {
				tiptop.setAttribute("id", this.target.id + "mediumTiptop");
				tiptop.className = "mediumTiptop";
			}
		} else {
			tiptop.setAttribute("id", this.target.id + "smallTiptop");
			tiptop.className = "smallTiptop";
		}

		var tipcont = document.createElement("div");  
		this.tip.appendChild(tipcont);  
		if(!this.small) {
			if(!this.medium) {
				tipcont.setAttribute("id", this.target.id + "tipcont");  
				tipcont.className = "tipcont";
			} else {
				tipcont.setAttribute("id", this.target.id + "mediumTipcont");
				tipcont.className = "mediumTipcont";
			}
		} else {
			tipcont.setAttribute("id", this.target.id + "smallTipcont");
			tipcont.className = "smallTipcont";
		}
		tipcont.innerHTML = this.msg;  

		var tipbot = document.createElement("div");  
		this.tip.appendChild(tipbot);  
		if(!this.small) {
			if(!this.medium) {
				tipbot.setAttribute("id", this.target.id  + "tipbot");  
				tipbot.className = "tipbot";
			} else {
				tipbot.setAttribute("id", this.target.id + "mediumTipbot");
				tipbot.className = "mediumTipbot";
			}

		} else {
			tipbot.setAttribute("id", this.target.id  + "smallTipbot");  
			tipbot.className = "smallTipbot";
		}
			
		this.tip.style.display="none";
		this.tip.style.zIndex = "410065408";  
		this.tip.style.position = "absolute";  
	}
};

TooltipHover = Class.create();
TooltipHover.prototype = {
	initialize: function(target, msg, small, medium, xrel, yrel) {
		
		this.xrel = xrel;
		if(this.xrel==null)
			this.xrel = 20;
			
		this.yrel = yrel;
		if(this.yrel==null)
			this.yrel = 20;
		
		this.target = target;
		this.msg = msg;
		this.small = small;
		this.medium = medium;
		
		this.removeOlderIfExist();
		this.createTip();

		Event.observe($(target), "mouseover", this.show.bind(this));		
		Event.observe($(target), "mouseout", this.hide.bind(this));
		Event.observe($(target), "mousedown", this.hide.bind(this));
		Event.observe($(target), "mousemove", this.move.bind(this));	
	},

	move: function(e) {
		this.tip.style.left = this.xrel + Event.pointerX(e) +"px";
		this.tip.style.top = this.yrel + Event.pointerY(e) +"px";
	},
	
	removeOlderIfExist : function() {
		if(!this.small) {
			if(!this.medium) {
				tmp = document.getElementById(this.target.id + "tip");
			} else {
				tmp = document.getElementById(this.target.id + "mediumTip");
			}
		} else {
			tmp = document.getElementById(this.target.id + "smallTip");
		}
		if (tmp != null) {
			document.getElementById(tmp.id).remove()
		}
	},
	
	createTip : function() {

		var innerLinks = this.target.getElementsByTagName("a");
		for (i=0;i<innerLinks.length;i++) {
			$(innerLinks[i]).removeAttribute("title");
		}
		innerLinks = this.target.getElementsByTagName("span");
		for (i=0;i<innerLinks.length;i++) {
			$(innerLinks[i]).removeAttribute("title");
		}
		this.target.removeAttribute("title");
		this.target.removeAttribute("alt");
		this.tip = document.createElement("div");  
		document.body.appendChild(this.tip);
		if(!this.small) {
			if (!this.medium){
				this.tip.setAttribute("id", this.target.id + "tip");  
				this.tip.className = "tip";
			} else {
				this.tip.setAttribute("id", this.target.id + "mediumTip");
				this.tip.className = "mediumTip";
			}
		} else {
			this.tip.setAttribute("id", this.target.id + "smallTip");
			this.tip.className = "smallTip";
		}
		
		var tiptop = document.createElement("div");  
		this.tip.appendChild(tiptop);
		if(!this.small) {
			if (!this.medium){
				tiptop.setAttribute("id", this.target.id + "tiptop");  
				tiptop.className = "tiptop";
			}else{
				tiptop.setAttribute("id", this.target.id + "mediumTiptop");
				tiptop.className = "mediumTiptop";
			}
		} else {
			tiptop.setAttribute("id", this.target.id + "smallTiptop");
			tiptop.className = "smallTiptop";
		}
	
		var tipcont = document.createElement("div");  
		this.tip.appendChild(tipcont);
		if(!this.small) {
			if (!this.medium){
				tipcont.setAttribute("id", this.target.id + "tipcont");  
				tipcont.className = "tipcont";
			} else {
				tipcont.setAttribute("id", this.target.id + "mediumTipcont");
				tipcont.className = "mediumTipcont";
			}
		} else {
			tipcont.setAttribute("id", this.target.id + "smallTipcont");
			tipcont.className = "smallTipcont";
		}
		tipcont.innerHTML = this.msg;  
	
		var tipbot = document.createElement("div");  
		this.tip.appendChild(tipbot);
		if(!this.small) {
			if (!this.medium){
				tipbot.setAttribute("id", this.target.id  + "tipbot");  
				tipbot.className = "tipbot";
			} else {
				tipbot.setAttribute("id", this.target.id  + "mediumTipbot");  
				tipbot.className = "mediumTipbot";
				
			}
		} else {
			tipbot.setAttribute("id", this.target.id  + "smallTipbot");  
			tipbot.className = "smallTipbot";
		}
			
		this.tip.style.display="none";
		this.tip.style.zIndex = "410065408";  
		this.tip.style.position = "absolute";  
	},
	
	hide: function hide(event) {  
		new Effect.Opacity(this.tip, {duration:1.0, from:0.85, to:0});
		this.tip.style.display="none";
	},

	show: function show(event) {  
		this.tip.style.display = "block";
		new Effect.Opacity(this.tip, {duration:1.0, from:0.0, to:0.85});
		//setTimeout(this.hide.bind(this), 3000);
	}
};
