/* JTip
 * Original implementation by Cody Lindley (http://www.codylindley.com)
 * Derivation by Matt Saladna <matt@apisnetworks.com>
 * 
 * Under an Attribution, Share Alike License
 * 
 */
var tooltip = {
	options: {
		// tooltip is AJAX, implies tipSource -> href
		ajax : true,
		// tooltip width
		width: 275,
		// tooltip source attribute
		tipSource : 'title',
		// disable clicks on links
		unbind: true
	},
	
	show: function($el) {
		var offset = $el.offset();
		var width  = $el.outerWidth();
		var height = $el.outerHeight()/2;
		var o      = this.options;
		var winWidth  = $('body').width();
		var winHeight = $('body').height();
		// flip margin if tt is flipped across Y-axis
		var marginMirror = 1;
		var $tooltip = $("<div id='tooltip' style='display:none; width:" + parseInt(o.width)+ "px'><div id='tooltip_frame'><div id='tooltip_inside'></div><div id=\"tooltip_underlay\" /></div></div>").appendTo('body');
		var tHeight   = $tooltip.height()
		if (winWidth-offset.left-width-o.width <= 0) {
			offset.left = offset.left-o.width-width;
			// IE6 bug... CSS is not evaluated until e is part of DOM
			marginMirror = -1;			
		}
		
		$tooltip.css({
			left:       offset.left+width + "px",
			top:        offset.top-(tHeight/2)-height + "px",
			marginLeft: parseInt($tooltip.css('marginLeft'))*marginMirror+'px'
		});
		
		if (!o.ajax) {
			var t = $el.attr(o.tipSource);
			$el.attr(o.tipSource,"");
			this.insert(t);
			$el.mouseleave(function() { $el.attr(o.tipSource, t); })
		} else {
			if ($el.attr('title'))
				$el.attr('href',$el.attr('title')).attr('title','');	
			this.load_ajax($el.attr('href'));
		}
		
		if (offset.top+$tooltip.height() > winHeight) {
			offset.top = winHeight-tHeight;
		}
		
		$tooltip.fadeIn('fast');	
	},	
	
	insert: function(tip, title) {
		$("#tooltip_inside").html(
			(title ? "<span id=\"tooltip_title\">" + title + "</span>" : "") + '<span id="tooltip_tip">' + tip + '</span>');	
	},
	
	load_ajax: function(url) {
		$("#tooltip_inside").html("<img src=\"/images/spinner-small.gif\" alt=\"loading\" border=\"0\" align=\"absmiddle\" /> Loading");
			$.post(url, {
			}, 
		function(data) {
			eval("data = " + data + ";");
			tooltip.insert(data.body, data.title);
		});
	},
	
	_parseParams: function(query) {
		var Params = new Object;
	   if(!query) { return Params; }
	   var Pairs = query.split(/[;&]/);
	   for(var i = 0; i < Pairs.length; i++) {
	      var KeyVal = Pairs[i].split("=");
	      if(!KeyVal || KeyVal.length != 2) {
	         continue}
	      var key = unescape(KeyVal[0]);
	      var val = unescape(KeyVal[1]);
	      val = val.replace(/\+/g," ");
	      Params[key] = val}
	   return Params
	}
};
$.fn.tooltip = function(o) {
	o = $.extend(tooltip.options, tooltip.options, o);
	return this.each(function() {
		$(this).hover(function(e){
			tooltip.show($(this));
	   }
	   , 
	   function() { $("#tooltip").remove()}).
	   click(function() {return !o.unbind;}
	);});
}
$(document).ready(function() {
	$('.tooltip').tooltip();
});
