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);
}

/**
 * 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;
	}
}






