/*
jquery.tooltip.js
Copyright (c) 2006 Jörn Zaefferer, Stefan Petre
Dual licensed under the MIT and GPL licenses: http://www.opensource.org/licenses/mit-license.php, http://www.gnu.org/licenses/gpl.html
*/

(function($){	
	var helper, tTitle, tBody, tUrl, current, oldTitle, tID,
	plugin = $.fn.Tooltip = function(settings){
		settings = $.extend($.extend({}, arguments.callee.defaults), settings || {});
		if(!helper){
			// create tooltip
			helper = $('<div id="tooltip"><h3></h3><p class="body"></p><p class="url"></p></div>').hide().css({position:'absolute', zIndex:3000}).appendTo('body');
			tTitle = $('h3', helper);
			tBody = $('p:eq(0)', helper);
			tUrl = $('p:eq(1)', helper);
		}
		$(this).filter('[@title]').each(function(){this.tSettings = settings;}).bind("mouseover", save).bind(settings.event, handle);
		return this;
	},
	handle = function(event){
		if(this.tSettings.delay) tID = setTimeout(show, this.tSettings.delay); else show();
		if(this.tSettings.track) $('body').bind('mousemove', update);
		update(event);
		$(this).bind('mouseout', hide);
	},
	save = function() {
		if(this == current || !this.title) return;
		current = this;
		var source = $(this), settings = this.tSettings;
		oldTitle = title = source.attr('title');
		source.attr('title','');
		if(settings.showBody){var parts = title.split(settings.showBody); tTitle.html(parts.shift()); tBody.empty(); for(var i = 0, part; part = parts[i]; i++){if(i < 0) tBody.append("<br/>"); tBody.append(part);} tBody.show();} else {tTitle.html(title); tBody.hide();}
		href = (source.attr('href') || source.attr('src'));
		if( settings.showURL && href ) tUrl.html(href.replace('http://', '')).show(); else  tUrl.hide();
		if(settings.extraClass){helper.addClass(settings.extraClass);}
		if (settings.fixPNG && $.browser.msie){helper.each(function(){if (this.currentStyle.backgroundImage != 'none'){var image = this.currentStyle.backgroundImage; image = image.substring(5, image.length - 2); $(this).css({'backgroundImage':'none', 'filter':"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')"}); tPNGfix = true;}});}
	},
	show = function(){
		tID = null;
		helper.fadeIn('fast');
	},
	update = function(event){
		if(current == null){$('body').unbind('mousemove', update); return;}
		function pos(c){var p = c == 'X' ? 'Left' : 'Top'; return event['page' + c] || (event['client' + c] + (document.documentElement['scroll' + p] || document.body['scroll' + p])) || 0;}
		// position tooltip from mouse position
		helper.css({top: pos('Y') + -75 + 'px', left: pos('X') + -25 + 'px'});
	},
	hide = function(){
		if(tID) clearTimeout(tID);
		current = null;
		helper.hide();
		if(this.tSettings.extraClass){helper.removeClass( this.tSettings.extraClass);}
		$(this).attr('title', oldTitle).unbind('mouseout', hide);
		if(this.tSettings.fixPNG && $.browser.msie){helper.each(function(){$(this).css({'filter':'', backgroundImage:''});});}
	};
	// define default settings
	plugin.defaults = {
		delay: 500,
		event: "mouseover",
		track: true,
		showURL: false,
		showBody: null,
		extraClass: null,
		fixPNG: false
	};
})(jQuery);