$(function(){	
	$('#left form textarea').autoResize({
	    animateDuration : 300,
	    extraSpace : 0
	});
	
	$('#left form textarea').prev('textarea').attr('req','');
	
	$(':text, :password, textarea').each(function(){
		if($(this).val().length > 0) $(this).addClass('unplaceholded');
	}).focus(function(){
		$(this).addClass('unplaceholded');
	}).blur(function(){
		if($(this).val().length == 0) $(this).removeClass('unplaceholded');
	});
	
	$('#kontakt_form form').submit(function(event){
		var fields = $(this).find(':text, textarea, select, :radio');
		var valid = true;
		var msg = '';
		
		fields.each(function(){
			switch($(this).attr('type')){
				case 'radio':
					$(this).parent().parent().removeClass('invalid');
					
					var req = $(this).attr('req');
					if(typeof(req) != undefined && req == 'true'){
						if(!($('[name="'+ $(this).attr('name') +'"]:checked').length > 0)){
							$(this).parent().parent().addClass('invalid');
							if(valid){
								valid = false;
								msg = 'Należy wybrać jedno z pól "' + $(this).attr('name') + '"!';
							}
						}
					}
					
					break;
				default:
					$(this).removeClass('invalid');
				
					var required = false;
					var req = $(this).attr('req');
					var min_length = $(this).attr('min-length');
					var max_length = $(this).attr('max-length');
					var validator = $(this).attr('validator');
					
					if(typeof(req) != undefined && req == 'true'){
						required = true;
						if(!($(this).val().length > 0)){
							$(this).addClass('invalid');
							if(valid){
								valid = false;
								msg = 'Pole "' + $(this).attr('name') + '" jest wymagane!';
							}
						}
					}
					if(typeof(min_length) != undefined && min_length > 1){
						if($(this).val().length < min_length && required){
							$(this).addClass('invalid');
							if(valid){
								valid = false;
								msg = 'Pole "' + $(this).attr('name') + '" musi mień przynajmniej '+ min_length +' znaków!';
							}
						}
					}
					if(typeof(max_length) != undefined && max_length > 1){
						if($(this).val().length > max_length && required){
							$(this).addClass('invalid');
							if(valid){
								valid = false;
								msg = 'Pole "' + $(this).attr('name') + '" może mieć conajwyżej '+ max_length +' znaków!';
							}
						}
					}
					if(typeof(validator) != undefined){
						switch(validator){
							case 'email':
								var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
							    if(reg.test($(this).val()) != true && (required || $(this).val().length > 0)){
									$(this).addClass('invalid');
									if(valid){
										valid = false;
										msg = 'Pole "' + $(this).attr('name') + '" nie jest poprawnym adresem e-mail!';
									}
							    }
							    break;
						}
					}
			}
		});
		
		if(!valid) {
			jAlert('warning', '<p>' + msg + '</p>', 'Formularz');
		} else {
			$("body").mask("Wysyłanie ...");
			$.ajax({
		        url: $(this).attr('action'),
		        type: 'POST',
		        async: false,
		        data: $(this).serialize(),
		        success: function(data){
		          if(data.status == true){
		            jAlert('success', "<p><strong>Dziękujemy!</strong></p><p>Formularz został wysłany pomyślnie.</p>", 'Formularz');
		          } else {
		            jAlert('error', '<p><strong>Wystąpił problem!</strong></p><p>Spróbuj ponownie.</p>', 'Formularz');
		          }
		        },
		        error: function(){
		          jAlert('error', '<p><strong>Wystąpił problem!</strong></p><p>Spróbuj ponownie.</p>', 'Formularz');
		        }
			});
			$("body").unmask();
		}
		
		event.preventDefault();
	});
	
	$('#main a.lightbox').lightBox({fixedNavigation:true});
});
