/* GET WIDTH AND HEIGHT */
function getwh() {
  // Declare global variables as well as return them
  if (self.innerHeight) {
    w = self.innerWidth
    h = self.innerHeight
  }
  else if (document.documentElement && document.documentElement.clientHeight) {
    w = document.documentElement.clientWidth
    h = document.documentElement.clientHeight
  }
  else if (document.body) {
    w = document.body.clientWidth
    h = document.body.clientHeight
  }
  return true
}

/* ADJUST PAGE HEIGHT */
function adjust() {
  var rightextra, ah

  // Get page height
  var wh = getwh()

  // Extra height if the right column is too tall
  rightextra = (rightcdiv.offsetHeight + 185) - (h - footerdiv.offsetHeight)
  // But not the other way round
  rightextra < 0 ? rightextra = 0 : rightextra = rightextra

  // Get available height
  ah = h - menudiv.offsetHeight - search_form.offsetHeight - footerdiv.offsetHeight

  // Adjust it!
  document.getElementById(adjustid).style.minHeight = (ah - eh + rightextra) + 'px'
}


/* SHOW OR HIDE SEARCH FORM */
function shsform() {
  if (search_link.className == '') {
    search_link.className = 'clicked'
    search_form.style.display = 'block'
    search_field.focus()
  }
  else if (search_link.className == 'clicked') {
    search_link.className = ''
    search_form.style.display = 'none'
  }

  adjust()
}

/*
function send_resolution() {
  var wh = getwh()
  // Submit screen resolution along with request number
  var http = new httpobj()
  http.url = dirup + "site/add-screen-resolution.php"
  http.param['a'] = request_number
  http.param['b'] = w + 'x' + h + ',' + screen.width + 'x' + screen.height
  http.method = 'get'
  http.send()
}
*/

/* ONLOAD */
window.onload = function() {
  // Extra height is 0
  eh = 0

  if (document.getElementById('text')) {
    adjustid = 'text'
    // Extra height is 10 because padding = 5 [5] 25 [5] (5+25)
    eh = 30
  }
  else if (document.getElementById('stuff')) {
    adjustid = 'stuff'
    // Extra height is 32 because of padding and margin
    eh = 52
  }

  // Get search elements
  search_link = document.getElementById('searchlink')
  search_form = document.getElementById('searchform')
  search_field = document.getElementById('searchfield')

  // Only show/hide search box if not on search page
  if (search_link.className.indexOf('current') == -1)
    search_link.onclick = shsform
  else
    search_link.onclick = function() {
      search_field.focus()
    }

  menudiv = document.getElementById('menu')
  footerdiv = document.getElementById('footer')
  leftcdiv = document.getElementById('leftcolumn')
  rightcdiv = document.getElementById('rightcolumn')

  // If an element exists
  if (eh) {
    // Adjust
    adjust()

    // Prepare to adjust on resize
    window.onresize = adjust
  }

  //setTimeout('send_resolution()', 500) // Some browsers need time for this

  try {extra()}
  catch(e) {}
}
