$(document).ready(function(){

	if($("a[rel^='prettyPhoto']").length >= 1)
	{
		$("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_rounded'});
		
		$('div.icons_small a img').click(function(){
			
			clickedImg = $(this).attr('title');
			$('div.icons_large a img').each(function(){

				if($(this).attr('title') == clickedImg)
					$(this).parent().removeClass('hidden');
				else
					$(this).parent().addClass('hidden');
			});
			
			return false;
		});
	}
	
	if($('#button_send').length == 1)
	{
		$('#client_message').blur(function()
		{
			if($('#client_message').val().length > 512)
			{
				alert('Wiadomość jest zbyt długa, maksymalna długość tekstu, to 128 znaków.');
			}
		});
		
		$('#client_email').blur(function(){
		
			if($('#client_email').val().length > 5)
			{
				$.ajax({
					type: "POST",
					url: 'validate.php',
					data: 'client_email=' + $('#client_email').val(),
					success: function(data)
					{
						end = true;
						response = data.split('#####');
						if(response[0].substring(0,1)=='1')
						{
							if(response[1] == 'unvalid')
							{
								alert('Adres e-mail:' + $('#client_email').val() + 'jest nie poprawny.');
								unvaild = true;
							}
						}
						else
						{
							alert('Wystąpił błąd przy połączeniu z serwerem, widomość nie została wysłana.');
						}
					}
				});
			}
			else
			{	
				alert('Adres e-mail:' + $('#client_email').val() + 'jest nie poprawny.');
				unvaild = true;
			}
		});
		
		$('#button_send').click(function(){
		
			unvaild = false;
			if($('#client_name').val().length < 2 )
			{
				alert('Nie podano danych imienia / nazwiska osoby kontaktującej się.');
				unvaild = true;
			}
			
			if($('#client_email').val().length < 5 )
			{
				alert('Nie podano adresu e-mail.');
				unvaild = true;
			}
			
			if($('#client_message').val().length < 5)
			{
				alert('Brak treści wiadomości.');
				unvaild = true;
			}
			
			if(unvaild)
			{
				alert('Nie można wysłać wiadomości');
			}
			else
			{
				if($('#client_item_code').length > 0)
				{
					dataString =   'client_name=' + $('#client_name').val() + 
								'&client_email=' + $('#client_email').val() + 
								'&client_message=' + $('#client_message').val() +
								'&client_company=' + $('#firm2').val() +
								'&client_address=' + $('#adres2').val() +
								'&client_item_code=' + $('#client_item_code').val() +
								'&client_item_size=' + $('#client_item_size').val() + 
								'&client_item_count=' + $('#client_item_count').val() +
								'&client_item_color=' + $('#client_item_color').val() +
								'&client_phone=' + $('#phone2').val();
				}
				else
				{
					dataString =   'client_name=' + $('#client_name').val() + 
								'&client_email=' + $('#client_email').val() + 
								'&client_message=' + $('#client_message').val() +
								'&client_company=' + $('#firm2').val() +
								'&client_address=' + $('#adres2').val() +
								'&client_phone=' + $('#phone2').val();
				}
				$.ajax({
					type: "POST",
					url: 'emailme.php',
					data: dataString,
					success: function(data)
						{
						end = true;
						response = data.split('#####');
						if(response[0].substring(0,1)=='1')
						{
							alert(response[1]);
							$('#contact_form').remove();
						}
						else
						{
							alert('Wystąpił błąd przy połączeniu z serwerem, widomość nie została wysłana.');
						}
					}
				});
			}
			
			return false;
		});	
	}
	
	$('#perPageSelector').change(function(){
		$(this).submit(); 
	});
});
