;(function($,window,document){$.jatt=function(options){var opt,process,cache=[],pageBody=$('body'),doc=$(document),win=$(window),o=$.extend({},$.jatt.defaultOptions,options),init=function(){var evt=(o.live)?'live':'bind',preloads=[];$.each('initialized.jatt beforeReveal.jatt revealed.jatt hidden.jatt'.split(' '),function(i,e){var cb=e.split('.')[0];if($.isFunction(o[cb])){doc.bind(e,o[cb]);}});$(o.tooltip)
[evt](o.activate,function(e){$.jatt.removeTooltips();var tmp,tt,$tt,rel,url,ttloader,ttl,$obj=$(this),meta=(o.metadata.toString()==='false')?[o,'']:$.jatt.getMeta($obj);doc.trigger('initialized.jatt',$obj);opt=meta[0];tt=($obj.attr(opt.content)==='')?$obj.data('tooltip')||'':$obj.attr(opt.content)||'';rel=$obj.attr('rel')||'';url=$obj.attr('href')||'';$obj.data('tooltip',tt);$obj.attr('title','');tmp='<div id="'+o.tooltipId+'" style="position:absolute;z-index:'+opt.zIndex+';'+
meta[1]+'"><span class="body"></span><span class="close">x</span></div>';if(opt.local){$obj.before(tmp);}else{pageBody.append(tmp);}
$tt=$('#'+o.tooltipId);if(tt===''){if(rel!==''){tt=$(rel).html()||o.notFound;}else if(url!==''){tt=o.loading;ttloader=$('<div />');ttloader.load(url,function(){ttl=ttloader.html();cache=(o.cacheData)?ttl:'';$tt.hide().find('.body').html(ttl);$obj.data('tooltip',cache);$.jatt.ttrelocate(e,o.tooltipId);$tt.fadeIn(opt.speed);});}}
$tt.data('options',opt).find('.body').html(tt);$.jatt.ttrelocate(e,o.tooltipId);if($obj.is(o.sticky)){$tt.find('.close').show().click(function(){$.jatt.removeTooltips();});}
doc.trigger('beforeReveal.jatt',$obj);$tt.fadeIn(opt.speed);doc.trigger('revealed.jatt',$obj);})
[evt](o.deactivate,function(e){if(!$(this).is(o.sticky)){$.jatt.removeTooltips();doc.trigger('hidden.jatt',this);}})
[evt]('mousemove',function(e){if($('#'+o.tooltipId).length&&opt.followMouse){$.jatt.ttrelocate(e,o.tooltipId);}});process=function(e,$obj,content){doc.trigger('initialized.jatt',$obj);var $tt,tt,tmp,meta=(o.metadata.toString()=='false')?[o,'']:$.jatt.getMeta($obj);opt=meta[0];tt=($obj.attr(opt.content)==='')?$obj.data('tooltip')||'':$obj.attr(opt.content)||'';$obj.data('tooltip',tt);if(opt.content==='title'){$obj.attr(opt.content,'');}
tmp='<div id="'+o.previewId+'" style="position:absolute;z-index:'+opt.zIndex+';'+meta[1]+'"><span class="body"><img src="'+content;tmp+=(tt!=='')?'<br/>'+tt:'';tmp+='</span><span class="close">x</span></div>';if(opt.local){$obj.before(tmp);}else{pageBody.append(tmp);}
$tt=$('#'+o.previewId);doc.trigger('beforeReveal.jatt',$obj);$tt.hide().data('options',opt).fadeIn(opt.speed);if($obj.is(o.sticky)){$tt.find('.close').show().click(function(){$.jatt.removeTooltips();});}
$.jatt.ttrelocate(e,o.previewId);doc.trigger('revealed.jatt',$obj);};$(o.preview)
[evt](o.activate,function(e){$.jatt.removeTooltips();process(e,$(this),$(this).attr('href')+'" alt="'+o.imagePreview+'" />');}).each(function(){preloads.push($(this).attr('href'));});$(o.screenshot)
[evt](o.activate,function(e){$.jatt.removeTooltips();var $obj=$(this),ss=($obj.attr('rel')==='#')?o.websitePreview+$obj.attr('href'):$obj.attr('rel');ss+='" alt="'+o.siteScreenshot+$obj.attr('href')+'" />';process(e,$obj,ss);}).each(function(){var $obj=$(this);preloads.push(($obj.attr('rel')==='#')?o.websitePreview+$obj.attr('href'):$obj.attr('rel'));});$(o.preview+','+o.screenshot)
[evt](o.deactivate,function(e){if(!$(this).is(o.sticky)){$.jatt.removeTooltips();doc.trigger('hidden.jatt',this);}})
[evt]('mousemove',function(e){if($('#'+o.previewId).length&&opt.followMouse){$.jatt.ttrelocate(e,o.previewId);}});$.jatt.preloadContent(preloads);};$.jatt.ttrelocate=function(e,ttid){var tt=$('#'+ttid),ttw=tt.outerWidth(),tth=tt.outerHeight(),opt=tt.data('options')||o,tip={e:[opt.xOffset,-tth/2,ttw+opt.xOffset,tth/2],se:[opt.xOffset,opt.yOffset,ttw+opt.xOffset,tth+opt.yOffset],s:[-ttw/2,opt.yOffset,ttw/2,tth+opt.yOffset],sw:[-ttw-opt.xOffset,opt.yOffset,-opt.xOffset,tth+opt.yOffset],w:[-ttw-opt.xOffset,-tth/2,-opt.xOffset,tth/2],nw:[-ttw-opt.xOffset,-tth-opt.yOffset,-opt.xOffset,-opt.yOffset],n:[-ttw/2,-tth-opt.yOffset,ttw/2,-opt.yOffset],ne:[opt.xOffset,-tth-opt.yOffset,ttw+opt.xOffset,-opt.yOffset]},dir=tip[opt.direction],wscrY=win.scrollTop(),wscrX=win.scrollLeft(),tar=$(e.target),curX=e.pageX||tar.position().left+tar.width()/2,curY=e.pageY||tar.position().top+tar.height()/2;if(!opt.followMouse){var objw=tar.outerWidth(),objh=tar.outerHeight(),obj={e:[objw,objh/2],se:[objw,objh],s:[objw/2,objh],sw:[0,objh],w:[0,objh/2],nw:[0,0],n:[objw/2,0],ne:[objw,0]};curX=tar.offset().left+obj[opt.direction][0];curY=tar.offset().top+obj[opt.direction][1];}
var ttleft=curX+dir[0],tttop=curY+dir[1];if(curX+dir[2]>wscrX+win.width()-opt.xOffset){ttleft=win.width()-ttw-opt.xOffset;}
if(curY+dir[3]>wscrY+win.height()-opt.yOffset){tttop=wscrY+win.height()-tth-opt.yOffset;}
if(ttleft<wscrX+opt.xOffset){ttleft=wscrX+opt.xOffset;}
if(tttop<wscrY+opt.yOffset){tttop=wscrY+opt.yOffset;}
if(curX>ttleft&&curX<ttleft+ttw&&curY>tttop&&curY<tttop+tth){tttop+=((tttop-tth/2-opt.yOffset)<wscrY+opt.yOffset)?tth/2+opt.yOffset:-tth/2-opt.yOffset;}
tt.css({left:ttleft+'px',top:tttop+'px'});};$.jatt.getMeta=function(el){opt=$.extend({},o);var t,m=[],opts='direction|followMouse|content|speed|local|xOffset|yOffset|zIndex',meta=el.attr(o.metadata)||'';meta=(meta.match(/(\{.*\})/g))?meta.match(/(\{.*\})/g)[0]:el.attr(o.metadata)||'';meta=(meta.match('width|background|color|border|'+opts))?meta:el.attr('data-jatt')||'';if(meta!==''){meta=meta.replace(/(\{|\'|\"|\})/g,'');if(meta.match(opts)){$.each(meta.split(';'),function(i,val){t=val.split(':');if(t[0].match(opts)){var k=$.trim(t[0]),v=$.trim(t[1]);if(v=='true'||v=='false'){opt[k]=(v=='true')?true:false;}else{opt[k]=(isNaN(v))?v:parseFloat(v);}}else{m.push(val);}});meta=m.join(';');}}
return[opt,meta];};$.jatt.removeTooltips=function(){$('#'+o.previewId+', #'+o.tooltipId).remove();while($('#'+o.previewId+', #'+o.tooltipId).length>0){$('#'+o.previewId+', #'+o.tooltipId).remove();}};$.jatt.preloadContent=function(preloads){var cacheImage,$this,$div,url,i,divs=[],$tt=$(o.tooltip),len=preloads.length;for(i=len;i--;){cacheImage=document.createElement('img');cacheImage.src=preloads[i];cache.push(cacheImage);}
$tt.each(function(i){$this=$(this);if(this.tagName==='A'&&$this.is(o.preloadContent)){url=$(this).attr('href')||'';if(url!==''&&!url.match(/^#/)){$div=$('<div rel="'+i+'" />');divs.push($div);$div.load(url,function(){$tt.eq($div.attr('rel')).data('tooltip',$div.html());});}}});};init();};$.jatt.defaultOptions={direction:'n',followMouse:true,content:'title',speed:300,local:false,xOffset:20,yOffset:20,zIndex:1000,live:false,metadata:'class',activate:'mouseenter focusin',deactivate:'mouseleave focusout',cacheData:true,websitePreview:'http://api1.thumbalizr.com/?width=250&url=',loading:'Loading...',notFound:'No tooltip found',imagePreview:'Image preview',siteScreenshot:'URL preview: ',tooltip:'.tooltip',screenshot:'a.screenshot',preview:'a.preview',preloadContent:'.preload',sticky:'.sticky',tooltipId:'tooltip',previewId:'preview'};})(jQuery,this,document);

