// Globale Variabeln
var init = false;		// gibt an, ob Initialisierung abgeschlossen wurde
var DHTML = false;
var DOM = false;
var IE = false;
var NC = false;
var OP = false;
var aTab = new Array();
var mask = null;		// Transparenzmaske
var wpup = null;		// Popup-Fenster
var hr = null;		// HttpRequest

// Preload statische Elemente
var aPrl = new Array(
   new Array('img/btn/ticket_l.gif','img/btn/ticket_h.gif'),
   new Array('img/btn/prn_l.gif','img/btn/prn_h.gif'),
   new Array('img/btn/dia_l.png','img/btn/dia_h.png'),
   new Array('img/btn/mov_l.png','img/btn/mov_h.png'),
   new Array('img/btn/prv_l.png','img/btn/prv_h.png'),
   new Array('img/btn/nxt_l.png','img/btn/nxt_h.png'),
   new Array('img/btn/cls_l.png','img/btn/cls_h.png')
);
var aPrl_l = new Array();
var aPrl_h = new Array();

// Initialisierung
function initJS(x,y)
{
   // DHTML initialisieren
   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;}

   // übergebene Scrollposition setzen
   window.scrollTo(x,y);

   // Schlaufe für Tab-Objekte
   for (i=0; i<aTab.length; i++) {tabFcs(aTab[i]);}

   // Schlaufe durch Preload
   for (i = 0; i < aPrl.length; i++)
   {
      eval ('aPrl_l['+i+'] = new Image(); aPrl_l['+i+'].src = "'+aPrl[i][0]+'";');
      eval ('aPrl_h['+i+'] = new Image(); aPrl_h['+i+'].src = "'+aPrl[i][1]+'";');
   }

   // Objekte
   mask = getE('mask');
   wpup = getE('wpup');

   // Initialisierung abgeschlossen
   init = true;
}


// --- DHTML-Basics --------------------------------------------------------------------------------------

// Element (DHTML-abhängig zurückliefern)
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
// - obj: Objekt, welches Event verarbeiten soll
// - event: Typ des Events
// - func: Funktion, die von Event aufgerufen wird
// - b, BOOL: Capture (Objekt erhält Event als erstes)
function addEvent(obj,event,func,b)
{
   if (IE || OP)  // ältere Operaversionen kennen addEventListener noch nicht!
   {
      if (obj == window) {eval("obj.on" + event + " = document.on" + event + " = func;"); return true;}
      else {eval("obj.on" + event + " = func;"); return true;}
   }
   else if (DOM)
   {
      if (event == 'mousewheel')
      {
         var e = (/Firefox/i.test(navigator.userAgent) ? "DOMMouseScroll" : "mousewheel");
         if (obj.attachEvent) {obj.attachEvent('on'+e,func); return true;}
         else if (obj.addEventListener) {obj.addEventListener(e,func,b); return true;}
         else {return false;}
      }
      else if (obj.addEventListener) {obj.addEventListener(event, func, true); return true;}
      else {return false;}
   }
   if (NC)
   {
      eval("obj.captureEvents(Event." + event.toUpperCase() + ");");
      eval("obj.on" + event + " = func;");
      return true;
   }
}


// --- Scroll --------------------------------------------------------------------------------------------

// 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)
{
   var scx = Math.max(0,(getScx()));
   var scy = Math.max(0,(getScy()));
   if (scx)
   {
      if (l.href != null) {l.href = l.href+(l.href.indexOf('?') == -1 ? '?' : '&')+'scx='+scx;}
      else if (l.action != null) {l.action = l.action+(l.action.indexOf('?') == -1 ? '?' : '&')+'scx='+scx;}
   }
   if (scy)
   {
      if (l.href != null) {l.href = l.href+(l.href.indexOf('?') == -1 ? '?' : '&')+'scy='+scy;}
      else if (l.action != null) {l.action = l.action+(l.action.indexOf('?') == -1 ? '?' : '&')+'scy='+scy;}
   }
}


// --- Focus-Register ------------------------------------------------------------------------------------

function tabFcs(id)
{
   var tab = getE('tabFcs'+id);
   tab.btnOpn = getE('btnOpn'+id);
   tab.btnCls = getE('btnCls'+id);
   tab.i = id;

   tab.crt = function()
   {
      this.btnOpn.onclick = function() {return tab.opn()};
      this.btnCls.onclick = function() {return tab.cls()};
   }

   tab.opn = function()
   {
      // alle schliessen
      for (i=0; i<aTab.length; i++) {if (this.i != aTab[i]) {getE('tabFcs'+aTab[i]).cls();}}

      // selbst öffnen
      tab.style.zIndex = 1;
      tab.style.top = '0px';
      tab.style.bottom = 'auto';
      tab.btnOpn.style.visibility = 'hidden';
      return false;
   }

   tab.cls = function()
   {
      tab.style.zIndex = 2;
      tab.style.top = 'auto';
      tab.style.bottom = '0px';
      tab.btnOpn.style.visibility = 'visible';
      return false;
   }

   tab.crt();
   return (tab);
}


// --- Popup und Transparenzmaske ------------------------------------------------------------------------

// Maske und Popup anzeigen
function pupOpn()
{
   // Maske zurücksetzen
   mask.style.filter = 'alpha(opacity:0)';
   mask.style.KHTMLOpacity = .0;
   mask.style.MozOpacity = .0;
   mask.style.opacity = .0;
   mask.style.visibility = 'visible';

   // Einblenden starten
   pupOpnFad(0);

   return false;
}

// Maske einblenden
function pupOpnFad(v)
{
   if (v < mskAph)
   {
      v = Math.min(mskAph,v+mskStp);
      v2 = v/100;
      mask.style.filter = 'alpha(opacity:'+v+')';
      mask.style.KHTMLOpacity = v2;
      mask.style.MozOpacity = v2;
      mask.style.opacity = v2;
      setTimeout(function () {pupOpnFad(v);},mskTim);
   }
   else {setTimeout(function () {pupOpnShw();},mskWit);}
}

// Popup-Fenster anzeigen
function pupOpnShw()
{
   wpup.style.visibility = 'visible';
}

// Maske ausblenden
function pupClsFad(v)
{
   if (v > 0)
   {
      v = Math.max(0,v-mskStp);
      v2 = v/100;
      mask.style.filter = 'alpha(opacity:'+v+')';
      mask.style.KHTMLOpacity = v2;
      mask.style.MozOpacity = v2;
      mask.style.opacity = v2;
      setTimeout(function () {pupClsFad(v);},mskTim);
   }
   else {mask.style.visibility = 'hidden';}
}


// --- 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) {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)
   {
      // JS-Funktion
      if (fJs) {eval('h.onreadystatechange = function() {'+fJs+'(\''+p+'\');};');}

      // PHP-Script
      h.open((fom === '' ? 'GET' : 'POST'),fPhp+'?'+p,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)
   {
      // HTML mit AJAX einlesen (typ)
      if (hr.responseText.substr(0,7) === 'var v =') {try {eval(hr.responseText);} catch (err) {}}
      hr.abort();
      hr = null;

      // Parameter-Werte aufschlüsseln
      p = p.split('&');
      for (var i=0; i<p.length; i++)
      {
         var val = p[i].split('=');
         eval ('var prm_'+val[0]+'=\''+val[1]+'\';');
      }

      // Schlaufe durch Inhaltsblöcke
      if (typeof v != 'undefined')
      {
         for (var i=0; i<v.length; i++)
         {
            switch(v[i][0])
            {
               case 'pupopn':
                  wpup.innerHTML = v[i][1];
                  if (v[i][2])
                  {
                     var fo0 = new FlashObject(v[i][2][0],'fo0',v[i][2][1],v[i][2][2],6,'#000000');
                     fo0.addParam('wmode','transparent');
                     fo0.write('fo0');
                  }
                  if (wpup.style.visibility != 'visible')
                  {
                     wpup.className = 'pup'+prm_pup.substr(0,1);
                     pupOpn();
                  }
                  break;
               case 'pupcls':
                  wpup.style.visibility = 'hidden';
                  wpup.className = '';
                  wpup.innerHTML = '';
                  setTimeout(function () {pupClsFad(mskAph);},mskWit);
                  break;
               case 'err': alert('Fehler'); break;
            }
         }
      }
   }

   // HTTP-Fehler abfangen (Fortsetzung von JS ermöglichen)
   else if (hr && hr.readyState == 4)
   {
      hr.abort();
      hr = null;
   }
}
