/* SVN: $Id: jquery.show_hint.js 21 2011-02-06 10:20:27Z brother79 $*/
(function($){
	$.fn.ShowHint = function(options){
		options = $.extend({
	        lookupResource: null,
			html: null,
			fhtml: null,
	        flyoutClass: 'hint',
	        width:0
		},options);
		this.hint=null;		
		return this.hover(function(event){
			if (options.fhtml != null) {
				options.html = options.fhtml(this);
			}
			if (options.html) {
				$('div.'+options.flyoutClass).fadeOut(100,function(){$(this).remove();});				
				var left = (options.width==0) ? event.pageX : (event.pageX + options.width > $('body').width() ? $('body').width() - options.width : event.pageX);
				this.hint = $('<div></div>')
					.css({
						position: 'absolute',
						left: left,
						top: $(this).offset().top + $(this).height() + 12,
						cursor: 'pointer',
						display: 'none',
						width: options.width == 0 ? 'auto' : options.width+'px'
					})
					.html(options.html)
				    .addClass(options.flyoutClass)
				    .click(function(){
				        $(this).fadeOut(100,function(){$(this).remove();})
				    })
				    .appendTo('body')
				    .fadeIn();
				var d = this.hint.offset().left + this.hint.width() - $('body').width();
				if (d > 0) {
					this.hint.css('left', event.pageX - d);
				}
			}
		},
		function(event) {
			if (this.hint != null) {
//				this.hint.fadeOut(100,function(){$(this).remove();});
				$('div.'+options.flyoutClass).fadeOut(100,function(){$(this).remove();});
				this.hint = null;
			}
		});
	};
})(jQuery);

$.fn.extend({
    termifier: function(options) {
      options = $.extend({
        lookupResource: 'getTerm',
        flyoutClass: 'lookerUpperFlyout'
      },options);
      this.click(function(event){
        $.ajax({
          url: options.lookupResource,
          type: 'get',
          data: {term: this.innerHTML},
          dataType: 'html',
          success: function(data) {
            $('<div></div>')
              .css({
                position: 'absolute',
                left: event.pageX,
                top: event.pageY,
                cursor: 'pointer',
                display: 'none'
              })
              .html(data)
              .addClass(options.flyoutClass)
              .click(function(){
                $(this).fadeOut(1500,function(){$(this).remove();})
               })
              .appendTo('body')
              .fadeIn();
            }
          });
        });
      return this;
    }
  }
);
