(function($) {
  $.fn.tooltip = function(content, settings, callback){
    // Catching the jQuery object with all elements matched.
    var jQueryMatchedObj = this;
    
    // If another tooltip is opened.
    var tooltipOpened = false;
    
    // Settings to configure the tooltip.
    settings = jQuery.extend({
      // Dimensions.
      height: 200,
      width: 200,
      align: "right"
    }, settings);
    
    /**
     * Shows the tooltip.
     *
     * @return  boolean true
     */
    function showTooltip(triggeredObject){
      // Close tooltip if it's already open.
      if(tooltipOpened){
        $("#tooltip-wrapper").hide();
        tooltipOpened = false;
      }
      
      // Reset arrow state.
      $("#tooltip-arrow-r").css("display", "none");
      $("#tooltip-arrow-l").css("display", "none");
      
      // Set height of main container.
      $("#tooltip-mm").css({
        "height": settings.height + "px",
        "width": settings.width + "px"
      });
      
      // Set width of t, m and b.
      $('#tooltip-t').css("width", (settings.width + 40) + "px");
      $('#tooltip-m').css("width", (settings.width + 40) + "px");
      $('#tooltip-b').css("width", (settings.width + 40) + "px");
      
      // Set height of t, m and b.
      $('#tooltip-t').css("height", "20px");
      $('#tooltip-m').css("height", settings.height + "px");
      $('#tooltip-b').css("height", "20px");
      
      // Set height of ml and mr.
      $("#tooltip-ml").css("height", settings.height + "px");
      $("#tooltip-mr").css("height", settings.height + "px");
      
      // Set width of tm and bm.
      $('#tooltip-tm').css("width", settings.width + "px");
      $('#tooltip-bm').css("width", settings.width + "px");
      
      // Set the content.
      $("#tooltip-mm").empty();
      $("#tooltip-mm").append("<div id=\"tooltip-close\"></div>");
      $("#tooltip-mm").append(content);
      
      var elemPosition = $(triggeredObject).offset();
      $("#tooltip-wrapper").css({
        "position": "absolute",
        "height": (settings.height + 40) + "px",
        "width": (settings.width + 40) + "px",
        "top": (elemPosition.top - 20) + "px"
      });
      
      if(settings.align == "left"){
        $("#tooltip-wrapper").css("left", (elemPosition.left - settings.width - 40) + "px");
        $("#tooltip-arrow-r").css("display", "block");
      } else {
        $("#tooltip-wrapper").css("left", (elemPosition.left + 20) + "px");
        $("#tooltip-arrow-l").css("display", "block");
      }
      
      $("#tooltip-wrapper").show();
      tooltipOpened = true;
      return true;
    }
    
    /**
     * Hides the tooltip.
     *
     * @return  boolean true
     */
    function hideTooltip(){
      // Close tooltip if it's already open.
      if(tooltipOpened){
        $("#tooltip-wrapper").hide();
        tooltipOpened = false;
      }
      
      return true;
    }
    
    this.each(function(){
      $(this).click(function(){
        showTooltip(this);
        if(typeof(callback) == "function"){
          callback.call();
        }
        
        $("#tooltip-close").click(hideTooltip);
      });
      
      $(this).css("cursor", "pointer");
    });
  };
})(jQuery);