/* Sticky Tooltip script (v1.0)
* Created: Nov 25th, 2009. This notice must stay intact for usage
* Author: Dynamic Drive at http://www.dynamicdrive.com/
* Visit http://www.dynamicdrive.com/ for full source code
*/

var stickytooltip={
	tooltipoffsets: [0, 0], //additional x and y offset from mouse cursor for tooltips
	fadeinspeed: 30, //duration of fade effect in milliseconds
	maxwidth: 400,
	stickybordercolors: ["black", "#333333"], //border color of tooltip depending on sticky state
	t1:null,

	positiontooltip:function($, $tooltip, e){
		var $cell,o,x,y,tipw,tiph;
		$cell=$(e.currentTarget);
		o=$cell.offset();
		x=o.left+$cell[0].offsetWidth; y=o.top-1;
		$tooltip.width('auto').css({left:0, top:0});
		if($tooltip.outerWidth() > stickytooltip.maxwidth) {
			$tooltip.width(stickytooltip.maxwidth);
		}
		tipw=$tooltip.outerWidth(), tiph=$tooltip.outerHeight();
		x=(x+tipw>$(document).scrollLeft()+$(window).width()) ? x-$cell[0].offsetWidth-tipw : x;
		y=(y+tiph>$(document).scrollTop()+$(window).height())? y+$cell[0].offsetHeight-tiph+1 : y;
		$tooltip.css({left:x, top:y}).width(tipw);
	},

	showbox:function($, $tooltip, e){
		$tooltip.fadeIn(this.fadeinspeed);
		this.positiontooltip($, $tooltip, e);
	},

	hidebox:function($, $tooltip){
		$tooltip.stop(false, true).hide();
	},

	init:function(targetselector, tipid){
		jQuery(document).ready(function($){
			var $targets=$(targetselector);
			var $tooltip=$('#'+tipid).appendTo(document.body);
			if ($targets.length==0) {
				return;
			}
			var $alltips=$tooltip.find('div.atip');
			stickytooltip.hidebox($, $tooltip);
			$targets.bind('mouseenter', function(e){
				$alltips.hide().filter('#'+$(this).attr('data-tooltip')).show();
				stickytooltip.showbox($, $tooltip, e);
				clearTimeout(stickytooltip.t1);
			})
			.bind('mouseleave', function(e){
				stickytooltip.t1 = setTimeout(function(){
					stickytooltip.hidebox($, $tooltip);
				}, 200);
			});
			$tooltip.bind('mouseleave', function(e){
				stickytooltip.t1 = setTimeout(function(){
					stickytooltip.hidebox($, $tooltip);
				}, 200);
			})
			.bind("mouseenter", function(){
				clearTimeout(stickytooltip.t1);
			})
			.bind("click", function(e){
				e.stopPropagation();
			});
			$(this).bind("click", function(e){
				if (e.button==0){
					stickytooltip.hidebox($, $tooltip);
				}
			});
		}) //end dom ready
	}
}

//~ stickytooltip.init("*[data-tooltip]", "mystickytooltip")

