/*
 *
 * Copyright (c) 2006 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 */

 
/*
 * jQuery ToolTip Demo. Demo of how to add elements and get mouse coordinates
 *	There is also a ToolTip plugin found at http://interface.eyecon.ro/,
 *	  which uses a CSS class to style the tooltip, but shows it below the input/anchor, rather than where the mouse is
 *
 *
 * @name     ToolTipDemo
 * @param    bgcolour  Background colour
 * @param    fgcolour  Foreground colour (i.e. text colour)
 * @author   Sam Collett (http://www.texotela.co.uk) and Jack (longsoft3000@gmail.com) 
 * @example  $("a,input").ToolTipDemo('#fff');
 *
 */
$.fn.ToolTip = function(bgcolour, fgcolour,imgurl)
{
	this.mouseover(
		function(e)
		{
			if((!$(this).attr("tip_title") && !this.title && !this.alt) && !this.tooltipset) return;
			// get mouse coordinates
			// based on code from http://www.quirksmode.org/js/events_properties.html
			var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
			var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop : 0);
			mouseX += 5;
			mouseY += 5;
			bgcolour = bgcolour || "#E6FFE6";
			fgcolour = fgcolour || "#000";
			//imgurl = imgurl || "/scripts/jquery-tooltip/smile.gif";
			if(imgurl) //Jack add
			    showimage = "<img src='" + imgurl + "' border='0' />";
			else
				showimage = "";
			// if there is no div containing the tooltip
						
			if(!this.tooltipdiv)
			{
				// create a div and style it
				var div = document.createElement("div");
				this.tooltipdiv = div;
				$(div).css(
				{
					border: "1px solid #00B800",
					font:   "12px Tahoma",
					padding: "2px",
					paddingLeft: "4px",
					paddingRight: "4px",		
					backgroundColor: bgcolour,
					color: fgcolour,
					position: "absolute"
				})
				// add the title/alt attribute to it
				.html(showimage + ( $(this).attr("tip_title") || this.title || this.alt ));
				this.title = "";
				this.alt = "";
				$("body").append(div);
				this.tooltipset = true;
			}
			//$(this.tooltipdiv).show().css({left: mouseX + "px", top: mouseY + 3 + "px"});
			$(this.tooltipdiv).fadeIn("normal").css({left: mouseX + "px", top: mouseY + 3 + "px"});
		}
	).mouseout(
		function()
		{
			if(this.tooltipdiv)
			{
				//$(this.tooltipdiv).hide();
				$(this.tooltipdiv).fadeOut("normal"); 
			}
		}
	);
	return this;
}
