$(document).ready(function(){
	
	swfobject.embedSWF("/flash/MainBanner.swf", "MainBannerPlace", "600", "290", "9.0.0", "expressInstall.swf", false, {	'menu': 'false', 'wmode': 'transparent' }, false);
	swfobject.embedSWF("/flash/Flash.swf", "Flasher", "440", "87", "9.0.0", "expressInstall.swf", false, {	'menu': 'false', 'wmode': 'transparent' }, false);
	
	var mid = "jsmenu";
	var list = $("#"+mid).find("ul");
	for(var i=0;i<list.length;i++) {
		if($(list[i]).parent().parent().attr("id")==mid) {
			$(list[i]).parent().hover(function(){
				var tw = ($(this).width()>180 ? $(this).width() : 180);
				$(this).find("ul").css({"visibility":"visible","width":tw});
				var list = $(this).find("a");
				$(list[0]).parent().addClass("hover");
			},function(){
				$(this).find("ul").css({"visibility":"hidden"});
				$(this).removeClass("hover");
			});
		}
	}
	
	$("#Top form .input").each(function(){
		var lab = $("label[for='"+$(this).attr("id")+"']").text();
		$(this).val(lab);
		$(this).blur(function(){ var val = $(this).val(); if(val.length==0) $(this).val(lab); });
		$(this).focus(function(){ var val = $(this).val(); if(val==lab) $(this).val(""); });
	});
	
	$('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);
	});
	
	$("a[href$=.mp3]").addClass("sm2_button");
	
	$(".ShowVideo").fancybox({
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'	: 'none',
		'type'				: 'swf'
	});
	$(".lightbox").fancybox({
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'	: 'none',
		'type'				: 'image'
	});
	
	$(".SubMenu").each(function(){
		$(this).find("li:odd").addClass("lp");
	});
	
	$("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();
		}
	});
	
	$('.ContactForm .text, .ContactForm .textarea').each(function(){ $(this).fadeTo(0,0.8) });
	
	$('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"));
	});
	$('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 CheckContactForm() {
	var n = $("#FName").val();
	var e = $("#FEmail").val();
	var p = $("#FPhone").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(t.length==0) return ShowError(ContactFormErrors[4]);
	if(c.length!=5) return ShowError(ContactFormErrors[5]);
	
	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;
}

