window.oldOnLoad = (window.onload) ? window.onload : new Function;
window.onload = function() {
    var links = document.getElementsByTagName('a');
    for(var i=0;i<links.length;i++) {
        if(links[i].href.substr(location.protocol.length+2, location.hostname.length)!=location.hostname
          || links[i].href.substr(links[i].href.length-4)==".jpg"
          || links[i].href.substr(links[i].href.length-4)==".pdf"
          || links[i].href.substr(links[i].href.length-4)==".gif"
          || links[i].href.substr(links[i].href.length-4)==".png"
          || links[i].href.substr(links[i].href.length-4)==".zip"
          || links[i].href.substr(links[i].href.length-4)==".doc"
          || links[i].href.substr(links[i].href.length-4)==".xls") {
            if(links[i].href.substr(0, 11)=='javascript:') continue;
            links[i].oldOnClick = (links[i].onclick) ? links[i].onclick : new Function;
            links[i].onclick = function() {
                statistikCountExternalLink(this.href);
                if(this.rel == "extern" || this.rel == "download") {
                    window.open(this.href);
                    return false;
                }
                return this.oldOnClick();
            }
        } else if(links[i].rel=="extern" || links[i].rel=="download") {
            links[i].onclick = function() {
                window.open(this.href);
                return false;
            }
        }
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    var req = loadXMLHttpRequest();
    if(req) {
        req.open('POST', '/statistik_count_screen', true);
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        var data = 'windowWidth='+encodeURIComponent(windowWidth);
        data += '&windowHeight='+encodeURIComponent(windowHeight);
        data += '&screenWidth='+encodeURIComponent(screen.width);
        data += '&screenHeight='+encodeURIComponent(screen.height);
        data += '&flashVersion='+encodeURIComponent(getSwfVer());
        req.send(data);
    }
    window.oldOnLoad();
}

function statistikCountExternalLink(href)
{
  var req = loadXMLHttpRequest();
  if(!req) return(false);
  req.open('POST', '/statistik_count', true);
  req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
  req.send('href='+encodeURIComponent(href)+'&ref='+encodeURIComponent(location.href));
}

var blockCloseAll = function() {
    var divs = document.getElementsByTagName('div');
    for(i=0;i<divs.length;i++) {
        if(divs[i].className=='blockOpened') {
            blockSwitch(divs[i]);
        }
    }
};

var blockSwitch = function(tag) {
    if(tag.className=='blockOpened') {
        tag.className='blockClosed';
    } else if(tag.className=='blockClosed') {
        tag.className='blockOpened';
    } else if(tag.parentNode) {
        blockSwitch(tag.parentNode);
    }
};


function evalHandler(response) {
    try {
        eval(response);
    } catch(e) {
        alert('Ein Fehler beim Updaten ist aufgetrenten: '+e.toString());
    }
}

function _postback(url, data, handler)
{
  var req = loadXMLHttpRequest();
  if(!req) return(false);

  var content = '';
  for(var i in data)
  {
    content += (content.length ? '&' : '') + i + '=' + encodeURIComponent(data[i]);
  }

  function callBack()
  {
    if(req.readyState == 4)
    {
      if(req.status == 200)
      {
        if(typeof handler == 'function')
        handler(req.responseText, req);
      }
      else
      {
        alert('An error has occurred: ' + req.statusText);
      }
    }
  }

  req.onreadystatechange = callBack;

  req.open('POST', url, true);
  req.setRequestHeader
  (
      'Content-Type',
      'application/x-www-form-urlencoded; charset=UTF-8'
  );
  //alert(content);
  req.send(content);
}


function loadXMLHttpRequest() {
    var pageRequest = false;
    /*@cc_on
    @if (@_jscript_version >= 5)
        try {
            pageRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                pageRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                pageRequest = false;
            }
        }
    @end
    @*/

    if (!pageRequest && typeof XMLHttpRequest != 'undefined') {
        pageRequest = new XMLHttpRequest()
    }
    return pageRequest;
}

// JavaScript helper required to detect Flash Player PlugIn version information
/* some parts by quirksmode.org and www.javascriptkit.com/javatutors/conditionalcompile3.shtml */
function getSwfVer(i){

    /*@cc_on
    @if (@_jscript_version >= 5)
        // IE flash detection.
        for(var i=17; i>0; i--){
                try{
                        var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
                        if(flash) {
                            return i;
                        }
                }
                catch(e){
                }
        }
    @end
    @*/

    // NS/Opera version >= 3 check for Flash plugin in plugin array
    if (navigator.plugins != null && navigator.plugins.length > 0) {
        if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
            var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
            var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
            descArray = flashDescription.split(" ");
            tempArrayMajor = descArray[2].split(".");
            return tempArrayMajor[0];
        } else {
            flashVer = -1;
        }
    }
    // MSN/WebTV 2.6 supports Flash 4
    else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
    // WebTV 2.5 supports Flash 3
    else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
    // older WebTV supports Flash 2
    else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
    // Can't detect in all other cases
    else {

        flashVer = -1;
    }
    return flashVer;
}


