/* Copyright (c) 2008 Jordan Kasper
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 * Requires: jQuery 1.2+
 */
;(function($) {
  
  var setPosition = function (e, n) {
    var n = $(n);
    var w = $(window).width();
    var h = $(window).height();
    var top = e.pageY+$.fn.simpleTip.topBottomOffset;
    var left = e.pageX+$.fn.simpleTip.leftRightOffset;
    var right = 0, bottom = 0;
    var placeOnLeft = false;
    var placeOnTop = false;
    
    if ((left + n.width()) > (w - 30)) {
      placeOnLeft = true;
      right = w-e.pageX-$.fn.simpleTip.leftRightOffset;
    }
    if ((top + n.height()) > h) {
      placeOnTop = true;
      bottom = h-e.pageY-$.fn.simpleTip.topBottomOffset;
    }
    
    // reset left,right,top,bottom for clean slate
    n.css({left:'auto', right:'auto', top:'auto', bottom:'auto'});
    if (placeOnLeft)
      n.css({right: right});
    else
      n.css({left: left});
    if (placeOnTop)
      n.css({bottom: bottom});
    else
      n.css({top: top});
  };
  
  var setUpEvents = function(base, tip) {
    base.bind('mouseover.simpleTip', function(e) {
      if (!$(this).hasClass('disableSimpleTips')) {
        setPosition(e, tip);
        tip.show();
      }
    });
    base.bind('mousemove.simpleTip', function(e) {
      setPosition(e, tip);
    });
    base.bind('mouseout.simpleTip', function(e) {
      tip.hide();
    });
  }
  
  var getTipNode = function(base) {
    var n = $(base);
    base = n.get(0);
    if (!base.id || base.id.length < 1) {
      base.id = 'simpleTipBase_'+(++$.fn.simpleTip.uid);
    }
    // see if tip already exists
    var t = $('#'+base.id+'_tip');
    if (t.length < 1) {
      $('body').append(
        "<p class='simpleTip' id='"+base.id+"_tip'>"+
          base.title+
        "</p>"
      );
      $(base).removeAttr('title');
      t = $('#'+base.id+'_tip')
            .css({position: 'absolute'})
            .hide()
            .get(0);
    }
    return t;
  }
  
  $.fn.addSimpleTips = function() {
    var n = this;
    if (n.length < 1) { return n; }
    
    n.each(function() {
      if ($('#'+this.id+'_tip').length < 1) {
        setUpEvents($(this), $(getTipNode(this)));
      }
    });
    
    return n;
  }
  
  $.fn.simpleTip = function(tipNode) {
    var n = this;
    if (n.length < 1) { return n; }
    
    n.each(function() {
      var t;
      if (typeof tipNode == 'string' || (tipNode && tipNode.tagName)) {
        t = $(tipNode);
        t.remove()
          .addClass('simpleTip')
          .appendTo('body')
          .css({position: 'absolute'})
          .hide();
      } else {
        t = $(getTipNode(this));
      }
      
      setUpEvents($(this), t);
    });
    return n;
  };
  
  $.fn.removeSimpleTip = function() {
    var n = this;
    if (n.length < 1) { return n; }
    
    $('.simpleTip').hide();
    n.unbind('.simpleTip');
    // See if we can find the matching tip and remove it
    n.each(function() {
      $('#'+$(this).attr('id')+'_tip').remove();
    });
    
    return n;
  };
  
  $.fn.disableSimpleTips = function() {
    var n = this;
    if (n.length < 1) { return n; }
    $('.simpleTip').hide();
    n.addClass('disableSimpleTips');
    return n;
  }
  
  $.fn.enableSimpleTips = function() {
    var n = this;
    if (n.length < 1) { return n; }
    n.removeClass('disableSimpleTips');
    return n;
  }
  
  $.fn.simpleTip.uid = 0;
  $.fn.simpleTip.topBottomOffset = 5;
  $.fn.simpleTip.leftRightOffset = 10;

})(jQuery);
