(function($){
  jQuery.support.placeholder = (function(){
    var i = document.createElement('input');
    return 'placeholder' in i;
  })();
  
  jQuery.fn.inputToggleValue = function () {
      $(this).val($(this).attr('placeholder'));
      
      $(this).focus(function(){
        //toggleValue($(this));
        if ($(this).attr('value') == $(this).attr('placeholder')) {
          $(this).attr('value', '');
        }
      });

      $(this).blur(function() {
        if ($(this).attr('value') == ''){
          $(this).attr('value', $(this).attr('placeholder'));
        }
      });
  }
  
  
  jQuery.fn.reCaptchaSlide = function(options) {
    // the options argument is optional
    options = options ? options : {};
    
    options.reCaptchaWrapper = $(this);
    options.reCaptchaHeightVal = Number(options.reCaptchaWrapper.height());
    options.reCaptchaForm = options.reCaptchaForm ? $(options.reCaptchaForm) : $(this).parents('form');
    options.formInPadding = options.formInPadding ? options.formInPadding : false;
    options.speed = options.speed ? options.speed : 'slow';
  
    if (options.formInPadding) {
      // padding needs adjusted as well
      options.outerWrapper = options.outerWrapper ? options.outerWrapper : '#mainBody';
      options.outerWrapperPaddingArea = options.outerWrapperPaddingArea ? options.outerWrapperPaddingArea : 'paddingBottom';
    
      options.outerWrapperPaddingVal = Number($(options.outerWrapper).css(options.outerWrapperPaddingArea).replace('px', ''));
    
      options.reCaptchaForm.find('input, textarea').change(function(){
        var emptyForm = true;
      
        options.reCaptchaForm.find('input[type="text"], textarea').each(function(){
          if ($(this).val() != '' && $(this).val() != $(this).attr('placeholder')) {
            emptyForm = false;
          }
        });
      
        if (emptyForm) {
          //console.log('form is empty');
          // hide the captcha
          this.tempAnimateOptions = {};
          this.tempAnimateOptions[options.outerWrapperPaddingArea] = options.outerWrapperPaddingVal;
          
          $(options.outerWrapper).animate(this.tempAnimateOptions, options.speed);

          options.reCaptchaWrapper.slideUp(options.speed);
        }
        else {
          //console.log('form is not empty');
          if (options.reCaptchaWrapper.is(':hidden')) {
            this.tempNewPaddingHeight = options.outerWrapperPaddingVal + options.reCaptchaHeightVal;
            if (options.outerWrapperPaddingVal < this.tempNewPaddingHeight) {
              this.tempAnimateOptions = {};
              this.tempAnimateOptions[options.outerWrapperPaddingArea] = this.tempNewPaddingHeight;
              
              $(options.outerWrapper).animate(this.tempAnimateOptions, options.speed);
            }

            options.reCaptchaWrapper.slideDown(options.speed);
          }
        }
      });
    }
    else {
      options.reCaptchaForm.find('input, textarea').change(function(){
        var emptyForm = true;
      
        options.reCaptchaForm.find('input[type="text"], textarea').each(function(){
          if ($(this).val() != '' && $(this).val() != $(this).attr('placeholder')) {
            emptyForm = false;
          }
        });
      
        if (emptyForm) {
          //alert('form is empty');
          // hide the captcha
          options.reCaptchaWrapper.slideUp(options.speed);
        }
        else {
          //alert('form is not empty');
          if (options.reCaptchaWrapper.is(':hidden')) {

            options.reCaptchaWrapper.slideDown(options.speed);
          }
        }
      });
    }
  
  }
})(jQuery);

(function($){
  $(document).ready(function(){
    // slide the recaptcha into view on change
    $('#ftrMsgForm .reCaptchaWrapper').reCaptchaSlide();
  
  
    // perform actions on recaptcha forms
    // since recaptcha only works on first form, prevent others from being used
    if ($('.reCaptchaForm').length > 1) {
      $('.reCaptchaForm').each(function(index, value) {
        if (index == 0) {
          // this is the first form, the one that has the recaptcha
          // test to see if their is already content in form (page refreshed)
          $(this).find('input[type="text"], textarea').each(function(){
            if ($(this).val() != '') {
              // trigger change event so that proper reCaptchaSlide occurs
              $(this).change();
              return;
            }
          });
        }
        else {
          $(this).find('input[type="text"], textarea, select').each(function(){
            $(this).attr({
              'disabled': 'disabled', 
              'value':'Disabled'
            });
          });
        }
      });
    }
  
  
    // watch submit of footer form and submit via ajax
    $('form').submit(function(e){
      e.preventDefault();
      var form = $(this);
      form.mask('Loading...');
      $.ajax({
        type: 'POST',
        url: '/framework/ajax-forms/form-handler.php',
        data: $(this).serialize(),
        success: function(data){
          form.unmask();
          if (data.success == true) {
            form.replaceWith('<div id="ftrMsgForm" style="height:'+ form.height() +'px;">'+ data.msg +'</div>');
          } else {
            // display errors
            var errors = 'Please correct the following errors:\n';
            $.each(data.errors, function(index, value){
              if (value != '') {
                errors += index + ": " + value + '\n';
              }
            });
            alert(errors);
            // reload reCaptcha so it will work
            //$('#recaptcha_reload_btn').click();
            Recaptcha.reload ();
          }
        },
        error: function(jqXHR, textStatus, errorThrown){
          //alert('Fatal Error: ' + errorThrown);
          alert('An error occurred and your message was not sent.');
          form.unmask();
        },
        dataType: 'json'
      }
      );
    });
  
  
    // check for placeholder support
    if ($.support.placeholder) {
      // good to go
    } else {
      $('[placeholder]').each(function(){
        $(this).inputToggleValue();
      }).parents('form').submit(function() {
        $(this).find('[placeholder]').each(function() {
          var input = $(this);
          if (input.val() == input.attr('placeholder')) {
            input.val('');
          }
        })
      });
    }
    
  });
})(jQuery);
