/**
 * 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
 */
/* toto budu moci smazat - nize je verze 0.2 a vypada to ze funguje
jQuery.extend({
	updateSnippet: function (id, html) {
		$("#" + id).html(html);
	},

	netteCallback: function (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]);
			}
		}
		// chtelo by to nejaky callback, pokud bude vice udalosti
		$('#folderContainer > ul').tabs();
	}
});

jQuery.ajaxSetup({
	success: jQuery.netteCallback,
	dataType: "json"
});
*/

jQuery.extend({
    nette: {
        updateSnippet: function (id, html) {
            $("#" + id).html(html);
        },
        success: function (payload) {
            // redirect
            if (payload.redirect) {
                window.location.href = payload.redirect;
                return;
            }
            // snippets
            if (payload.snippets) {
                for (var i in payload.snippets) {
                    jQuery.nette.updateSnippet(i, payload.snippets[i]);
                }
            }
        }
    }
});

jQuery.ajaxSetup({
    success: jQuery.nette.success,
    dataType: "json",
    error: function (xhr, status, thrownError){
		
		jQuery.noticeAdd({
	        text: '<h3>Fatální chyba</h3>' + 
	        	  '<p><b>Status:</b> '+xhr.status+'<br />' + 
	        	  '<b>Typ chyby:</b> '+xhr.statusText+'<br />' +
	        	  '<b>Text:</b> '+xhr.responseText+'</p>' +
	        	  '<div class="info">Chybová zpráva byla zaznamenána a zaslána administrátorovi. Požádejte administrátora o prověření a následné odstranění této chyby. Omlouváme se za způsobené problémy.</div>',
	        stay: true
		});
	}
});



/**
 * AJAX form plugin for jQuery
 *
 * @copyright  Copyright (c) 2009 Jan Marek
 * @license    MIT
 * @link       http://nettephp.com/cs/extras/ajax-form
 * @version    0.1
 */

jQuery.fn.extend({
	ajaxSubmit: function (callback) {
		var form;
		var sendValues = {};

		// submit button
		if (this.is(":submit")) {
			form = this.parents("form");
			sendValues[this.attr("name")] = this.val() || "";

		// form
		} else if (this.is("form")) {
			form = this;

		// invalid element, do nothing
		} else {
			return null;
		}

		// validation
		if (form.get(0).onsubmit && !form.get(0).onsubmit()) return null;

		// get values
		var values = form.serializeArray();

		for (var i = 0; i < values.length; i++) {
			var name = values[i].name;

			// multi
			if (name in sendValues) {
				var val = sendValues[name];

				if (!(val instanceof Array)) {
					val = [val];
				}

				val.push(values[i].value);
				sendValues[name] = val;
			} else {
				sendValues[name] = values[i].value;
			}
		}

		// send ajax request
		var ajaxOptions = {
			url: form.attr("action"),
			data: sendValues,
			type: form.attr("method") || "get"
		};

		if (callback) {
			ajaxOptions.success = callback;
		}

		return jQuery.ajax(ajaxOptions);
	}
});


$(function() {
	// apply AJAX unobtrusive way
	$("a.ajax").live("click", function(event) {
		$.get(this.href);
		return false;
	});
});

