/*-------------------------------------------------------------------------------
	A Better jQuery Tooltip
	Version 1.0
	By Jon Cazier
	jon@3nhanced.com
	01.22.08
-------------------------------------------------------------------------------*/

$.fn.betterTooltip = function (options) {

	/* Setup the options for the tooltip that can be 
	accessed from outside the plugin              */
	var defaults = {
		speed: 200,
		delay: 300
	};

	var options = $.extend(defaults, options);

	/* Create a function that builds the tooltip 
	markup. Then, prepend the tooltip to the body */
	getTip = function () {
		var tTip =
			"<div class='tip'>" +
				"<div class='tipMid'>" +
				"</div>" +
				"<div class='tipBtm'></div>" +
			"</div>";
		return tTip;
	}
	$("body").prepend(getTip());

	/* Give each item with the class associated with 
	the plugin the ability to call the tooltip    */
	$(this).each(function () {

		var $this = $(this);
		var tip = $('.tip');
		var tipInner = $('.tip .tipMid');

		var tTitle = (this.title);
		this.title = "";

		var offset = $(this).offset();
		var tLeft = offset.left;
		var tTop = offset.top;
		var tWidth = $this.width();
		var tHeight = $this.height();

		/* Mouse over and out functions*/
		$this.hover(
			function () {
				// acording DE5238 we dont show tooltip when hover, but only show on mouse click
				// and hide on mouse out from control
				// showTip();
			},
			function () {
				tip.hide();
			}
		);

		$this.click(
			function (event) {
				var offset = $(this).offset();
				var tLeft = offset.left;
				showTip(tLeft);
			}
		);

		/* Position the tooltip relative to the class 
		associated with the tooltip                */
		setTip = function (top, left) {

			var topOffset = tip.height();
			var leftoffset = tip.width();

			var xTip;
			if (left - 60 < 0) {
				xTip = leftoffset / 2 - 150 + "px";

			} else {
				xTip = left - 60 + "px";
			}

			var yTip = (top - topOffset - 40) + "px";
			tip.css({ 'top': yTip, 'left': xTip });

		}

		showTip = function (x) {
			tipInner.html(tTitle);
			setTip(tTop, x);
			tip.animate({ "opacity": "toggle" }, defaults.speed);
		}

	});
};
