function anchor(target) {
	var targetOffset = $(target+'').offset().top;
	$('html').animate({scrollTop: targetOffset}, 500);
	return false;
}

function defaultValueRestore(selector) {
	$(selector).focus(function(){
		var def = $(this)[0].defaultValue;
		var cur = $(this).val();
		if (def==cur) {
			$(this).attr('value','');
		}
	});
	$(selector).blur(function(){
		var def = $(this)[0].defaultValue;
		var cur = $(this).val();
		if (def==cur || cur=='') {
			$(this).attr('value',def);
			$(this).removeClass('ok');
		}
		else {
			$(this).addClass('ok');
		}
	});	
}



function toTop(id){
	document.getElementById(id).scrollTop=0;
}

defaultStep=1;
step=defaultStep;
timerTime = 20;
function scrollDivDown(id) { 
	document.getElementById(id).scrollTop+=step;
	timerDown=setTimeout("scrollDivDown('"+id+"')",timerTime);
}

function scrollDivUp(id){
	document.getElementById(id).scrollTop-=step;
	timerUp=setTimeout("scrollDivUp('"+id+"')",timerTime);
}

function toBottom(id){
	document.getElementById(id).scrollTop=document.getElementById(id).scrollHeight;
}

function toPoint(id){;
	document.getElementById(id).scrollTop=100;
}




















function isEmail(email) {
	email = email.replace(/[ ]/g, '');
	var regEmail = /^([a-zA-Z0-9._-]{1,})@([a-zA-Z0-9._-]{1,})\.([a-zA-Z]{2,4})$/;
	if (!regEmail.test(email)) return false;
	else return true;
}

var ilosc = 0;

$(document).ready(function() {
	defaultValueRestore('input[type=text]');
	
	$("a.LB").attr('rel','prettyPhoto[gal1]');
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded', hideflash:true});
	
	
	$('.bann').each (function() {
		ilosc++;
	});
	
	//alert(ilosc);
	
	if (ilosc>=1) {
		$('div#bann1').fadeIn(300);
		$('#kropa1').addClass("on");
	}
	
	$('.bann').each (function() {
		var idek = $(this).attr("id");
		idek=idek.split("bann");
		idek=idek[1];
		//alert(idek);
		$('#kropa'+idek).click(function() { 
			clearInterval(id);
			id = setInterval("wywInt();", 5000);
			fades(idek);
		});
	});
	
	var ss = 2, id;
	
	id = setInterval("wywInt();", 5000);
});


var ss=2;
function wywInt() {
	$(document).ready(function(){
     fades(ss++);
	});
  if(ss > ilosc) ss=1;
}


function fades(i) {
	for (var x=1; x<=ilosc; x++) {
		if (i==x) {
			$('div#bann'+x).fadeIn(300);
			$('#kropa'+x).addClass("on");
		}
		else {
			$('div#bann'+x).fadeOut(300);
			$('#kropa'+x).removeClass("on");
		}
	}
}

function send_mail(obj) {
	var id = $(obj).attr('id');
	if (validacja_formy(id)) {
		$(obj).slideUp(500);
		$('#wysylanie').slideDown(500);
		var imie = $(obj).find('input[name="imie"]').val();
		var mail = $(obj).find('input[name="mail"]').val();
		var tresc = $(obj).find('textarea[name="tresc"]').val();
		imie = encodeURIComponent(imie);
		mail = encodeURIComponent(mail);
		tresc = encodeURIComponent(tresc);
		
		$.ajax({
			type: "GET",
			url: "/ajax2.php?mail="+mail+"&imie="+imie+"&tresc="+tresc,
			cache: true, // to prevent doubles ;)
			success: function(info) {
				if (info) {
					$('#loader').slideUp(500);
					if (info=="OK") {
						$('#wysylanie').slideUp(500);
						$('#sukces').slideDown(500);
					}
					else {
						//alert(info);
						alert("Nieznany błąd. Spróbuj ponownie. Jeżeli błąd będzie się powtarzał - proszę skorzystaj z innej formy kontaktu.");
						$(obj).slideDown(500);
						$('#wysylanie').slideUp(500);
					}
				}
				else {
					alert("Żądanie przekroczyło dozwolony czas oczekiwania. Sprawdź połączenie z internetem, odśwież stronę i spróbuj ponownie.");
					$(obj).slideDown(500);
					$('#wysylanie').slideUp(500);
				}
	         } // success
		});// ajax
	}
	return false;
}


/**
 * Waliduje formularze
 * @param id - [atrybut html] id formularza 
 * @return bool - jezeli wszystko ok to true
 * TODO przepisac
 */
function validacja_formy(id, or_ok) {
	var blad='';
	$('#'+id+' .req_text').each (function () {
		var def = $(this)[0].defaultValue;
		var cur = $(this).val();
		if (cur=='' || cur==def) {
			blad += "- " + $(this).attr('title') + ".\n";
		}
	});
	$('#'+id+' .req_mail').each (function () {
		if (!isEmail($(this).attr('value'))) {
			blad += "- " + $(this).attr('title') + "\n";
		}
	});
	$('#'+id+' .type_mail').each (function () {
		if (!isEmail($(this).attr('value')) && ($(this).attr('value')!='')) {
			blad += "- Niepoprawny " + $(this).attr('title') + "\n";
		}
	});
	$('#'+id+' .req_checkbox').each (function () {
		if (!$(this).attr('checked')) {
			blad += "- " + $(this).attr('title') + "\n";
		}
	});
	
	if (or_ok != undefined) {
		or_ok = false;
		dodaj_txt = "Podaj "
		$('#'+id+' .req_or').each (function () {
			dodaj_txt += $(this).attr('title')+" lub ";
			if ($(this).attr('value')!='') {
				or_ok=true;
			}
		});
		dodaj_length=dodaj_txt.length;
		dodaj_txt=dodaj_txt.substring(0, dodaj_length-5)+'.';
		
		if (or_ok==false) {
			blad += "- "+dodaj_txt+"\n";
		}
	}
	

	if (blad != '') {
		alert(blad);
		return false;
	}
	else {
		return true;
	}
}






