var current_id = null;

// read current ID from query strin
function qdf_get_current_id () {
// Build an empty URL structure in which we will store
    // the individual query values by key.
    var objURL = new Object();
     
     
    // Use the String::replace method to iterate over each
    // name-value pair in the query string. Location.search
    // gives us the query string (if it exists).
    window.location.search.replace(
      new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
       
        // For each matched query string pair, add that
        // pair to the URL struct using the pre-equals
        // value as the key.
        function( $0, $1, $2, $3 ){
          objURL[ $1 ] = $3;
        }
    );
    
    if (objURL['ID']) {
      return objURL['ID'];
    }
    return null;
}

function qdf_focus_post (new_id) {
  if (!current_id) {
    current_id = qdf_get_current_id ();  
  }
  
  // no id could be determined. abort
  if (!current_id) return;
  if (!new_id) return;
  
  // do nothing if ids coincide
  
  qdf_show (new_id);

  if (current_id == new_id) return;
  
  // set current ul to correct css class
  // set new current ul to correct css class
  
  var ul_new = document.getElementById ('ul-'+new_id);
  var ul_current = document.getElementById ('ul-'+current_id);
  
  var ne_new_parent = document.getElementById ('nediv-'+new_id);
  var ne_div = document.getElementById ('disk-answer');
  var ne_form = document.getElementById ('disk-answer-form');
  
  // the divs and uls must all be present
  if (!ul_new || !ul_current || !ne_new_parent || !ne_div) {
    return;
  }
  // neuen aktiv erscheinen lassen
  if (ul_new.className == 'l0') {
    ul_new.className = 'l0-active';
  }
  if (ul_new.className == 'l1') {
    ul_new.className = 'l1-active';
  }
  // alten inaktiv erscheinen lassen
  if (ul_current.className == 'l0-active') {
    ul_current.className = 'l0';
  }
  if (ul_current.className == 'l1-active') {
    ul_current.className = 'l1';
  }
  
  // nun noch die Antwortbox umhängen
  ne_new_parent.appendChild (ne_div);
  
  ne_form.action = ne_form.action.replace ("ID="+current_id, "ID="+new_id);
  
  
  // aktuelle ID für später merken
  current_id = new_id;
}



function qdf_toggle (id) {
  var div = document.getElementById ('div-'+id);
  var togglelink = document.getElementById ('toggle-'+id);
  if (div && togglelink) {
    if (div.className == 'post-hidden') {
      div.className = 'post-visible';
      togglelink.className = "arrow-up toggle-post";
    } else {
      div.className = 'post-hidden';
      togglelink.className = "arrow-down toggle-post";
    }
  }
}

function qdf_show (id) {
  var div = document.getElementById ('div-'+id);
  var togglelink = document.getElementById ('toggle-'+id);
  if (div && togglelink) {
    div.className = 'post-visible';
    togglelink.className = "arrow-up toggle-post";
  }
}

function qdf_hide (id) {
  var div = document.getElementById ('div-'+id);
  var togglelink = document.getElementById ('toggle-'+id);
  if (div && togglelink) {
    div.className = 'post-hidden';
    togglelink.className = "arrow-down toggle-post";
  }
}


function qdf_show_posts () {
  for (i=0; i < qdf_post_ids.length; i++) {
    qdf_show (qdf_post_ids[i]);
  }
}
function qdf_show_new_posts () {
  qdf_hide_posts ();
  for (i=0; i < qdf_new_ids.length; i++) {
    qdf_show (qdf_new_ids[i]);
  }
}
function qdf_hide_posts () {
  for (i=0; i < qdf_post_ids.length; i++) {
    qdf_hide (qdf_post_ids[i]);
  }
}
