$(document).ready(function() {
   
   // prepare the reminder form
   prepReminderForm();
   
   // prepare the suggestion links
   prepSuggestLinks();
   
   // turn on the event tabs
   $('ul.tabs').tabs();
});

function prepSuggestLinks() {
   
   // change the href of the "suggest a topic" links so they use the ajax form
   $('a.suggest-a-topic').attr('href', 'ajax-topic-suggestion/?width=600&amp;height=450');
}

function prepReminderForm() {
   
   // convert the form to an ajax form
   $('#news-reminder form').ajaxForm({
      url: '/events/ajax-reminder-signup/',
      target: $('#news-reminder'),
      success: function(responseText) {
         prepReminderForm();
      }
   });
   
   // replace the ugly submit button with a stylized button (a tag)
   var submitBtn = $('.event-tab form input.submit');
   submitBtn.hide();
   submitBtn.after('<div class="blue button submit"><span><a href="#">Submit</a></span></div>');
   $('.event-tab form .submit a').click(function(e) {
      if (submitBtn.length > 0) {
         submitBtn.trigger('click.form-plugin', e);
         $(submitBtn[0].form).trigger('submit.form-plugin');
      }
      return false;
   });
   
   // replace the unsubscribe button with a plain link (a tag)
   var unsubscribeBtn = $('.event-tab form input.unsubscribe');
   unsubscribeBtn.hide();
   unsubscribeBtn.after('<p><a class="unsubscribe" href="#">Unsubscribe me</a>.</p>');
   $('.event-tab form a.unsubscribe').click(function(e) {
      if (unsubscribeBtn.length > 0) {
         unsubscribeBtn.trigger('click.form-plugin', e);
         $(unsubscribeBtn[0].form).trigger('submit.form-plugin');
      }
      return false;
   });
   
   // add "Enter Email Address" directions to the email input
   var emailMsg = "Enter Email Address";
   var emailField = $('.event-tab form input[name=email]');
   if (emailField.val() == '' || emailField.val() == emailMsg) {
      
      emailField.css('color', '#888');
      emailField.val(emailMsg);
      emailField.focus(function() {
         field = $(this);
         if (field.val() == emailMsg) {
            field.val("");
            field.css('color', '#494949');
         }
      });
   }
}