function Init() {
//	InsertFlash('fla/d20.swf', 980, 324, 'Flash');
	$('#FlashHome_2In').each(function(){
		InsertFlash('fla/slogan.swf', 884, 123, 'FlashHome_2In');
	});
	$('#FlashHome').each(function(){
		InsertFlash('fla/bigflash.swf', 884, 345, 'FlashHome');
	});
	$('#FlashSub').each(function(){
		InsertFlash('fla/smallflash.swf', 884, 161, 'FlashSub');
	});
	LightboxInit('#Content');
	EventsInit();
	
	if ($('#Menu').attr('name') == 'full') {
		cl = $('#ContentLeft').height();
		cr = $('#ContentRight').height();
		if (cl < cr) $('#Menu').height(cr - 70);
	}
	
	$('#FormularzKontaktowy TEXTAREA').each(function(){
	var obj = $(this).parent().prev();
	obj.css('vertical-align', 'top');
	obj.css('padding-top', '5px');
	});
	
	$('#DystrybucjaFlash').each(function(){
		InsertFlash('fla/mapka.swf?pid=' + $('#Pid').val(), 550, 400, 'DystrybucjaFlash');
	});
	
	var maxMiniOutside = 0;
	$('.fotoMiniOutside').each(function(){
		if (maxMiniOutside < parseInt($(this).height())) maxMiniOutside = parseInt($(this).height());
	});
	$('.fotoMiniOutside').css('height', maxMiniOutside + 'px');
}

function Zaokraglij(co, ile) {
	li = Math.pow(10, ile);
	w = Math.round(co * li) / li;
	return w;
}

	// ---=========================---
	// ---=== Obsługa LightBoxa ===---
	// ---=========================---

function LightboxInit() {
	function in_array(n, h, s) {
	    var f = false, k, s = !!s;
	    for (k in h) {
	        if ((s && h[k] === n) || (!s && h[k] == n)) {
	            f = true;
	            break;
	        }
	    }
	    return f;
	}
	function UseLightbox(string) {
		string = string.toLowerCase();
		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;
	}
	$("A[rel='']").each(function(){
		if (UseLightbox($(this).attr('href'))) $(this).attr('rel', 'lightbox[643r1dth3f67id53hr]');
	});
	
	var lightboxTab = Array();
	$("A[rel^='lightbox']").each(function(){
		var v = $(this).attr('rel');
		if (!in_array(v, lightboxTab)) lightboxTab[lightboxTab.length] = v;
	});
	for (i = 0; i < lightboxTab.length; i++) {
		$("A[rel='" + lightboxTab[i] + "']").lightBox();
		$("A[rel='" + lightboxTab[i] + "']").attr("title", "Kliknij z boku zdjęcia aby zobaczyć następne.");		
	}
}

	// ---========================---

function CheckRegex(str, typ) {
	if (typ == 'godzina')    var regex = /^[0-9]{1,2}[/:]{1}[0-9]{1,2}$/;
	else if (typ == 'data')  var regex = /^[0-9]{4}(-[0-9]{2}){2}$/;
	else if (typ == 'email') var regex = /^[a-zA-Z0-9.\-_]+@[a-zA-Z0-9\-.]+\.[a-zA-Z]{2,4}$/;
	else return false;
	if (regex.test(str) == true) return true;
	else return false;
}

function InsertFlash(fla, w, h, div) {
	var so = new SWFObject(fla, "mymovie", w, h, "8", "transparent");
   so.addParam("wmode", "transparent");
   so.write(div);
}

function EventsInit() {
	$('#FormularzKontaktowy INPUT[type="button"]').click(function(){
		var adr = $('SELECT[name="adres"]').val();
		var imi = $('INPUT[name="imie"]').val();
		var ema = $('INPUT[name="email"]').val();
		var tem = $('SELECT[name="temat"]').val();
		var tre = $('TEXTAREA[name="tresc"]').val();
		if ((imi.length > 0) && (ema.length > 0) && (tem.length > 0) && (tre.length > 0)) {
			if (CheckRegex(ema, 'email')) {
				$.ajax({
					type: "POST",
					url : "ajax/sendMail.ajax.php",
					data: "adr=" + parseInt(adr) + "&imi=" + imi + "&ema=" + ema + "&tem=" + tem + "&tre=" + tre,
					success: function(odp) {
						if (odp == 'ok') alert('Wiadomość została wysłana.');
						else alert('BŁĄD PODCZAS WYSYŁANIA!\n' + odp);
					},
					error  : function() {
						alert('Wystąpił błąd podczas komunikacji!');
					}
				});
			} else alert('Błędny format adresu e-mail!')
		} else alert('Nie wszystkie pola zostały wypełnione!');
	});
	
	$('#Newsletter .submit').click(function(){
		var nl_email = $('#Newsletter .pole').val();
		if (CheckRegex(nl_email, 'email')) $(this).parent().submit();
												else alert('Wpisz prawidłowy adres e-mail');
	});
	
	$('#Send').hover(function(){
		$(this).css('background-position', 'bottom');
	}, function(){
		$(this).css('background-position', 'top');
	});
	
	$('#ZalogujBtn').click(function(){
		var l = $('INPUT[name="login"]').val();
		var p = $('INPUT[name="pass"]').val();
		$.ajax({
			type		: "POST",
			url		: "ajax/log.ajax.php",
			data		: "l=" + l + "&p=" + p,
			success	: function(odp) {
								if (odp == 'ok') document.location.href = 'index.php?pid=5';
												else alert(odp);
			},	error	: function() {
								alert('Błąd komunikacji!');
			}
		});
	});
	
	$('#WylogujBtn').click(function(){
		$.ajax({
			type		: "POST",
			url		: "ajax/logout.ajax.php",
			data		: "",
			success	: function(odp) {
								if (odp == 'ok') document.location.href = 'index.php?pid=5';
												else alert(odp);
			}, error	: function() {
								alert('Błąd komunikacji!');
			}
		});
	});
}
if (window.addEventListener) {
	window.addEventListener("load", Init, false);
}else if (window.attachEvent) {
	window.attachEvent("onload", Init);
}