/**
 * AJAX Nette Framwork plugin for jQuery
 *
 * @copyright  Copyright (c) 2009 Jan Marek
 * @license    MIT
 * @link       http://nettephp.com/cs/extras/jquery-ajax
 * @version    0.1
 */
 
var jQueryData;

jQuery.extend({
	updateSnippet: function(id, html) {
		switch (id.replace(/^_+/, '')) {
		case 'foo':
			$('#' + id).animate({ textIndent: '800px' }, 500, 'linear', function () {
				$(this).html(html).animate({ textIndent: 0 }, 600, 'swing');
			});
			break;
		default:
			$('#' + id).html(html);
		}
	},

	netteCallback: function(data) {
		jQueryData = data;

		// redirect
		if (data.redirect) {
			window.location.href = data.redirect;
		}

		// snippets
		if (data.snippets) {
			for (var i in data.snippets) {
				jQuery.updateSnippet(i, data.snippets[i]);
			}
		}
	}
});


jQuery.ajaxSetup({
	success: function (data) {
		jQuery.netteCallback(data);
	},

	dataType: "json"
});



$(function() {
	// apply AJAX unobtrusive way
	$("a.ajax").live("click", function(event) {

		$(this).parent('tr').slideUp('normal');
		
		$.get(this.href);

		// show spinner
		$('<div id="ajax-spinner"></div>').css({
			position: "absolute",
			left: event.pageX + 20,
			top: event.pageY + 20

		}).ajaxStop(function() {
			$(this).remove();

		}).appendTo("body");
		
		return false;
	});
});

