/**
 * AJAX Nette Framwork plugin for jQuery
 *
 * @copyright  Copyright (c) 2009, 2010 Jan Marek
 * @copyright  Copyright (c) 2009, 2010 David Grudl
 * @license    MIT
 * @link       http://nette.org/cs/extras/jquery-ajax
 */

/*
if (typeof jQuery != 'function') {
	alert('jQuery was not loaded');
}
*/

(function($) {

	$.nette = {
		success: function(payload)
		{
			// redirect
			if (payload.redirect) {
				window.location.href = payload.redirect;
				return;
			}

			// state
			if (payload.state) {
				$.nette.state = payload.state;
			}

			// snippets
			if (payload.snippets) {
				for (var i in payload.snippets) {
					$.nette.updateSnippet(i, payload.snippets[i]);
				}
			}
		},

		updateSnippet: function(id, html)
		{
			$('#' + id).html(html);
		},

		// create animated spinner
		createSpinner: function(id)
		{
			return this.spinner = $('<div></div>').attr('id', id ? id : 'ajax-spinner').ajaxStart(function() {
				$(this).show();

			}).ajaxStop(function() {
				$(this).hide().css({
					position: 'fixed',
					left: '50%',
					top: '50%'
				});

			}).appendTo('body').hide();
		},

		// current page state
		state: null,

		// spinner element
		spinner: null
	};


})(jQuery);

$.handler = function(event,href){
	if ($.active) return;
	
	$.post(href, $.nette.success);

	/*$.nette.spinner.css({
		position: 'fixed',
		left: event.pageX,
		top: event.pageY
	});*/
};

$.alertWin = function(msg,event,href) {
	jConfirm(msg, 'Potvrzovací okno', function(result) {
		if(result) {
			$.handler(event,href);
		} else {
			return false;
		}
	});
}

$(function($) {

	$.ajaxSetup({
		success: $.nette.success,
		dataType: 'json'
	});

	//$.nette.createSpinner();

	// apply AJAX unobtrusive way
	$('a.ajax').live('click', function(event) {
		event.preventDefault();
		if ($(this).attr('data-confirm') != null) {
			$.alertWin($(this).attr('data-confirm'),event,this.href);
		} else {
			$.handler(event,this.href);
		}
	});

});

$.nette.updateSnippet = function(id, html) {
	$("#" + id).fadeTo('fast', 0.01, function(){
		$(this).html(html).fadeTo('fast', 1);
	});
};
