function anchor(target) {
	var targetOffset = $(target+'').offset().top;
	$('html').animate({scrollTop: targetOffset}, 1000);
	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 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;
}


$(document).ready(function(){
	$('img.ta').click(function(){
		$('.db_text>p, .db_text>div._short>p').css('font-size', '12px');
	});
	$('img.taa').click(function(){
		$('.db_text>p, .db_text>div._short>p').css('font-size', '14px');
	});
	$('img.taaa').click(function(){
		$('.db_text>p, .db_text>div._short>p').css('font-size', '16px');
	});
	
	$('img.tprint').click(function(){
		document.location="?print=1&ajax=1";
	});
	
	$("a.LB").attr('rel','prettyPhoto[gal1]');

	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded', hideflash:true});

});


$(document).ready(function() {
	defaultValueRestore('input[type=text]');
	
	$('#slider').nivoSlider({
		effect: 'sliceDown', //Specify sets like: 'fold,fade,sliceDown'
		slices:9,
		animSpeed:500, //Slide transition speed
		pauseTime:5000,
		startSlide:0, //Set starting Slide (0 index)
		directionNav:true, //Next & Prev
		directionNavHide:true, //Only show on hover
		controlNav:true, //1,2,3...
		controlNavThumbs:false, //Use thumbnails for Control Nav
		controlNavThumbsFromRel:false, //Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', //Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
		keyboardNav:true, //Use left & right arrows
		pauseOnHover:true, //Stop animation while hovering
		manualAdvance:false, //Force manual transitions
		captionOpacity:0.8, //Universal caption opacity
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){} //Triggers after all slides have been shown
	});
});

function send_mail(obj) {
	var id = $(obj).attr('id');
	if (validacja_formy(id)) {
		$(obj).hide(500);
		$('#wysylanie').show(500);
		var imie = $(obj).find('input[name="imie"]').val();
		var temat = $(obj).find('select[name="temat"]').val();
		var tresc = $(obj).find('textarea[name="tresc"]').val();
		imie = encodeURIComponent(imie);
		temat = encodeURIComponent(temat);
		tresc = encodeURIComponent(tresc);
		
		$.ajax({
			type: "GET",
			url: "/ajax.php?temat="+temat+"&imie="+imie+"&tresc="+tresc,
			cache: true, // to prevent doubles ;)
			success: function(info) {
				if (info) {
					$('#loader').hide(500);
					if (info=="OK") {
						$('#wysylanie').hide(500);
						$('#sukces').show(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).show(500);
						$('#wysylanie').hide(500);
					}
				}
				else {
					alert("Żądanie przekroczyło dozwolony czas oczekiwania. Sprawdź połączenie z internetem, odśwież stronę i spróbuj ponownie.");
					$(obj).show(500);
					$('#wysylanie').hide(500);
				}
	         } // success
		});// ajax
	}
	return false;
}

function send_mail2(obj) {
	var id = $(obj).attr('id');
	if (validacja_formy(id)) {
		$(obj).hide(500);
		$('#wysylanie').show(500);
		var imie = $(obj).find('input[name="imie"]').val();
		var mail = $(obj).find('input[name="mail"]').val();
		var telefon = $(obj).find('input[name="tel"]').val();
		var temat = $(obj).find('select[name="temat"]').val();
		var tresc = $(obj).find('textarea[name="tresc"]').val();
		var podpis = $(obj).find('input[name="podpis"]').val();
		imie = encodeURIComponent(imie);
		mail = encodeURIComponent(mail);
		telefon = encodeURIComponent(telefon);
		temat = encodeURIComponent(temat);
		tresc = encodeURIComponent(tresc);
		podpis = encodeURIComponent(podpis);
		
		$.ajax({
			type: "GET",
			url: "/ajax2.php?temat="+temat+"&imie="+imie+"&tresc="+tresc+"&mail="+mail+"&telefon="+telefon+"&podpis="+podpis,
			cache: true, // to prevent doubles ;)
			success: function(info) {
				if (info) {
					$('#loader').hide(500);
					if (info=="OK") {
						$('#wysylanie').hide(500);
						$('#sukces').show(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).show(500);
						$('#wysylanie').hide(500);
					}
				}
				else {
					alert("Żądanie przekroczyło dozwolony czas oczekiwania. Sprawdź połączenie z internetem, odśwież stronę i spróbuj ponownie.");
					$(obj).show(500);
					$('#wysylanie').hide(500);
				}
	         } // success
		});// ajax
	}
	return false;
}


function send_mail3(obj) {
	var id = $(obj).attr('id');
	if (validacja_formy(id)) {
		$(obj).hide(500);
		$('#wysylanie').show(500);
		var imie = $(obj).find('input[name="imie"]').val();
		var mail = $(obj).find('input[name="mail"]').val();
		var telefon = $(obj).find('input[name="tel"]').val();
		var temat = $(obj).find('select[name="temat"]').val();
		var tresc = $(obj).find('textarea[name="tresc"]').val();
		var podpis = $(obj).find('input[name="podpis"]').val();
		imie = encodeURIComponent(imie);
		mail = encodeURIComponent(mail);
		telefon = encodeURIComponent(telefon);
		temat = encodeURIComponent(temat);
		tresc = encodeURIComponent(tresc);
		podpis = encodeURIComponent(podpis);
		
		$.ajax({
			type: "GET",
			url: "/ajax3.php?temat="+temat+"&imie="+imie+"&tresc="+tresc+"&mail="+mail+"&telefon="+telefon+"&podpis="+podpis,
			cache: true, // to prevent doubles ;)
			success: function(info) {
				if (info) {
					$('#loader').hide(500);
					if (info=="OK") {
						$('#wysylanie').hide(500);
						$('#sukces').show(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).show(500);
						$('#wysylanie').hide(500);
					}
				}
				else {
					alert("Żądanie przekroczyło dozwolony czas oczekiwania. Sprawdź połączenie z internetem, odśwież stronę i spróbuj ponownie.");
					$(obj).show(500);
					$('#wysylanie').hide(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;
	}
}



function load() {
	var x = 50.117331;
	var y = 22.062736;
	var zoom=11; 
	var point = new GLatLng(x,y);  
	var marker = new GMarker(point);
	
	if (GBrowserIsCompatible()) { 
		var mapa = new GMap2(document.getElementById("mapka")); 
		GEvent.addListener(marker, "click", function(){
			var myHtml = "<b>BIURO HANDLOWE:</b><br />36-002 Jasionka 74A, k/Rzeszowa<br />Telefony: +48 17 7724997, 772 34 06<br />Fax: +48 17 7724400<br />Biuro: esp@esp.com.pl<br />Pion techniczny: pt@esp.com.pl";
		    mapa.openInfoWindowHtml(point, myHtml);
		});
		
		mapa.addControl(new GSmallZoomControl());
		mapa.setCenter(new GLatLng(x,y), zoom); 
		mapa.addOverlay(marker);
	}
}


