// ===================================================================
// Author: Frank Stroebel
// WWW: http://www.chessgamble.de/
// ===================================================================

//
// Funktionen für Turnier-Angebote-Liste: Auswahl und Sortierung.
//

//
// Cookie: Sortierung speichern
// Name des Cookie: "TurnierAngeboteSortierung"
//
// Werte:
//
// 0 = Keine (nutze default: "Erstellt DESC ascii")
//
// 1 = Erstellt ASC ascii
// 2 = Name ASC ascii
// 3 = Einsatz ASC ascii
// 4 = Bedenkzeit ASC ascii
// 5 = Spieler ASC ascii
// 6 = Runden ASC ascii
// 7 = Gewinnverteilung ASC ascii
// 8 = Farbverteilung ASC ascii
//
// 11 = Erstellt DESC ascii
// 12 = Name DESC ascii
// 13 = Einsatz DESC ascii
// 14 = Bedenkzeit DESC ascii
// 15 = Spieler DESC ascii
// 16 = Runden DESC ascii
// 17 = Gewinnverteilung DESC ascii
// 18 = Farbverteilung DESC ascii
//
// 21 = Erstellt ASC numeric
// 22 = Name ASC numeric
// 23 = Einsatz ASC numeric
// 24 = Bedenkzeit ASC numeric
// 25 = Spieler ASC numeric
// 26 = Runden ASC numeric
// 27 = Gewinnverteilung ASC numeric
// 28 = Farbverteilung ASC numeric
//
// 31 = Erstellt DESC numeric
// 32 = Name DESC numeric
// 33 = Einsatz DESC numeric
// 34 = Bedenkzeit DESC numeric
// 35 = Spieler DESC numeric
// 36 = Runden DESC numeric
// 37 = Gewinnverteilung DESC numeric
// 38 = Farbverteilung DESC numeric
//


//
// Cookie: Angebote-Auswahl speichern
// Name des Cookie: "TurnierAngeboteAuswahl"
//
// Werte:
//
// 0 = Zeige alle
// 1 = Zeige nur Schnellschach
// 2 = Zeige nur Fernschach
//

//
// Hängt die EventHandler an die TurnierAngebote-Tabelle.
//
function InitTurnierAngeboteAuswahl()
{
  // EventHandler für Turnierarten-Selektor
  $("#IdTurnierArtAlle").click(function() {
    OnTurnierArtSelected('IdTurnierArtAlle');
  });
  $("#IdTurnierArtSchnellschach").click(function() {
    OnTurnierArtSelected('IdTurnierArtSchnellschach');
  });
  $("#IdTurnierArtFernschach").click(function() {
    OnTurnierArtSelected('IdTurnierArtFernschach');
  });
  
  // EventHandler für Sortierung
  if (document.getElementById('IdTurnierListeLinkErstellt') == null) {
    // Spalte Turnierausschreibungszeit fehlt
    $("a#IdTurnierListeLinkSpielart").attr('href', "javascript:$('#IdTurnierListeTable').sortTable({onCol: 2, keepRelationships: true})");
    $("a#IdTurnierListeLinkName").attr('href', "javascript:$('#IdTurnierListeTable').sortTable({onCol: 4, keepRelationships: true})");
    $("a#IdTurnierListeLinkEinsatz").attr('href', "javascript:$('#IdTurnierListeTable').sortTable({onCol: 5, keepRelationships: true, sortType: 'numeric'})");
    $("a#IdTurnierListeLinkBedenkzeit").attr('href', "javascript:$('#IdTurnierListeTable').sortTable({onCol: 6, keepRelationships: true})");
    $("a#IdTurnierListeLinkSpieler").attr('href', "javascript:$('#IdTurnierListeTable').sortTable({onCol: 7, keepRelationships: true})");
  }
  else {
    // Spalte Turnierstart bzw. Turnierende ist vorhanden
    $("a#IdTurnierListeLinkSpielart").attr('href', "javascript:$('#IdTurnierListeTable').sortTable({onCol: 2, keepRelationships: true})");
    $("a#IdTurnierListeLinkErstellt").attr('href', "javascript:$('#IdTurnierListeTable').sortTable({onCol: 9, keepRelationships: true})");
    $("a#IdTurnierListeLinkName").attr('href', "javascript:$('#IdTurnierListeTable').sortTable({onCol: 5, keepRelationships: true})");
    $("a#IdTurnierListeLinkEinsatz").attr('href', "javascript:$('#IdTurnierListeTable').sortTable({onCol: 6, keepRelationships: true, sortType: 'numeric'})");
    $("a#IdTurnierListeLinkBedenkzeit").attr('href', "javascript:$('#IdTurnierListeTable').sortTable({onCol: 7, keepRelationships: true})");
    $("a#IdTurnierListeLinkSpieler").attr('href', "javascript:$('#IdTurnierListeTable').sortTable({onCol: 8, keepRelationships: true})");
  }
}

//
//            A C H T U N G
//            Testfunktion
//
//            Wird aufgerufen von "QuellText_Turniere_NeuGeladen.html"
//
function AlertAktuelleSortierParameter()
{
  var ausgabe = "Keine Sortierung.";
  if ((document.getElementById('IdTurnierListeTable') != null) &&
      (document.getElementById('IdTurnierListeTable').tableSortParams != null)) {
    var currentSortParams = document.getElementById('IdTurnierListeTable').tableSortParams;
    
    // Spalte bestimmen
    if (!currentSortParams.onCol) {
      ausgabe = "Fehler: Keine Spalte definiert!";
    }
    else {
      ausgabe = "Spalte " + currentSortParams.onCol + " ist sortiert.";
    }
    
    // Sortierrichtung bestimmen
    if (currentSortParams.sortDesc) {
      ausgabe += " Richtung: Descending.";
    }
    else {
      ausgabe += " Richtung: Ascending.";
    }
    
    // Sortierart
    if (currentSortParams.sortType == 'ascii') {
      ausgabe += " Art: ASCII.";
    }
    else {
      ausgabe += " Art: Numerisch.";
    }
  }
  alert(ausgabe);
}

//
// Gibt den aktuellen Sortierstatus der Tabelle tableId zurück.
//
// Result:
// = null, falls Tabelle nicht existiert oder keine Sortierattribute zu finden sind.
// Sonst:
// result[0] = SpaltenId, nach der sortiert ist (beginnt bei 1)
// result[1] = Sortierrichtung: 0 = Asc, 1 = Desc.
// result[2] = Sortierart: 'numeric' oder 'ascii'.
//
function GetAktuelleSortierParameter(tableId)
{
  var result = null;
  if ((document.getElementById(tableId) != null) &&
      (document.getElementById(tableId).tableSortParams != null)) {
    var currentSortParams = document.getElementById(tableId).tableSortParams;
    
    // Spalte bestimmen
    if (currentSortParams.onCol) {
      result = new Array(3);
      result[0] = currentSortParams.onCol;
    
      // Sortierrichtung bestimmen
      if (currentSortParams.sortDesc) {
        result[1] = 1;
      }
      else {
        result[1] = 0;
      }
      
      // Sortierart
      result[2] = currentSortParams.sortType;
    }
  }
  return result;
}

//
// Speichert den aktuellen Sortier-Zustand und die Turnierart-Auswahl der Turnier-Angebote-Liste
//
function SaveCurrentTurnierAngeboteAuswahlUndSortierungToCookie()
{
  if (document.getElementById("IdTurnierListeTable") != null) {
    // Die Auflistung ist im Hauptfenster: Speichern...
    
    //
    // Auswahl
    //
    var auswahl = 0;
    var found = false;
    if (document.getElementById("IdTurnierArtAlle").checked == true) {
      found = true;
      auswahl = 0;
    }
    else if (document.getElementById("IdTurnierArtSchnellschach").checked == true) {
      found = true;
      auswahl = 1;
    }
    else if (document.getElementById("IdTurnierArtFernschach").checked == true) {
      found = true;
      auswahl = 2;
    }
    
    // Falls es gefunden wurde, Wert in Cookie schreiben
    if (found) {
      WriteCookie('TurnierAngeboteAuswahl', auswahl, 1);
    }
    
    
    //
    // Sortierung
    //
    var directionASC = true;
    var currentSorting = GetAktuelleSortierParameter('IdTurnierListeTable');
    if (currentSorting != null) {
      // Spalte
      var sortierung = currentSorting[0] - 1;
      // Richtung
      if (currentSorting[1] == 1) {
        sortierung += 10;
      }
      // Art
      if (currentSorting[2] == 'numeric') {
        sortierung += 20;
      }
      // Wert in Cookie schreiben
      WriteCookie('TurnierAngeboteSortierung', sortierung, 1);
    }
  }
}

//
// Stellt die Sortierung der Turnier-Angebote entsprechend Cookie-Wert auf der Oberfläche ein.
//
function RestoreTurnierAngeboteAuswahlUndSortierungFromCookie()
{
  return; // Das lassen wir momentan...

  if (document.getElementById("IdTurnierListeTable") != null) {
    // Die Auflistung ist im Hauptfenster: Gespeicherte Werte wieder herstellen...
  
    //
    // Auswahl
    //
    var auswahl = ReadSavedTurnierAngeboteAuswahlFromCookie();
    var auswahlId = "IdTurnierArtAlle";
    switch (auswahl) {
      case 0:
        auswahlId = "IdTurnierArtAlle";
        break;
      case 1:
        auswahlId = "IdTurnierArtSchnellschach";
        break;
      case 2:
        auswahlId = "IdTurnierArtFernschach";
        break;
      default:
        break;
    }
    document.getElementById(auswahlId).checked = true;
    // Und Zeilen gem. Auswahl sichtbar/unsichtbar machen
    OnTurnierArtSelected(auswahlId);

    //
    // Sortierung
    //
    // Aktuelle Sortierung aus Cookie lesen
    var sortierung = ReadSavedTurnierAngeboteSortierungFromCookie();
    if (sortierung >= 0) {
      // Sortier-Spalte bestimmen
      var spalte = 2;
      switch (sortierung) {
        case 1:
        case 11:
        case 21:
        case 31:
          spalte = 2;
          break;
        case 2:
        case 12:
        case 22:
        case 32:
          spalte = 3;
          break;
        case 3:
        case 13:
        case 23:
        case 33:
          spalte = 4;
          break;
        case 4:
        case 14:
        case 24:
        case 34:
          spalte = 5;
          break;
        case 5:
        case 15:
        case 25:
        case 35:
          spalte = 6;
          break;
        case 6:
        case 16:
        case 26:
        case 36:
          spalte = 7;
          break;
        case 7:
        case 17:
        case 27:
        case 37:
          spalte = 8;
          break;
        case 8:
        case 18:
        case 28:
        case 38:
          spalte = 9;
          break;
        default:
          break;
      }
      
      // Sortierrichtung bestimmen
      var sortDescParam = true;
      if ((sortierung == 0) ||
          ((sortierung > 0) && (sortierung <= 10)) ||
          ((sortierung > 20) && (sortierung <= 30))) {
        sortDescParam = false;
      }
      
      // Sortierart bestimmen
      var sortTypeParam = 'ascii';
      if ((sortierung > 20) && (sortierung <= 40)) {
        sortTypeParam = 'numeric';
      }
      
      // Sortierung ausführen
      var aufruf = {onCol: spalte, keepRelationships: true, sortType: sortTypeParam, sortDesc: sortDescParam};
      $('#IdTurnierListeTable').sortTable(aufruf);
    }
  }
}

//
// Liest die gespeicherte Turnierart-Auswahl der Turnier-Angebote-Liste aus dem Cookie.
// Return: Werte siehe oben.
//
function ReadSavedTurnierAngeboteAuswahlFromCookie()
{
  var result = ReadCookie('TurnierAngeboteAuswahl');
  if (result != '') {
    result *= 1;
  }
  else {
    result = 0;
  }
  return result;
}

//
// Liest das gespeicherte Sortier-spalte der Turnier-Angebote-Liste aus dem Cookie.
// Return: Werte siehe oben.
//
function ReadSavedTurnierAngeboteSortierungFromCookie()
{
  var result = ReadCookie('TurnierAngeboteSortierung');
  if (result != '') {
    result *= 1;
  }
  else {
    result = 0;
  }
  return result;
}

//
// Wird aufgerufen, wenn der User einen Radio-Button bei der Turnierauflistung klickt.
// id: Die Id der Auswahl (als Text):
// - IdTurnierArtAlle
// - IdTurnierArtSchnellschach
// - IdTurnierArtFernschach
//
function OnTurnierArtSelected(id)
{
  //alert('Param = '+id);
  
  // Attribute richtig setzen
  //SetZeileToFernOrSchnell();
  
  // Zeilen ein-/ausblenden
  switch (id) {
    case "IdTurnierArtAlle":
      $(".SchnellschachZeile").removeClass("Unsichtbar");
      $(".FernschachZeile").removeClass("Unsichtbar");
      break;
    case "IdTurnierArtSchnellschach":
      $(".FernschachZeile").addClass("Unsichtbar");
      $(".SchnellschachZeile").removeClass("Unsichtbar");
      break;
    case "IdTurnierArtFernschach":
      $(".SchnellschachZeile").addClass("Unsichtbar");
      $(".FernschachZeile").removeClass("Unsichtbar");
      break;
    default:
      break;
  }
  //alert('OK');
}

//
// Beim Umsortieren wandern die Attribute "FernschachZeile" bzw. "SchnellschachZeile" nicht mit.
// Das wird in dieser Funktion behoben.
//
function SetZeileToFernOrSchnell()
{
  if (document.getElementById("IdTurnierListeTable") != null) {
    var oTable = document.getElementById("IdTurnierListeTable");
    if (oTable) {
      for (var loop=1, max=oTable.rows.length; loop<max; loop++) {
        //oTable.rows[loop].cells[0].innerHTML = loop + 1;
        //alert(oTable.rows[loop].innerHTML); // gibt Inhalt der Zeile aus (ab <td>)
      }
    }
  }
}

