/*global $ */
/*jslint browser: true, eqeqeq: true, bitwise: true, devel: true, immed: true, onevar: true, forin: true, evil: true, undef: true */



(function () {

  // shortcuts
  var $fields = $('input[type=text], textarea'),
      $form = $('#get_started');

  $fields.each(function() { // set placeholder text
    if ($(this).val() === '' || $(this).val() === $(this).siblings('label:last').text()) { // if the field is empty or already has the placeholder text (as on reload)
      $(this).addClass('placeholder').val($(this).siblings('label:last').text()); // set 'placeholder' class and text
    }
  }).focus(function() { // handle focused fields
    if ($(this).val() === $(this).siblings('label:last').text()) { // if the field has the placeholder text
      $(this).val('').toggleClass('placeholder'); // clear value and toggle placeholder class
    }
  }).blur(function() { // handle blurred fields
    if ($(this).val().trim() === '') { // if the field is empty
      $(this).toggleClass('placeholder').val($(this).siblings('label:last').text()); // toggle placeholder class and reset placeholder text
    }
  });

  // remove any placeholder text before the form is submitted
  $form.submit(function() {
    $fields.each(function() { // for each field
      if ($(this).val() === $(this).siblings('label:last').text()) { // if the field has the placeholder text
        $(this).val(''); // clear value
      }
    });
  });

  // select 'other' radio button when its field is focused
  $('#other_specify').focus(function() {
    $('#other').attr('checked', 'checked');
  });

  // handle reset button click
  $('.reset').click(function() {
    $form[0].reset(); // reset form text
    $fields.each(function() { // for each field
      $(this).addClass('placeholder').val($(this).siblings('label:last').text()); // set 'placeholder' class and text
    });
    $('.alert').remove(); // remove alert box (if present)
  });

}());
