(function($) {
	$.fn.delay = function(time, callback){
    jQuery.fx.step.delay = function(){};
    return this.animate({delay:1}, time, callback);
	}
	
  $.fn.tipsy = function(opts) {

      opts = $.extend({fade: false, gravity: 'n'}, opts || {});
      var tip = null, cancelHide = false;
      this.hover(function() {
          
          $.data(this, 'cancel.tipsy', true);

          var tip = $.data(this, 'active.tipsy');
          switch (opts.gravity.charAt(0)) {
              case 'e':
                  $direction = "right";
                  break;
              case 'w':
              	$direction = "left";

                  break;
          }
          if (!tip) {
              tip = $('<div class="tipsy"><div class="tipsy-inner">' + $(this).attr('title') + '</div><img src="http://static.allegrostatic.pl/site_images/1/0/templates_files/community/tip-btm-' + $direction + '.gif"  /></div>');
              tip.css({position: 'absolute', zIndex: 1});
              $(this).attr('title', '');
              $.data(this, 'active.tipsy', tip);
          }
          
          var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
          tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
          var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
          
          switch (opts.gravity.charAt(0)) {
              case 'e':
                  tip.css({top: pos.top - actualHeight, left: pos.left - actualWidth + 10}).addClass('tipsy-east');
                  break;
              case 'w':
                  tip.css({top: pos.top - actualHeight - 5, left: pos.left + pos.width - 50}).addClass('tipsy-west');
                  break;
          }

          if (opts.fade) {
              tip.css({opacity: 0, display: 'block', visibility: 'visible'}).delay(1000).animate({opacity: 1});
          } else {
              tip.css({visibility: 'visible'});
          }
          
      }, function() {
          var self = this;
          var tip = $.data(self, 'active.tipsy');
          tip.css({visibility: 'hidden', opacity: 0});
          tip.remove();
      },500);

  };
})(jQuery);
