/*
Sweet Titles (c) Creative Commons 2005-2008
http://creativecommons.org/licenses/by-sa/2.5/
Author: Dustin Diaz | http://www.dustindiaz.com
Ported to mootools B. Gerritsen bgerritsen@gmail.com (8-10-2007 16:14)
*/
var sweetTitles = new Class({
	Implements: [Options, Events],
	
	options: {
		tipElements : ['a','abbr','acronym','area','label','img'],	// @Array: Allowable elements that can have the toolTip
		active : 0,							 // @Number: 0: Not Active || 1: Active
		fading : 0,              // @Number: 0: Disable Fading || 1: Enable tooltip fading
		delay  : 0               // @Number: Delay in ms before tooltip will be showed
	},

	initialize : function(options) {
		this.setOptions(options);
		this.xCord = 0;
		this.yCord = 0;
		
		this.oTip = new Element('div', {'id': 'toolTip', 'styles': {'top' : '0', 'visibility' : 'hidden'}}).injectInside(document.body);
		var iTipLen = this.options.tipElements.length;
		for (var i=0; i<iTipLen; i++ )
		{
			$$(this.options.tipElements[i]).each(function(oElement)
			{
				
					this.addTooltipEvents(oElement);
				
				
			}.bind(this));
		}
	},
	addTooltipEvents : function(oElement)
	{
		var bAddEvent = false;
		if (oElement.getProperty('title') != null && oElement.getProperty('title') != "") 
		{
			oElement.setProperty('temptitle', oElement.getProperty('title'));
			oElement.removeProperty('title');
			bAddEvent = true;			
		}
		
		else if ( oElement.getProperty('alt') != null && oElement.getProperty('alt') != "" && oElement.getProperty('alt') != "*")
		{	
			// remove property alt when an image is a child element			
			oElement.setProperty('temptitle', oElement.getProperty('alt'));
			//oElement.setProperty('temptitle', oElement.getProperty('tempalt'));
			oElement.removeProperty('alt');
			bAddEvent = true;
		}
		
		if (bAddEvent)
		{
			oElement.addEvents({
				'mouseover': function(event) { this.tipOver(event, oElement); }.bind(this),
				'mouseout' : function(event) { this.tipOut(); }.bind(this),
				'mousemove': function(event) { this.tipMove(event); }.bind(this)
			});
		}
		
	},
	updateXY : function(e) {
		if (e.pageX)
		{
			this.xCord = e.pageX;
			this.yCord = e.pageY;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
		{
			this.xCord = e.client.x + document.documentElement.scrollLeft;
			this.yCord = e.client.y + document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			this.xCord = e.client.x + document.body.scrollLeft;
	  	this.yCord = e.client.y + document.body.scrollTop;
		}
	},
	tipOut: function() {
		this.oTimer = $clear(this.oTimer);
		if (this.oElement)
			this.oElement.removeProperty('title');
		if (this.oTip)
			this.oTip.setStyle('visibility', 'hidden');
		else
			$('toolTip').setStyle('visibility', 'hidden');
	},
	tipOver : function(event, oElement) {
		
		this.oElement = oElement;
		this.tipMove(event);
		this.oTimer = this.tipShow.delay(200, this);
	},
	tipShow : function() {
		this.oTip.set('html', "<p>"+this.oElement.getProperty('temptitle')+"</p>");
		this.oTip.setStyle('visibility', 'visible');
	},
	tipMove: function(e) {
		this.updateXY(e);
		var iScreenX  = Number(this.xCord);
		var iScreenY  = Number(this.yCord);
		var iTop  = parseInt(iScreenY+15);
		var iLeft = parseInt(iScreenX+10);
		this.oTip.setStyles({
			'left' : iLeft+'px',
			'top'  : iTop+'px'
		});
	},
	tipFade: function(opac) {
		var passed = parseInt(opac);
		var newOpac = parseInt(passed+10);
		if ( newOpac < 80 ) {
			this.tip.style.opacity = '.'+newOpac;
			this.tip.style.filter = "alpha(opacity:"+newOpac+")";
			opacityID = window.setTimeout("sweetTitles.tipFade('"+newOpac+"')",20);
		}
		else { 
			this.tip.style.opacity = '.80';
			this.tip.style.filter = "alpha(opacity:80)";
		}
	}
});

var oTooltip;
window.addEvent('load', function()
{
	oTooltip = new sweetTitles();
});