/**
 * Obsuga formularza dodawania komentarzy do obrazków galerii
 */
function galleryCommentFormInit() {

	$("#pp_gallery_add_comment input.submit").click(function() {

		var text = $("#pp_gallery_add_comment div.text textarea").val();
		var name;

		if (text.replace(/\s/g, "") == "") {
			jAlert('Podaj treść komentarza', 'Błąd');
			return;
		}

		jPrompt('Podaj imię:', '', 'Dodaj komentarz do zdjęcia', function(r) {
			if (r.replace(/\s/g, "") == "") {

				jAlert('Podaj swoje imię', 'Błąd');
				return;
			} else {
				name = r;

				id = $.prettyPhoto.getImageId();
				
				// dodawanie komentarza
				// ladowanie komentarzy
				$.ajax( {
					url : "/ajax/runner.php?act=galleryComments",
					cache : false,
					type : "POST",
					data : ( {
						id : id,
						name : name,
						text : text
					}),
					dataType : "html",
					success : function(msg) {
						if (msg == 1) {
							jAlert('Komentarz zosta dodany!', '');
							
							$.prettyPhoto.loadComments();
							
							galleryCommentFormHide();
						} else {
							jAlert(msg, 'Błąd');
						}
					},
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						jAlert('Wystąpił błąd podczas dodawania komentarza');
					}

				});
			}
		});
	});

	$("#pp_gallery_add_comment input.cancel").click(function() {
		galleryCommentFormHide();
	});

	$("#pp_gallery_add_comment div.text .activator").click(function() {
		$(this).hide();
		$("#pp_gallery_add_comment div.text textarea").show();
		$("#pp_gallery_add_comment input.submit").show();
		$("#pp_gallery_add_comment input.cancel").show();
		
		$.prettyPhoto.fitScreen();
		
		$("#pp_gallery_add_comment div.text textarea").focus();
	});

}

function galleryCommentFormHide() {

	$("#pp_gallery_add_comment div.text textarea").val('');

	$("#pp_gallery_add_comment div.text textarea").hide();
	$("#pp_gallery_add_comment input.submit").hide();
	$("#pp_gallery_add_comment input.cancel").hide();

	$("#pp_gallery_add_comment div.text .activator").show();
	
	$.prettyPhoto.fitScreen();
}
