/* -------------------------------------------------------------
 *	@Project: Frankfurt Airport City
 *	@Author: Florian Ludwig (triplesense.de)
 *	@Created: 2008-11-20
 *	@Last edited: Felix Toth, 2009-04-22
 *
 *	Usage:
 *		> Initialise:
 *		$("a.tooltipp").tooltipp();
 *		
 *		> Tooltipp using title:
 *		<a class="tooltipp" href="" title="Ich bin ein Beschreibungstext"></a>
 *		<a class="tooltipp" href="" title="Ich bin eine Headline||Ich bin ein Beschreibungstext"></a>
 *		
 *		> Tooltipp using image from href:
 *		<a class="tooltipp" href="imagefile.jpg" rel="image"></a>
 *		
 *		> Tooltipp using innerHTML from following DIV:
 *		<a class="tooltipp" href="" rel="innerHTML"></a><div class="tooltipp-content"><p>Ich bin der anzuzeigende Tooltipp-Content</p></div>
 *
 * ------------------------------------------------------------- */

jQuery.fn.tooltipp = function()
{
	return this.each(
		function()
		{
			var el = $(this);
			var title = $(this).attr("title");
			el.bind(
					"mouseover",
					function(e)
					{
						var innerContent;
						
						switch ( el.attr("rel") )
						{
							case "innerHTML":
								innerContent = el.next().html();
							break;
							case "image":
								tt_href = el.attr("href");
								innerContent = '<img src="' + tt_href + '" />';
							break;
							default:
								var tt_title = el.attr("title");
							 	var tt_desc;
								
								if (tt_title.indexOf("||") != -1) tt_desc = tt_title.substring(tt_title.indexOf("||") + 2);
								if ( tt_desc != undefined && tt_desc != "" ) {
									innerContent = '<h3>' + tt_title.substring(0, tt_title.indexOf("||")) + '</h3>';
									innerContent += '<p>' + tt_desc + '</p>';
								} else {
									innerContent = '<p>' + tt_title + '</p>';
								}
							break;
						}
						
						// stop here, if there is no content to show
						if ( !innerContent ) return false;
						
						// add tooltipp-holder
						$("body").append('<div id="tooltipp"></div>');
						var holder = $("#tooltipp");
											
						// show tooltipp
						holder.append(innerContent)
							.css("top", (e.pageY - 10) + "px")
							.css("left", (e.pageX + 20) + "px")
							.fadeIn("fast")
						
						// show in front of selects (IE)
						if ($.fn.bgiframe) { holder.bgiframe(); }
						
						// limit width
						if ( holder.width() > 260 ) holder.css("width", 240);
					}
				).bind(
					"mouseout",
					function()
					{
						var holder = $("#tooltipp");
						holder.remove();
						//if ( title != "" ) el.attr("title", $(this).attr("rel"));
					}
				).bind(
					"mousemove",
					function(e)
					{
						var holder = $("#tooltipp");
						holder.css("top", (e.pageY - 10) + "px").css("left", (e.pageX + 20) + "px");
					}
				);
			
		}
	);
}