// Vertikales Ping-Pong mit einem HTML-Element
var x = 0;             // current top-position of the div-area
var speed = 50;        // Intervall in ms, in dem das Zitat um ein Pixel bewegt wird
var xoff = 1;          // offset-Wert 1=down; -1=up
var browser = "";      // default-Wert
var idz = "zitat";     // Default-Bezeichner für Zitat (div-area)
var idt = "body";     // Höhe der Tabelle
var zh, th;            // Tabellen und Zitathöhe
var pause = 5000       // Anfangspause in ms
var tstart;            // Startzeit
var scroller;		   // Timeout-Variable
var divs = new Array();

function getBrowser() {
  if (browser == "") {
    // IE4
    if (document.all) {
      browser = "ie4";
    }
    // IE5, IE6, Netscape6, Opera6
    // xxx - Blödsinn, hier wird noch offset abgefragt, tasächlich wird aber style.top verwendet
    // if ((document.getElementById) && (document.getElementById(idz).offsetTop >= 0)) {
    if (document.getElementById) {
      browser = "ie5c";
    }
    // Netscape 4.x
    if (document.layers) {
      browser = "n4";
    }
  }
  return browser;
}

// die wichtigsten Parameter initialisieren
function init() {
  tstart = new Date().getTime();
  getParams();
}

// startet das Script
function start() {
  setInterval("run()", speed);
}

// move the object within a certain interval over the screen
function run() {
  if (new Date().getTime() - tstart <= pause) {
    // warten
  }
  else {
    move(idz, true, 0, th-zh);
  }
}

// Höhe des Zitats bestimmen
function getParams() {
  if (getBrowser() == "ie4") {
    zh = document.all[idz].offsetHeight;
    th = document.all[idt].offsetHeight;
  }
  if (getBrowser() == "ie5c") {
    zh = parseInt(document.getElementById(idz).offsetHeight);
    th = parseInt(document.getElementById(idt).offsetHeight);
  }
  if (getBrowser() == "n4") {
    zh = document.layers[idz].clip.height;
    // wenn sich die Tabellenhöhe nicht als Parameter auslesen läßt, hilfsweise body benutzen
    th = document.layers[idt].clip.height;
  }
}


// actually moves the object
function move(id, turn, xMin, xMax) {
  x = x + xoff;
  // turn around at top and bottom
  if (turn) {
    if ((x == xMax) || (x == xMin)) {
      xoff = xoff * -1;
    }
  }
  else {

  }
  xset = x + "px";
  if (getBrowser() == "ie4") {
    document.all[id].style.top = xset;
  }
  if (getBrowser() == "ie5c") {
    // document.getElementById(idz).style.top = xset;
    document.getElementById(id).style.top = xset;

  }
  if (getBrowser() == "n4") {
    xnset = parseInt(xset);
    document.layers[id].top = xnset;
  }
}

// Popup-Fenster
function popWin(w, h, target, scrollbars) {
  posy = (screen.height - h)/2;
  posx = (screen.width - w)/2;
  param = new String("width=" + w + ",height=" + h + ",screenX=" + posx + ",screenY=" + posy + ";resizable=no,dependent=yes");
  if (scrollbars) param = param + ", scrollbars";
  popup = window.open("", target, param);
  popup.focus();
}
function popBio(w, h) {
  param = new String("width=" + w + ",height=" + h + ",resizable=yes,dependent=yes,scrollbars=yes");
  popup = window.open("", "bio", param);
  popup.focus();
}

function isVisible(id) {
  return (document.getElementById(id).style.visibility == 'visible');
}

function startMove(diff, id) {
  stopMove();
  scroller = setInterval("move('" + id + "', " + diff + ")", speed/2);
}
/*
function startMove(diff) {
  var id = getActiveDiv();
  stopMove();
  scroller = setInterval("move('" + id + "', " + diff + ")", speed/2);
}
*/

function move(id, diff) {
  var i = parseInt(document.getElementById(id).style.left);
  // document.getElementById("zitat").innerHTML = i;
  i = i + diff;
  var min = diff - parseInt(document.getElementById(id).offsetHeight);
  if (i > 10 || i < min) { stopMove(); }
  else { document.getElementById(id).style.left = i; }
}

function moveLeftRight(diff, id, min) {
  var i = parseInt(document.getElementById(id).style.left);
  i = i + diff;
  if (i >= 0) { i = 0; }
  if (i <= min) { i = min; }
  // zur Kontrolle
  // document.getElementById("zitat").innerHTML = i + "/ " + min;
  document.getElementById(id).style.left = i;
}


function stopMove() {
  clearInterval(scroller);
}

function change(idnew) {
  var id;
  var length = divs.length;
  for(i = 0; i < length; i++) {
    document.getElementById(divs[i]).style.visibility = 'hidden';
    // document.getElementById(divs[i]).style.display = 'none';
  }
  document.getElementById(idnew).style.visibility = 'visible';
  // document.getElementById(idnew).style.display = 'block';
  document.getElementById('title').firstChild.nodeValue = idnew;
  /*
  if(getBrowser() == 'ie5c') {
    alert(document.getElementById('title').innerHTML);
    document.getElementById('title').innerHTML = idnew;
  }
  */
}

function initDivs(divarray) {
  for(i = 0; i < divarray.length; i++) {
    divs[i] = divarray[i];
  }
  if(divs.length > 0) {
    change(divs[0]);
  }
}

function getActiveDiv() {
  var id;
  var length = divs.length;
  for(i = 0; i < length; i++) {
    id = divs[i];
    if(document.getElementById(id).style.visibility == 'visible') {
      break;
    }
  }
  return id;
}
