(function($){
	
	tooltip_disapear = function(id) { $('div#tooltip_' + id).hide(); };
	
	//REMON EDIT
	$( 'div.tooltip_container' )
		.live(
			'mouseover' ,
			function()
			{
				$( this ).hide();
			}
		);
	
	$.fn.tooltip = function(opts)
	{
		
		var defaults = {
			max_width : null,
			offset : {
				top : 10,
				left : 10
			},
			img_path : '/templates/default/images/tooltip.png',
			dom : null,
			pause : 1000
		};
		
		var options = $.extend(defaults,opts);
		
		return this.each(function(){
			
			// console.info( this );
			
		    var	$this = $(this),
				id = Math.floor(Math.random() * 999999),
		    	tip = $('<div class="tooltip_container" id="tooltip_' + id + '" style="display: none;">' +
							'<img alt="|\" src="' + options.img_path + '" class="tooltip_image"/>' +
							'<div class="tooltip_content" />' +
						'</div>'),
				content = tip.children('.tooltip_content'),
      			html = $this.html(),
				dom = $(options.dom);
				
			
			$this.data('html', html);
			$this.data('timeout', null);
			
			//console.info(tip, dom);
			
			tip
				.hover(function() {
					if ($this.data('timeout')) { clearTimeout($this.data('timeout')); }
				},
				function() {
					$this.data('timeout', setTimeout('tooltip_disapear(' + id + ');', options.pause));
				});
			
			$("body").append(tip);
			
			if (options.max_width > 0)
			{
				tip.css({ maxWidth : options.max_width + 'px' });
			}
			
			// console.info (dom);
			
			if (dom.size() > 0)
			{
				dom.remove();
				$this.data('html', dom);
			}

		    /* Mouse over and out functions*/  
		    $this.hover(
				function( e ) {
					if ($this.data('timeout')) { clearTimeout($this.data('timeout')); }
					content.html($this.data('html'));
					//console.info('current tip:', tip);
					
					$('.tooltip_container').hide();
					
					var	
						offset = $this.offset(),
						Left = offset.left,
						Top = offset.top,
						Width = $this.width(),
						Height = $this.height();
					
					tip
						.css({
								//top : Top + Height + options.offset.top + 'px',
								//left : Left + Width + options.offset.left + 'px'
								top: e.pageY + options.offset.top + 'px',
								left: e.pageX + options.offset.left + 'px'
							})
						.show();
				},   
				function() {
					$this.data('timeout', setTimeout('tooltip_disapear(' + id + ');', options.pause));
					//console.info('timeout set');
				}
			)
			.bind('mousemove', function(e){
					tip
						.css({
								//top : Top + Height + options.offset.top + 'px',
								//left : Left + Width + options.offset.left + 'px'
								top: e.pageY + options.offset.top + 'px',
								left: e.pageX + options.offset.left + 'px'
							});
				}
		
			);
		
		});
		
	}
	
	/* Infobox */
    $(".tooltip, .infobox").each(function(){
    	$(this).tooltip({
    		max_width : 350,
    	    dom : $(this).next("ul"),
    		pause : 0
    	});
    });
	
})(jQuery);
