/**
 * 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
 */

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]);
			}
		}

		if (data.title) {
			document.title = data.title;
		}
	}
});

jQuery.ajaxSetup({
	success: jQuery.netteCallback,
	dataType: "json"
});

/**
 * 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 () {
    $("a.ajax").live("click", function (event) {
        $.get(this.href);
		if ($(this).hasClass('scrollTop'))
			$.scrollTo('h2:first', 1000, { margin: true });
		event.preventDefault();
    });
});

// form submit

$("form.ajax").livequery("submit", function () {
	$(this).ajaxSubmit();
	return false;
});

$("form.ajax :submit, form .ajax:submit").livequery("click", function () {
	$(this).ajaxSubmit();
	return false;
});

/**
 * Common AJAX setup
 */

// ajax spinner
$(function () {
	$('<div id="ajax-spinner"></div>').hide().ajaxStart(function () {
		$(this).show();
	}).ajaxStop(function () {
		$(this).hide();
	}).appendTo("body");
});

// prolínací efekt při updatu snippetu
if (!jQuery.browser.msie) {
	jQuery.extend({
		updateSnippet: function (id, html) {
			if (id.substr(0, 6) != 'dialog') {
				$("#" + id).fadeTo("fast", 0.3, function () {
					$(this).html(html).fadeTo("fast", 1);
				});
			} else {
				$("#" + id).html(html);
			}
		}
	});
}

// skrývání flash zpráviček
/*$("div.flash").livequery(function () {
	var el = $(this);
	setTimeout(function () {
		el.animate({"opacity": 0}, 2000);
		el.slideUp();
	}, 7000);
});*/

