/**
 * $.inputTitle
 * 
 * Hides the default text on focus and restores on blur if the field is left empty
 * 
 * @param object options An options object (Optional)
 * @return void
 * @author Dom Hastings
 */
(function($) {
  $.fn.inputTitle = function(options) {
    /**
     * options
     * 
     * @var object The default options
     * @property string active A classname to apply to the element when it has focus
     * @property string inactive A classname to apply to the element when it has lost focus
     * @property string read The attribute to read the default text from
     * @property string write The attribute to write the default text to
     */
    options = $.extend({
      'active': '',
      'inactive': '',
      'read': 'title',
      'write': 'value'
    }, options || {});
    
    // loop through all matched elements
    $(this).each(function() {
      // store the options locally
      $(this).data('inputTitleOptions', options);
      
      // if the title has been omitted, use the current value
      if (!$(this).attr(options.read)) {
        $(this).attr(options.read, $(this).val());
      }
      
      // if we have an inactive class, set it now (assumes any focus events will happen after this)
      if (options.inactive) {
        $(this).add(options.inactive);
      }
      
      // bind the blue function
      $(this).blur(function() {
        // load the options
        var options = $(this).data('inputTitleOptions');
        
        // remove the active classname if specified
        if (options.active) {
          $(this).remove(options.active);
        }
        
        // remove the inactive classname if specified
        if (options.inactive) {
          $(this).add(options.inactive);
        }
        
        // set the attribute if required
        $(this).attr(options.write, ($(this).attr(options.write) == '') ? $(this).attr(options.read) : $(this).attr(options.write));
      });
      
      // bind the focus event
      $(this).focus(function() {
        var options = $(this).data('inputTitleOptions');
        
        if (options.active) {
          $(this).add(options.active);
        }
        
        if (options.inactive) {
          $(this).remove(options.inactive);
        }
        
        $(this).attr(options.write, ($(this).attr(options.write) == $(this).attr(options.read)) ? '' : $(this).attr(options.write));
      });
    });
  }
})(jQuery);


$(function() {

  // contact form
  $('a.ebrochure-submit').click(function() {
    $.ajax({
      type: "POST",
      url: "/ebrochure.php",
      data: "name=" + $('#name').val() + "&organisation=" + $('#organisation').val() + "&email=" + $('#email').val() + "&message=" + $('#message').val() + "&contact_form=" + $('#contact_form').val(),
      success: function(msg){
        $('#ebrochure').html(msg);
      }
    });
  });

  $('a.contact-submit').live('click', function() {
    $.ajax({
      type: "POST",
      url: "/contact.php",
      data: "name=" + $('#name').val() + "&organisation=" + $('#organisation').val() + "&email=" + $('#email').val() + "&telephone=" + $('#telephone').val() + "&message=" + $('#message').val() + "&contact_form=" + $('#contact_form').val(),
      success: function(msg){
        $('#contact_us_form').html(msg);
      }
    });
  });
  
  $('input[type=text], textarea').inputTitle();
});
