$(document).ready(function(){

	$("#main1 *").tooltip({
		showURL: false
	});

	setInterval(function() { pokaz_slidow(); }, 3500);

});

function trim(str) {
	a = str.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
}

function get_home_url(){
	return "http://"+window.location.host+"/";
}

function newsletter() {

	$("#newsletter_box").css("top", ($("#right").height()+1)+"px");

	if($("#newsletter_box").css("display") == "none") {
		$("#newsletter_box").load(get_home_url()+'newsletter.php', function() {
			$("#newsletter_box").slideDown();
		});
	} else $("#newsletter_box").slideUp();

}

function zapisz_email() {

	var email = trim($("#newsletter_form #email").val());

	if(email.length == 0) {
		alert("Proszę wpisać adres email.");
	} else if(!email.match(/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/)) {
		alert("Podany adres email jest nieprawidłowy.");
	} else {
		$("#newsletter_form input").attr("disabled", "disabled");
		$.post(get_home_url()+'newsletter.php', {'email':email},
			function(data) {
			$("#newsletter_form input").removeAttr("disabled");
			$("#newsletter_box").html(data);
			setTimeout("$('#newsletter_box').slideUp()", 5000);
		});
	}

}

function kalendarz(miesiac, rok) {

	var wys = $("#kalendarz_container").height();
	$("#kalendarz_container").html('<div style="height: '+(Math.floor(wys/2)+33)+'px; padding-top:'+(Math.ceil(wys/2)-33)+'px; text-align: center;"><img src="tpl/gfx/ajax-loader.gif" border="0" title="" alt="" /></div>');
	$.post(get_home_url()+'kalendarz.php', {'miesiac':miesiac,'rok':rok},
	function(data) {
		$("#kalendarz_container").html(data);
		$("#main1 *").tooltip({showURL: false});
	});

}

function pokaz_slidow() {

	if(document.getElementById("slideshow") != null) {

		var zmiana = false;
		if($("#slideshow img:first") != null) {
			var first = parseInt($("#slideshow img:first").attr("id").replace(/ssi/, ''));
		}
		if($("#slideshow img:last") != null) {
			var last = parseInt($("#slideshow img:last").attr("id").replace(/ssi/, ''));
		}

		if(first != null && last != null && first != last) {

			$("#slideshow img").each(function() {
				if($(this).css("display") != "none" && !zmiana) {
					zmiana = true;
					var act = parseInt($(this).attr("id").replace(/ssi/, ''));
					if(act < last) next = act+1;
					else next = 1;
					//~ $(this).fadeOut(1000, function() {
						//~ $("#ssi"+next).fadeIn(1000);
					//~ });
					$(this).fadeOut(2000);
					$("#ssi"+next).fadeIn(2000);
				}
			});
			zmiana = false;
		}

	}

}
