jQuery.fn.tinyValidator = function(options){
	var defaults = {
		emptyMsg: "Proszę uzupełnić to pole.",
		emailMsg: "Proszę wpisać prawidłowy email.",
		urlMsg: "	Type proper url with \
						<code>http://</code> portion \
						or leave it blank<br /> \
						e.g. &nbsp; <code>http://yourdomain.com</code><br /> \
						<code>http://you.yourdomain.net</code>",
		sbtMsg: "Proszę prawidłowo uzupełnić wszystkie pola.",

		genError: function(field, msg){
			var holder = field.parent();
			if(!holder.hasClass('warning')){
				holder.addClass('warning').append($('<p>' + msg + '</p>')).children('p').hide().slideDown(400);
			}
		},
		
		canError: function(field){
			var holder = field.parent();
			if(holder.hasClass('warning')){
				holder.removeClass('warning').children('p').slideUp(200, function() {jQuery(this).remove()});
			}
		}
	};
	
	var opts = jQuery.extend(defaults, options);
	
	return this.each(function(){
		var form = jQuery(this);
		
		form.one('submit', function(){
			jQuery(':input', form)
				.filter('.empty')
				.blur(function(){
					if(this.value == '' || /^\s+$/.test(this.value)){
						opts.genError(jQuery(this), opts.emptyMsg);
					}
					else{
						opts.canError(jQuery(this));
					}
				})
				.end()
				.filter('.email')
				.blur(function(){
					if(this.value == '' || !/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value)){
						opts.genError(jQuery(this), opts.emailMsg);
					}
					else{
						opts.canError(jQuery(this));
					}
				})
				.end()
				.filter('.url')
				.blur(function(){
					if((this.value == '' || /^\s+$/.test(this.value)) && jQuery(this).hasClass('optional')){
						jQuery(this).val('');
						opts.canError(jQuery(this));
						return;
					}
					else if(this.value == '' || !/^(http|https):\/\/.+\.\w{2,4}$/.test(this.value)){
						opts.genError(jQuery(this), opts.urlMsg);
					}
					else{
						opts.canError(jQuery(this));
					}
				});
			}).submit(function(){
				jQuery(':input', form).trigger('blur');
				var warnings = jQuery('.warning', form).length;
				var parent = jQuery(':submit', form).parent('div');
				parent.find('p').slideDown(400).remove();
				if(warnings > 0){
					parent.prepend(jQuery("<p><strong>" + opts.sbtMsg + "</strong></p>"));
					return false;
				}
				else{
					parent.find('p').remove();
					return true;
				}
		});
	});
};

