/*
	anebogin@gmail.com
	2011
*/

function ToolTip( selector, options )
{
	var self = this;
	var obj = null;
	var objTooltip = null;
	var canBeHidden = true;
	
	this.options = {
		'path': '/',
		'text': '',
		'class': 'tooltip',
		'align_to_object': null
	};
	
	// replace options
	for( option in options )
	{
		this.options[option] = options[option];
	}
	
	var showAlert = function( txt )
	{
		alert( txt );
	};
	
	var show = function( event )
	{
		var t = ( self.options['align_to_object'] && $(self.options['align_to_object']).length > 0 )? $(self.options['align_to_object']) : $(event.currentTarget);
		objTooltip.removeClass( 'tooltip_corner_n' );
		objTooltip.removeClass( 'tooltip_corner_s' );
		var sumTop = t.offset().top;
		var t1 = sumTop + t.height() + 5;
		var t2 = sumTop - 16 - objTooltip.height();
		var top = t2;
		if( t2 >= 0 )
		{
			objTooltip.addClass( 'tooltip_corner_s' );
		}
		else
		{
			top = t1;
			objTooltip.addClass( 'tooltip_corner_n' );
		}
		objTooltip.css({
			'left': (t.offset().left+t.width()/2-objTooltip.width()/2) +'px',
			'top': top +'px'
		});
		objTooltip.css( 'opacity', 1 );
		objTooltip.stop().fadeIn( function() {
		});
	};
	
	var ifParentClassExists = function( target, css )
	{
		var retval = false;
		while( target != null )
		{
			if( target.className && target.className.indexOf(css) >= 0 )
			{
				retval = true;
				break;
			}
			target = target.parentNode;
		}
		return( retval );
	};
	
	var hide = function( event )
	{
		if( canBeHidden )
		{
			//objTooltip.hide();
			objTooltip.fadeOut( function() {
			});
		}
	};
	
	this.remove = function() {
		objTooltip.fadeOut( function() {
			objTooltip.remove();
		});
	};
	
	var checker = function()
	{
		var thisTimer;
		thisTimer = setInterval( function()
		{
			if( !obj.context )
			{
				remove();
				clearInterval( thisTimer );
			}
		}, 500);
	};
	
	var init = function( selector )
	{
		if( $(selector).length > 0 )
		{
			obj = $(selector);
			obj.bind( 'mouseenter', function(event) {
				canBeHidden = false;
				var thisTimer;
				thisTimer = setInterval( function()
				{
					show( event );
					clearInterval( thisTimer );
				}, 100);
			} );
			obj.bind( 'mouseout', function(event) {
				canBeHidden = true;
				var thisTimer;
				thisTimer = setInterval( function()
				{
					hide( event );
					clearInterval( thisTimer );
				}, 200);
			} );
			if( !objTooltip )
			{
				objTooltip = $('<div class="tooltip_container"><div class="tooltip_content"><div class="'+ self.options['class'] +'">'+ self.options.text +'</div></div></div>');
				$(document.body).append( objTooltip );
			}
			checker();
			objTooltip.bind( 'mouseenter', function(event) {
				canBeHidden = false;
			} );
			objTooltip.bind( 'mouseout', function(event) {
				if( !ifParentClassExists(event.relatedTarget,'tooltip_container') )
				{
					canBeHidden = true;
					var thisTimer;
					thisTimer = setInterval( function()
					{
						hide( event );
						clearInterval( thisTimer );
					}, 200);
				}
			} );
		}
		else
		{
			showAlert( 'element not found by selector: '+ selector );
		}
	};
	
	init( selector );
};

