/* Wayfarer Tooltip
 * Version 1.0.2
 * 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 : "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 #d0d0d0",
        background : "#f7f8f5",
        color : "#0b2b62",
        padding : "2px",
        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(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(x) {
        var v = (x) ? x : window.event, u = this;
        k = this;
        if (f.follow || b) {
          var y = a(window).scrollTop();
          var z = a(window).scrollLeft();
          var w = v.clientY + y + f.offsetY;
          var p = v.clientX + z + f.offsetX;
          var o = a(window).width() + z - a(s).outerWidth();
          var t = a(window).height() + y - a(s).outerHeight();
          n = (w > t || p > o) ? true : false;
          if (p - z <= 0 && f.offsetX < 0) {
            p = z
          } else {
            if (p > o) {
              p = o
            }
          }
          if (w - y <= 0 && f.offsetY < 0) {
            w = y
          } else {
            if (w > t) {
              w = t
            }
          }
          s.style.top = w + "px";
          s.style.left = p + "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);
