/**
* ansForms jQuery plugin (v.1.0) - http://idc.anavallasuiza.com/ansforms/
*
* ansForms is released under the GNU Affero GPL version 3
*
* More information at http://www.gnu.org/licenses/agpl-3.0.html
*/
(function($) {
	function hasAttrSupport (tag, attr) {
		var object = document.createElement(tag);
		
		if (attr in object) {
			return true;
		}
		
		return false;
	}
	
	function hasInputTypeSupport (type) {
		var object = document.createElement('input');
		
		object.setAttribute('type', type);
		
		return object.type !== 'text';
	}
	
	$.extend({
		ansForms: function (user_settings) {
			var settings = {};
			var inputs = $('input,select,textarea');
			
			$.extend(settings, this.ansForms_default_settings, user_settings);
			
			$.each(settings.attributes, function (attribute, fn) {
				if (!hasAttrSupport(attribute)) {
					if ($.isFunction(fn)) {
						$(inputs).filter('[' + attribute + ']').each(fn);
					} else if (fn === true) {
						switch (attribute) {
							case 'autofocus':
							$(inputs).filter('[' + attribute + ']').focus();
							break;
							
							case 'maxlength':
							$('textarea[maxlength]').attrMaxlength();
							break;
							
							case 'pattern':
							$(inputs).filter('[' + attribute + ']').attrPattern(settings.texts.pattern_error);
							break;
							
							case 'placeholder':
							$(inputs).filter('[' + attribute + ']').attrPlaceholder();
							break;
							
							case 'required':
							$(inputs).filter('[' + attribute + ']').attrRequired(settings.texts.required_error);
							break;
						}
					}
				}
			});

			$.each(settings.types, function (type, fn) {
				if ($.isFunction(fn) && (!settings.only_unsupported || !hasInputTypeSupport(type))) {
					$('input[type=' + type + ']').each(fn);
				}
			});
		},
		
		ansForms_default_settings: {
			'only_unsupported': true,

			'texts': {
				'required_error': 'This value cannot be empty',
				'pattern_error': 'You cannot complete this form until the field is correct.'
			},

			'attributes': {
				'autofocus': true,
				'maxlength': true,
				'pattern': true,
				'placeholder': true,
				'required': true
			},

			'types': {}
		}
	});
	
	$.fn.extend({
		changeInputType: function (type) {
			var input = $(this).clone().attr('type', type).insertAfter(this);
			$(this).remove();
			
			return input;
		},
		attrMaxlength: function () {
			return $(this).click(function () {
				var maxlength = $(this).attr('maxlength');
				
				if (maxlength && maxlength < $(this).val().length) {
					return false;
				}
			});
		},
		attrPattern: function (error_text) {
			if (!error_text) {
				error_text = $.ansForms_default_settings.texts.pattern_error;
			}
			
			return $(this).each(function () {
				var form = $(this).parents('form');
				var input = $(this);
				var val = $(this).val();
				var pattern = $(this).attr('pattern');
				
				$(this).blur(function () {
					if (val && !val.match('/^'+pattern+'$/')) {
						alert(error_text);
						$(this).focus();
						return false;
					}
				});

				$(form).submit(function () {
					if (val && !val.match('/^'+pattern+'$/')) {
						alert(error_text);
						$(this).focus();
						return false;
					}
				});
			});
		},
		attrPlaceholder: function () {
			return $(this).each(function () {
				var placeholder = $(this).attr('placeholder');
				
				if (!$(this).val()) {
					$(this).val(placeholder);
				}
				
				$(this).focus(function () {
					if ($(this).val() == placeholder) {
						$(this).val('');
					}
				});
				
				$(this).blur(function () {
					if ($(this).val() == '') {
						$(this).val(placeholder);
					}
				});
			});
		},
		attrRequired: function (error_text) {
			if (!error_text) {
				error_text = $.ansForms_default_settings.texts.required_error;
			}
			
			return $(this).each(function () {
				var form = $(this).parents('form');
				var input = $(this);

				$(form).submit(function () {
					if (!$.trim($(input).val())) {
						alert(error_text);
						$(input).focus();
						return false;
					}
				});
			});
		}
	});
})(jQuery);
