var SCROLL_ENDE = 'scroll_ende';
var SCROLL_ANFANG = 'scroll_anfang';
var SCROLL_HOCH = 'scroll_hoch';
var SCROLL_RUNTER = 'scroll_runter';
var REITER_STAB1 = 'reiter_stab1';
var REITER_STAB2 = 'reiter_stab2';
var REITER_PFOSTEN = 'reiter_pfosten';
var REITER_GESAMT = 'reiter_gesamt';

function ZeigeReiter(Reiter) {
  Attribute = new Array();
  xajax_ZeigeReiter(Reiter, KonfIndex);
  
  AlleReiter = new Array('stab1', 'stab2', 'pfosten', 'gesamt');
  for(var i = 0; i < AlleReiter.length; i++) {
    AktReiter = document.getElementById('reiter_' + AlleReiter[i]);
    AktReiter.style.backgroundColor = '#EDEDED';
    AktReiter.style.fontWeight = 'normal';
  }
  
  AktReiter = document.getElementById(Reiter);
  AktReiter.style.backgroundColor = '#FFFFFF';
  AktReiter.style.fontWeight = 'bold';
}

function Konfiguration() {
  this.Hintergrundfarbe = '#C0C0C0';
  this.Hintergrundfarbname = 'Hellgrau';
  this.Stab1 = '0037';
  this.Stab2 = '0107';
  this.AnzahlStaebe = 2;
  this.Pfostenrumpf = '1107';
  this.Pfostenkopf = '2101';
  this.Stabkombination = '12';
  this.Bild = 'temp/gkonf/standard.png';
}

function ReiterGeladen(Reiter) {
  if(Reiter == REITER_GESAMT) {
    ZeigeKonfiguration(KonfIndex);
  }
}

function ZeigeKonfiguration(Konfiguration) {
  Konf = Konfigurationen[Konfiguration];
  KonfIndex = Konfiguration;
  document.getElementsByName('Stab1')[0].value = Konf.Stab1;
  document.getElementsByName('Stab2')[0].value = Konf.Stab2;
  document.getElementsByName('Pfostenkopf')[0].value = Konf.Pfostenkopf;
  document.getElementsByName('Pfostenrumpf')[0].value = Konf.Pfostenrumpf;
  document.getElementsByName('Stabkombination')[0].value = Konf.Stabkombination;
  document.getElementsByName('Hintergrundfarbe')[0].value = Konf.Hintergrundfarbe;
  document.getElementsByName('Hgfarbname')[0].value = Konf.Hintergrundfarbname;
  document.getElementsByName('Konfiguration')[0].value = Konfiguration;
  
  // Visuelle Aspekte
  document.getElementById('ContImg').style.backgroundColor = Konf.Hintergrundfarbe;
  SetzeHintergrundfarbe(Konf.Hintergrundfarbe, Konf.Hintergrundfarbname);
  document.getElementById('ImgGelaender').src = Konf.Bild;
  Thumbs[ThumbIndex].src = Konf.Bild;
  AenderAnzahlStaebe(Konf.AnzahlStaebe);
}

function SetzeKonfiguration(Konfiguration) {
  Konf = Konfigurationen[Konfiguration];

  Konf.Stab1 = document.getElementsByName('Stab1')[0].value;
  Konf.Stab2 = document.getElementsByName('Stab2')[0].value;
  Konf.Pfostenkopf = document.getElementsByName('Pfostenkopf')[0].value;
  Konf.Pfostenrumpf = document.getElementsByName('Pfostenrumpf')[0].value;
  Konf.Stabkombination = document.getElementsByName('Stabkombination')[0].value;
  Konf.Hintergrundfarbe = document.getElementsByName('Hintergrundfarbe')[0].value;
  Konf.Hintergrundfarbname = document.getElementsByName('Hgfarbname')[0].value;
  Konf.Bild = document.getElementById('ImgGelaender').src;
  Konf.AnzahlStaebe = document.getElementsByName('AnzahlStaebe')[0].value;
}

function NeueKonfiguration() {
  // Falls noch nicht alle Thumbbildcontainer erzeugt wurden
  // einen neuen erstellen
  var NKonf = new Konfiguration();
  var NKonfI = Konfigurationen.length;
  Konfigurationen[NKonfI] = NKonf;

  xajax_KonfHinzufuegen();

  for(var i = 0; i < Thumbs.length; i++) {
    if(Thumbs[i] == null) {
      ThumbIndex = i;
      Img = document.createElement('img');
      Img.className = 'Konf';
      Img.style.width = '94px';
      Img.style.height = '85px';
      
      // Img.setAttribute("onclick", "ZK("+ NKonfI + ", " + ThumbIndex + ")");
      
      Img.Konfiguration = NKonfI;
      Img.ThumbIndex = ThumbIndex;
      Img.onclick = ZK;
      
      // Img.onclick = new function() { ZK(NKonfI, ThumbIndex); };
      Thumbs[ThumbIndex] = Img;
      DivKonfs.appendChild(Thumbs[ThumbIndex]);
      break;
    } else if(Thumbs[i].style.visibility == 'hidden') {
      Thumbs[i].style.visibility = 'visible';    
    }
  }
  
  if(Konfigurationen.length > Thumbs.length) {
    Scrollen(SCROLL_ENDE);
  }
  
  ZeigeKonfiguration(NKonfI);
}

function KonfEntfernen() {
  if(Konfigurationen.length > 1) {
    for(var i = KonfIndex; i < Konfigurationen.length-1; i++) {
      Konfigurationen[i] = Konfigurationen[i+1];
    }
    Konfigurationen.pop();
    
    for(var i = ThumbIndex; i < Thumbs.length; i++) {
      if(Thumbs[i] != null) {
        if(i < Konfigurationen.length) {
          Thumbs[i].src = Konfigurationen[i+ObereKonf].Bild;
          // Thumbs[i].setAttribute("onclick", "ZK("+ (i+ObereKonf) + ", " + i + ")");
          // Thumbs[i].onclick = new function() { ZK(i+ObereKonf, i); };
          
          Thumbs[i].Konfiguration = i+ObereKonf;
          Thumbs[i].ThumbIndex = i;
          Thumbs[i].onclick = ZK;
        } else
          Thumbs[i].style.visibility = 'hidden';
      }
    }

    xajax_KonfEntfernen(KonfIndex);
    if(KonfIndex >= Konfigurationen.length)
      KonfIndex = Konfigurationen.length-1;
    
    ZeigeKonfiguration(KonfIndex);
  } else {
    alert("Die Konfiguration kann nicht gelöscht werden,\nda mindestens eine vorhanden sein muss!");
  }
}

// Konfiguration anzeigen, für den Thumbnailclick
//function ZK(Konf, Thumb) {
function ZK() {
  ThumbIndex = this.ThumbIndex;
  KonfIndex = this.Konfiguration;
  ZeigeKonfiguration(KonfIndex);
}

function Scrollen(Position) {
  AnzKonf = Konfigurationen.length;
  AnzThumbs = Thumbs.length;
  
  if(typeof(Position == "string")) {
    if(Position == SCROLL_ENDE)
      Position = AnzKonf - AnzThumbs;
    
    if(Position == SCROLL_ANFANG)
      Position = 0;
    
    if(Position == SCROLL_HOCH)
      Position = ObereKonf - 1;
    
    if(Position == SCROLL_RUNTER)
      Position = ObereKonf + 1;      
  }
  
  if(Position >= 0 && Position <= AnzKonf - AnzThumbs) {
    ObereKonf = Position;
    for(var i = 0; i < Thumbs.length; i++) {
      Thumbs[i].src = Konfigurationen[i+ObereKonf].Bild;
      // Thumbs[i].setAttribute('onclick', "ZK("+ (i+ObereKonf) + ", " + i + ")");
      // Thumbs[i].onclick = new function() { ZK(i+ObereKonf, i); };
      
      Thumbs[i].Konfiguration = i+ObereKonf;
      Thumbs[i].ThumbIndex = i;
      Thumbs[i].onclick = ZK;
    }
  }

  document.getElementById('ScrollPosition').innerHTML = '(' + (ObereKonf+1) + '-' + (ObereKonf + AnzThumbs) + '/' +
                                                        AnzKonf + ')';
}

function WaehleHg() {
  Fenster = window.open("gkonf/gkonf_hg.php", "Hintergrundfarbe", "scrollbars=yes, height=300, width=310, top=300, left=500");
}


WStab = 1;
function WaehleStab(Stab) {
  WStab = Stab;
  Fenster = window.open("gkonf/gkonf_stab.php?wg=edelstahl", "Stab", 
            "scrollbars=yes, height=300, width=300, top=300, left=500");
}

function WaehlePfRumpf() {
  Fenster = window.open("gkonf/gkonf_pfrumpf.php", "Pfostenrumpf", 
                        "scrollbars=yes, height=300, width=300, top=300, left=500")
}

function WaehlePfKopf() {
  Fenster = window.open("gkonf/gkonf_pfkopf.php", "Pfostenkopf", "height=300, width=310, top=300, left=500");
}

function Drucken() {
  Fenster = window.open("index.php?go=gk&druck=druck&konfiguration=" + KonfIndex, "Konfigurationsdruck",
            "scrollbars=yes, height=500, width=500, top=200, left=200, menubar=yes");
  Fenster.print();
}

function HilfeVideo() {
  Fenster = window.open("gkonf/video.html", "Hilfe", "scrollbars=no, width=632, height=575, top=50, left=50");
}


function SetzeStab(Stab) {
  if(WStab != 1 && WStab != 2)
    WStab = 1;
  document.getElementsByName('Stab' + WStab)[0].value = Stab;
}

function SetzePfRumpf(Rumpf) {
  document.getElementsByName('Pfostenrumpf')[0].value = Rumpf;
}

function SetzePfKopf(Kopf) {
  document.getElementsByName('Pfostenkopf')[0].value = Kopf;
}

function SetzeHintergrundfarbe(Farbwert, Farbname) {
  document.getElementsByName('Hintergrundfarbe')[0].value = Farbwert;
  document.getElementsByName('Hgfarbname')[0].value = Farbname;
}

function AenderAnzahlStaebe(Anzahl) {
  if(Anzahl == 1 || Anzahl == 2) {
    ObjTrStab2 = document.getElementById('TrStab2');
    ObjInpStabkombi = document.getElementsByName('Stabkombination')[0];
    ObjSelAnzStaebe = document.getElementsByName('AnzahlStaebe')[0];
    ObjReiterStab2 = document.getElementById(REITER_STAB2);
    
    if(Anzahl == 1) {
      ObjTrStab2.style.visibility = 'hidden';
      ObjInpStabkombi.style.visibility = 'hidden';
      ObjReiterStab2.style.display = 'none';
      ObjSelAnzStaebe.selectedIndex = 0;
    } else
    if(Anzahl == 2) {
      ObjTrStab2.style.visibility = 'visible';
      ObjInpStabkombi.style.visibility = 'visible';
      ObjReiterStab2.style.display = 'block';
      ObjSelAnzStaebe.selectedIndex = 1;
    }
  }
}

function AenderungenUebernehmen(Sender) {
  xajax_AenderungenUebernehmen(xajax.getFormValues('Frm' + Sender));
}

function AenderungenUebernommen(Datei) {
  document.getElementById('ImgGelaender').src = Datei;
  document.getElementById('ContImg').style.backgroundColor = 
  document.getElementsByName('Hintergrundfarbe')[0].value;
  Thumbs[ThumbIndex].src = Datei;
  SetzeKonfiguration(document.getElementsByName('Konfiguration')[0].value);
}

function InitKonfigurationen() {
  DivKonfs = document.getElementById('Konfigurationen');
  Thumbs = new Array(null, null, null);

  for(var i = 0; i < Thumbs.length; i++) {
    if(Konfigurationen[i] != null) {
      if(Thumbs[i] == null) {
        Thumbs[i] = document.createElement('img');
        Thumbs[i].className = 'Konf';
        Thumbs[i].style.width = '94px';
        Thumbs[i].style.height = '85px';
        DivKonfs.appendChild(Thumbs[i]);
      } 
   
      Thumbs[i].src = Konfigurationen[i+ObereKonf].Bild;
      // Thumbs[i].setAttribute('onclick', 'ZK('+ (i+ObereKonf) + ', ' + i +')');
      // Thumbs[i].onclick = new function() { ZK(i + ObereKonf, i); };
      
      Thumbs[i].ThumbIndex = i;
      Thumbs[i].Konfiguration = i+ObereKonf;
      Thumbs[i].onclick = ZK;
    }
  }
  
  // Da Internet-Explorer Mist ist, Umweg gehen
  KonfIndex = 0;
  ThumbIndex = 0;
  ZeigeKonfiguration(0);
  Scrollen(SCROLL_ANFANG);
  
  
  /* Hinweise erstellen */
  var HinwNeu = new Hinweis('Neue Konfiguration erstellen und zu der ' +
                            'Liste der Konfigurationen hinzufügen', 'BtnNeu');
  var HinwEntfernen = new Hinweis('Die aktuell ausgewählte Konfiguration entfernen', 'BtnEntfernen');
  var HinwRunter = new Hinweis('In der Konfigurationsliste nach unten scrollen', 'BtnRunter');
  var HinwHoch = new Hinweis('In der Konfigurationsliste nach oben scrollen', 'BtnHoch');
  var HinwAnfang = new Hinweis('Zum Anfang der Konfigurationsliste scrollen', 'BtnAnfang');
  var HinwEnde = new Hinweis('Zum Ende der Konfigurationsliste scrollen', 'BtnEnde');
  var HinwStabkombination = new Hinweis('Mit Hilfe der Stabkombination können Sie flexibel ' +
                                        'zwei unterschiedliche Stäbe in verschiedenen Kombinationsmöglichkeiten' +
                                        'darstellen. ' +
                                        'Die Kombination "12" lässt bspw. auf einen ersten Stab' +
                                        'einen zweiten Stab folgen', 'HinwStabkombination');
}

/* Beim Start sollen nachfolgendes ausgeführt werden */
var Konfigurationen = null;
var Thumbs = null;
var ThumbIndex = 0; // Ausgewaehltes Thumbnail-Container-Bild
var KonfIndex = 0;
var DivKonfs = null;
var ObereKonf = 0;
ZeigeReiter(REITER_GESAMT);
