$(document).ready(function() {
	resizeFooter();
	$(window).resize(resizeFooter);
	
	
	$("#newsletter-email").focus(function() {
		if($(this).val() == "Wpisz adres e-mail") $(this).val("");
	}).blur(function() {
		if($(this).val() == "") $(this).val("Wpisz adres e-mail");
	});
	
	var isSending = false;

	var trigger = $("a[rel=#newsletter-dialog]").overlay({ 
	    expose: { 
	        color: '#333', 
	        loadSpeed: 200, 
	        opacity: 0.9 
	    }, 
	    closeOnClick: false 
	});

	$("a[rel=#newsletter-dialog]").click(function() {
		if(!isSending) {
			isSending = true;
			$.ajax({
				url: "/newsletter/zapisz?format=html",
				type: "POST",
				data: "email=" + $("#newsletter-email").val(),
				beforeSend: function() {
					$("#newsletter-dialog .content").html("Zapisuję do bazy newslettera");
				},
				success: function(html) {
					$("#newsletter-dialog .content").html(html);
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$("#newsletter-dialog .content").html('<span style="color: red">Wystąpił błąd podczas zapisywania Twojego adresu e-mail do newslettera</span>');
				},
				complete: function() {
					isSending = false;
				}
			});
		}
		return false;
	});
	
	$("#newsletter-dialog .close a").click(function() {
	    trigger.overlay().close(); 
		return false;
	});
	
	$(".confirm").click(function() {
		if(confirm("Usunąć wybrany wiersz?")) {
			window.location.href = $(this).attr("rel");
		}
		return false;
	});
	
	if($(".mini-gallery").length > 0) {
		// select the thumbnails and make them trigger our overlay
		$(".mini-gallery a").overlay({
			target: '#gallery',
			expose: '#f1f1f1'
		}).gallery({
			speed: 800
		});
	}
});

function resizeFooter()
{
	var footerH = $("#footer").height();
	var footerPos = $("#footer").position();
	var windowH = $(window).height();

	if(footerH > 82 || windowH > (footerH + footerPos.top)) {
		var delta = windowH - footerH - footerPos.top;
		$("#footer").css("height", (footerH + delta));
	}
}
