// --- Globale Variabeln -----------------------------------------------------------------------------------------------------------------
var DHTML = false;
var DOM = false;
var IE = false;
var NC = false;
var OP = false;
var SAFARI = false;
var init = false;		// Initialisierung abgeschlossen
var hr = null;		// HttpRequest
var ifma = null;		// iFrame-Objekt
var ani = false;		// Animations-Status


// --- DHTML -----------------------------------------------------------------------------------------------------------------------------

// Element (DHTML-abhängig zurückliefern)
// - p: HTML-ID des Elements
function getE(p)
{
   if (DOM) {if (typeof document.getElementById(p) == "object") {return document.getElementById(p);} else {return void(0);}}
   else if (IE) {if (typeof document.all[p] == "object") {return document.all[p];} else {return void(0);}}
   else if (NC) {if (typeof document[p] == "object") {return document[p];} else {return void(0);}}
   else return void(0);
}

// Event zu Objekt hinzufügen
function addEvent(obj,event,func)
{
   if (IE || OP)  // ältere Operaversionen kennen addEventListener noch nicht!
   {
      if (obj == window) {eval("obj.on" + event + " = document.on" + event + " = func;");}
      else {eval("obj.on" + event + " = func;");}
   }
   else if (DOM)
   {
      if (event == 'mousewheel')
      {
         var e = (/Firefox/i.test(navigator.userAgent) ? "DOMMouseScroll" : "mousewheel");
         if (obj.attachEvent) {obj.attachEvent('on'+e,func);}
         else if (obj.addEventListener) {obj.addEventListener(e,func,false);}
      }
      else {obj.addEventListener(event, func, true);}
   }
   if (NC)
   {
      eval("obj.captureEvents(Event." + event.toUpperCase() + ");");
      eval("obj.on" + event + " = func;");
   }
}


// --- AJAX ------------------------------------------------------------------------------------------------------------------------------

// AJAX-Aufruf
// - fom: '' => GET; sonst POST
// - p : n1=v1&n2=v2... Name/Wert-Paare für Übergabe an PHP-Script
function ajx(fom,p)
{
   if (init && !hr && !ani) {hr = hrGet(fom,'hrAjx','ajx.php',p);}
   return false;
}

// HTTP-Request
// - fom: '' => GET; sonst POST
// - fJs: JS-Funktion, an welche Server-Antwort übergeben wird
// - fPhp: PHP-Script, welches ausgeführt wird
// - p : n1=v1&n2=v2... Name/Wert-Paare für Übergabe an PHP-Script
function hrGet(fom,fJs,fPhp,p)
{
   h = null;
   try {h = new ActiveXObject("MSXML2.XMLHTTP");}
   catch (err_MSXML2)
   {
      try {h = new ActiveXObject("Microsoft.XMLHTTP");}
      catch (err_Microsoft)
      {
         if (typeof XMLHttpRequest != 'undefined') {h = new XMLHttpRequest;}
      }
   }
   if (h)
   {
      // ID des aktuellen Blocks als 'akt' übergeben
      var obj = getE('iscr'); if (obj != null)
      {
         var akt = obj.firstChild;
         if (akt.nodeType == 3) {akt = akt.nextSibling;}
         akt = akt.id.substr(3);
      }

      // JS-Funktion
      if (fJs) {eval('h.onreadystatechange = function() {'+fJs+'(\''+p+'\');};');}

      // PHP-Script
      h.open((fom === '' ? 'GET' : 'POST'),fPhp+'?'+p+(obj == null ? '' : '&akt='+akt),true);
      if (fom === '') {h.send(null);}
      else {
         var prm = cmsPrm(fom);
         h.setRequestHeader("Content-type","application/x-www-form-urlencoded");
         h.setRequestHeader("Content-length",prm.length);
         h.setRequestHeader("Connection","close");
         h.send(prm);
      }
   }
   return h;
}

// Für Formular bestimmte Formular-Felder sammeln und als URL-Parameter zurückliefern
// - fom: Formular-Name
function cmsPrm(fom)
{
   var aPrm = new Array();
   for (i=0; i<document.fom.elements.length; i++)
   {
      // ARRAY (SELECT MULTIPLE) übergeben
      var l = document.fom.elements[i].name.length-2;
      if (document.fom.elements[i].name.substr(l) == '[]')
      {
         var o = document.fom.elements[i];
         for (e=0; e<o.length; e++) {if (o.options[e].selected) {aPrm[aPrm.length] = document.fom.elements[i].name.substr(0,l)+'['+e+']'+'='+encodeURIComponent(o.options[e].value);}}
      }

      // ARRAY (RADIO/CHECKBOX) übergeben
      else if (document.fom.elements[i].type == 'radio' || document.fom.elements[i].type == 'checkbox')
      {
         if (document.fom.elements[i].checked) {aPrm[aPrm.length] = document.fom.elements[i].name+'='+encodeURIComponent(document.fom.elements[i].value);}
      }

      // Formular-Name einfügen
      else if (document.fom.elements[i].name == 'fom') {aPrm[aPrm.length] = 'fom='+encodeURIComponent(fom);}

      // normalen Wert übergeben
      // - Zeilenumbrüche unabhängig von effektivem Wert in TEXTAREA (\r, \n, \r\n) bei IE immer: \r\n, bei FireFox immer: \n
      // - Zeilenumbrüche unabhängig von Browser auf \r\n vereinheitlichen
      else {aPrm[aPrm.length] = document.fom.elements[i].name+'='+encodeURIComponent(document.fom.elements[i].value.replace(/\r\n/g,"\n").replace(/\n/g,"\r\n"));}
   }
   return aPrm.join('&');
}

// JS-Funktion (AJAX-Auswertung)
function hrAjx(p)
{
   if (hr && hr.readyState == 4 && hr.status == 200)
   {
      ani = true;

      // HTML mit AJAX einlesen (typ,blkPth,blkDpg,blkNam,blkTyp,aTxt1,aTxt2)
      if (hr.responseText.substr(0,7) === 'var v =') {try {eval(hr.responseText);} catch (err) {}}
      hr.abort();
      hr = null;

      // Schlaufe durch Inhaltsblöcke
      if (typeof v != 'undefined')
      {
         for (var i=0; i<v.length; i++)
         {
            switch(v[i][0].substr(0,3))
            {
               case 'nav':
                  var obj = getE('sclm');
                  obj.innerHTML = v[i][1];
                  break;
               case 'shw':
                  var obj = getE('cont');
                  obj.innerHTML = v[i][1];
                  ani = false;
                  break;
               case 'ani':
                  var d = (v[i][0].substr(3) == 'H');
                  var obj = getE('iscr');
                  var div = document.createElement('div');
                  div.id = 'blk'+v[i][1];
                  div.className = 'blk';
                  div.innerHTML = v[i][3];
                  var txt = document.createTextNode("\n");

                  // hinauf scrollen
                  if (v[i][2])
                  {
                     // neuen Block am Ende hinzufügen
                     if (d)
                     {
                        var lst = obj.lastChild;
                        if (lst.nodeType == 3) {obj.insertBefore(txt,lst); obj.insertBefore(div,lst);} else {obj.appendChild(div);}
                     }
                     else
                     {
                        var lst = obj.lastChild; if (lst.nodeType == 3) {lst = lst.previousSibling.previousSibling;}
                        if (lst.nodeType == 3) {obj.insertBefore(txt,lst);} obj.insertBefore(div,lst);
                     }

                     setTimeout(function () {aniScrU(d,0);},aniT);
                  }

                  // hinunter scrollen
                  else
                  {
                     // neuen Block am Anfang hinzufügen
                     var fst = obj.firstChild;
                     if (fst.nodeType == 3) {obj.insertBefore(txt,fst);} obj.insertBefore(div,fst);
                     if (d) {obj.style.top = '-'+aniH+'px';} else {obj.style.left = '-'+aniH+'px';}

                     setTimeout(function () {aniScrD(d,-aniH);},aniT);
                  }
                  break;
            }
         }
      }
   }

   // HTTP-Fehler abfangen (Fortsetzung von JS ermöglichen)
   else if (hr && hr.readyState == 4)
   {
      hr.abort();
      hr = null;
   }
}

// einen Block nach oben scrollen
function aniScrU(d,v)
{
   var obj = getE('iscr');
   if (v > -aniH)
   {
      v = Math.max(-aniH,v - aniS);
      if (d) {obj.style.top = v+'px';}
      else {obj.style.left = v+'px';}
      setTimeout(function () {aniScrU(d,v);},aniT);
   }
   else
   {
      // ersten Block löschen
      if (obj.firstChild.nodeType == 3) {obj.removeChild(obj.firstChild);} obj.removeChild(obj.firstChild);
      if (d) {obj.style.top = '0px';} else {obj.style.left = '0px';}
      ani = false;
   }
}

// einen Block nach unten scrollen
function aniScrD(d,v)
{
   var obj = getE('iscr');
   if (v < 0)
   {
      v = Math.min(0,v + aniS);
      if (d) {obj.style.top = v+'px';}
      else {obj.style.left = v+'px';}
      setTimeout(function () {aniScrD(d,v);},aniT);
   }
   else
   {
      // letzten Block löschen
      if (d)
      {
         if (obj.lastChild.nodeType == 3) {obj.removeChild(obj.lastChild);} obj.removeChild(obj.lastChild);
      }
      else
      {
         var lst = obj.lastChild; if (lst.nodeType == 3) {lst = lst.previousSibling.previousSibling;}
         if (lst.nodeType == 3) {var lst2 = lst.previousSibling; obj.removeChild(lst); lst = lst2;} obj.removeChild(lst);
      }
      ani = false;
   }
}


// --- Scroll-Position -------------------------------------------------------------------------------------------------------------------

// x-Scroll-Position zurückliefern
function getScx()
{
   if (self.pageXOffset) {return self.pageXOffset;}
   else if (window.pageXOffset) {return window.pageXOffset;}
   else if (document.documentElement && document.documentElement.scrollLeft) {return document.documentElement.scrollLeft;}
   else if (document.body && document.body.scrollLeft) {return document.body.scrollLeft;}
   else {return 0;}
}

// y-Scroll-Position zurückliefern
function getScy()
{
   if (self.pageYOffset) {return self.pageYOffset;}
   else if (window.pageYOffset) {return window.pageYOffset;}
   else if (document.documentElement && document.documentElement.scrollTop) {return document.documentElement.scrollTop;}
   else if (document.body && document.body.scrollTop) {return document.body.scrollTop;}
   else {return 0;}
}

// Scrollposition für Reload anhand aktueller Scrollposition berechnen
function setScl(l)
{
   if (l.href != null) {l.href = l.href+(l.href.indexOf('?') == -1 ? '?' : '&')+'scx='+Math.max(0,(getScx()))+'&scy='+Math.max(0,(getScy()));}
   else if (l.action != null) {l.action = l.action+(l.action.indexOf('?') == -1 ? '?' : '&')+'scx='+Math.max(0,(getScx()))+'&scy='+Math.max(0,(getScy()));}
}


// --- iFrame-Objekt ---------------------------------------------------------------------------------------------------------------------

// Objekt für up-/down-Buttons
function btn(idS,idP,idB,t)
{
   // Initialisierung
   var div = document.createElement('div');
   div.idS = idS		// ID des Scrollelements
   div.idP = idP		// ID des Elternelements
   div.idB = idB		// ID des Buttons
   div.t = t;        	// Typ des Buttons: 1=up, -1=down
   div.ids = null;	// Scrollelement
   div.idp = null;	// Elternelement
   div.tim = null;	// Timer
   div.s = 1;		// Schrittweite für Scroll (in Pixel)

   // Button erzeugen
   div.crt = function()
   {
      this.setAttribute('id',this.idB);
      this.setAttribute('class','sc'+((t==1) ? 'u' : 'd'));
      this.setAttribute('className','sc'+((t==1) ? 'u' : 'd'));
      this.ids = getE(this.idS);
      this.idp = getE(this.idP);
      this.idp.appendChild(this);
      addEvent(this,"mouseover",this.eMouseover);
      addEvent(this,"dblclick",this.eDblclick);
      addEvent(this,"mouseout",this.eMouseout);
   }

   // Event onMouseover
   div.eMouseover = function(e)
   {
      this.scr();
   }

   // Event onDblclick (IE-Fehler: bei zwei schnellen Klicks wird nur ein einfacher Klick ausgelöst)
   div.eDblclick = function(e)
   {
      if (IE) {this.ids.scr(this.t,this.s);}
   }

   // Event onMouseout
   div.eMouseout = function(e)
   {
      if (this.tim)
      {
         window.clearTimeout(this.tim);
         this.tim = false;
      }
   }

   // Scroll ausführen
   div.scr = function()
   {
      if (this.ids.scr(this.t,this.s))
      {
         obj = this;
         if (!this.tim) {this.tim = window.setTimeout(function(){obj.scr();},50,obj);}
         else {this.tim = window.setTimeout(function(){obj.scr();},10,obj);}
      }
   }

   return (div);
}

// Objekt für iFrame-Fenster
function ifm(idB,idP,idF,idC)
{
   // Initialisierung
   var div = document.createElement('div');
   div.h = 0;		// Höhe des Scrollbereichs
   div.hCnt = 0;		// Inhalts-Höhe
   div.hScr = 0;		// Höhe, um die maximal gescrollt werden kann
   div.idB = idB		// ID des Elternelements (für Buttons)
   div.idP = idP		// ID des Elternelements (für Scrollbereich)
   div.idF = idF		// ID des iFrames
   div.idC = idC		// ID des Inhalts
   div.bup = null;
   div.bdn = null;
   div.idc = null;	// Inhalt
   div.sy = 0;		// Vertikaler Scroll-Versatz;

   // iFrame erzeugen
   div.crt = function()
   {
      var idp = getE(this.idP);

      // Inhaltselement analysieren
      this.idc = getE(this.idC);
      this.w = idp.offsetWidth;
      this.h = idp.offsetHeight;

      // iFrame erstellen und an Eltern-Element anhängen
      this.setAttribute('id',this.idF);
      this.setAttribute('class','ifm');
      this.setAttribute('className','ifm');
      if (DOM || IE) {this.style.visibility = 'hidden';} else if (NC) {this.visibility = 'hide';}
      this.style.width = this.w+'px';
      this.style.height = this.h+'px';
      idp.appendChild(this);

      // Inhaltselement in iFrame einbinden
      this.idc.style.position = 'absolute';
      this.idc.style.backgroundColor = 'transparent';
      this.idc.style.padding = '0px';
      this.idc.style.left = '0px';
      this.appendChild(this.idc);
      this.hCnt = this.idc.offsetHeight;
      this.hScr = this.hCnt - this.h;

      // Scrollelemente erstellen und einbinden (falls Scroll nötig)
      if (this.hCnt > this.h)
      {
         // Button Up in iFrame einbinden
         this.bup = btn(this.idF,this.idB,'bup',1);
         this.bup.crt();

         // Button Down in iFrame einbinden
         this.bdn = btn(this.idF,this.idB,'bdn',-1);
         this.bdn.crt();
      }

      // Event hinzufügen
      addEvent(this,"mousewheel",this.eMousewheel);
   }

   // iFrame sichtbar machen
   div.shw = function()
   {
      if (DOM || IE) {this.style.visibility = 'visible';} else if (NC) {this.visibility = 'show';}
   }

   // iFrame schrittweise scrollen (falls in gewünschte Richtung gescrollt werden kann)
   div.scr = function(t,s)
   {
      {stp = s*t;}
      if (this.sy+stp <= -this.hScr) {this.sy = -this.hScr; go = false;}
      else if (this.sy+stp >= 0) {this.sy = 0; go = false}
      else {this.sy = this.sy+stp; go = true;}
      this.idc.style.top = this.sy+'px';
      return (go);
   }

   div.eMousewheel = function(e)
   {
      // Mouse-Event für alle Browser vereinheitlichen
      if (!e) {e = window.event;}
      if (e.wheelDelta)
      {
         d = e.wheelDelta/120;
      }
      else if (e.detail)
      {
         d = -e.detail/3;
      }

      // Abhängig von Scroll Event ausführen
      if (d) {this.scr(d/Math.abs(d),1*18);}

      if (e.preventDefault) {e.preventDefault();} else {return false;}
   }

   return (div);
}


// --- Initialisierung -------------------------------------------------------------------------------------------------------------------

// Initialisierung
function initJS(scrX,scrY)
{
   // DHTML starten
   if (window.opera) {OP = true;}
   if (document.getElementById) {DHTML = true; DOM = true;}
   if (document.all && !OP) {DHTML = true; IE = true;}
   if (window.netscape && window.screen && !DOM && !OP) {DHTML = true; NC = true;}
   if (navigator.appVersion.search(/Safari/) != - 1) {SAFARI = true;}
   init = true;

   // Scroll setzen
   window.scrollTo(scrX,scrY);

   // iFrame erstellen und anzeigen
   if (getE('ifmc') != null)
   {
      ifma = ifm('sclm','ifmc','ifm','ict');
      ifma.crt();
      ifma.shw();
   }
}
