/*!
 * jQuery Tools v1.2.5 - The missing UI library for the Web
 *
 * tooltip/tooltip.js
 *
 * NO COPYRIGHTS OR LICENSES. DO WHAT YOU LIKE.
 *
 * http://flowplayer.org/tools/
 *
 */
/**
 * Customised by E3, 2011-08-23 to improve the way positioning works.
 *
 * By default, all positioning of the tip is done using top and left, which falls over badly when the height of the
 * tooltip isn’t known until after the tip’s changed. This code positions top- and left-aligned tips using their bottom
 * and right edges, respectively. This means that tips remain anchored correctly even when their content changes.
 *
 * The positioning code also respects the viewport, so an edge-aligned tooltip that’d otherwise fall out of the viewport
 * is now aligned to the opposite edge
 *
 *
 * NOTE: returning false/otherwise blocking from on* event handlers WILL NOT WORK in this implementation as they have
 *       been disabled because of a FF bug—http://bugs.jquery.com/ticket/8474
 */

(function(d){d.tools=d.tools||{version:"v1.2.5-e3"};d.tools.tooltip={conf:{effect:"toggle",fadeOutSpeed:"fast",predelay:0,delay:30,opacity:1,tip:0,position:["top","center"],offset:[0,0],relative:false,cancelDefault:true,events:{def:"mouseenter,mouseleave",input:"focus,blur",widget:"focus mouseenter,blur mouseleave",tooltip:"mouseenter,mouseleave"},layout:"<div/>",tipClass:"tooltip",assumeHeight:null,assumeWidth:null,triggerInhibitClass:""},addEffect:function(e,g,f){c[e]=[g,f]}};var c={toggle:[function(e){var f=this.getConf(),g=this.getTip(),h=f.opacity;if(h<1){g.css({opacity:h})}g.show();e.call()},function(e){this.getTip().hide();e.call()}],fade:[function(e){var f=this.getConf();this.getTip().fadeTo(f.fadeInSpeed,f.opacity,e)},function(e){this.getTip().fadeOut(this.getConf().fadeOutSpeed,e)}]};function b(g,n,k){var o=k.relative?g.position():g.offset();var j={height:g.outerHeight(false),width:g.outerWidth(false)};var l=d(window);var p=d(document);var e={height:((k.assumeHeight==null)?n.outerHeight():k.assumeHeight),width:((k.assumeWidth==null)?n.outerWidth():k.assumeWidth)};if(/iPad/i.test(navigator.userAgent)){o.top-=l.scrollTop()}var q={left:o.left+j.width+k.offset[1],right:p.width()-o.left-k.offset[1],top:o.top+j.height+k.offset[0],bottom:p.height()-o.top-k.offset[0]};var m="";var i="";var h="";var f="";switch(k.position[1]){case"left":if((q.right+e.width)<(p.width()-l.scrollLeft())){i=q.right}else{m=q.left-(k.offset[1]*2)}break;case"center":m=o.left-((e.width-j.width)/2)+k.offset[1];break;case"right":if((q.left+e.width)<(l.width()+l.scrollLeft())){m=q.left}else{i=q.right+(k.offset[1]*2)}break}switch(k.position[0]){case"top":if((q.bottom+e.height)<(p.height()-l.scrollTop())){f=q.bottom}else{h=q.top-(k.offset[0]*2)}break;case"center":h=o.top-((e.height-j.height)/2)+k.offset[0];break;case"bottom":if((q.top+e.height)<(l.height()+l.scrollTop())){h=q.top}else{f=q.bottom+(k.offset[0]*2)}break}return{top:h,bottom:f,left:m,right:i}}function a(h,j){var r=this,g=h.add(r),o,f=0,q=0,m=h.attr("title"),i=h.attr("data-tooltip"),s=c[j.effect],n,l=h.is(":input"),e=l&&h.is(":checkbox, :radio, select, :button, :submit"),k=h.attr("type"),p=j.events[k]||j.events[l?(e?"widget":"input"):"def"];if(!s){throw'Nonexistent effect "'+j.effect+'"'}p=p.split(/,\s*/);if(p.length!=2){throw"Tooltip: bad events configuration for "+k}h.bind(p[0],function(t){clearTimeout(f);if(j.predelay){q=setTimeout(function(){r.show(t)},j.predelay)}else{r.show(t)}}).bind(p[1],function(t){clearTimeout(q);if(j.delay){f=setTimeout(function(){r.hide(t)},j.delay)}else{r.hide(t)}});if(m&&j.cancelDefault){h.removeAttr("title");h.data("title",m)}d.extend(r,{show:function(u){if(!o){if(i){o=d(i)}else{if(j.tip){o=d(j.tip).eq(0)}else{if(m){o=d(j.layout).addClass(j.tipClass).appendTo(document.body).hide().append(m)}else{o=h.next();if(!o.length){o=h.parent().next()}}}}if(!o.length){throw"Cannot find tooltip for "+h}}if(r.isShown()){return r}if((j.triggerInhibitClass!="")&&h.hasClass(j.triggerInhibitClass)){return r}o.stop(true,true);var v=b(h,o,j);if(j.tip){o.html(h.data("title"))}u=u||d.Event();u.type="onBeforeShow";g.trigger(u,[v]);v=b(h,o,j);v.position="absolute";o.css(v);n=true;s[0].call(r,function(){u.type="onShow";n="full";g.trigger(u)});var t=j.events.tooltip.split(/,\s*/);if(!o.data("__set")){o.bind(t[0],function(){clearTimeout(f);clearTimeout(q)});if(t[1]&&!h.is("input:not(:checkbox, :radio), textarea")){o.bind(t[1],function(w){if(w.relatedTarget!=h[0]){h.trigger(p[1].split(" ")[0])}})}o.data("__set",true)}return r},hide:function(t){if(!o||!r.isShown()){return r}t=t||d.Event();t.type="onBeforeHide";g.trigger(t);n=false;c[j.effect][1].call(r,function(){t.type="onHide";g.trigger(t)});return r},isShown:function(t){return t?n=="full":o.is(":visible")},getConf:function(){return j},getTip:function(){return o},getTrigger:function(){return h}});d.each("onHide,onBeforeShow,onShow,onBeforeHide".split(","),function(u,t){if(d.isFunction(j[t])){d(r).bind(t,j[t])}r[t]=function(v){if(v){d(r).bind(t,v)}return r}})}d.fn.tooltip=function(e){var f=this.data("tooltip");if(f){return f}e=d.extend(true,{},d.tools.tooltip.conf,e);if(typeof e.position=="string"){e.position=e.position.split(/,?\s/)}this.each(function(){f=new a(d(this),e);d(this).data("tooltip",f)});return e.api?f:this}})(jQuery);
