document.observe("dom:loaded", function() {
// hides hints we provide for non-javascript enabled browsers
$$('.default_hidden').invoke('hide');
})

document.observe("dom:loaded", function() {
// focus first field of form where form's first field is a text input element
  if (document.forms[1] != null && document.forms[1].elements[1] != null && document.forms[1].elements[1].type == 'text')
    document.forms[1].elements[1].focus();
})


/******************************
 * Comments
 ******************************/

function comment_setup() {
  $$('div.comment form textarea').each(function(textarea) {
    textarea.innerHTML = 'Add a comment...';
    textarea.value = 'Add a comment...';                      // needed for Firefox
    textarea.observe('focus', function () {
      if (textarea.innerHTML == 'Add a comment...' || $('comment_body').value == 'Add a comment...') {
        textarea.innerHTML = '';
        textarea.value = '';                                  // needed for Firefox
      }
    });
  });
  
  // clear existing submit event handler (if any)
  $$('div.comment form').each(function(form) {
    form.stopObserving('submit', comment_form_submit);
  });
  
  // set up submit event handler
  $$('div.comment form').each(function(form) {
    form.observe('submit', comment_form_submit);
  });
}

function comment_form_submit(event) {
  Event.stop(event);
  var form_element = Event.element(event);
  var div_element = form_element.up();
  $(Event.element(event)).request({
    onSuccess: function(transport) {
      div_element.insert({before: transport.responseText});
      var comment_body = div_element.down('textarea');
      comment_body.innerHTML = 'Add a comment...';
      comment_body.value = 'Add a comment...';       // needed for Firefox
    },
    onFailure: function() {
      alert('A problem occurred trying to save your comment. Please send us an e-mail informing us of this problem.');
    }
  });
}

document.observe('dom:loaded', hint_text_observer);
function hint_text_observer() {
  $$('.hint_text').each(function(e) {
    e.setAttribute('original_value',e.readAttribute('value'));
    
    e.observe('focus', function(e){
      e = e.target;
      if(e.value == e.readAttribute('original_value')) 
        e.value = '';
    });
    
    e.observe('blur', function(e) {
      e = e.target;
      if(e.value == '')
        e.value = e.readAttribute('original_value');
    })
  })
}

