﻿/* Wayfarer Tooltip
 * Version 1.0.4
 * Author Abel Mohler
 * URI: http://www.wayfarerweb.com/wtooltip.php
 * Released with the MIT License: http://www.wayfarerweb.com/mit.php
 */
(function(a){a.fn.wTooltip=function(f,r){f=a.extend({content:null,ajax:null,follow:true,auto:true,fadeIn:0,fadeOut:0,appendTip:document.body,degrade:false,offsetY:10,offsetX:1,style:{},className:null,id:null,callBefore:function(t,p,o){},callAfter:function(t,p,o){},clickAction:function(p,o){a(p).hide()},delay:0,timeout:0,cloneable:false},f||{});if(!f.style&&typeof f.style!="object"){f.style={};f.style.zIndex="1000"}else{f.style=a.extend({border:"1px solid gray",background:"#edeef0",color:"#000",padding:"10px",zIndex:"1000",textAlign:"left"},f.style||{})}if(typeof r=="function"){f.callAfter=r||f.callAfter}f.style.display="none",f.style.position="absolute";var m,l,q,c,k={},b=true,e=false,n=false,s=document.createElement("div"),g=(typeof document.body.style.maxWidth=="undefined")?true:false,j=(typeof a.talk=="function"&&typeof a.listen=="function")?true:false;if(f.id){s.id=f.id}if(f.className){s.className=f.className}f.degrade=(f.degrade&&g)?true:false;for(var d in f.style){s.style[d]=f.style[d]}function i(o){if(o){if(f.degrade){a(s).html(f.content.replace(/<\/?[^>]+>/gi,""))}else{a(s).html(f.content)}}}if(f.ajax){a.get(f.ajax,function(o){if(o){f.content=o}i(f.content)})}function h(p){function t(u){if(m&&!f.content){u.title=m;m=null}}function o(){if(!e&&f.auto){clearInterval(c);if(f.fadeOut){a(s).fadeOut(f.fadeOut,function(){t(p)})}else{t(p);s.style.display="none"}}if(typeof f.callAfter=="function"){f.callAfter(s,p,f)}if(j){f=a.listen(f)}}if(f.timeout>0){q=setTimeout(function(){o()},f.timeout)}else{o()}}a(s).hover(function(){e=true},function(){e=false;h(k)});if(j){f.key=s;f.plugin="wTooltip";f.channel="wayfarer";a.talk(f)}i(f.content&&!f.ajax);a(s).appendTo(f.appendTip);return this.each(function(){this.onmouseover=function(t){var p=this;clearTimeout(q);if(this.title&&!f.degrade&&!f.content){m=this.title;this.title=""}if(f.content&&f.degrade){this.title=s.innerHTML}function o(){if(typeof f.callBefore=="function"){f.callBefore(s,p,f)}if(j){f=a.listen(f)}if(f.auto){var u;if(f.content){if(!f.degrade){u="block"}}else{if(m&&!f.degrade){a(s).html(unescape(m));u="block"}else{u="none"}}if(u=="block"&&f.fadeIn){a(s).fadeIn(f.fadeIn)}else{s.style.display=u}}}if(f.delay>0){l=setTimeout(function(){o()},f.delay)}else{o()}};this.onmousemove=function(y){var w=(y)?y:window.event,v=this;k=this;if(f.follow||b){var A=a(window).scrollTop(),B=a(window).scrollLeft(),x=w.clientY+A+f.offsetY,t=w.clientX+B+f.offsetX,z=a(f.appendTip).outerHeight(),p=a(f.appendTip).innerHeight(),o=a(window).width()+B-a(s).outerWidth(),u=a(window).height()+A-a(s).outerHeight();x=(z>p)?x-(z-p):x;n=(x>u||t>o)?true:false;if(t-B<=0&&f.offsetX<0){t=B}else{if(t>o){t=o}}if(x-A<=0&&f.offsetY<0){x=A}else{if(x>u){x=u}}s.style.top=x+"px";s.style.left=t+"px";b=false}};this.onmouseout=function(){clearTimeout(l);var o=this;b=true;if(!f.follow||n||(f.offsetX<0&&f.offsetY<0)){setTimeout(function(){c=setInterval(function(){h(o)},1)},1)}else{h(this)}};if(typeof f.clickAction=="function"){this.onclick=function(){f.clickAction(s,this)}}})}})(jQuery);