$(document).ready(function(){
	
	swfobject.embedSWF("/flash/banner_0"+(Math.round(Math.random()*5)+1)+".swf", "BannerPlace", "756", "337", "9.0.0", "expressInstall.swf", false, {	'menu': 'false', 'wmode': 'transparent' }, false);
	
	$('a.Rep,a.RepGB').each(function(){
		var e = $(this).attr("href").replace("/","").split(".html")[0]+"@"+$(this).attr("rel");
		$(this).attr({"href":"mailto:"+e});
		if($(this).attr('class').indexOf('RepGB')==-1) $(this).text(e);
	});
	
	$(".ShowVideo").fancybox({
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'	: 'none',
		'type'				: 'swf'
	});
	$(".lightbox").fancybox({
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'	: 'none',
		'type'				: 'image'
	});
	
	
	$("table.tableFormat").each(function(){
		$(this).find("tr:odd").not("td table tr").addClass("lp");
		$(this).find("tr:last").css({borderBottom:"none"});
	});
	
	$("img.resize").each(function(){
		var tw = $(this).attr("width");
		var th = $(this).attr("height");
		if(!isNaN(tw)&&!isNaN(th)) {
			var ts = $(this).attr("src").split("/");
			$(this).attr({"src":"/galeria/"+ts[ts.length-1]+"?xmax="+tw+"&ymax="+th+"&crop&s"});
		}
	});
	
	$("#Captcha img").click(function(){
		$(this).animate({ opacity: 'hide' }, "fast",null,function(){
			$(this).attr({"src":"/captcha.jpg?sid="+ Math.round(Math.random()*100000) });
			$(this).animate({ opacity: 'show' }, "slow");
		});
	});
	
	$('textarea[maxlength]').keypress(function(event){
		var key = event.which;
		var maxLength = $(this).attr("maxlength");
		var length = $(this).val().length;
		if(key>=32||key==13) {
			if(length >= maxLength) event.preventDefault();
		}
	});
	$('textarea[maxlength]').keyup(function(){
		var length = $(this).attr("maxlength") - $(this).val().length;
		if(length<0) length = 0;
		$(this).parent().find('.charsRemaining').text(length+" "+Odmiana(length,"znak","znaki","znaków"));
	});
	
	$('#Main .GuestBook .Txt').each(function(){
		var th = $(this).height();
		if(th>(62*2)) {
			$(this).css({"overflow":"hidden","height":62});
			$(this).after('<a href="javascript:void(null)" class="SeeAll" name="'+th+'">[Pokaż całość]</a>');
		}
	});
	$('#Main .GuestBook a.SeeAll').click(function(){
		$(this).prev('.Txt').animate({ height: $(this).attr("name") }, "slow");
		$(this).animate({ height: 'hide', opacity: 'hide' }, "fast");
	});
	
	$('textarea.textarea').autoResize({
	    onResize : function() {
	        $(this).css({opacity:0.8});
	    },animateCallback : function() {
	        $(this).css({opacity:1});
	    }, animateDuration : 300, extraSpace : 10
	});
	
});

function Odmiana(l,w1,w2,w3) {
	if(l==1) return w1;
	else if(	(l%10>1) && (l%10<5)	&& !((l%100>=10) && (l%100<=21)) ) return w2;
	return w3;
}

function PrevMonth(l) {
	var d = $("#CurrentData").val().split("-");
	d[0]--;
	if(d[0]==0) {
		d[0] = 12;
		d[1]--;
	}
	LoadCalendar(d,l);
}
function NextMonth(l) {
	var d = $("#CurrentData").val().split("-");
	d[0]++;
	if(d[0]==13) {
		d[0] = 1;
		d[1]++;
	}
	LoadCalendar(d,l);
}
function LoadCalendar(d,l) {
	var date = d[0]+"-"+d[1];
	$.ajax({
		url: "/"+l+"/"+date+"/newCalendar.json",
		success: function(p){
			$("#CurrentData").val(date);
			$("#CalendarPlace").html(p);
		}
	});
}

function CheckGuestBookForm() {
	var n = $("#FName").val();
	var e = $("#FEmail").val();
	var w = $("#Www").val();
	var t = $("#FText").val();
	var c = $("#FCode").val();
	
	if(n.length==0) return ShowError(GuestBookErrors[0]);
	if(!checkEmail(e) && e.length>0) return ShowError(GuestBookErrors[1]);
	if(t.length==0) return ShowError(GuestBookErrors[2]);
	if(c.length!=5) return ShowError(GuestBookErrors[3]);
	
	return true;
}

function CheckContactForm() {
	var n = $("#FName").val();
	var e = $("#FEmail").val();
	var p = $("#FPhone").val();
	var d = $("#FDate").val();
	var o = $("#FOkazja").val();
	var wz = $("#FWzrost").val();
	var wa = $("#FWaga").val();
	var t = $("#FText").val();
	var c = $("#FCode").val();
	
	if(n.length==0) return ShowError(ContactFormErrors[0]);
	if(p.length==0) return ShowError(ContactFormErrors[1]);
	if(e.length==0) return ShowError(ContactFormErrors[2]);
	if(!checkEmail(e)) return ShowError(ContactFormErrors[3]);
	if(d.length==0) return ShowError(ContactFormErrors[4]);
	if(o.length==0) return ShowError(ContactFormErrors[5]);
	if(wz.length==0) return ShowError(ContactFormErrors[6]);
	if(wa.length==0) return ShowError(ContactFormErrors[7]);
	if(c.length!=5) return ShowError(ContactFormErrors[8]);
	
	return true;
}

function checkEmail(x){return/^[\w\.-]+@([\w-]+\.)*[a-z]{2,4}$/.test(x)}

function ShowError(txt) {
	$.fancybox({
		'autoScale'			: true,
		'content'			: '<div id="Question"><div class="txt">'+txt+'</div></div>'
	});
	return false;
}