function email_validate(src) 
{
	var regex = /^([a-z0-9]{1})([^\s\t\.@]*)((\.[^\s\t\.@]+)*)@((([a-z0-9]{1})((([a-z0-9-]*[-]{2})|([a-z0-9])*|([a-z0-9-]*[-]{1}[a-z0-9]+))*)((\.[a-z0-9](([a-z0-9-]*[-]{2})|([a-z0-9]*)|([a-z0-9-]*[-]{1}[a-z0-9]+))+)*)\.([a-z0-9]{2,6})([.]?))|((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))$/i;
  	return regex.test(src);
}

document.createElement('header');
document.createElement('nav');
document.createElement('section');
document.createElement('footer');

var is_working = false;

$(document).ready(function() { 
	var fadingElement;
	var isOpened = false;
	
	$('nav ul').mouseenter(function() {
		if ( $('section').hasClass('active') )
		{
			$('nav ul').animate({
				opacity: 1
			}, 200);
		}
	}).mouseleave(function() {
		if ( $('section').hasClass('active') )
		{
			$('nav ul').animate({
				opacity: 0.5
			}, 200);
		}
	});
	
	$('nav ul li a').each(function() {		
		$(this).click(function() {			
			fadingElement = $(this);
			
			if ( isOpened != false && fadingElement.hasClass('active') )
			{
				is_working = true;
				$('#cont_' + fadingElement.children('h2').attr('id')).removeClass('active');
				
				$('#cont_' + fadingElement.children('h2').attr('id')).fadeOut('slow', function(){
					$('nav').animate({
						width: '-=300'
					}, 1500);		
					
					$('nav ul').animate({
						opacity: 1
					}, 200);
					
					is_working = false;
					
					fadingElement.removeClass('active');	
					isOpened = false;
				});
			}
			else 
			{
				if ( isOpened != false )
				{			
					is_working = true;
					$('section.active').stop(true, true).fadeOut('slow', function(){
						$('nav').animate({
							width: '-=300'
						}, 1500, function(){
							$('#cont_' + fadingElement.children('h2').attr('id')).addClass('active');
							
							$('nav').animate({
								width: '+=300'
							}, 1500);
							
							$('nav ul').animate({
								opacity: 0.5
							}, 200);
							
							$('nav ul#nav_right').animate({
					    		marginLeft: '+=300'
							}, 1500, function() {
						    	$('#cont_' + fadingElement.children('h2').attr('id')).fadeIn('slow');
								$(this).css('margin-left', '0');
								is_working = false;
		  					});
				
							fadingElement.addClass('active');
							isOpened = true;
						});										  
					});
					
					$('section').removeClass('active');
					$('nav ul li a').removeClass('active');	
				}
				else if ( is_working == false ) {				
					is_working = true;
					$('#cont_' + fadingElement.children('h2').attr('id')).addClass('active');
				
					$('nav ul').stop(true, true).animate({
						opacity: 0.5
					}, 200);
				
					$('nav').stop(true, true).animate({
						width: '+=300'
					}, 1500);
				
					/*$('nav ul#nav_left').animate({
    					opacity: 0.25,
			    		marginLeft: '-=50'
					}, 2000, function() {
				    	// Animation complete.
	  				});*/
			
					$('nav ul#nav_right').stop(true, true).animate({
			    		marginLeft: '+=300'
					}, 1500, function() {
				    	$('#cont_' + fadingElement.children('h2').attr('id')).fadeIn('slow');
						$(this).css('margin-left', '0');
						
						is_working = true;
						
						fadingElement.addClass('active');
						isOpened = true;
  					});
				}
			}
		});
	});
	
	$('#contact_form').submit(function() {
		if ( $('#name').val() == '' )
		{
			$('section#working p').html('Nie wpisano imienia i nazwiska. Prosimy uzupełnić zawartość pola.');
			$('section#working').fadeIn('fast', function(){ $(this).css('display', 'table'); });
			setTimeout("$('section#working').fadeOut('fast');", 2000);	
		}
		else if ( $('#email').val() == '' )
		{
			$('section#working p').html('Nie wpisano adresu e-mail. Prosimy uzupełnić zawartość pola.');
			$('section#working').fadeIn('fast', function(){ $(this).css('display', 'table'); });
			setTimeout("$('section#working').fadeOut('fast');", 2000);	
		}
		else if ( email_validate($('#email').val()) == false )
		{
			$('section#working p').html('Wpisano niepoprawny adres e-mail. Prosimy poprawić zawartość pola.');
			$('section#working').fadeIn('fast', function(){ $(this).css('display', 'table'); });
			setTimeout("$('section#working').fadeOut('fast');", 2000);
		}
		else if ( $('#message').val() == '' )
		{
			$('section#working p').html('Nie wpisano treści wiadomości. Prosimy uzupełnić zawartość pola.');
			$('section#working').fadeIn('fast', function(){ $(this).css('display', 'table'); });
			setTimeout("$('section#working').fadeOut('fast');", 2000);		
		}
		else {
			$('#submit').attr('disabled', true);
			$('section#working p').html('Trwa wysyłanie wiadomości...');
			$('section#working').fadeIn('fast', function(){ $(this).css('display', 'table'); });
		
			$.post($(this).attr('action'), {
				name:		$('#name').val(),
				email:		$('#email').val(),
				message:	$('#message').val(),
				submited:	$('#submit').val(),
				spole: 		$('#spole').val(), 
				shpole: 	( $('#shpole').val() == '' || $('#shpole').val() === undefined ? '' : $('#shpole').val() )
			}, function(data) {
				$('section#working p').html(data);
				
				$('#name').val('');
				$('#email').val('')
				$('#message').val('')
				
				setTimeout("$('section#working').fadeOut('fast');", 2000);
				$('#submit').attr('disabled', false);
			});	
		}
		return false;
	});
});
