
(function( $ ){

  $.fn.error = function(options) {
  
/**  we can't validate the same reCaptcha challenge twice, becuase we have to keep the captcha
	 check on the server to prevent a bot from submitting for POST's directly [skipping the JS validation
	 on the form itself, for now we cannot do inline captcha valiation
	 
 	jQuery.validator.addMethod("captcha", 
								function(value, element, params) 
								{ 
									return $.validator.methods.remote.call(this, value,element,params + '&recaptcha_challenge_field=' + $(element).closest('form').find('#recaptcha_challenge_field').val());
								},
								$.format('Captcha is not valid!')
							  );
 */
 
	jQuery.validator.addMethod("requiredghost", 
								function(value, element, params) 
								{ 
									return ( this.optional(element) || ( $.trim(value) != '' && $(element).attr('data-ghostinput') != value ) );
								},
								$.format("This field is required")
							  );
  
	var defaults = {errorClass:'this-is-blank',inline:false,highlightSuccessful:false,hideError:true};
	var options = jQuery.extend(defaults, options);

	return this.each( function ()
							{					
								var form = $(this);
								
								var validator = $(this).validate({
													rules: options.rules,
													messages: options.messages,
													errorClass:options.errorClass,
									  
													highlight: function(element, errorClass) 
													{
														var el = $(element).closest('div').addClass('error-highlight' + (options.inline ? ' inline' : '')).next('.validation-result');
														if ( options.hideError === true && $(element).val() == '' ) 
														{
															el.hide();
														}
														else														
														{
															el.removeClass('input-success').addClass('input-error').show();
														}	
													},
													
													unhighlight: function(element, errorClass) 
													{
														var el = $(element).closest('div').removeClass('error-highlight' + (options.inline ? ' inline' : '')).next('.validation-result');
														if ( !options.highlightSuccessful )
														{
															el.hide();
														}	
													},
													
													errorPlacement: function(error, element) 
													{
														// check for the error input div in case it has been created already
														var result = element.closest('div').next('.validation-result');
														if ( result.length == 0 )
														{
															if ( options.inline ) { element.closest('div').css({display:'inline-block'}); }
															result = $('<div class="validation-result input-error' + (options.inline ? ' inline' : '')+'"></div>');
															element.closest('div').after(result);
														}	

														error.appendTo(result);
														if ( options.hideError === true && $(element).val() == '' )
														{
															result.hide();
														}
														else														
														{
															result.show();
														}
													} 
													
													,success: function(label) 
													{ 
														if ( options.highlightSuccessful )
														{
															label.parent().removeClass('input-error').addClass("input-success").show();
														}	
														else
														{
															label.parent().hide();
														}
													} 
												 });
												 
								if ( options.errors )
								{
									validator.showErrors(options.errors);
								}		

								return validator;								
							}
						);
  };
})( jQuery );
		
