function Init() {

	LightboxInit('#Content');
	EventsInit();
	
	$('#FormularzKontaktowy TEXTAREA').each(function(){
	var obj = $(this).parent().prev();
	obj.css('vertical-align', 'top');
	obj.css('padding-top', '5px');	
	});
	

	/*$('#Karuzela').each(function(){
		InsertFlash('fla/karuzela.swf', 533, 270, 'Karuzela');
	});*/
	
	$('#KaruzelaFull').each(function(){
		InsertFlash('fla/karuzela2.swf', 930, 471, 'KaruzelaFull');
	});
	
	$('#DystrybucjaFlash').each(function(){
		InsertFlash('fla/mapka.swf?pid=6', 550, 400, 'DystrybucjaFlash');
	});
	
	$('#TopBody').each(function(){
		InsertFlash('fla/top.swf', 970, 272, 'TopBody');
	});
	
	$('#PolecamyBox').each(function(){
		InsertFlash('fla/promo.swf', 175, 110, 'PolecamyBox');
	});
	
	$('#Map').each(function(){
		MapLoad('Map');
	});
	
	$('#calendar-container').each(function(){
	  function dateChanged(calendar) {
	      var y = calendar.date.getFullYear();
	      var m = calendar.date.getMonth() + 1;
	      var d = calendar.date.getDate();
			if (d < 10) d = '0' + d;
			if (m < 10) m = '0' + m;
			var dt = y + '.' + m + '.' + d;
			$('INPUT[name="data"]').val(dt);
	  };

	  Calendar.setup(
	    {
	      flat         : "calendar-container", // ID of the parent element
	      flatCallback : dateChanged           // our callback function
	    }
	  );
	});

	$('#Karuzela').each(function(){
		var col_1_height = parseInt($('#ContentLeft').height());
		var col_2_height = parseInt($('#ContentCenter').height());
		var col_3_height = parseInt($('#ContentRight').height());
		var maks = Math.max(col_1_height, col_2_height);
		var maks = Math.max(maks, col_3_height);
		
		var ls = $('#ContentLeft').children('.separator');
		ls.css('margin-top', (parseInt(ls.css('margin-top')) + (maks - col_1_height)) + 'px');
		
		var ls = $('#ContentCenter').children('.separator');
		ls.css('margin-top', (parseInt(ls.css('margin-top')) + (maks - col_2_height)) + 'px');
		
		var ls = $('#ContentRight').children('.separator');
		ls.css('margin-top', (parseInt(ls.css('margin-top')) + (maks - col_3_height)) + 'px');
	});
	
	$('#Potwierdzenie').each(function(){
		NewAlert($(this).attr('name'));
		$('.alertBtn').click(function(){$('#AlertBox').css('display', 'none');});
	});
	
}

function NewAlert(txt, btns) {
	var aBox = $('#AlertBox');
	if (!btns) btns = Array('Ok');
	aBox.html('<div id="Alert"><p>' + txt + '</p></div>');
	if (btns.length > 0) {
		$('#Alert').append('<div id="AlertBtns"></div>');
		var aBtns = $('#AlertBtns');
		for (i = 0; i < btns.length; i++) {
			aBtns.append('<div class="alertBtn" name="' + i + '">' + btns[i] + '</div>');
		}
	}
	$('#AlertBox').css('display', 'block');
	$('#AlertBox').css('left', '-4000px');
	var alertHeight		= $('#Alert').height();
	var screenSize			= getScreenSize();
	var scrollPosition	= getScrollXY();
	var alertY = (parseInt(screenSize[1] / 2) - parseInt(alertHeight / 2)) + parseInt(scrollPosition[1]);
	$('#Alert').css('margin-top', alertY + 'px');
	$('#AlertBox').css('left', '0px');
}

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=6';
												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=6';
												else alert(odp);
			}, error	: function() {
								alert('Błąd komunikacji!');
			}
		});
	});
	
	$('.but').click(function(){
		if ($(this).next().hasClass('selSub')) {
			var obj = $(this).next();
			if (parseInt($(this).attr('name')) == 0) {
				obj.css('display', 'block');
				$('.but[name="1"]').click();
				$(this).attr('name', 1);
			} else {
				obj.css('display', 'none');
				$(this).attr('name', 0);
			}
		}
	});
	
	function InputColor(obj){
		if (obj.val() == obj.attr('name')) obj.css('background-position', 'top');
							  					else obj.css('background-position', 'bottom');
	}
	
	$('.inp').click(function(){
		if ($(this).val() == $(this).attr('name')) $(this).select();
	});
	
	$('.inp').blur(function(){
		if ($(this).val().length == 0) $(this).val($(this).attr('name'));
		InputColor($(this));
	});
	
	$('.selSub A').click(function(){
		if ($(this).text() != '--brak--') {
														$(this).parent().prev().prev().val($(this).text());
														InputColor($(this).parent().prev().prev());
													 } else {
													 	$(this).parent().prev().prev().val($(this).attr('name'));
														InputColor($(this).parent().prev().prev());
													 }
		
		$(this).parent().prev().click();
	});
	
	$('.bt1').click(function(){
		$(this).parent().parent().submit();
	});
	
	$('#PrzypominaczBtn').click(function(){
		var email 	= $('.emailText').val();
		var date		= $('INPUT[name="data"]').val();
//		if (!CheckRegex(email ,'email')) alert('Musisz podać prawidłowy adres e-mail!');
		if (!CheckRegex(email ,'email')) {
			NewAlert('Musisz podać prawidłowy adres e-mail!');
			$('.alertBtn').click(function(){
				var ret = parseInt($(this).attr('name'));
				$('#AlertBox').css('display', 'none');
			});
		} else {
				if (!CheckRegex(date ,'data2')) {
					NewAlert('Musisz wybrać datę przypomnienia!');
					$('.alertBtn').click(function(){
						var ret = parseInt($(this).attr('name'));
						$('#AlertBox').css('display', 'none');
					});
				} else {
//						if (confirm('Przypomnienie zostanie wysłane na adres ' + email + '.\r\nOtzrymasz je 14 dni przed wydarzeniem, które ma nastąpić ' + date + '.\r\n\r\nCzy potwierdzasz chęć otrzymania przypomnienia?')) {
						NewAlert('Przypomnienie zostanie wysłane na adres <b>' + email + '</b>.<br>Otrzymasz je w dniu <b>' + date + '</b>.<br><br>Czy potwierdzasz chęć otrzymania przypomnienia?', Array('Potwierdzam', 'Rezygnuję'));
						$('.alertBtn').click(function(){
							var ret = parseInt($(this).attr('name'));
							$('#AlertBox').css('display', 'none');
							if (ret == 0) {
								$.ajax({
									type		: "POST",
									url		: "ajax/przypomnienieSave.ajax.php",
									data		: "email=" + email + "&date=" + date,
									success	: function(odp){
													if (odp == 'ok') {
														NewAlert('Zlecenie zostało przyjęte.');
														$('.alertBtn').click(function(){
															var ret = parseInt($(this).attr('name'));
															$('#AlertBox').css('display', 'none');
														});
													} else alert(odp);
									}, error	: function() {
														NewAlert('Wystąpił błąd komunikacji!');
														$('.alertBtn').click(function(){
															var ret = parseInt($(this).attr('name'));
															$('#AlertBox').css('display', 'none');
														});
									}
								});
							}
						});
//						};
					}
			 }
	});
	
	$('.buyoutBtn').click(function(){
		var id = $(this).attr('name');
		$.ajax({
			type		: "POST",
			url		: "ajax/cartAdd.ajax.php",
			data		: "id=" + id + "&il=1",
			success	: function(odp){
								dane = odp.split('|');
								if (dane[0] == 'ok') {
									$('.cartIlosc').text(dane[1]);
									$('.cartWartosc').text(dane[2]);
									alert('Produkt został dodany do koszyka!');
//									document.location.reload();
								} else alert(odp);
			}, error	: function(){
								alert('Błąd komunikacji!');
			}
		});
	});
	
	$('.cartRemoveBtn').click(function(){
		var id = parseInt($(this).parent().parent().attr('name'));
		if (confirm('Czy na pewno chcesz usunąć ten produkt z koszyka?')) {
			$.ajax({
				type		: "POST",
				url		: "ajax/cartRemove.ajax.php",
				data		: "id=" + id,
				success	: function(odp){
									if (odp == 'ok') dlh('index.php?pid=cart');
													else alert(odp);
				}, error	: function(){
									alert('Błąd komunikacji!');
				}
			});
		}
	});
	
	$('INPUT[name="sendBtn"]').click(function(){
		if (($('INPUT[name="osoba"]').val().length == 0) ||
			($('INPUT[name="adres"]').val().length == 0) ||
			($('INPUT[name="kod"]').val().length == 0) ||
			($('INPUT[name="miejscowosc"]').val().length == 0) ||
			($('INPUT[name="email"]').val().length == 0)) alert('Nie wypełniono wszystkich wymaganych pól!');
			else {
				if (!CheckRegex($('INPUT[name="email"]').val() ,'email')) alert('Błędny format adresu e-mail!');
				else {
					$('.zamowienie').submit();
				}
			}
	});
	
	$('#FormularzZamBtn').click(function(){
		var obj = $('#FormularzZam');
		if (obj.attr('name') == "0") {
			obj.attr('name', '2');
			obj.animate({height: 'show', opacity: 'show'}, "slow", null, function(){obj.attr('name', '1')});
		} else if (obj.attr('name') == "1") {
			obj.attr('name', '2');
			obj.animate({height: 'hide', opacity: 'hide'}, "slow", null, function(){obj.attr('name', '0')});
		}
	});
	
	$('#FormularzZapBtn').click(function(){
		var obj = $('#FormularzZap');
		if (obj.attr('name') == "0") {
			obj.attr('name', '2');
			obj.animate({height: 'show', opacity: 'show'}, "slow", null, function(){obj.attr('name', '1')});
		} else if (obj.attr('name') == "1") {
			obj.attr('name', '2');
			obj.animate({height: 'hide', opacity: 'hide'}, "slow", null, function(){obj.attr('name', '0')});
		}
	});
	
	$('INPUT[name="zapBtn"]').click(function(){
		var productId	= parseInt($('INPUT[name="productId"]').val());
		var email		= $('INPUT[name="zapEmail"]').val();
		var telefon		= $('INPUT[name="zapTelefon"]').val();
		var tresc		= $('TEXTAREA[name="zapTrescZapytania"]').val();
		if (CheckRegex(email, 'email')) {
			$.ajax({
				type		: "POST",
				url		: "ajax/zapytanieSend.ajax.php",
				data		: "id=" + productId + "&email=" + email + "&telefon=" + telefon + "&tresc=" + tresc,
				success	: function(odp){
									if (odp == 'ok') {
										NewAlert('Twoje zapytanie zostało wysłane.');
										$('.alertBtn').click(function(){
											$('#AlertBox').css('display', 'none');
											dlh('index.php?pid=3&idp=' + productId);
										});
									} else {
										NewAlert(odp);
										$('.alertBtn').click(function(){$('#AlertBox').css('display', 'none');});
									}
				}, error	: function(){
									NewAlert('Błąd komunikacji!');
									$('.alertBtn').click(function(){$('#AlertBox').css('display', 'none');});
				}
			});
		} else {
			NewAlert('Adres e-mail ma błędny format!');
			$('.alertBtn').click(function(){$('#AlertBox').css('display', 'none');});
		}
		
	});
	
	$('#SubmitBtn').click(function(){
		var productId	= parseInt($('INPUT[name="productId"]').val());
		var ilosc		= $('INPUT[name="zamIlosc"]').val();
		var imie			= $('INPUT[name="zamImie"]').val();
		var nazwisko	= $('INPUT[name="zamNazwisko"]').val();
		var adres		= $('INPUT[name="zamAdres"]').val();
		var miasto		= $('INPUT[name="zamMiasto"]').val();
		var kod			= $('INPUT[name="zamKod"]').val();
		var telefon		= $('INPUT[name="zamTelefon"]').val();
		var email		= $('INPUT[name="zamEmail"]').val();
		var uwagi		= $('TEXTAREA[name="zamUwagi"]').val();
		
		var dane = "id=" + productId + "&ilosc=" + ilosc + "&imie=" + imie + "&nazwisko=" + nazwisko + "&adres=" + adres + "&miasto=" + miasto + "&kod=" + kod + "&telefon=" + telefon + "&email=" + email + "&uwagi=" + uwagi;
			if ((imie.length * nazwisko.length * adres.length * miasto.length * kod.length * telefon.length * email.length * parseInt(ilosc)) > 0) {
				if (CheckRegex(email, 'email')) {
					if (CheckRegex(kod, 'kodpocztowy')) {
						$.ajax({
							type		: "POST",
							url		: "ajax/zamowienieStep1.ajax.php",
							data		: dane,
							success	: function(odp) {
//												if (odp == 'ok') dlh('index.php?pid=3&idp=' + productId + '&s2');
												if (odp == 'ok') $('FORM').submit();
																else {
																	NewAlert(odp);
																	$('.alertBtn').click(function(){$('#AlertBox').css('display', 'none');});
																}
							}, error	: function() {
												NewAlert('Błąd komunikacji!');
												$('.alertBtn').click(function(){$('#AlertBox').css('display', 'none');});
							}
						});
					} else {
						NewAlert('Błędny format kodu pocztowego!');
						$('.alertBtn').click(function(){$('#AlertBox').css('display', 'none');});
					  }
				} else {
					NewAlert('Proszę podać poprawny adres e-mail!');
					$('.alertBtn').click(function(){$('#AlertBox').css('display', 'none');});
				  }
			} else {
				NewAlert('Nie wypełniono wszystkich pól!');
				$('.alertBtn').click(function(){$('#AlertBox').css('display', 'none');});
			}
	});
	
	$('#FormularzZamBtn').click(function(){
		dlh('index.php?pid=3&idp=' + $(this).attr('name') + '&s2');
	});
	
	function Rozwin(obj, chk) {
		if (obj.attr('name') == '0') {
			chk.attr('checked', true);
			obj.attr('name', '2');
			obj.animate({height: 'show', opacity: 'show'}, "slow", null, function(){obj.attr('name', '1')});
		} else if (obj.attr('name') == '1') {
			chk.attr('checked', false);
			obj.attr('name', '3');
			obj.animate({height: 'hide', opacity: 'hide'}, "slow", null, function(){obj.attr('name', '0')});
		} else if (obj.attr('name') == '2') {
			chk.attr('checked', true);
		} else if (obj.attr('name') == '3') {
			chk.attr('checked', false);
		}
	}
	
	$('.checkboxDod').click(function(){
		var obj = $(this).parent().next();
		Rozwin(obj, $(this));
	});
	
	$('.dodatek .top SPAN').click(function(){
		var obj = $(this).parent().next();
		Rozwin(obj, $(this).prev());
	});
	
	$('INPUT[name="acceptRules"]').click(function(){
		if ($(this).attr('checked')) $('#SubmitBtn').attr('disabled', false);
										else $('#SubmitBtn').attr('disabled', true);
	});
/*	
	$('.emailText').click(function(){
		NewAlert('Sed suscipit pulvinar sem, ut pellentesque justo venenatis tempor. Ut vestibulum tellus neque, non hendrerit nisi. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;', Array('Tak', 'Nie', 'Może'));
		$('.alertBtn').click(function(){
			var ret = parseInt($(this).attr('name'));
			$('#AlertBox').css('display', 'none');
			if (ret == 0) alert('zero');
			else if (ret == 1) alert('jeden');
			else if (ret == 2) alert('dwa');
		});
	});
*/
}

$(document).ready(function(){
	$('#slider').jFader({
		slideClass: "SlidePage",
		naviClass: "SlideNavi",
		naviActClass: "act",
		easing: "easeInOutExpo",
		delay: 4500,
		animationTime: 3000,
		navi: false	
	}); 
})

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