/**
 * Copyright (c) 2008-2009 Wydawnictwo Bauer Sp. z o.o., Sp.k. All Rights Reserved
 * Author: Krzysztof Kozlowski, http://www.kozik.net.pl
 *
 * $Id: func_jquery.js 3382 2009-04-08 12:17:31Z kkozlowski $
 * @file
 * @see JQuery
 */

/**
 * AJAX Tooltip - show tooltip with data from AJAX GET request. Usage in HTML:
\code
	<a href="REAL_URL" class="ajax_tooltip" label="ACTION_IDNUM" title="Optional title">Shit happens</a>
\endcode
 */
$(document).ready(function() {
	offset_x = 15 ; // From cursors position
	offset_y = 15 ; // From cursors position
	$(".ajax_tooltip").each(function() {
		$(this).hover(
			function(e) {
				// Display tooltip with temporary text
				$("body").append('<div id="tooltip">Wczytuję...</div>') ;
				$("#tooltip")
					.css("top", (e.pageY + offset_y) + "px")
					.css("left", (e.pageX + offset_x) + "px")
					.css("position", "absolute")
					.fadeIn("fast") ;
				url_data_s = $(this).attr("label") ;
				// Try to split label for URL params
				if (url_data_s)
				{
					url_data_a = url_data_s.split('_') ;
					if (url_data_a.length == 2)
					{
						// URL params OK, GET it
						// url = "/ajax.php?action=" + url_data_a[0] + "&id=" + url_data_a[1] ;
						$.get("/ajax.php", {
							"action": url_data_a[0],
							"id": url_data_a[1]
							}, function (data, text_status) {
								$("#tooltip").html(data) ;
							});
						// We are done, exit
						return ;
					}
				}
				// Ooops, no label with URL? Other error? Bah...
				if ($(this).attr("title"))
				{
					$("#tooltip").html($(this).attr("title")) ;
				}
				else
				{
					$("#tooltip").html("Brak danych...") ;
				}
		    },
			function() {
				$("#tooltip").remove() ;
		    }
		);
	});
	$("a.ajax_tooltip").mousemove(
		function(e) {
			$("#tooltip")
				.css("top", (e.pageY + offset_y) + "px")
				.css("left", (e.pageX + offset_x) + "px") ;
		}
	);
});
