var Za = {
    // ajax
    ajax: function (url) {
        // spinner
        if ($('#ajax_spinner').length == 0) $('<span id="ajax_spinner"></span>').appendTo('body');
        $('#ajax_spinner').css({
            top: Math.round($(window).height()/2) + $('html, body').scrollTop(),
            left: Math.round($(window).width()/2) + $('html, body').scrollLeft()

        }).ajaxStop(function() {
            $(this).remove();
        });

        $.get(url, function (payload) {
            $.nette.success(payload);
            $('#ajax_spinner').remove();
        });
        return false;
    },
	// zobrazeni dialogoveho okna
    dialog: function (text, url, callback_close) {
		if ($('#dialog').length > 0) $('#dialog').remove();
		$('body').append('<div id="dialog"></div>');

        if (text != null) $('#dialog').append('<p class="dialog">'+text+'</p>');
        if (url != null) $('#dialog').load(url);

		$('#dialog').dialog({
			resizable: true,
			draggable: true,
			position: 'center',
			width: 400,
			title: 'Info',
			close: function(){ if ($.isFunction(callback_close)) callback_close.apply(); }
		});
        return false;
	},
	// zobrazeni alert okna
    alert: function (text, callback_close) {
		if ($('#dialog_cl').length > 0) {$('#dialog_cl').remove();}
		$('body').append('<div id="dialog_cl"></div>');
		$('#dialog_cl').append('<p class="dialog_alert"><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 50px 0;"></span>'+text+'</p>');
		$('#dialog_cl').dialog({
			modal: true,
			resizable: false,
			draggable: false,
			position: 'center',
			width: 400,
			title: 'Upozornění',
			buttons: {
				'Ok': function(){
					$(this).dialog('close');
				}
			},
			close: function(){ if ($.isFunction(callback_close)) callback_close.apply(); }
		});
	},
	// zobrazeni confirm okna
    confirm: function (text, callback_ok, callback_storno) {
		if ($('#dialog_cl').length > 0) {$('#dialog_cl').remove();}
		$('body').append('<div id="dialog_cl"></div>');
		$('#dialog_cl').append('<p class="dialog_confirm"><span class="ui-icon ui-icon-info" style="float:left; margin:0 7px 50px 0;"></span>'+text+'</p>');
		$('#dialog_cl').dialog({
			modal: true,
			resizable: false,
			draggable: false,
			position: 'center',
			width: 400,
			title: 'Upozornění',
			overlay: {
				backgroundColor:'#FFFFFF',
				opacity:0.8
			},
			buttons: {
				'Ok': function(){
					$(this).dialog('close');
					if ($.isFunction(callback_ok)) callback_ok.apply();
				},
				'Storno': function(){
					$(this).dialog('close');
					if ($.isFunction(callback_storno)) callback_storno.apply();
				}
			}
		});
	}
}
$(function(){
    // menu top hover effect
    $('#menu_top li').hover(
        function(){
            $(this).parent().addClass($(this).attr('id') + '_bg');
            if ($(this).children().attr('id') != 'menu_top_active') {
                $('#menu_top_active').css('color', '#3A3A3A');
            }
        },
        function(){
            $(this).parent().removeClass('menu_top_li_1_bg menu_top_li_2_bg menu_top_li_3_bg');
            if ($(this).children().attr('id') != 'menu_top_active') {
                $('#menu_top_active').css('color', '#FFFFFF');
            }
        }
    );
    // registrace confirm dialogu na data-confirm atribut
    $.fn.extend({
        triggerAndReturn: function (name, data) {
            var event = new $.Event(name);
            this.trigger(event, data);
            return event.result !== false;
        }
    });
    $('a[data-confirm], button[data-confirm], input[data-confirm]').live('click', function(e) {
         var el = $(this);
         if (el.triggerAndReturn('confirm')) {
            // nejdrive se zakaze vykonani ajaxu
            e.preventDefault();
            e.stopImmediatePropagation();
            // volani confirm
            Za.confirm(el.attr('data-confirm'), function() {
                if (el.hasClass('ajax')) /*$.get(el.attr('href'));*/Za.ajax(el.attr('href')); else window.location.href = el.attr('href');
            });
            return false;
         }
         return false;
     });
	// zaveseni zpracovani pres ajax na css tridu ajax
    // pozor! musi byt az za confirm, jinak se nezrusi vykonani ajaxu pri confirmu
    $('a.ajax').live('click', function (event) {
        event.preventDefault();
        //$.get(this.href);
        return Za.ajax(this.href);
    });
    // autocomplete do hledani
    $('#frmsearchForm-q').autocomplete({
			source: function(request, response) {
				$.ajax({
					url: '/pozadavky/poz-autocomplete/' + $('#frmsearchForm-q').val(),
                    data: '',
					dataType: 'json',
					success: function(data) {
						response($.map(data, function(item) {
							return {
								//label: item.ID_POZ + ' - ' + ((item.NAZEV.length > 35) ? item.NAZEV.substr(0, 35) + '...' : item.NAZEV),
                                label: item.ID_POZ + ' - ' + item.NAZEV,
								value: item.ID_POZ
							}
						}));
					}
				});
			},
			minLength: 3,
			select: function(event, ui) {
                $('#frmsearchForm-q').val(ui.item.value);
                $('#frm-searchForm').submit();
			}
    });
});
