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].oldOnClick = (links[i].onclick) ? links[i].onclick : new Function;
            links[i].onclick = function() {
                statistikCountExternalLink(this.href);
                return this.oldOnClick();
            }
          }
    }
}

function statistikCountExternalLink(href)
{
  var req = false;
  try {
      req  = new ActiveXObject("Msxml2.XMLHTTP");
  } catch(e) {
    try {
        req  = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
        req  = false;
    }
  }
  if (!req  && typeof XMLHttpRequest != 'undefined') {
    req = new XMLHttpRequest();
  }
  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 currentPic = null;
function closePic()
{
    if(currentPic) {
        currentPic.onclick();
    }
}
function openPic(href, text, width, height)
{
    if(!document.createElement) return(true);

    if(currentPic) {
        currentPic.onclick();
    }

    if(text!="") var textHeight = 30; else textHeight = 0;

    var div = document.createElement('div');
    if(!div) return(true);

    var x,y;
    if (window.innerHeight) // all except Explorer
    {
      x = window.innerWidth;
      y = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
      // Explorer 6 Strict Mode
    {
      x = document.documentElement.clientWidth;
      y = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
      x = document.body.clientWidth;
      y = document.body.clientHeight;
    }

    y = ((y-height)/2);
    x = ((x-width)/2);

    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        y += window.pageYOffset;
        x += window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        y += document.body.scrollTop;
        x += document.body.scrollLeft;
    } else if( document.documentElement &&
        ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        y += document.documentElement.scrollTop;
        x += document.documentElement.scrollLeft;
    }

    div.style.top = y + "px";
    div.style.left = x + "px";
    div.style.width = (width+20)+"px";
    div.style.height = (height+20+textHeight)+"px";
    div.className = "popupPic";
    div.onclick = function() {
      this.parentNode.removeChild(this);
      currentPic = null;
    }
    var border = document.createElement('div');
    border.className = "border";
    border.style.width = (width)+"px";
    border.style.height = (height+textHeight)+"px";
    div.appendChild(border);


    var img = document.createElement('img');
    img.height = height;
    img.width = width;
    img.src = href;
    div.appendChild(img);

    var divLoading = document.createElement('div');
    divLoading.className = "loading";
    divLoading.style.width = width+"px";
    divLoading.style.height = height+"px";
    var p = document.createElement('p');
    p.appendChild(document.createTextNode('Bild wird geladen...'));
    divLoading.appendChild(p);
    div.appendChild(divLoading);

    var divText = document.createElement('div');
    divText.style.top = (height+10)+"px";
    divText.style.width = width+"px";
    divText.style.height = textHeight+"px";
    divText.className = "text";
    p = document.createElement('p');
    p.appendChild(document.createTextNode(text));
    divText.appendChild(p);
    div.appendChild(divText);

    var aClose = document.createElement('a');
    var imgClose = document.createElement('img')
    imgClose.src = "/images/close.gif";
    imgClose.height = imgClose.width = 15;
    aClose.appendChild(imgClose);
    aClose.href = "javascript:closePic()";
    aClose.className = "close";
    aClose.style.left = (width-15)+"px"
    div.appendChild(aClose);

    document.body.appendChild(div);

    currentPic = div;

    return(false);
}
