// JavaScript Document
var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject() {
  var xmlHttp;
  try {
    xmlHttp = new XMLHttpRequest();
  } catch(e) {
    var xmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
                                    'MSXML2.XMLHTTP.5.0',
                                    'MSXML2.XMLHTTP.4.0',
                                    'MSXML2.XMLHTTP.3.0',
                                    'MSXML2.XMLHTTP',
                                    'Microsoft.XMLHTTP');
    for(var i = 0; i < xmlHttpVersions.length && !xmlHttp; i++) {
      try {
        xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
      } catch(e) { 
      }
    }
  }
    
  if(!xmlHttp) {
    alert("Das XMLHttpRequest-Object konnte nicht erstellt werden!");
  } else {
    return xmlHttp;
  }
}

function loadPicture(direction) {
  if(xmlHttp) {
    try { 
      var parameter = "direction=" + encodeURI(direction) + "&now="+ document.getElementById('now').value;
      
      xmlHttp.open("GET", "/xml/loadRetroPicture.php"+"?"+parameter, true);
      xmlHttp.onreadystatechange = handleXmlRequest;
      xmlHttp.send(null);
    } catch (e) {
      alert("Can't connect to Server."+ e.toString());
    }
  }
}


function handleXmlRequest() {
  if(xmlHttp.readyState == 4) {
    if(xmlHttp.status == 200) {
      try {
        handleServerResponse();
      } catch(e) {
        alert("Can't reading the response."+e.toString());
      }
    } else {
      alert("There is a problem with the status:\n" + xmlHttp.statusText);
    }
  }
}


function handleServerResponse() {
  /* Fehlerbehandlung */
  var xmlResponse = xmlHttp.responseXML;  
  if(!xmlResponse || !xmlResponse.documentElement) {
    // fängt Fehler beim IE und Opera ab
    throw("Fehlerhafter XML-Aufbau. \n" + xmlHttp.responseText);
  }
  
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if(rootNodeName == "parsererror") {
    // fängt Fehler beim Firefox ab
    throw("Fehlerhafter XML-Aufbau. \n" + xmlHttp.responseText);
  }
  
  
  xmlRoot = xmlResponse.documentElement;  
  var id = xmlRoot.getElementsByTagName("id");
  var beschreibung = xmlRoot.getElementsByTagName("beschreibung");
  var back = xmlRoot.getElementsByTagName("back");
  var next = xmlRoot.getElementsByTagName("next");
  
  if(beschreibung.item(0).hasChildNodes() == true) var beschreibung = beschreibung.item(0).firstChild.data;
  else var beschreibung = "";
  document.getElementById('pictureRetro').innerHTML = '<a href="javascript://" onclick="show(\'box\');" title="'+beschreibung+'"><img src="/member/includes/thumbnail/thumbnailRetro.php?id='+id.item(0).firstChild.data+'" alt="'+beschreibung+'" /></a>';
  document.getElementById('now').value = id.item(0).firstChild.data;

  if(back.item(0).firstChild.data == "1") document.getElementById('back').style.display = "block";
  else document.getElementById('back').style.display = "none";
  
  if(next.item(0).firstChild.data == "1") document.getElementById('next').style.display = "block";
  else document.getElementById('next').style.display = "none";
  
  document.getElementById('headlineBox').innerHTML = beschreibung;
  document.getElementById('contentBox').innerHTML = '<img src="/member/includes/thumbnail/thumbnailRetro.php?id='+id.item(0).firstChild.data+'&box=1" alt="'+beschreibung+'" />';
}

function show(id) {
  document.getElementById(id).style.display = "block";
}

function hide(id) {
  document.getElementById(id).style.display = "none";
}