/* Version 1.0.2 */

(function($)
{
	$.fn.glossarize = function(options)
	{
		// Set default options
		var settings = {
			"datapath" : "glossary.xml",
			"bracket_wrap" : true,
			"hard_close" : true,
			"soft_close" : true,
			"close_text" : "Close",
			"global_link" : true,
			"link_href" : "#",
			"link_text" : "Go to glossary",
			"speed" : 400,
			"shadow_size" : 6
		};

		// Create variables
		var termReq = "";
		var termRel = "";
		var termPattern = "";
		var termName = "";
		var termDef = "";
		var termI = 0;
		var termHover = false;
		
		// Get if the browser is Chrome or not
		$.browser.chrome = ($.browser.webkit && /chrome/.test(navigator.userAgent.toLowerCase())) ? true : false;

		// Wrap the link in braces, to indicate functionality, if enabled
		if(settings.bracket_wrap===true)
		{
			$(this).each(function()
			{
				$(this).text("[" + $(this).text() + "]")	
			});
		}

		// Use onlick as the trigger for the entire script
		$(this).click(function(z)
		{
			// If the rel "glossarize: [value]" is in being used, use that as the target term instead of the links text node
			if($(this).attr("rel") && String($(this).attr("rel")).match(/^glossarize\:/i))
			{
				termReq = String($(this).attr("rel")).replace(/^glossarize\:\s*/,"");
			}

			// Remove wrapping brackets, if applicable, for the term request
			else
			{
				termReq = $(this).text().replace(/\[*|\]*/,"");
			}

			// Get the line-height of the clicked link, so we can adjust where the popup is located
			var termLH = Number($(this).css("line-height").replace("px", ""));

			// Get the location of the mouse for the popup
			var termX = z.pageX;
			var termY = z.pageY + termLH

			// Merge user-defined options with, and over-ride, default settings
			if(options) $.extend(settings, options);

			// If the speed setting is NOT a number, set it to the default value
			if(isNaN(settings.speed)) settings.speed = 150;

			// Load the XML
			$.ajax(
			{
				// Use "async: false" so the XML will get loaded by Safari
				type: "GET",
				async: false,
				url: settings.datapath,
				dataType: "xml",
				error: function(xml){},
				success: function(xml)
				{
					if($("#glossarize").length==0)
					{
						// Create the overlay container for our UI
						$('<div id="glossarize" style="display:block;overflow:hidden;position:absolute;top:' + termY + 'px' + ';left:' + termX + 'px' + '"></div>').appendTo($("body"));

						// Create the outer wrapper for tooltip
						$('<div class="wrapper"></div>').appendTo($("#glossarize"));

						// Set the opacity for the tooltip
						// Note: We're using opacity, since opacity ties into false queueing via animate
						$("#glossarize").css(
						{
							"opacity" : 0
						});

						// Create the inner wrapper for the tooltip
						$('<div class="interior"></div>').appendTo($("#glossarize .wrapper"));

						// Create the tooltip structure
						$('<h1 class="name"></h1><p class="definition"></p>').appendTo($("#glossarize .wrapper .interior"));

						// If global_link is on, append the link to the tooltip
						if(settings.global_link===true)
						{
							$('<a class="link" href="' + settings.link_href + '"><span>' + settings.link_text + '</span></a>').appendTo($("#glossarize .wrapper .interior"));				
						}

						// If hard_close is on, append the link to the tooltip
						if(settings.hard_close===true)
						{
							$('<a class="close"><span>' + settings.close_text + '</span></a>').appendTo($("#glossarize .wrapper .interior"));				
						}

						$(xml).find("term").each(function()
						{
							// Get term information
							termName = $(this).find("termName").text();
							termNameRegexp = $(this).find("termName").text().replace(" ", "\\s");
							termPattern = new RegExp("^" + termName + "$", "i");
							termDef = $(this).find("termDef").text();
							termI++;

							// The the requested term matches the current iterated term, insert that terms information
							if(termReq.match(termPattern))
							{
								$("#glossarize h1").html(termName);
								$("#glossarize p").html(termDef);
							}

							// If the popup is outside of the viewport, to the right, then position it inside the viewport
							if(($("#glossarize").position().left + $("#glossarize").outerWidth(true)) > $(window).width())
							{	
								$("#glossarize").css(
								{
									"left" : ($(window).width() - $("#glossarize").outerWidth(true) - settings.shadow_size) + "px"
								});
							}

							// Run the magic
							$("#glossarize").hide().each(function()
							{
								// Fade the tooltip in
								$(this).css(
								{
									"opacity" : 1
								}).fadeIn(settings.speed, function()
								{

								});

								// Hard close
								$(".close", this).click(function()
								{
									$("#glossarize").fadeOut(settings.speed, function()
									{
										$(this).remove();
									});
								});
							});
						});

						// If soft_close is on, enable the function to use it
						if(settings.hard_close===true)
						{
							// Determine if the user is hovering over the tooltip
							$("#glossarize").hover(function(){}, function()
							{
								// Soft close
								$(document).click(function()
								{
									$("#glossarize").fadeOut(settings.speed, function()
									{
										$(this).remove();
									});
								});
							});			
						}
					}
				}
			});

			// If the user is using Chrome, return the link as true (as the plugin won't work for them)
			if($.browser.chrome)
			{
				return true;
			}

			// If the user is NOT using chrome, return as false so the link doesn't open
			else
			{
				return false;
			}
		});
	}
})(jQuery);
