/*
* Jatt - just another tooltip
* by Rob Garrison (aka Mottie)
*
* based on tooltip by Alen Grakalic (http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery)
* tooltip modification by Rob G, aka Mottie (http://wowmotty.blogspot.com/)
*
* v2.2 10/3/2010 Added support for old tooltip script (css contained in rel attribute)
* v2.1 8/22/2010 Converted to a plugin, added enhancements & commented out dhtmltooltip script
* v2.01 8/29/2009 Added Websnapr.com site screenshot option
* v2.0 6/10/2009 Combined scripts, added support for dhtmltooltip
* v1.0 5/8/2008 Original tooltip script by Alen Grakalic
*/

function objClone(l){if(null===l||"object"!=typeof l)return l;var a=l.constructor(),d;for(d in l)if(l.hasOwnProperty(d))a[d]=l[d];return a}
(function($){$.jatt=function(l){var a,d=$.extend({},$.jatt.defaultOptions,l);$.jatt.ttrelocate=function(i,e){var c=$(e).outerWidth(),b=$(e).outerHeight(),g={e:[a.xOffset,-b/2,c+a.xOffset,b/2],se:[a.xOffset,a.yOffset,c+a.xOffset,b+a.yOffset],s:[-c/2,a.yOffset,c/2,b+a.yOffset],sw:[-c-a.xOffset,a.yOffset,-a.xOffset,b+a.yOffset],w:[-c-a.xOffset,-b/2,-a.xOffset,b/2],nw:[-c-a.xOffset,-b-a.yOffset,-a.xOffset,-a.yOffset],n:[-c/2,-b-a.yOffset,c/2,-a.yOffset],ne:[a.xOffset,-b-a.yOffset,c+a.xOffset,-a.yOffset]}[a.direction],
k=$(window).scrollTop(),m=$(window).scrollLeft(),f=i.pageX,j=i.pageY;if(!a.followMouse){j=$(i.target);f=j.outerWidth();var h=j.outerHeight();h={e:[f,h/2],se:[f,h],s:[f/2,h],sw:[0,h],w:[0,h/2],nw:[0,0],n:[f/2,0],ne:[f,0]};f=j.offset().left+h[a.direction][0];j=j.offset().top+h[a.direction][1]}h=f+g[0];var n=j+g[1];if(f+g[2]>m+$(window).width()-a.xOffset)h=$(window).width()-c-a.xOffset;if(j+g[3]>k+$(window).height()-a.yOffset)n=j-b-a.yOffset;if(h<m+a.xOffset)h=m+a.xOffset;if(n<k+a.yOffset)n=j+a.yOffset;
if(f>h&&f<h+c&&j>n&&j<n+b)n+=n-b/2-a.yOffset<k+a.yOffset?b/2+a.yOffset:-b/2-a.yOffset;$(e).css({left:h+"px",top:n+"px"})};$.jatt.getMeta=function(i){a=objClone(d);var e,c=[],b=i.attr(d.metadata).match(/(\{.*\})/g)||"";if(b!==""){b=b[0].replace(/(\{|\'|\"|\})/g,"");if(b.match("direction|followMouse|content|speed|local|xOffset|yOffset|zIndex")){$.each(b.split(";"),function(g,k){e=k.split(":");if(e[0].match("direction|followMouse|content|speed|local|xOffset|yOffset|zIndex")){var m=$.trim(e[0]),f=$.trim(e[1]);
a[m]=f=="true"||f=="false"?f=="true"?true:false:isNaN(f)?f:parseFloat(f)}else c.push(k)});b=c.join(";")}}e=i.attr("rel")||"";if(e!==""&&!/^[#|\.]|[\/]/.test(e)){e=e.split(",");b+=";width:"+e[0]+"px;";if(typeof e[1]!="undefined")b+="background:"+e[1]}return[a,b]};(function(){var i=d.live?"live":"bind";$(d.tooltip)[i](d.activate,function(e){var c=$(this),b=d.metadata.toString()=="false"?[d,""]:$.jatt.getMeta(c);a=b[0];var g=c.attr(a.content)===""?c.data("tooltip")||"":c.attr(a.content)||"",k=c.attr("rel")||
"",m=c.attr("href")||"";c.data("tooltip",g);c.attr("title","");if(g==="")if(k!=="")g=$(k).html()||"No tooltip found";else if(m!==""){g="Loading...";var f=$("<div />");f.load(m,function(){$("#"+d.tooltipId).html(f.html())})}b='<div id="'+d.tooltipId+'" style="position:absolute;z-index:'+a.zIndex+";"+b[1]+'">'+g+"</div>";a.local?c.before(b):$("body").append(b);$.jatt.ttrelocate(e,"#"+d.tooltipId);$("#"+d.tooltipId).fadeIn(a.speed)})[i](d.deactivate,function(){$("#"+d.tooltipId).remove()})[i]("mousemove",
function(e){a.followMouse&&$.jatt.ttrelocate(e,"#"+d.tooltipId)});$(d.preview+","+d.screenshot)[i](d.activate,function(e){var c=$(this),b=d.metadata.toString()=="false"?[d,""]:$.jatt.getMeta(c);a=b[0];var g=c.attr(a.content)===""?c.data("tooltip")||"":c.attr(a.content)||"";c.data("tooltip",g);a.content=="title"&&c.attr(a.content,"");b='<div id="'+d.previewId+'" style="position:absolute;z-index:'+a.zIndex+";"+b[1]+'"><img src="';g=g!==""?"<br/>"+g:"";var k=c.is(d.screenshot)&&this.rel=="#"?"http://images.websnapr.com/?url="+
this.href:this.rel;b+=c.is(d.preview)?this.href+'" alt="Image preview" />':k+'" alt="URL preview: '+this.href+'" />';b+=g+"</div>";a.local?c.before(b):$("body").append(b);$("#"+d.previewId).data("options",a);$.jatt.ttrelocate(e,"#"+d.previewId);$("#"+d.previewId).fadeIn(a.speed)})[i](d.deactivate,function(){$("#"+d.previewId).remove()})[i]("mousemove",function(e){a.followMouse&&$.jatt.ttrelocate(e,"#"+d.previewId)})})()};$.jatt.defaultOptions={direction:"n",followMouse:true,content:"title",speed:300,
local:false,xOffset:20,yOffset:20,zIndex:1E3,live:false,metadata:"class",activate:"mouseenter focusin",deactivate:"mouseleave focusout",tooltip:".tooltip",screenshot:"a.screenshot",preview:"a.preview",tooltipId:"tooltip",previewId:"preview"}})(jQuery);
