// browsers.js
// cross browser compatibility
// is included as first javascript file and self-initializes
//--------------------------------------------------------------

//
// GLOBALS
//

// global behavior switches
var DYN = 0;     // check for dynamic html, if 0 -> old browser (< V4)
var DOM = 0;     // check for support of the DOM, if 0 NET or MI4 V4

// global browser switches
var GEC = 0;     // Switch for Netscape 6 (Gecko) and new Mozilla
var NET = 0;     // Switch for Netscape 4
var MI4 = 0;     // Switch for Microsoft IE V4
var MIC = 0;     // Switch for Microsoft IE V5,6
var OPE = 0;     // Switch for Opera

// global strings for browser type and version
var brType;      // Browser type: Net (Netscape 4), Gec (Netscape 5+), Mic or Ope
var brName;      // Browser type, full string
var brVersion;   // Browser version


// global screen and window information
var myScWidth;    // Available width of the screen
var myScHeight;   // Available height of the screen
var myWinWidth;   // Available inner width of the window
var myWinHeight;  // Available inner height of the window


//
// BROWSER SNIFFING
//

// set global browser and behavior switches,
// redirect to sorry.html if DYN is not supported
function sniffBehavior()
{
    // Recognize Opera
    if (window.opera)   OPE = 1;

    // if document.getElementById is supported, the browser supports DYN and DOM,
    // probably new browser, like Gecko, Opera 6 or IE 5,6
    if (document.getElementById)
    {
        DYN = 1;
        DOM = 1;
    }

    // if document.all is supported, the browser supports DYN, but not DOM
    // probably IE 4 (Opera supports them all)
    if (document.all && !OPE)
    {
        DYN = 1;
        MI4 = 1;
    }

    // if document.layers is supported, the browser supports DYN, but not DOM
    // probably Netscape 4 (Opera supports them all)
    if (document.layers && !OPE)
    {
        DYN = 1;
        NET = 1;
    }

    // redirect if not dynamic (old browsers < V4)
    if (!DYN)   location.replace("sorry.html");

    // remove comment for testing
    // alert("DOM:" + DOM + " DYN:" + DYN);
}



// browser type and version info
// recognise netscape, internet explorer and opera
function sniffBrowser()
{

    brType    = navigator.appName.substring(0,3);
    brName    = navigator.appName;
    brVersion = parseInt(navigator.appVersion.substring(0,3));


    // Set Browser switches and distinguish between Netscape 4 and Gecko
    // and between IE 4 and other IE
    if ((brType == "Net") && (DOM))    GEC = 1;
    if ((brType == "Mic") && (DOM))    MIC = 1;

    // check browser type an alert if unknown
    if (!GEC && !NET && !MIC && !MI4 && !OPE)
    {
      // alert a warning message
      alert("Diese Seiten wurden nicht fuer Ihren Browser: \n" +
            brName + " Version " + brVersion + "\ngetestet und werden " +
            "daher u.U. nicht korrekt dargestellt !\n\n" +
            "These pages were not tested for your browser: \n" +
            brName + " Version " + brVersion + "\nand may not display correctly !");

      // treat them depending on DOM as gecko or IE4
      if   (DOM)
           GEC = 1;
      else MI4 = 1;
    }

    // remove comments for testing
    // alert ("Name: " + brName + " Typ: " + brType + " Version: " + brVersion);
    // alert ("GEC:" + GEC + " NET:" + NET + " MIC:" + MIC + " OPE:" + OPE);
}


// function to sniff behavior and browser
// called at the end of this file to initialize
function initPage()
{
    sniffBehavior();
    sniffBrowser();

    // available screen width and height
    myScWidth  = screen.availWidth;
    myScHeight = screen.availHeight;
}


//
// WINDOW- AND SCREEN INFO
//

// move the browser window to the given x,y position
function moveMyWindow(newX, newY)
{
  if ((newX < myScWidth) && (newY < myScHeight))
      self.window.moveTo(newX,newY);
}


// resize the browser window to the given sizes
// make sure the size fits to the available screen
function resizeMyWindow(newWidth, newHeight)
{
  if (newWidth >= myScWidth)      newWidth = myScWidth - 20;
  if (newHeight >= myScHeight)    newHeight = myScHeight - 20;

  if   (MI4 || OPE || GEC)
       self.window.resizeTo(newWidth, newHeight);
  else
  {
       self.window.outerWidth  = newWidth;
       self.window.outerHeight = newHeight;
  }
}


// returns inner width
function getInnerWidth()
{
  var myWidth,
      ok;

  if (NET || GEC || OPE)
  {
    myWidth = window.innerWidth;
  }
  else
  {
    ok = (document.body.clientWidth) ? 1:0
    if   (ok)
         myWidth = document.body.clientWidth;
    else myWidth = 700;
  }
  return myWidth;
}

// returns inner height
function getInnerHeight()
{
    var myHeight,
        ok;

    if (NET || GEC || OPE)
    {
        myHeight = window.innerHeight;
    }
    else
    {
        ok = (document.body.clientHeight) ? 1:0
        if   (ok)
             myHeight = document.body.clientHeight;
        else myHeight = 500;
    }
    return myHeight;
}


// initialise and start sniffing
initPage();

