// Started on load (display last modified)
function body_onload() {
  document.getElementById('lamo').innerHTML=lamo();
}

// Show description of planet in sign or house
function show_planet() {
  var id = 't' + $F('where1') + '_' + $F('planet');
  show(id);
  add_sub_links(id);
}

// Show description of axis in sign
function show_axis() {
  var id = 't' + $F('where2') + '_' + $F('achse');
  show(id);
  add_sub_links(id);
}

// Hide all descriptions
function hide_all() {
  var divs = $('all').getElementsByTagName("div");
  for (var key in divs) {
    if (divs[key].style) divs[key].style.display = 'none';
    add_sub_links(divs[key].id);
  }
  hide('unknown');
}

// Show all descriptions
function show_all() {
  var divs = $('all').getElementsByTagName("div");
  for (var key in divs) {
    if (divs[key].style) divs[key].style.display = 'block';
    add_sub_links(divs[key].id);
  }
  hide('unknown');
}

// Show a description (if it exists, or else the 'unknown' description)
function show(id) {
  if ($(id) && $(id).style) $(id).style.display = 'block';
  else show_unknown();
}

// Show the 'unknown' description
function show_unknown() {
  if ($('unknown')) $('unknown').style.display = 'block';
}

// Hide a description
function hide(id) {
  if ($(id) && $(id).style)
    $(id).style.display = 'none';
}

// Add links to a description (like remove, up, down)
function add_sub_links(id) {
  var node = $(id);
  if (!node) return;
  
  var last = node.lastChild;
  if (last.className == "links") return;

  node.innerHTML += "<p class='links'><a href='#' onclick='hide(\""
    + id + "\")'>Verstecken</a> <a href='#' onclick='up(\""
    + id + "\")'>Rauf</a> <a href='#' onclick='down(\""
    + id + "\")'>Runter</a> </p>";
}

// Move a description upwards
function up(id) {
  var node = $(id);
  if (!node) return;

  var uppee = node;
  var parent = uppee.parentNode;
  
  while (node = node.previousSibling) {
    if (node.style && node.style.display == 'block') break;
  }
  if (!node) return;
  
  parent.insertBefore(uppee, node);
}

// Move a description downwards
function down(id) {
  var node = $(id);
  if (!node) return;

  var downee = node;
  var parent = downee.parentNode;
  
  while (node = node.nextSibling) {
    if (node.style && node.style.display == 'block') break;
  }
  if (!node) return;
  
  parent.insertBefore(downee, node.nextSibling);
}

// For aspects: if the user changes one of the planets/axes involved,
// update the option list of the select element
function update_aspect_options() {
  var pa1 = $F('pa1');
  var pa2 = $F('pa2');
  var options = $('aspect').options;
  var has_aspect = false;
  var i;

  // go through all options and verify if there's a description for it
  for (i = 0; i < options.length; i++)
    has_aspect |= update_aspect_option(options[i].value, pa1, pa2);
  
  // if there's no description, display 'no description' option only
  if (!has_aspect) $('opt_none').style.display = '';
  
  // then select the first available option if the current selected is hidden
  if (options[$('aspect').selectedIndex].style.display == 'none') {
    for (i = 0; i < options.length; i++)
      if (options[i].style.display != 'none') break;  
    $('aspect').selectedIndex = i;
  }
}

// Check whether the pair of planets/axes has a description with id
// (for example id='co' for conjunction) and show/hide the option. The
// option 'none' is always hidden because it is shown later if there
// are no other options.
function update_aspect_option(id, pa1, pa2) {
  var opt_id = 'opt_' + id;
  if (id == 'none' || pa1 == pa2) {
    if ($(opt_id)) $(opt_id).style.display = 'none';
    return false;
  }
  
  // Construct the element id
  var tid = 't' + id + '_' +  pa1 + '_' + pa2;
  
  // If not found, swap the planets
  if (!$(tid)) tid = 't' + id + '_' +  pa2 + '_' + pa1;
  
  // If description exists, show its option, else hide it
  $(opt_id).style.display = $(tid) ? '' : 'none';

  // Return truth value about existence (true if it exists)  
  return !!$(tid);
}

// Show an aspect description
function show_aspect() {
  // Construct description element id, swapping planets if neccessary
  var id = 't' + $F('aspect') + '_' + $F('pa1') + '_' + $F('pa2');
  if (!$(id)) id = 't' + $F('aspect') + '_' + $F('pa2') + '_' + $F('pa1');
  
  show(id);
  add_sub_links(id);
}

// Calculate age of page (last modified)
function lamo() {
  var lamo_epoch_s = new Date(document.lastModified).getTime() / 1000;
  var now_epoch_s = new Date().getTime() / 1000;
  var age_s = Math.round(now_epoch_s - lamo_epoch_s);
  
  if (age_s < 2) return "1 Sekunde";
  if (age_s < 60) return age_s + " Sekunden";
  if (age_s < 75) return "1 Minute";
  if (age_s < 105) return "1 1/2 Minute";
  
  var age_m = Math.round(age_s / 60);
  if (age_m < 11) return age_m + " Minuten";
  if (age_m < 22) return "1/4 Stunde";
  if (age_m < 39) return "1/2 Stunde";
  if (age_m < 52) return "3/4 Stunde";
  if (age_m < 90) return "1 Stunde";
  
  var age_h = Math.round(age_m / 60);
  if (age_h < 41) return age_h + " Stunden";
  
  return Math.round(age_h / 24) + " Tage";
}


