/* 
Tokofu Handler for "contact me" form
Requires jquery 1.2.6+

Copyright (c) 2009 Tokofu, All Rights Reserved

1.0	| 04.Mar.2009 | Initial Release
*/

if (jQuery) {
  jQuery(function($) {

    // hide form
    function hideForm(obj, speed, timeout) {
      speed = (speed == undefined) ? 'fast' : speed;

      if (timeout != undefined) {
        setTimeout(function() {
          hideForm('#contactform', speed);
        }, timeout);
      } else {
        $(obj).slideUp(speed, function() {
          // update status
          $(obj).removeClass('showing');
          clearForm('#contactform');
        });
      }
    }

    // show form
    function showForm(speed) {
      speed = (speed == undefined) ? 'fast' : speed;
      
      var form = $.find('#contactform');

      // if the form exists, remove it, and return
      if ($(form).hasClass('showing')) {
        hideForm('#contactform', 1200);
      } else {
        $(form).show().addClass('showing');
        $.scrollTo( '#contactform', speed );
        
        // don't use animate ( {scrollTop} ) for this as it is the wrong
        // way to do it - and breaks in Opera
      }        
      return false;
    }

    // wire up the form buttons following the ajax get
    function wireUpForm(obj) {
      // wire up the reset button
      // run this as a function to prevent it executing on load
      obj.find('button[type="reset"]').bind('click', function() {
        hideForm('#contactform', 1200);
        return false;
      });
  
      // wire up the submit button to ajax post
      obj.submit(function() {
        // check the required fields were completed
        var valid = true;
        $(this).find('.required').each(function() {
          if ($(this).val() == '') {
            valid = false;
          }
        });
        // error if the required fields were not completed
        if (!valid) {
          $('<p class="error">Required fields were not filled in</p>')
            .insertAfter(obj.find('fieldset .description'));                
          return false;
        }
      
        // concatenate the url to the message
        // we do this to prevent the OpenID plugins from interfering
        var comment = $(this).find('#comment');
        var url = $(this).find('#url');
      
        comment.val("URL: " + url.val()
                      + "\n\n" + comment.val());
        url.val('');
      
        // required data is completed so serialize it
        var data = $(this).serialize();
      
        // emote(data);
      
        // now post the data
        $.ajax({
          'url': this.action,
          'type': 'POST',
          'data': data,
          'success': function(html) {
            obj.find('fieldset').empty().append(
              $('<p class="description">Your message has been successfully sent</p>'));
              hideForm('#contactform', 'slow', 1200);
          },
          'error': function() {
            $('<p class="error">Your comment could not be sent</p>')
              .insertAfter(obj.find('fieldset .description'));
            hideForm('#contactform', 'slow', 1200);
          }
        });            
        return false; 
      });
    }
  });
}
