﻿function getXMLHTTP(){
  var xh = null;
  if (window.ActiveXObject){
    try{
      xh = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
      try{
        xh = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e){}
    }
  }else if (window.XMLHttpRequest){
    try{
      xh = new XMLHttpRequest();
    } catch(e){}
  }
  return xh;
}

var xhc = getXMLHTTP(),
  cont = { //Nachladen des Inhalts
    arr : ["home", "neu", "liebling", "bestell", "antiquariat", "gang", "gaeste", "kontakt", "anfahrt", "impressum"],
    arrIndex : 0,
    stateChanged : function(){
      if (xhc.readyState == 4){
        if (xhc.status == 200) x = xhc.responseText; else x = "<h2>Http-Error: " + xhc.status + " - " + xhc.statusText + "</h2>";
        document.getElementById("id_content").innerHTML = x;
        switch (cont.arrIndex){
          case(1): zeige(0,0); nl.index_prev = 0; break;
          case(2): zeige(1,0); nl.index_prev = 0; break;
          case(4): asfInit(); break;
          case(5): initShow(); break;
          case(6): gaesteListe(); break;
        }
      }
    }
  },
  nl = { //Neuerscheinungen und Lieblinge
    index : 0,
    index_prev : 0,
    mode : 0,
    stateChanged : function(){
      if (xhc.readyState == 4 && xhc.status == 200){
        document.getElementById("id_nl_list_" + (nl.index_prev<10?"0":"") + nl.index_prev).className = "";
        nl.index_prev = nl.index;
        document.getElementById("id_nl_list_" + (nl.index_prev<10?"0":"") + nl.index_prev).className = "selected";
        var tmp = String(xhc.responseText).split(/\r\n/);
        var x = "<img alt=' " + tmp[1] + " ' src='img/" + (nl.mode==0 ? "neu" : "liebling") + "_0" + nl.index + ".jpg' align='left' />";
        var brIndex = tmp[3].lastIndexOf("<br />");
        var sTmp = tmp[3];
        var sISBN = sTmp.match(/(978-3){1}-\d+-\d+-\d/);
        if (brIndex > 0 && sISBN != null){
          sTmp = sTmp.substr(0, brIndex) + "<div class=\"bleft\">" + sTmp.substr(brIndex + 6, sTmp.length - brIndex) + 
            "</div><div class=\"bright\"><a href=\"http://buchsf.shop-asp.de/shop/action/quickSearch;" + 
            "jsessionid=14B8457407B60309B1F29A604F09AE55.www02?aUrl=90008265&amp;facetNodeId=-1&amp;searchString=" + 
            sISBN[0] + "&amp;mainsearchSubmit=Los%21\" target=\"_blank\">zum Shop</a></div>";
        }
        x += "<b>" + tmp[0] + " &#150; " + tmp[1] + "</b><br />" + tmp[2] + "<br /><br />" + sTmp;
        document.getElementById("id_nl_detail").innerHTML = x;
      }
    }
  },
  dia = { //Diashow
    nr : 0, speed : 3000, trans : 100, showId : 0
  },
  blende = { //Ein- und Ausblenden des Navigationsboards
    value : -1, obj : null, step : 0, end : -1, taskId : 0
  };

function nachladen(x){
  if(xhc.readyState < 4) xhc.abort(); xhc = getXMLHTTP();
  cont.arrIndex = x;
  xhc.open("GET", "templates/" + cont.arr[x] + ".htm");
  xhc.onreadystatechange = cont.stateChanged;
  xhc.send(null);
}

function zeige(mode, index){
  if (xhc.readyState < 4) xhc.abort(); xhc = getXMLHTTP();
  nl.index = index; nl.mode = mode;
  xhc.open("GET", "texte/" + (nl.mode==0 ? "neu" : "liebling") + "_0" + nl.index + ".txt");
  xhc.onreadystatechange = nl.stateChanged;
  xhc.send(null);
}

function initShow(){
  dia.nr = 1; dia.speed = 3000; dia.trans = 100;
  setTrans(document.getElementById("id_gang_navi"), 70);
  initImages();
  show();
}
function show(){
  var ele = document.getElementById("id_gang_laden_1");
  if (!ele) return; //Nach Seitenwechsel
  ele.src = "img/Buchhandlung_" + dia.nr + ".jpg";
  dia.trans = 100; setTrans(ele, dia.trans); 
  setImgPos();
  ele = document.getElementById("id_gang_laden_2")
  ele.src = "img/Buchhandlung_" + (dia.nr == 16 ? 1 : dia.nr+1) + ".jpg";
  if (dia.speed==0) return;
  if (dia.speed>0) dia.showId = window.setTimeout("showTrans()", dia.speed);
}

function naviMode(x){
  if (x==0) {dia.speed = 0; if(dia.trans==100) window.clearTimeout(dia.showId);}
  else {if (dia.speed==0) {dia.speed = 3000; dia.trans = 100; showTrans();}}
}

function initImages(){
  for (var i = 1; i<=16; i++){
    var ele = document.getElementById("id_gang_img_" + (i<10 ? "0" : "") + i);
    ele.onclick = showNavi;
    ele.style.left = getImgLeft(i) + "px";
    ele.style.top =  getImgTop(i) + "px";
  }
}
function getImgLeft(i){return 24 + 25 * ((i-1) % 6);}
function getImgTop(i){return 33 + 25 * Math.floor((i-1) / 6);}

function showTrans(){
  if (dia.trans == 0){
    dia.nr++; if (dia.nr>16) dia.nr = 1;
    dia.trans = 100;
    show();
  }
  else {
    dia.trans--;
    var ele = document.getElementById("id_gang_laden_1");
    if (!ele) return;
    setTrans(ele, dia.trans);
    setImgPos();
    dia.showId = window.setTimeout("showTrans()", 15);
  }
}

function setTrans(x, value){
  x.style.opacity = (value == 100 ? "1.00" : "0." + (value < 10 ? "0" : "") + value);
  x.style.filter = "alpha(opacity=" + value + ")";
  x.opacity = value;
}

function getTrans(x){if (x.opacity) return x.opacity; else return 100;}

function setImgPos(){
  var ele = document.getElementById("id_gang_navi_window");
  var x1 = getImgLeft(dia.nr), y1 = getImgTop(dia.nr);
  var dn = (dia.nr == 16 ? 1 : dia.nr + 1);
  var x2 = getImgLeft(dn), y2 = getImgTop(dn);
  ele.style.left = ((x1 + (x2 - x1) / 100 * (100 - dia.trans)) + (dia>9 ? 2 : 1)) + "px";
  ele.style.top = ((y1 + (y2-y1) / 100 * (100 - dia.trans)) - 4) + "px";
}

function showNavi(){
  if(dia.showId>0) window.clearTimeout(dia.showId);
  dia.nr = Number(this.id.substr(12,2));
  show();
}

function einausblenden(x, end){
  if (getTrans(x) == end || blende.taskId) return;
  blende.value = getTrans(x); blende.obj = x; blende.step = (blende.value > end ? -2 : 2); blende.end = end;
  blende.taskID = window.setTimeout(blenden, 1);
}

function blenden(){
  if (blende.value == blende.end) {blende.taskId = null; blende.end = -1; blende.value = -1; return;}
  if (!blende.obj) return;
  setTrans(blende.obj, blende.value);
  blende.value += blende.step;
  blende.taskID = window.setTimeout(blenden, 1);
}

function naviClose(){document.getElementById("id_gang_navi").style.visibility = "hidden";}

function gaesteListe(){
  if (xhc.readyState < 4) xhc.abort(); xhc = getXMLHTTP();
  xhc.open("GET", "texte/gaeste.txt");
  xhc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhc.setRequestHeader("If-Modified-Since", "0");
  xhc.onreadystatechange = stateChanged_gaeste;
  xhc.send(null);
}

function stateChanged_gaeste(){
  if (xhc.readyState == 4 && xhc.status == 200){
    var arr = xhc.responseText.split(/\r\n/), res = "";
    for (var i = arr.length; i>0;i--){
      if (arr[i-1].length > 0){
        var ar = arr[i-1].split("|");
        if (res.length > 0) res += "<br /><br />";
        var sep = ar[2].length - 9;
        res += "<b>" + unescape(ar[1]) + "</b> schrieb am " + ar[2].substring(0, sep) + " um" + ar[2].substring(sep, sep + 6) +
           ":" + "<br /><br />" + unescape(ar[3]) + "<br /><br />eMail: " + unescape(ar[0]) + "<br /><br />"
      }
    }
    document.getElementById("id_gaeste_left").innerHTML = res;
  }
}

function gaesteEintrag(){
  if (xhc.readyState < 4) xhc.abort(); xhc = getXMLHTTP();
  xhc.open("POST", "gaeste.php");
  xhc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhc.onreadystatechange = stateChanged_gaesteEintrag;
  xhc.send(
    "Name=" + escape(document.getElementById("id_frm_Name").value).replace(/%20/g, "+") + "&" +  
    "eMail=" + escape(document.getElementById("id_frm_eMail").value).replace(/%20/g, "+") + "&" +  
    "Inhalt=" + escape(document.getElementById("id_frm_Inhalt").value).replace(/%20/g, "+")
  );
}

function stateChanged_gaesteEintrag(){
  if (xhc.readyState == 4){
    var ele = document.getElementById("id_gaeste_left");
    if (xhc.status != 200)
      ele.innerHTML = "Error: " + xhc.status + " - " + xhc.statusText;
    else
      ele.innerHTML = xhc.responseText + "<br /><br /><div class=\"link\" onclick=\"nachladen(6);\">Zum Gästebuch</div>";
  }
}
