function Init() {
	LightboxInit();
	KontaktEventsInit();
}

function Zaokraglij(co, ile) {
	li = Math.pow(10, ile);
	w = Math.round(co * li) / li;
	return w;
}

function LightboxInit() {
	$(".textAreaContent A").each(function(){
		if (UseLightbox($(this).attr('href'))) $(this).attr('name', 'UseLBox');
	});
	$(".textAreaContent A[name='UseLBox']").lightBox();
	$(".textAreaContent A[name='UseLBox']").attr("title", "Kliknij z boku zdjęcia aby zobaczyć następne.");
}

function UseLightbox(string) {
	if (string.indexOf('.jpg') > 0) return true;
	if (string.indexOf('.jpeg') > 0) return true;
	if (string.indexOf('.gif') > 0) return true;
	if (string.indexOf('.png') > 0) return true;
	if (string.indexOf('.bmp') > 0) return true;
	if (string.indexOf('.tif') > 0) return true;
	return false;
}

function KontaktEventsInit() {
//	alert($('.kontakt_tab .input').val());
	$('.tab_kontakt .input').each(function(){
		if (($(this).val() == 'undefined') || ($(this).val().length == 0)) $(this).val($(this).attr('title'));
		$(this).focus(function(){
			if ($(this).val() == $(this).attr('title')) $(this).val('');
		});
		$(this).blur(function(){
			if (($(this).val() == 'undefined') || ($(this).val().length == 0)) $(this).val($(this).attr('title'));
		});
	});
	$('.tab_kontakt .textarea').each(function(){
		if (($(this).text() == 'undefined') || ($(this).text().length == 0)) $(this).text($(this).attr('title'));
		$(this).focus(function(){
			if ($(this).text() == $(this).attr('title')) $(this).text('');
		});
		$(this).blur(function(){
			if (($(this).text() == 'undefined') || ($(this).text().length == 0)) $(this).text($(this).attr('title'));
		});
	});
	
	$('.tab_kontakt .butt').click(function(){
		m = $(".tab_kontakt input[name='mail']").val();
		if (/^[a-zA-Z0-9.\-_]+@[a-zA-Z0-9\-.]+\.[a-zA-Z]{2,4}$/.test(m)) {
			nazwisko = $(".tab_kontakt input[name='nazwisko']").val();
			telefon = $(".tab_kontakt input[name='telefon']").val();
			tresc = $(".tab_kontakt textarea[name='tresc']").text();
			$('#formAlert').html('<span style="color: gray;">Proszę czekać...</span>');
			$.ajax({
						type: "POST",
						url : "ajax/sendMail.ajax.php",
						data: "email=" + m + '&nazwisko=' + nazwisko + '&telefon=' + telefon + '&tresc=' + tresc,
						success: function(odp) {$('#formAlert').html(odp);},
						error: function () {$('#formAlert').html('<span style="color: red;">Wystąpił błąd podczas wysyłania maila!</span>');}
			});
		} else alert('Błędny adres e-mail!');
	});

	
}

if (window.addEventListener) {
	window.addEventListener("load", Init, false);
}else if (window.attachEvent) {
	window.attachEvent("onload", Init);
}
