/**
 * Helper to make all the little ajax contact forms around the site
 * See the email controller for a little more insight as to why this is the way it is.
 *
 * Will turn element you give it into a ajax form for the email controller request action.
 * Will "hide" the form (or hide_element passed) with a "thank you" box after submission.
 *
 * Will *not* work with more than one form per page.  Don't cry.
 */ 
$.fn.email_form = function(hide_element) {

  // hide_element: what to cover up after form has been submitted.
  hide_element = hide_element ? $(hide_element) : this;
  var that = this;
  $butt = $(this).find('.submit input,.submit-button');


  $butt.click(function(e) {
    e.preventDefault();
    console.log('send');

    var data = {};
    $(that).find('input[name],textarea[name]').each(function() {
      if($(this).attr('type')=='checkbox')
        data[$(this).attr('name')] = $(this).attr('checked');
      else
        data[$(this).attr('name')] = $(this).val();
    });
    console.log(that);
    $.post('/email/request', data, function() {
      hide_form();
    });
  });

  function hide_form() {
   var pos = hide_element.offset();
   var h = hide_element.outerHeight();
   var w = hide_element.outerWidth();
   $('<div class="request-hide">Thank You.</div>').css({lineHeight:h+'px',opacity:0,position:'absolute',left:pos.left,top:pos.top,width:w,height:h}).appendTo('body').fadeTo('slow',0.8);
  }
};

