function Init() {

	LightboxInit('#Content');
	EventsInit();
	
	$('#FormularzKontaktowy TEXTAREA').each(function(){
	var obj = $(this).parent().prev();
	obj.css('vertical-align', 'top');
	obj.css('padding-top', '5px');	
	});
	
	$("#Menu .Exp").hover(function () {
		var t = $(this).find("ul").css({"display":"block"})
	},function () {
		var t = $(this).find("ul").css({"display":"none"})
	});	
	
	$('#FlashSub').each(function(){
		InsertFlash('fla/cerkomSub.swf', 990, 215, 'FlashSub');
	});
	
	$('#FlashZestawy').each(function(){
		InsertFlash('fla/cerkomZestawy.swf', 990, 215, 'FlashZestawy');
	});
	
	$('#FlashHP').each(function(){
		InsertFlash('fla/cerkom.swf', 758, 466, 'FlashHP');
	});
	
	$('#DystrybucjaFlash').each(function(){
		InsertFlash('fla/mapka.swf?pid=6', 550, 400, 'DystrybucjaFlash');
	});
	
/*	$('#ContentRight').each(function(){
		if ($(this).height() < $('#ContentLeft').height()) $(this).height($('#ContentLeft').height());
	});
	
*/

}

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=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();
	});
	
	$('#GdziekupicBtn').click(function(){
		var f_k  = $('INPUT[name="kontakt"]').val();
		var f_kp = $('INPUT[name="kodp"]').val();
		if ((f_k.length > 0) && (f_kp.length > 0)) $('#TabGdziekupic FORM').submit();
				else alert('Nie wypełniono wszystkich wymaganych pól!');
	});
}
if (window.addEventListener) {
	window.addEventListener("load", Init, false);
}else if (window.attachEvent) {
	window.attachEvent("onload", Init);
}

var actPlay = '';
$(document).ready(function(){
	
	if(self.parent.frames.length!=2) {
		$("#Player .close").remove();
	}else{
		var aid = self.parent.player.GiveId();
		if(aid.length>0) {
			SetPlayClass(aid)
			$("#"+actPlay).addClass('sm2_paused');
		}
	}
	
	$("#Player a").not('.close').click(function(){
		var mp3 = $(this).attr("href").split('/')[1];
		var id = $(this).attr("id");
		if(self.parent.frames.length!=2) {
			window.location = 'muzyka.php#'+id;
			return false;
		}else{
			var tc = $(this).attr("class")
			if(tc.indexOf('sm2_playing')!=-1) {
				$(this).removeClass('sm2_playing');
				$(this).addClass('sm2_paused');
				self.parent.player.StopPlay($(this).attr('id'));
				$("#"+$(this).attr('id')+" span").text("");
				$("#tu").text("");
				actPlay = '';
			}else{
				if(actPlay.length>0) {
					self.parent.player.StopPlay(actPlay);
					$("#"+actPlay+" span").text("");
					$("#tu").text("");
					$("#"+actPlay).removeClass('sm2_playing');
					$("#"+actPlay).addClass('sm2_paused');
				}
				$(this).addClass('sm2_playing')
				self.parent.player.GoPlay(id);
			}
			//
			
		}
		return false;
	});
	if(self.parent.frames.length!=2) {
		
		window.location = 'muzyka.php#'+$("#Player ul a:first").attr("id");
		return false;
	}
});

function Wylacz() {
	parent.location = self.parent.main.location
}
function SetPlayClass(id) {
	actPlay = id;
	$("#"+id).addClass('sm2_playing');
}
function SetPos(pos,dur) {
	$("#"+actPlay+" span").text( Sec2Min(Math.round(pos/1000))+'/'+Sec2Min(Math.round(dur/1000)) )
}
function Sec2Min(sec) {
	var m = Math.floor(sec/60);
	var se = sec-m*60;
	return (m>0?m+"m ":"")+se+"s";
}

