
var adminUI = function() {
	return {
		
		self: this,
		
		

		init: function() {
			this.fadeOutFlashes();
			this.filterFieldStyling();
			
			$('a.ajax').live('click', function (event) {
				event.preventDefault();

				var itemTitle = $('.title', $(this).parents('tr')).text();
				
				if ($(this).hasClass('delete') && !confirm('Opravdu chcete smazat položku "' + itemTitle + '"?')) {
					return false;
				}
				
				$.get(this.href, function(data) {

					// premisteni editacniho formulare na misto editovane polozky 
					if (data.editId) {
						$('#editWrapper').insertAfter('h2').hide();
						$('.tempWrapper').remove();
						$('tr.items').show();
						$('#editWrapper').insertAfter('#item' + data.editId).wrap('<tr class="tempWrapper"><td colspan="4"></td></tr>').slideDown(50);
						$('#item' + data.editId).hide();
					}

					// zobrazeni formulare pro pridani zbozi
					if (data.callback == 'add') {
						$('#addWrapper').slideDown(50);
					}

					// oznameni o smazani
					if (data.callback == 'deleted') {
						alert(data.message);
					}

					jQuery.nette.success(data);
					naradiasistUI.requireMarks();
				}, 'json');
			});
			
			$('form.ajax :submit').livequery('click', function (event) {
				event.preventDefault();
				
				$(this).ajaxSubmit(function (data) {
					var data = data;

					if (data.callback == 'saved') {
						$('#item' + data.itemData.id).css('background-color', '#def0b2');
					}

					// skryti formularu
					$('#addWrapper').slideUp(300);
					$('#editWrapper').slideUp(300, function() {
						$(this).insertAfter('h2');
						$('.tempWrapper').remove();
						$('tr.items').show();
						if (data.callback == 'saved') {
							alert(data.message);
						}
					});

					jQuery.nette.success(data);
				});
			});
			
			// live editace nazvu zbozi
			$('.editable').click(function() {
				if ($(this).hasClass('in-use')) {
					return;
				}

				$(this).addClass('in-use')
					.html(jQuery('<input/>').attr('type', 'text').addClass('ajaxtext').val($(this).text()));
					
				$('input[type=text].ajaxtext').trigger('focus')
					.bind('blur change keypress', function(e) {
						if (e.which && e.which != 13) return;
						var parent = $(this).parent('span');
						$(this).replaceWith($(this).val());
						// DOKONCIT
						/*$.get(this.href, {
							
						});*/
						parent.removeClass('in-use');
				});
			});
			             
			// live editace nazvu zbozi
			/*$('tr.items.expandable').click(function() {
				
			}*/

			$('tr.items').mouseenter(function() {
				$(this).css('background-color', '#f2f2f2');
			}).mouseleave(function() {
				$(this).css('background-color', 'transparent');
			});
			
			$('.items.expandable').click(function() {
				$(this).next('tr.order-details').toggleClass('hidden');
			});

		},
		
		
		
		fadeOutFlashes: function() {
			setTimeout(function() {
				$('.flash').fadeOut(750);
			}, 4000);
		},


		filterFieldStyling: function() {
			var text = 'Filtrovat…';
			$('#q').attr('value', text)
				.css('color', '#777')
				.bind('focus', function() {
					if ($(this).attr('value') != text)
						return;
					$(this).attr('value', '').css('color', '#000');
				})
				.bind('blur', function() {
					if ($(this).attr('value'))
						return;
					$(this).attr('value', text).css('color', '#777');
				});
			$('#search form').bind('submit', function() {
				if ($('#q').attr('value') == '' || $('#q').attr('value') == text) {
					alert('Zadejte ' + text);
					return false;
				}
			});
		}

	};
}();

