function PageLoad(hash) 
{
	$("#cindex, #cabout, #cportfolio, #ccontact, #cfriends, #cstat").hide();

	if(hash) 
	{
		$('#c' + hash).show();
		$('#content, #about, #portfolio, #contact, #friends').removeClass();
		$('#' + hash).addClass('on');
		$('#galeria').hide();

		if(hash == 'index' || hash == 'stat')
		{
			$('#content').show().addClass('ind');
			$('#top').hide();
			$('#top').attr('src', 'images/top1in.jpg').show();
			$('#content').show();
		}
		if(hash == 'about')
		{
			$('#top').hide();
			$('#top').attr('src', 'images/top1ab.jpg').show();
		}
		if(hash == 'portfolio')
		{
			$('#galeria').show();
			$('#top').hide();
			$('#top').attr('src', 'images/top1po.jpg').show();
		}
		if(hash == 'contact')
		{
			$('#content').hide().addClass('con');
			$('#top').hide();
			$('#top').attr('src', 'images/top1co.jpg').show();
			$('#content').show();
		}
		if(hash == 'friends')
		{
			$('#content').hide().addClass('fri');
			$('#top').hide();
			$('#top').attr('src', 'images/top1fr.jpg').show();
			$('#content').show();
		}

	}
	else 
	{
		$('#cindex').hide();
  }

}

$(document).ready(
	function()
	{
	
		//walidacja formularza kontaktowego
		
		if($('form').is('#formkont'))
		{
			$('#formkont').submit( function() {
				return WerForm();
			});
		}
		
		//przycisk WSTECZ dla ajax
	
		$.historyInit(PageLoad);
		
		$("ul.menu li a").click(function()
		{
		
			var hash = this.href.replace(/^.*#/, '');

      $.historyLoad(hash);

      return false;
			
		});
		
		//zmienianie zdjęć w portfolio
		
		$("ul.port li").click(function()
		{
			$("#projekt").attr('title', $(this).attr('id')).attr('src', 'images/galeria/' + $(this).attr('id') + '.jpg');
			$("ul.port li").removeClass();
			$(this).addClass('aktywny');
		});
		
		$("#prev").click(function() {
			if($("#projekt").attr('title') == 'the_escape')
			{
				var id = 'apple';
			}
			else
			{
				var id = $("ul.port").find('#' + $("#projekt").attr('title')).prev("li").attr('id');
			}		
			
			$("#projekt").attr('title', id).attr('src', 'images/galeria/' + id + '.jpg');
			$("ul.port li").removeClass();
			$('#' + id).addClass('aktywny');
		})
		
		$("#next").click(function() {
			if($("#projekt").attr('title') == 'apple')
			{
				var id = 'the_escape';
			}
			else
			{
				var id = $("ul.port").find('#' + $("#projekt").attr('title')).next("li").attr('id');
			}			

			$("#projekt").attr('title', id).attr('src', 'images/galeria/' + id + '.jpg');
			$("ul.port li").removeClass();
			$('#' + id).addClass('aktywny');
		})
		
	});


var imie;
var mail;
var tresc;
	
function WerForm()
{
	imie = document.getElementById('formkont').imie.value;
	mail = document.getElementById('formkont').email.value;
	tresc = document.getElementById('formkont').tresc.value;
	
	var error = false;
	var wyr_mail = /^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$/;
	var wyr_amp = /^[^&#%<>+]+$/;
	
  if(imie == "" || mail == "" || tresc == "")
  {
		alert("Uzupełnij wszystkie pola formularza, aby przesłać wiadomość.");
		error = true;
  }
	else
	{
		if(!wyr_mail.test(mail))
		{
			alert("Podany adres e-mail jest nieprawidłowy.\nProszę wpisać poprawny adres, aby przesłać wiadomość.");
			error = true;
		}
		else
		{	
			if(!wyr_amp.test(imie) || !wyr_amp.test(mail) || !wyr_amp.test(tresc))
			{
				alert("W formularzu znajdują się niewłaściwe znaki, które nie mogą być przesłane.\nProszę je usunąć, aby poprawnie przesłać wiadomość.");
				error = true;
			}
		}
	}
  
	if(!error)
	{
		Ajax("POST");
	}
	
	return false;
}

function Ajax(lacze)
{
	var zapytanie = false;
	var plik = 'mailer/wyslij.php';
	var dane = 'imie=' + imie + '&email=' + mail + '&tresc=' + tresc; // dla POST

	if(lacze == 'GET')
	{
		plik += '?' + dane; // dla GET
	}
	
	if(window.XMLHttpRequest)
	{
		zapytanie = new XMLHttpRequest();
	}
	else 
	{
		zapytanie = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if(zapytanie)
	{
		zapytanie.open(lacze, plik);
		
		if(lacze == 'POST')
		{
			zapytanie.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // dla POST
		}
		
		zapytanie.onreadystatechange = function()
		{
			if(zapytanie.readyState == 4)
			{
				if(zapytanie.responseText == 'przeslano')
				{
					document.getElementById('formkont').style.display = 'none';
					document.getElementById('komunikat').style.marginLeft = '0';
					document.getElementById('komunikat').innerHTML = '<br />Your message was successfully sent.<br /><br /><h2><strong>Thanks for contact.</strong></h2><br />' +
																													 '<a href="javascript:void(0);" title="&laquo; Back to contact form" onclick="Powrot();">&laquo; back</a>';
				}
				else
				{
					document.getElementById('komunikat').style.marginLeft = '0';
					document.getElementById('komunikat').innerHTML = zapytanie.responseText;
				}
			}
			else
			{
				document.getElementById('formkont').style.display = 'none';
				document.getElementById('komunikat').style.marginLeft = '60px';
				document.getElementById('komunikat').innerHTML = '<br /><img src="images/mail-ico-loading.gif" title="Message is sending..." style="float: none; margin-left: 25px"><br /><br />Message is sending...';
			}
		}
		
		if(lacze == 'POST')
		{
			zapytanie.send(dane); //dla POST
		}
		else
		{
			zapytanie.send(null); //dla GET
		}
	}
	else
	{
		alert("Wysłanie zawartości formularza nie jest możliwe.\nTwoja przeglądarka nie obsługuje technologii AJAX!\nDokonaj jej aktualizacji udając się na stronę: www.firefox.pl");
	}
}

function Powrot()
{
	document.getElementById('komunikat').innerHTML = '';
	document.getElementById('formkont').reset();
	document.getElementById('formkont').style.display = 'block';
}