// JavaScript Document
function mask() {
	setTimeout(function() {
		$("#subpage").html('<img src="images/ajax-loader.png" width="42" height="42" alt="loading" style="margin: 80px 0 80px 458px;"/>');
	}, 300);
}

function addOnclicks() {
	var activePage = 'kontakt';
	$("li.onas").click(function() {
		window.location = 'onas.html';
	});
	$("li.portfolio").click(function() {
		window.location = 'portfolio.html';
	});
	$("li.oferta").click(function() {
		window.location = 'oferta.html';
	});
	
	$("#iWyslijFormularz").click(function(){
		if (activePage == 'formularz_online')
			return false;
		$(this).attr('src', 'images/formularz_online_a.png');
		$("#subpage").fadeOut(300);
		mask();
		setTimeout(function() {
			$.get('formularz.html', function(data) {
				$("#subpage").html(data).fadeIn(300);
				bindForm();
				dontFocus();
			});
		}, 400);
		activePage = 'formularz_online';
		return false;
	});
	
	$("li.kontakt").click(function(){
		$("#iWyslijFormularz").attr('src', 'images/formularz_online.png');
		$("#subpage").fadeOut(300);
		mask();
		setTimeout(function() {
			$.get('kontakt.html', function(data) {
				$("#subpage").html(data).fadeIn(300);								  
			});
		}, 400);
		activePage = 'kontakt';
		return false;
	});
	
	//making submenu hovers
	var img;
	$("img[id^='i']").hover(function(){
		img = $(this).attr('src').slice(0,-4);
		if (img.slice(-2)=='_a')	//active
			return false;
		$(this).attr('src', img+'_a.png'); 
	},
	function() {
		if (img.indexOf(activePage)>=0)
			return false;
		$(this).attr('src', img+'.png');
	}
	);
}

function bindForm() { 
	var options = { 
			beforeSubmit:	checkForm,
	        success:		formSended  // post-submit callback 
	    }; 
	    // bind form using 'ajaxForm' 
	    $('#iForm').ajaxForm(options);
	    $("#iMsg").autogrow({maxHeight:400, minHeight:78});
	    return false;
}

var prepended = false;
var timeoutId;

function checkForm() {
	if ($("#iName").val().length==0 || $("#iMail").val().length==0 || $("#iMsg").val().length==0) {
		if (timeoutId > 0)
			clearTimeout(timeoutId);
		if (!prepended) {
			$("#iForm div").prepend(
				'<p class="err">Wypełnij wszystkie pola!</p>'
			);
			prepended = true;
		}
		else {
			$("#iForm p.err").html('Wypełnij wszystkie pola!');
		}
		$("#iForm p.err").fadeIn(600);
		timeoutId = setTimeout(function() {
			$("#iForm p.err").fadeOut(300);
		}, 3000);
		return false;
	}
	else {
		var valid = /^([a-zA-Z0-9_\.\-])+\@([a-zA-Z0-9])+\.([a-zA-Z0-9])+$/;
		if (!valid.test($("#iMail").val())) {
			if (timeoutId > 0)
				clearTimeout(timeoutId);
			if (!prepended) {
				$("#iForm div").prepend(
					'<p class="err">Podaj prawidłowy adres e-mail!</p>'
				);
			}
			else
				$("#iForm p.err").html('Podaj prawidłowy adres e-mail!');
			$("#iForm p.err").fadeIn(600);
			timeoutId = setTimeout(function() {
				$("#iForm p.err").fadeOut(300);
			}, 3000);
			return false;
		}
		else
			return true;
	}
}

function formSended(responseText, statusText) {
	$("#iForm").fadeOut(500);
	if (responseText=='1') {
		setTimeout(function(){
			$("#subpage").html(
				'<p class="info">E-mail został pomyślnie wysłany. Odpowiemy najszybciej jak to będzie możliwe!</p>'
			);
			$("#subpage p.info").fadeIn();
		});
	}
	else {
		setTimeout(function(){
			$("#subpage").html(
				'<p class="err">Niestety Twój e-mail nie został wysłany, wystąpił nieoczekiwany błąd! Spróbuj ponownie, lub wyślij używając programu pocztowego.</p>'
			);
			$("#subpage p.err").fadeIn();
		});
	}
	return false;
}
