
// Definition de la classe 'Tooltip'
// NB: L'ordre des methodes est important
function Tooltip(id)
{
	// Proprietes publiques
	this.Id = id;
	this.HtmlObject = $("#" + id);
	this.DisableFade = false;

	// Champs et methodes privees
	var _this = this;
	var _contentLeft = this.HtmlObject.find(".TooltipContentLeft");
	var _contentRight = this.HtmlObject.find(".TooltipContentRight");
	var _contentUpLeft = this.HtmlObject.find(".TooltipContentUpLeft");
	var _contentUpRight = this.HtmlObject.find(".TooltipContentUpRight");
	var _tooltipLeft = this.HtmlObject.find("#" + id + "Left");
	var _tooltipRight = this.HtmlObject.find("#" + id + "Right");
	var _tooltipUpLeft = this.HtmlObject.find("#" + id + "UpLeft");
	var _tooltipUpRight = this.HtmlObject.find("#" + id + "UpRight");
	var _offsetProvider = null;

	this.SetText = function(text)
	{
		_contentLeft.html(text);
		_contentRight.html(text);
		_contentUpLeft.html(text);
		_contentUpRight.html(text);
	}

	this.SetOffsetProvider = function(provider)
	{
		_offsetProvider = provider;
	}
	
	var ApplyMode = function(x, y, mode, offset)
	{
		var width = _this.HtmlObject.width();
		var height = _this.HtmlObject.height();
		var position = new Object();
		
		// La flèche est-elle à gauche ou à droite, en bas ou en haut ?
		// => elle est en haut à gauche par défaut
		
		if (mode && mode == "upright")
		{
			_tooltipUpLeft.hide();
			_tooltipUpRight.show();
			_tooltipLeft.hide();
			_tooltipRight.hide();
			position.top = (y + offset.Top + 32 - height) + "px";
			position.left = (x + offset.Left - width - 20) + "px";
		}
		else if (mode && mode == "upleft")
		{
			_tooltipUpLeft.show();
			_tooltipUpRight.hide();
			_tooltipLeft.hide();
			_tooltipRight.hide();
			position.top = (y + offset.Top + 32 - height) + "px";
			position.left = (x + offset.Left) + "px";
		}
		else if (mode && mode == "right")
		{
			_tooltipUpLeft.hide();
			_tooltipUpRight.hide();
			_tooltipLeft.hide();
			_tooltipRight.show();
			position.top = (y + offset.Top - 15) + "px";
			position.left = (x + offset.Left - width - 20) + "px";
		}
		else // if (mode == "left")
		{
			_tooltipUpLeft.hide();
			_tooltipUpRight.hide();
			_tooltipLeft.show();
			_tooltipRight.hide();
			position.top = (y + offset.Top - 15) + "px";
			position.left = (x + offset.Left) + "px";
		}
		
		_this.HtmlObject.css(position);
	}
	
	var CorrectOverflow = function(x, y, mode, offset)
	{
		var width = _this.HtmlObject.width();
		var height = _this.HtmlObject.height();
		if (x + width > $(document.body).width())
		{
			mode = "right";
			ApplyMode(x, y, mode, offset);
		}
		if (y + height > $(document.body).height())
		{
			mode = "up" + mode;
			ApplyMode(x, y, mode, offset);
		}
	}

	this.Show = function(x, y, mode)
	{
		var bodyClass = document.body.className;
		// _this.DisableFade = (bodyClass == "IE6" || bodyClass == "IE7" || bodyClass == "IE8");
			
		var offset = { Top: $(document.body).scrollTop(), Left: 0 };
		if (_offsetProvider)
			offset = _offsetProvider();
		
		// On essaie d'abord dans le mode passé en paramètre
		if (!mode)
			mode = "left";
		ApplyMode(x, y, mode, offset);
		
		// Si on sort de l'écran on flip le tooltip
		CorrectOverflow(x, y, mode, offset);
		
		if (_this.DisableFade)
			_this.HtmlObject.css("opacity", '').show();
		else
			_this.HtmlObject.show().stop().fadeTo("normal", 1.0);
	}

	this.Hide = function()
	{
		if (_this.DisableFade)
			_this.HtmlObject.hide();
		else
			_this.HtmlObject.stop().fadeTo("normal", 0.0, function() { $(this).hide() });
	}
}

