(function($) {
	 
	$.fn.input_helper = function()
	{
		var regexes = new Array();
		regexes['not-empty'] = /.{2,}/;
		regexes['username'] = /[a-z0-9]{4,}/;
		regexes['password'] = /[a-z0-9]{4,}/;
		regexes['email'] = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/;
		
		var errors = new Array();
		errors['password'] = 'Lösenordet måste bestå av minst 6 tecken, varav minst en siffra.';
		errors['email'] = 'Var god fyll i en fungerande epostadress.';
		
		return this.each(function()
		{
			var $form = $(this);
						
			/*
			Focus first (convenient when styling)

			$('span.input-help:first').addClass('show-input-help');
			return false;
			*/

			if ( ! $form.size())
			{
				return false;
			}
						
			var $inputs_parents = $form.find('p.input');

			if ( ! $inputs_parents.length)
			{
				return false;
			}
			
			// Get num required fields by counting fields with a rel attribute (they are used to store the name of the regex)
			
			var num_required_fields = $form.find(':input').not('button').filter('[rel]').size();
			
			// Only allow submit of the form if all required fields are completed
			// Do this by comparing num_required_fields against completed ones (completed have an ok class, so just count fields with the class of ok)
			
			$form.submit(function()
			{
				var num_completed_fields = $form.find(':input').not('button').filter('.ok').size();
				
				if (num_required_fields > num_completed_fields)
				{
					$('p.submit-error').remove();
					$('p.submit').after('<p class="error submit-error">Please complete all fields before submitting</p>');
					return false;
				}
			});
			
			$inputs_parents.each(function()
			{
				var $this = $(this);
				
				var $input 		= $this.find(':input').not('button'); // Get the field
				var $label 		= $this.find('label');
				var $input_help = $this.find('span.input-help');
				
				if (! $input.size())
				{
					return false;
				}
				
				var regex_type = $input.attr('rel'); // Regex type is stored in rel attr. If field is not required, then rel should be empty
				var regex = regexes[regex_type]; 	// Get regex from obj prop (array) based on regex_type
				
				var req_field = true; // flag for required field
				
				if (typeof(regexes[regex_type]) != 'undefined') // Check that we have a regex for it
				{	
					$input.blur(function() // show input helper and add error class if we have a valid that is not valid
					{
						var value = $(this).val();
						var valid = regex.test(value);
						
						if (value != '' && ! valid)
						{
							$this.addClass('error');
							$this.append('<span class="helper"><span>' + errors[regex_type] + '</span></span>');						
						}
						
						if (valid)
						{
							$this.find('span.helper').remove();
						}
						
						if (value == '')
						{
							$this.removeClass('error');
						}					
					});
				}
				
				// If we don't have a regex type, make regex just check for a value
				// (this is just to be able to display a success icon and these fields can't get errors)
				
				else
				{
					regex = regexes['not-empty']; 
					var req_field = false;
				}
								
				var elem_type = $input[0].tagName;
				elem_type = elem_type.toLowerCase();
				
				if (elem_type == 'input')
				{					
					elem_type = $input.attr('type');
				}
								
				var bind_type = '';
				
				switch(elem_type)
				{
					case 'select':
				  		bind_type = 'change';
				  	break;
				
					case 'checkbox':
						bind_type = 'click';
				  	break;
					
					default:
				  		bind_type = 'keyup';
				}				
				
				$input.bind(bind_type,function() // toggle error/info
				{
					check_input(false,$input, $input_help, req_field, regex, elem_type);
					
				});
				
				check_input(true,$input, $input_help, req_field, regex, elem_type);
				
			});		
		});
		
		function check_input(init_check, $input, $input_help, req_field, regex, elem_type)
		{			
			var $input_parent = $input.parents('p.input');
			
			var value = $input.val();
			var valid = regex.test(value);
			
			if (elem_type == 'checkbox')
			{
				valid = $input.attr('checked');						
			}
			
			if (valid)
			{
				$input_help.addClass('show');
				$input_parent.addClass('success');
			}
			else
			{
				$input_help.removeClass('show');
				$input_parent.removeClass('success');
			}
		}
	};
	
})(jQuery);