$(document).ready( function() {
	$("#showMessage").hide();
	$("#showMessage").click(function() {
		$(this).slideUp();
	});
	
	$(".polozka > .polozkaTop").click(function() {
		var parentTag = $(this).parent();
		var findInsideTag = parentTag.find('div.polozkaObsah');
		if (findInsideTag.css("display") == 'none') {
			findInsideTag.slideDown();
			parentTag.addClass('grayBack', 2000);
		} else {
			findInsideTag.slideUp();
			parentTag.removeClass('grayBack', 2000);
		}
		findInsideTag.click(function() {
			var parentTag = $(this).parent();
			var findInsideTag = parentTag.find('div.polozkaObsah');
			if (findInsideTag.css("display") == 'none') {
				findInsideTag.slideDown();
				parentTag.addClass('grayBack', 2000);
			} else {
				findInsideTag.slideUp();
				parentTag.removeClass('grayBack', 2000);
			}
		});
	});
	
	$("#rozbalVse").click(function() {
		if ($(this).html() == "Rozbal vše") {
			$(this).html("Zabalit vše");
		} else {
			$(this).html("Rozbal vše");
		}
		
		$(".polozka > .polozkaTop").each(function () {
			var parentTag = $(this).parent();
			var findInsideTag = parentTag.find('div.polozkaObsah');
			if (findInsideTag.css("display") == 'none') {
				findInsideTag.slideDown();
				parentTag.addClass('grayBack', 2000);
			} else {
				findInsideTag.slideUp();
				parentTag.removeClass('grayBack', 2000);
			}
			findInsideTag.click(function() {
				var parentTag = $(this).parent();
				var findInsideTag = parentTag.find('div.polozkaObsah');
				if (findInsideTag.css("display") == 'none') {
					findInsideTag.slideDown();
					parentTag.addClass('grayBack', 2000);
				} else {
					findInsideTag.slideUp();
					parentTag.removeClass('grayBack', 2000);
				}
			});
		});
	});
})

function showMessage(text) {
	$("#showMessage").html(text);
	$("#showMessage").slideDown();
	setInterval('hideMessage()',3000);
}

function hideMessage() {
	$("#showMessage").slideUp();
}

function addZajem(id, aukce) {
	$.ajax({
		method: "get",url: '/ajax/zajem/'+id+'/'+aukce+'/',data: "",
		beforeSend: function() {
			showMessage('Ukládám...');
		}, 
		success: function(html){
			if (html == 'error') {
				showMessage('Chyba při přidávání');
			} else if (html == 'already') {
				showMessage('O tuto položku jste již zájem projevil. Děkujeme');
			} else {
				showMessage('Děkujeme za zájem');
			}
		}, 
		error: function() {
			showMessage('Chyba při přidávání');
		}
	});
}

function deleteZajem(id, polozka) {
	$.ajax({
		method: "get",url: '/ajax/delzajem/'+id+'/',data: "",
		beforeSend: function() {
			showMessage('Mažu...');
		}, 
		success: function(html){
			if (html == 'error') {
				showMessage('Chyba při mazání');
			} else {
				$('#polozka'+polozka).remove();
				$('#delete'+polozka).remove();
				showMessage('Zájem vymazán');
			}
		}, 
		error: function() {
			showMessage('Chyba při mazání');
		}
	});
}
