/*
The qnote plugin
© Teemu Alapoikela 2010
MIT License (http://jquery.org/license)
*/
(function($){
  $.fn.qnote = function(options) {
  
    var defaults = {
      theAttr: "title"
    };
  
    var options = $.extend(defaults, options);
    
    var note = $(".qnote");
    if (note.size() == 0) note = $("<span></span>").appendTo(document.body).addClass("qnote").hide().css("position","absolute");
    
    return this.each(function(){
      
      $t = $(this);
      
      $t.bind('mouseover', function(e){
        showNote(e.pageX,e.pageY, $(this));
      }).bind('mouseout', function(){
        hideNote($(this));
      });
      
      var noteVal = "";
      
      function showNote(x,y, elem) {
        noteVal = elem.attr(options.theAttr);
        elem.removeAttr(options.theAttr);
        if (noteVal.length > 0) {
          note.html(noteVal);
          note.css({
            left: x+30,
            top: y+30
          }).show();
          
          $(document.body).bind('mousemove', function(e) {
            if (note.is(":visible")) {
              note.css({
                left: e.pageX+30,
                top: e.pageY+30
              });
            }
          });
        }
      };
      
      function hideNote(elem) {
        note = $(".qnote");
        note.hide();
        elem.attr(options.theAttr, noteVal);
        $(document.body).unbind();
      }
      
    });
    
  };
})(jQuery);
