/**
 * jquery.simpletip 1.3.1. A simple tooltip plugin
 * 
 * Copyright (c) 2009 Craig Thompson
 * http://craigsworks.com
 *
 * Licensed under GPLv3
 * http://www.opensource.org/licenses/gpl-3.0.html
 *
 * Launch  : February 2009
 * Version : 1.3.1
 * Released: February 5, 2009 - 11:04am
 */
(function($){

	function Simpletip(elem, conf)
	{
		var self = this;
		elem = jQuery(elem);
		var tooltip = elem.find('.glossarydescription');
		
		if(!conf.hidden) tooltip.css('display', 'block');
		else tooltip.css('display', 'none');
		
		/* HYP: persistent is true */
		elem.click(function(event)
		{
			if(event.target === elem.get(0))
			{
				if(tooltip.css('display') !== 'none')
					tooltip.css('display', 'none');
				else
					tooltip.css('display', 'block');
			};
		});
		
		jQuery(document).mousedown(function(event)
		{
			if(tooltip.css('display') !== 'none')
			{
				var check = (conf.focus) ? jQuery(event.target).parents('.glossarydescription').andSelf().filter(function(){ return (this === tooltip.get(0) || this === tooltip.parent().get(0)) }).length : 0;
				if(check === 0) tooltip.css('display', 'none');
			};
		});
	};

	jQuery.fn.simpletip = function(conf)
	{
		// Check if a simpletip is already present
		var api = jQuery(this).eq(typeof conf == 'number' ? conf : 0).data("simpletip");
		if(api) return api;
		
		// Default configuration
		var defaultConf = {
			content: 'A simple tooltip',
			persistent: true,
			focus: true,
			hidden: true
		};
		jQuery.extend(defaultConf, conf);
		
		this.each(function()
		{
			var el = new Simpletip(jQuery(this), defaultConf);
			jQuery(this).data("simpletip", el);
		});
		
		return this;
	};
})();
