// ***************************************************************************
// SHARED FUNCTIONS
// ***************************************************************************

// ===========================================================================
// Adds events to be executed after the document is loaded.
// By Simon Willison (See http://simonwillison.net)
// ===========================================================================

function addLoadEvent(func) {
  var oldonload = window.onload
  if (typeof window.onload != 'function') {
    window.onload = func
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload()
      }
      func()
    }
  }
}

// ===========================================================================
// Hide items marked with class 'hideable'
// ===========================================================================

function hideItems() {
  var hideables = $$('.hideable')
  
  hideables.each(function(hideable) {
    if (hideable.style.display != "none") {
      hideable.toggle()
    }
  })
}


// ***************************************************************************
// FORM TOGGLE SCRIPTS
// ***************************************************************************

function toggleForm() {
  var targetId = this.rel
  var targetForm = $(targetId)

  if (targetForm.style.display == "none") {
    Element.toggle(this)
    Element.toggle(targetForm)
  } else {
    var targetTrigger = $$('#content a.button[rel], #content a.minibutton[rel]')
    targetTrigger.each(function(trigger) {
      if (trigger.rel == targetId) {
        targetTrigger = trigger
      }
    })
    
    Element.toggle(targetForm)
    targetForm.reset()
    Element.toggle(targetTrigger)
  }
  
  
  return false
}

function setToggleForm() {
  var buttons = $$('#content a.button[rel], #content a.minibutton[rel], #content a.cancel[rel]')
  
  buttons.each(function(button) {
    button.onclick = toggleForm
  })
  
  hideItems()
}
