/**
 * @author 					Dave Shepard
 * @updated_at 				June 23, 2009
 * @required libraries:		Prototype 1.6.1_rc2 or later
 * @optional libraries:		Scriptaculous 1.8.1 or later
 * 
 * Custom designed tooltips
 *
 * Implementation:
 * ===============
 * Assign the class "tooltip" to any element you want to have a custom
 * tooltip assigned. The ToolTipper will automatically read the title
 * attribute of the element as the custom tooltip content.
 *
 */

var ToolTipper = {
	id: "tooltip_tip",
	template: '<div id="#{id}" class="tipOuter#{IE6Class}"><div class="tipInner">#{text}</div><div class="tipRight"></div></div>',
	options: {
		opacity: 1,
		duration: 0.25,
		transition: Effect.Transitions.linear,
		offsetX: 0,
		offsetY: -67,
		removeTitle: true
	},
	tips: [],

	make: function(params){
		var self = this;

		params.id = this.id;
		if(Prototype.Browser.IE && navigator.appVersion.indexOf('MSIE 6.0') != -1) params.IE6Class = "ie6";
		
		var template = new Template(this.template);
		
		$(document.body).insert({bottom: template.evaluate(params)});

		$(document).observe('mousemove',function(event){
			$(self.id).setStyle({
				top: (event.pointerY()+self.options.offsetY)+"px",
				left: (event.pointerX()+self.options.offsetX)+"px"
			});
		});
	},
	
	show: function(e){
		var self = this;
		
		var tipText = this.tips[this.elements.indexOf(e)];

		/** BEGIN :: Custom implementation for Shorthand team members */
		var textParts = this.tips[this.elements.indexOf(e)].split("|");
		tipText = new Template('<span class="name">#{name}</span><br /><span class="title">#{title}</span>').evaluate({
			name: textParts.first(),
			title: textParts.last()
		});
		/** END :: Custom implementation for Shorthand team members */
		
		if(!$(this.id)) this.make({text: tipText});
		
		$(this.id).down('.tipInner').update(tipText);

		if (!Object.isUndefined(Effect)) {
			$(this.id).appear({
				from: 0,
				to: self.options.opacity,
				duration: self.options.duration,
				transition: self.options.transition,
				queue: {
					scope: "tooltip",
					position: "end",
					limit: 1
				}
			});
		} else {
			$(this.id).setOpacity(this.options.opacity);
		}
	},
	
	hide: function(){
		if($(this.id)) {
			if(!Object.isUndefined(Effect)) {
				Effect.Queues.get('tooltip').each(function(effect){
					effect.cancel();
				});
			}
			$(this.id).hide();
		}
	},
	
	assign: function(){
		var self = this;
		this.elements.each(function(e,eInd){
			e.observe('mouseenter',function(){
				self.show(e);
			}).observe('mouseleave',function(){
				self.hide();
			});
		},this);
	},
	
	initialize: function(){
		this.elements = $$('.tooltip');
		
		if(this.elements.size() > 0) {
			this.elements.each(function(e,eInd){
				this.tips.push(e.title);
				if(this.options.removeTitle === true) e.removeAttribute('title');
			},this);
			
			this.assign();
		}
	}
}

document.observe('dom:loaded',function(){
	ToolTipper.initialize();
});
