/**
 * jquery.tooltips 1.0.
 * 
 * Copyright (c) 2011 Colin Mackie
 * http://www.colinmackie.com
 *
 * Licensed under GPLv3
 * http://www.opensource.org/licenses/gpl-3.0.html
 *
 * Version : 1.0
 * Released: 2011-01-24
 */

(function($) {

	var m_removeTagExp = /<\/?[^>]+>/gi;
	function removeTags(s)
	{
		s = s.replace(m_removeTagExp, "");
		return s;
	}

	var is_safari = (navigator.userAgent.toLowerCase().indexOf("safari") != -1);

	var methods =
	{
	
		// init function
		init : function(options)
		{
			var settings =
			{
				color: "#333333"
			};

			return this.each(function ()
			{
				var prop;
				
				if (options)
				{
					$.extend(settings, options);
				}
				
				var $this = $(this);
				var data = $this.data("tooltip");
				
				if (!data)
				{
					$(this).data("tooltip", {
						target : $this
					});
					data = $this.data("tooltip");
					
					// set settings
					if (settings.width)
					{
						$this.css("width", settings.width + "px");
					}
					if (settings.height)
					{
						$this.css("height", settings.height + "px");
					}
					var width = $this.width();
					var height = $this.height();

					// get the id				
					var id = $this.attr("id");
					if (!id)
					{
						id = "" + new Date().getTime();
						$this.attr("id", id);
					}

					// set the class and add the settings
					$this.addClass("tooltip");
					$this.css("cursor", "pointer");

					data["min-height"] = height;
					data.id = id;
					
					$this.bind({
						mouseover: function(e)
						{
							var $tip = $("#" + id + "_tooltip");
							
							// set the text
							var t = $(e.target);
							var title = removeTags(t.html());
							var text = (title.length != 0 ? "<strong>" + title + "</strong><br />" : "");
							text += t.attr("tooltip");
							
							$tip.html(text);
							
							var x = t.offset().left + t.outerWidth();
							var y = t.offset().top;

							// now set the right styles
							$tip.css("left", x + "px");
							$tip.css("top", y + "px");

							// show
							$tip.show();
						},
						mouseout: function (e)
						{
							$("#" + id + "_tooltip").hide();
						}
					});

					$("body").append("<div id=\"" + id + "_tooltip\" class=\"tooltip-panel\" style=\"position:absolute; display:none; width:200px; text-align:left; margin:5px; padding:5px; border:solid 1px #333; background-color:#ffffcc; color:#333; font-size:11px; z-index:999; border-radius:5px; -moz-border-radius:5px; -webkit-border-radius:5px; \"> </div>");
				}
			});
		},
		
		// seek method
		show : function()
		{
			var $this = $(this);
			var data = $this.data("tooltip");
			
				
			return $this;
		},
		
		isInit : function()
		{
			var $this = this;
			var data = $this.data("tooltip");
			return (data != null);
		},
		
		// destory method
		destroy : function() 
		{
			return this.each(function()
			{
				var $this = this;
				var data = $this.data("tooltip");
				data.tooltip.remove();
				$this.removeData("tooltip");
			});
		}
		
	};
  	
	// call the appropriate method
  $.fn.tooltip = function(method)
  {
		if (methods[method])
		{
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		}
		else if (typeof method === 'object' || ! method)
		{
			return methods.init.apply(this, arguments);
		}
		else
		{
			$.error('Method ' +  method + ' does not exist on jQuery.tooltip');
		}
  };
 
})(jQuery);

