// --------------------------------------------------------------------------------
//  Main DHTML Library for EXYST
// --------------------------------------------------------------------------------
// Die folgende MDL-Bibliothek ist fuer das Sidemenu erforderlich. Am Skriptende 
// befindet sich noch eine Funktion fuer das Error-Handling. 
// Achtung: hier keinen weiteren Code einfuegen!

//// Browser überprüfen
var NS4 = (navigator.appName.indexOf("Netscape") >= 0 && parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
var NS6 = (NS4 && document.getElementById) ? 1 : 0;
if (NS6) NS4 = 0;
var IE4 = (document.all) ? 1 : 0;
var IE5 = (IE4 && navigator.appVersion.indexOf("5.") >= 0) ? 1 : 0;

//// ein paar Variablen
var initok = 0;
var ll1;  // L-L-eins; wird in dhtmlinit() gesetzt
var rx = 0;
var ry = 0;

// var lc = "<p style='background-color:silver'><a href='../hostname.php' onmouseover='pclose()'>Halli hallo</a></p>";

//// Popup-Fenster
function popup(url, width, height) {
  if (!width) { width = 630; }
  if (!height) { height = 470; }

  window.open(url, 'popup', "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width="+ width+ ",height="+ height);
}


//// Resize abfangen
if (!window.saveInnerWidth) {
    window.onresize = resizeIt;
    window.saveInnerWidth = window.innerWidth;
    window.saveInnerHeight = window.innerHeight;
}


//// Reload anstoßen bei Netscape 4 nach resize
function resizeIt() {
    if (IE4 || IE5 || NS6) {
      checkpos();
    } else {
      if (saveInnerWidth < window.innerWidth ||
        saveInnerWidth > window.innerWidth ||
        saveInnerHeight > window.innerHeight ||
        saveInnerHeight < window.innerHeight )
      {
        window.history.go(0);
      }
    }
}

function dhtmlinit() {
    if (IE4 || NS6 || NS4 || IE5) {
      ll1 = mdl_getLayer('l1');
      mdl_changeLayerContent(ll1,lc);
      checkpos();
    }
    initok = 1;
}

function checkpos() {			// Bildposition feststellen und Layer dorthinplazieren
    ipc = mdl_getImage("sidemenu")
    rx = mdl_getImageX(ipc);
    ry = mdl_getImageY(ipc);
    mdl_moveLayerTo(ll1,rx-150,ry);
}

function popen() {
    mdl_showLayer(ll1);
    mdl_moveLayerTo(ll1,rx,ry);
}

function pclose() {
    mdl_hideLayer(ll1);
}



// --------------------------------------------------------------------------------
// Layer anzeigen / ausblenden
// --------------------------------------------------------------------------------

function mdl_showLayer(lyr) {	// Layer anzeigen
    if (IE4 || NS6) { lyr.style.visibility = "visible"; }
    if (NS4)        { lyr.visibility = "show"; }
}

function mdl_hideLayer(lyr) {	// Layer ausblenden
    if (IE4 || NS6) { lyr.style.visibility = "hidden"; }
    if (NS4)        { lyr.visibility = "hide"; }
}



// --------------------------------------------------------------------------------
// Layer plazieren
// --------------------------------------------------------------------------------

function mdl_moveLayerTo(lyr,xx,yy) {	// Layer absolut positionieren
    if (IE4 || NS6) {
      lyr.style.left = xx+"px";
      lyr.style.top = yy+"px";
      return false;
    }
    if (NS4) {
      lyr.moveTo(xx,yy);
    }
}

function mdl_moveLayerBy(lyr,x,y) {  // Layer absolut positionieren
    if (IE4) {
      lyr.style.pixelLeft += x;
      lyr.style.pixelTop += y;
    }
    if (NS4) {
      lyr.moveBy(x,y);
    }
    if (NS6) {
      lyr.style.left = (mdl_getLayerLeft(lyr)+x)+"px"
      lyr.style.top  = (mdl_getLayerTop(lyr)+y)+"px"
    }
}


// --------------------------------------------------------------------------------
// Pfad auf einen Layer holen (wichtig für alle anderen Funktionen)
// --------------------------------------------------------------------------------

function mdl_getLayer(lyrname) {
    if (IE4)
      return eval('document.all.'+lyrname);
    if (NS6)
      return document.getElementById(lyrname);
    if (NS4)
      return mdl_findLayer(lyrname,document);
}

function mdl_findLayer(lyrname,ds) {			// Routine, um den 'Pfad' zum Layer unter NS zu bestimmen
    for (var i=0;i<ds.layers.length;i++) {		// Die Layer des aktuellen 'document' Objekts durchsuchen
      var lyr = ds.layers[i];
      if (lyr.name == lyrname)			// Ist der aktuelle Layer der gesuchte?
        return lyr;
      if (lyr.document.layers.length > 0) {		// Wenn nicht, enthält er weitere Layer
        var lyr = mdl_findLayer(lyrname,lyr);	// Dann such dort, mit einem neuen document Objekt
        if (lyr != "not_found")
          return lyr;				// In tieferer Ebene gefunden
      }
    }
    return "not_found";				// Layer nicht gefunden ;-[
  }


// --------------------------------------------------------------------------------
// Pfad auf ein Image holen (wichtig für alle anderen Funktionen
// --------------------------------------------------------------------------------

function mdl_getImage(name) {
    if (NS6) {
      var images = document.getElementsByTagName("img")
      for (var i=0;i<images.length;i++) {
        if (images[i].getAttribute("name") == name)
          return images[i];
      }
      return null;
    }
    if (NS4) {
      return mdl_findImage(name,document);
    }
    if (IE4)
      return eval('document.all.' + name);
    return null;
}

function mdl_findImage(name,doc) {
    var i, img;

    for (i=0; i<doc.images.length; i++)
      if (doc.images[i].name == name)
        return doc.images[i];
    for (i=0; i<doc.layers.length; i++) {
      img = mdl_findImage(name, doc.layers[i].document);
      if (img != null) {
        img.container = doc.layers[i];
        return img;
      }
    }
    return null;
}

// --------------------------------------------------------------------------------
// Bildpositionen holen
// --------------------------------------------------------------------------------

function mdl_getImageX(img) {
    var x, obj;
    if (NS4)  {
      if (img.container != null)
        return img.container.pageX + img.x;
      else
        return img.x;
    }
    if (IE4 || NS6) {
      x = 0;
      obj = img;
      while (obj.offsetParent != null) {
        x += obj.offsetLeft;
        obj = obj.offsetParent;
      }
      x += obj.offsetLeft;
      return x;
    }
    return -1;
}


function mdl_getImageY(img) {
    var x, obj;

    if (NS4) {
      if (img.container != null)
        return img.container.pageY + img.y;
      else
        return img.y;
    }
    if (IE4 || NS6) {
      x = 0;
      obj = img;
      while (obj.offsetParent != null) {
        x += obj.offsetTop;
        obj = obj.offsetParent;
      }
      x += obj.offsetLeft;
      return x;
    }
    return -1;
}


// --------------------------------------------------------------------------------
// Funktionen um den Inhalt auszutauschen
// --------------------------------------------------------------------------------

function mdl_changeLayerContent(lyr,content) {
    if (NS6) {
      var rng = document.createRange();
      rng.setStartBefore(lyr);
      newContent = rng.createContextualFragment(content);

      while( lyr.hasChildNodes() )
        lyr.removeChild( lyr.lastChild );

      lyr.appendChild(newContent);
    }
    if (IE4 || IE5) {
      lyr.innerHTML = content;
    }
    if (NS4) {
      lyr.document.open();
      lyr.document.write(content);
      lyr.document.close();
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// SR: Funktionen aus der Webseite
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function init() {
  dhtmlinit();
}

function dn(i) {
  if (document.images) {
  	img = mdl_getImage(i);
	if (i=="sidemenu") i = "share/sidemenu/" + i;  //SR: eingefügt, weil die Sidemenu-Dateien in den Unterunterordner share/sidemenu/... verschoben wurden. 
  	img.src = i + "_on.gif";
  }
}

function up(i) {
  if (document.images) {
  	img = mdl_getImage(i);
	if (i=="sidemenu") i = "share/sidemenu/" + i;  //SR: eingefügt, weil die Sidemenu-Dateien in den Unterunterordner share/sidemenu/... verschoben wurden. 
  	img.src = i + ".gif";
  }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// SR: Error Handler: Trapping and Logging
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// This library module enables suppression of JavaScript errors in a way that allows 
// errors to be hidden from view, logged to a server log file for further review, and 
// optionally displayed in the window.status area if desired. Source: Webmonkey Code 
// Library (http://www.hotwired.com/webmonkey/javascript/code_library/)


trapErrors = false;	// Setting this to false will turn off JS error suppression, setting the 
			//  browser to its normal behavior. Useful for debugging during development.
logErrors = true;	// Setting this to false will stop errors from being logged. Useful when 
			//  working offline or without CGI.
showErrorAsStatusMessage = false;	// Setting this to true will cause the browser to display 
					//  the error message as the window.defaultStatus text,
			// resulting in a quieter, but not invisible, error message. Sometimes this 
			//  can be a more pleasant way to debug JavaScript.
logNumErrors = 3;	// Number of JS errors per page to enter into log before calling it quits. 
			//  Large values aren't recommended ...
			// if you're getting a lot of errors per pageview, then you're probably in 
			//  a nasty loop.
logScriptLoc = "path to your cgi script goes here";  // The script must reside within the htdocs 
			//  tree or in a server-aliased directory like /cgi-bin, since the error 
			//  message is sent via HTTP.
scriptName = "JS Err Log";	// Optional: This can be any string of text used to identify the 
				//  script.
logComment = "Testing";	// Optional: A short comment may be supplied here. You may want to separate 
			//  the remainder of the script from the preceding portion so that you can 
			//  call the same error trapping code with different variable settings from
			//  different HTML pages without repeating a lot of code.

MSIE = (navigator.userAgent.indexOf('MSIE') > -1) ? ( true ) : ( false );	// Is this browser an 
										//  MSIE browser?

if (!MSIE) { originalErrorHandler = window.onerror; }	// Read and store value of current window.onerror property for later restoring.
							// But exclude MSIE because it won't allow us to read the window.onerror property.

originalDefaultStatus = window.defaultStatus;	// Read and store value of current window.defaultStatus property for later restoring.
numErrorEvents = 0;	// Number of Errors we've experienced so far for this pageview.

function handleErrors(msg, errUrl, line) {
  if (logErrors || showErrorAsStatusMessage) {
    if (numErrorEvents < logNumErrors) {
      errImage = new Array(logNumErrors);	// This will hold all of our Image() objects we will 
      						//  create to pass error data to the server.
      d = new Date();	// We append a new Date() object to the end of every GET string to ensure 
      			//  that the URL is always unique, and therefore not subject to the disk 
      			//  cache frustrations of MSIE.
      sn = (scriptName) ? ('&scr=' + escape(scriptName)) : ('');
      lc = (logComment) ? ('&comm=' + escape(logComment)) : ('');
	// Escape the data from the optional scriptName and logComment variables if necessary.
      if (MSIE) {	// window.onerror in MSIE doesn't pass msg, errUrl, line values automatically like Netscape does, so we must approximate them.
        errUrl = this.location;
        msg = 'Unspecified JS Error';
        line = 'MSIE';
      } else {
        line = 'line ' + line;
      }
      if (logErrors) {
        errLogEntry = logScriptLoc + '?url=' + escape(errUrl) + '&msg=' + escape(msg) + '&line=' + escape(line) + sn + lc + '&d=' + escape(d);
	// Build a GET string from the data we have pieced together about the error event.
        errImage[numErrorEvents] = new Image();
        errImage[numErrorEvents].src = errLogEntry;
	// The Image() object is essentially the only Object in NS3, NS4, and MSIE that can have 
	//  its URL refreshed without reloading the page, so we use it as a background data carrier 
	//  for client->server communication.  We set the new URL equal to our GET string, which 
	//  will get parsed by the CGI on the server. The CGI will return empty/garbage data, but 
	//  we don't care, since the image we're loading here isn't for display.
      }
      if (showErrorAsStatusMessage) { window.defaultStatus = 'JavaScript Error: ' + msg + ' in ' + line; }
      numErrorEvents++;
    }
  }
  return trapErrors;
}

function restoreWindowToPreviousState() {	// Set window.onerror and window.defaultStatus to 
						//  their initial state when leaving the page.
  if (!MSIE) { window.onerror = originalErrorHandler; }
  window.defaultStatus = originalDefaultStatus;
}

window.onUnload = restoreWindowToPreviousState;
window.onerror  = handleErrors;
