var width_cache = Array();
var next_cache = Array ();
var parent_cache = Array ();
var lastDiv;
var newImage;
var counter = 0;

window.onload = add_zoom;

function check_dom()
{
    if (! document.getElementsByTagName ) return false;
    if (! document.getElementById ) return false;

    return true;
}

function add_zoom(){
    var id_counter = 1;
	if( check_dom() ){
		var links = document.getElementsByTagName("div");
		
		for( var i = 0; i < links.length; i++ ){
			classname = links[i].getAttribute("class");
			if( classname && classname == "zoomable" ){
				links[i].onclick = showLarge;
				links[i].id = "zoomable" + id_counter;
				id_counter = id_counter + 1;
			}
			else{
			    classname = links[i].getAttribute("className");
    			if( classname && classname == "zoomable" ){
    				links[i].onclick = showLarge;
    				links[i].id = "zoomable" + id_counter;
    				id_counter = id_counter + 1;
    			}
			}
		}
	}
}

function showLarge(){
    lastDiv = this;
    this.onclick = function(){ return false;};
    var img = this.getElementsByTagName("img");
    var links = this.getElementsByTagName("a");
    var tmp = links[0].href;
    
    //img[1] is the zoom button
    img[1].src = "/images/zoom_in.jpg";
    img[1].alt = "Bild verkleinern";
    img[1].title = "Bild verkleinern";
    
    links[0].href = img[0].src;
    links[1].href = img[0].src;

    newImage = new Image ();

    //really great bugfix for IE-Image-Caching
    if( tmp.indexOf("?") > -1 ){
        newImage.src = tmp.substring(0, tmp.indexOf("?")) + "?" + counter;
    }
    else{
        newImage.src = tmp + "?" + counter;
    }
    counter = counter + 1;

    newImage.onload = completeZoom;

    width_cache[this.id] = this.style.width;
  
    return false;
}

function completeZoom ()
{
    var img = lastDiv.getElementsByTagName("img");
    var width = newImage.width + "px";
   
    lastDiv.style.width = width;


    if (newImage.width > 400){
      // special code for mozilla, as IE cannot do this code
      // but it is not needed, as IE and Opera do ignore overflow:hidden for child nodes

      if (navigator.userAgent.search ("compatible; MSIE") == -1){
          if  (navigator.userAgent.search ("Opera") == -1 ){
              parent_cache[lastDiv.id] = lastDiv.parentNode;
              next_cache[lastDiv.id] = lastDiv.nextSibling;
              parent_cache[lastDiv.id].removeChild (lastDiv);
              document.getElementsByTagName("body")[0].appendChild (lastDiv);

              lastDiv.style.top = (window.pageYOffset + window.innerHeight/2 - newImage.height/2 )+ "px";
              lastDiv.zIndex = 50;
          }
      }

      lastDiv.style.position = "absolute";
      lastDiv.style.left = (400 -newImage.width/2) + "px";
  }

  img[0].src = newImage.src;

  newImage.onload = null;
  newImage = null;
  lastDiv.onclick = showSmall;
}

function showSmall()
{
    this.onclick = function(){ return false;};
    var img = this.getElementsByTagName("img");
    var links = this.getElementsByTagName("a");
    var tmp = links[0].href;
    
    lastDiv.zIndex = 0;
    img[1].src = "/images/zoom_out.jpg";
    img[1].alt = "Bild vergr&ouml;ssern";
    img[1].title = "Bild vergr&ouml;ssern";

    
    this.style.width = width_cache[this.id];
    links[0].href = img[0].src;
    links[1].href = img[0].src;
    img[0].src = tmp;

    this.style.position = "static";
    this.style.left = 0;

    if (parent_cache[this.id] != null)
    {
    if (navigator.userAgent.search ("compatible; MSIE") == -1)
     {
      document.getElementsByTagName("body")[0].removeChild (this);
      parent_cache[this.id].insertBefore (this,next_cache[this.id]);
     }
    }
    this.onclick = showLarge;
    return false;
}
