function changeImg(id) {
	if (id!=undefined) {
		ssCurrent=id;
		resetssInt();
	}
	else {
		ssCurrent++;
		
		if (ssCurrent>ssCount) { ssCurrent = 1; }
	}
	$('#banner>div').fadeOut(ssTime);
	$('#banner>div#item'+ssCurrent).fadeIn(ssTime);
}

function resetssInt() {
	clearInterval(ssInt);
	ssInt = setInterval(changeImg, ssChange);
}

var ssCount = 0;
var ssCurrent = 0;
var ssTime = 500;
var ssInt = undefined;
var ssChange = 5000;

rdy = false;

function startSlideShow() {
	// mapki tez
	
	
	slide();
	load();
}

function slide() {
	if (rdy==false) {
		rdy==true;
		ssCount = 0;
		$('#banner>div').each(function(){ssCount++;});
		changeImg();
		$('#banner').hover(function(){
			clearInterval(ssInt);
		},function(){
			ssInt = setInterval(changeImg, ssChange);
		});
	
		if (ssInt==undefined || ssInt == false) {
			ssInt = setInterval(changeImg, ssChange);
		}
		$('#controls>a').click(function(){
			var id = $(this).attr('rel');
			id = id.replace('item','');
			changeImg(id);
			return false;
		});
	}
}


function load() {
	var x = 50.037049;
	var y = 20.216114;
	var zoom=15; 
	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 = document.getElementById("text_mapka").innerHTML;
		    mapa.openInfoWindowHtml(point, myHtml);
		});
		
		mapa.addControl(new GSmallZoomControl());
		mapa.setCenter(new GLatLng(x,y), zoom); 
		mapa.addOverlay(marker);
	}
}

$(document).ready(startSlideShow);

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;
}

function add_comment(id) {
	if (validacja_formy(id)) {
		var nick = encodeURIComponent($('[name=nick]').val());
		var tresc = encodeURIComponent($('[name=tresc]').val());
		var mail = encodeURIComponent($('[name=mail]').val());
		var www = encodeURIComponent($('[name=www]').val());
		$.ajax({
			type: "GET",
			url: "?ajax=1&aaction=add_comment&nick="+nick+'&tresc='+tresc+'&mail='+mail+'&www='+www,
			cache: false,
			success: function(info) {
				if (info) {
					if (info=="OK") {
						alert("Komentarz dodany!");
						$('.add_table, .nocomments').slideUp(1000);
						$('.add_table input[type!=submit], .add_table textarea').val('');
						$('#comments_div').slideUp(1000, function(){
							$('#comments_div').html("Ładowanie komentarzy..");
							$('#comments_div').slideDown(500,function(){
								reload_comments();
							});
						});
					}
					else {
						alert("Błąd dodawania komentarza! Spróbuj ponownie, a jak nie podziała to zgłoś to proszę gg:2928054");
					}
					//alert(info);
				}
				else {
					alert("Ajax request timed out. Please refresh this page (F5) and try again.");
				}
	         }
		});// ajax
		return false;
	}
	else {
		return false;
	}
}

function reload_comments() {
	$.ajax({
		type: "GET",
		url: "?ajax=1&aaction=load_comments",
		cache: false,
		success: function(info) {
			if (info) {
				if (info!='ERROR') {
					$('#comments_div').slideUp(500, function(){
						$('#comments_div').html(info);
						$('#comments_div').slideDown(1000);
					});
				}
				else {
					alert("Błąd żądania. Proszę odśwież stronę.");
				}
				//alert(info);
			}
			else {
				alert("Ajax request timed out. Please refresh this page (F5) and try again.");
			}
         }
	});// ajax
}

$(document).ready(function() {
	defaultValueRestore('input[type=text]');
	var hash = (window.location.hash).replace("#", "");
	if (hash!='' && hash!=undefined) {
		var val = hash.match(/^JS:([0-9A-Za-z_-]+)$/);
		if (val[1]!=null && val[1]!=undefined) {
			anchor(val[1]);
		}
	}
	
	//$("a.LB").attr('rel','prettyPhoto[gal1]');
	//$("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded', hideflash:true});
	$("a.LB").prettyPhoto({theme:'dark_rounded', hideflash:true});
});

function expand_comments() {
	anchor('comments');
	$('#comments div.add_table').slideDown(1000);
}

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 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: "/ajax.php?mail="+mail+"&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;
}

/**
 * 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')) && $(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;
	}
}
