$(document).ready(function(){
  var aktuellesBild = 0;
  xmlAuslesenFotos();
});

function Zufallsbild(){
  var AnzahlBilder = $("#Bilder-gross").children().size();
  var Zufallszahl = Math.floor(Math.random() * AnzahlBilder) + 1;
  aktuellesBild = Zufallszahl;
  $("#Bilder-gross").children("img:nth-child(" + Zufallszahl + ")").fadeIn();
  $("#Bildtitelcontainer").children("div:nth-child(" + Zufallszahl + ")").fadeIn();
}

function Bildauswahl(Bildnummer){
  Bildnummer++;
  if (Bildnummer != aktuellesBild) {
    $("#Bilder-gross").children("img:nth-child(" + aktuellesBild + ")").fadeOut();
    $("#Bilder-gross").children("img:nth-child(" + Bildnummer + ")").fadeIn();
    $("#Bildtitelcontainer").children("div:nth-child(" + aktuellesBild + ")").fadeOut();
    $("#Bildtitelcontainer").children("div:nth-child(" + Bildnummer + ")").fadeIn();
    aktuellesBild = Bildnummer;
  }
}

function Mausverhalten(){
  $(".Minibild").css({
    opacity: 0.5
  });
  $(".Minibild").mouseover(function(){
    $(this).css({
      opacity: 1.0
    });
  });
  $(".Minibild").mouseout(function(){
    $(this).css({
      opacity: 0.5
    });
  });
}

function xmlAuslesenFotos(){
  //
  // XML als JSON parsen
  $.get('gartenreisen.xml?' + jetzt(), function(xml){
    var fotos = $.xml2json(xml);
    //
    // Variablen initialisieren
    var htmlAusgabe = '';
    var htmlAusgabeMini = '';
    var htmlAusgabeTitel = '';
    //
    // Die Daten jedes XML-Eintrags in HTML ablegen
    for (i = 0; i < fotos.Foto.length; i++) {
      var naechstesBild = 0;
      if (i < fotos.Foto.length-1) {
        naechstesBild = i + 1;
      } else {
				naechstesBild = 0;
			}
      htmlAusgabe += '\n  <img class="Bildergalerie" alt="" src="' + fotos.Foto[i].URL + '" onclick="Bildauswahl(' + naechstesBild + ')"/>';
      htmlAusgabeMini += '\n  <td><img class="Minibild" alt="" onclick="Bildauswahl(' + i + ')" src="' + fotos.Foto[i].URL + '" width="24px" height="18px"/></td>';
      htmlAusgabeTitel += '\n <div class="Bildtitel">' + fotos.Foto[i].Titel + '</div>'
    }
    //
    // neuen Inhalt in die Tabelle einfügen
    $("#Bilder-gross").empty().append(htmlAusgabe);
    $(".Bildergalerie_Minibilder tbody tr").empty().append(htmlAusgabeMini);
    $("#Bildtitelcontainer").empty().append(htmlAusgabeTitel);
    //
    // Transparenz für Bildtitel festlegen
    $(".Bildtitel").css("opacity", "0.7");
    //
    // Zufallsbild für den Start erzeugen
    Zufallsbild();
    //
    // Mausverhalten für Minibilder festlegen
    Mausverhalten();
  })
}

