// global variable needed for ids
var div_offset = 0;
var tb_modified = false;
var re_init_tb = true;

function protect_images(tb_only){
  for(var i = 0; i<document.images.length; i++){
    if(imgLoaded(document.images[i])==false){
      all_images_rdy = false;
    }
    else{
      all_images_rdy = true;
    }
  }
  if (all_images_rdy==true){
    // modify thickbox function to protect images displayed in thickbox
    if(typeof tb_show == 'function' && tb_modified == false){
        tb_keeper = tb_show;
        tb_show=function(caption, url, imageGroup){
            tb_keeper(caption, url, imageGroup);
            window.setTimeout(function(){protect_images('1');}, 100);
        }
        tb_modified = true;
    }
    protect_images_now(tb_only);
  }
  else{
    window.setTimeout(function(){protect_images('0');}, 100);
  }
}
function protect_images_now(tb_only){
var overlay_image = "images/transparenz.gif";
var container = new Array();
var original_image = new Array();
var image_height = new Array();
var image_width = new Array();
var count = 0;
var skip_image = new Array();

if(tb_only == 1){
    image_container = document.getElementById('TB_window').getElementsByTagName('img');
}
else {
    image_container = document.images;
}
  for(var i = 0; i<image_container.length; i++){
      // don't touch images inside a list
      if(image_container[i].parentNode.tagName.toLowerCase() == 'li' || (image_container[i].parentNode.tagName.toLowerCase() == 'a' && image_container[i].parentNode.parentNode.tagName.toLowerCase() == 'li')){
          skip_image[i] = true;
      }
      else{
          skip_image[i] = false;
      }
      //Container for the original image and the transparent overlay
      container[i] = document.createElement('div');
      container[i].setAttribute('id', 'prot_' + (div_offset+i));
      count++;
      
      //only get css style if there are any
      if(image_container[i].getAttribute('style') != null){
        //Modern Browsers return css styles as a string
        if(typeof image_container[i].getAttribute('style') == 'string'){
          css_style=image_container[i].getAttribute('style');
          tmp_float = image_container[i].style.cssFloat;
          container[i].setAttribute('style', css_style);
          image_container[i].setAttribute('style', '');
          image_container[i].style.cssFloat = tmp_float;
        }
        
        //Internet Explorer doesn't
        else{
          css_style=image_container[i].getAttribute('style').cssText;
          container[i].style.cssText=css_style;
          tmp_float = image_container[i].styleFloat;
          image_container[i].style.cssText = '';
          image_container[i].styleFloat = tmp_float;
        }
      }
      //the container needs to be positioned relative to keep it at the right place
      container[i].style.position="relative";
      
      //we position the original image relative
      image_container[i].style.position='relative';
      
      //and set its Z-Index to 1
      image_container[i].style.zIndex='1';

      // the parent node of the original image needs to be positioned relative
      // so we can position other elements inside it absolute
      image_container[i].parentNode.style.position = 'relative';

      //copy the parent node of the original image into our container (just in case it's in a tag we need (i.e. links))
      container[i].appendChild(image_container[i].parentNode.cloneNode(true));
      
      //we need a reference to our original image if we want to have the title- and alt-tags
      original_image[i] = image_container[i];
      //store the images width and height for later use
      image_height[i] = image_container[i].height;
      image_width[i] = image_container[i].width;
      
      //replace the original images parent with our new div
      if(skip_image[i] == false){
        image_container[i].parentNode.parentNode.replaceChild(container[i], image_container[i].parentNode);
      }
  }

  //lets append our overlay images
  for(var i = 0; i < count; i++){
      //create our overlay image and set its attributes
      overlay_img = document.createElement('img');
      overlay_img.setAttribute('src', overlay_image);
      overlay_img.setAttribute('alt', original_image[i].alt);
      overlay_img.setAttribute('onclick', original_image[i].getAttribute('onclick'));
      overlay_img.setAttribute('title', original_image[i].title);
      overlay_img.setAttribute('height', image_height[i]);
      overlay_img.setAttribute('width', image_width[i]);
      overlay_img.style.zIndex='2';
      overlay_img.style.position = 'absolute';
      if(original_image[i].parentNode.className != 'startpage'){
        overlay_img.style.top="0";
      }
      else{
        overlay_img.style.bottom="0";
      }
      if(original_image[i].id != 'TB_Image'){
        overlay_img.style.right="0"
      }
      else{
        // hack-fix for ie7
        if(internet_explorer_7 == true){
            overlay_img.style.left="0px";
        }
        else{
            overlay_img.style.left="15px";
        }
        overlay_img.setAttribute('onclick', 'tb_remove()');
        document.getElementById('TB_ImageOff').setAttribute('onclick', 'tb_remove()');
        document.getElementById('TB_ImageOff').href = 'javascript:void(0);';
      }
      if(skip_image[i] == false){
        document.getElementById('prot_' + (div_offset+i)).firstChild.appendChild(overlay_img);
      }
  }

  div_offset = div_offset+i;
  if(re_init_tb == true){
    re_init_tb = false;
    if(typeof tb_init == 'function'){
      tb_init('a.thickbox, area.thickbox, input.thickbox');
    }
  }

}

function imgLoaded(img) {
  if (!img.complete) {
  return false;
  }

  if (typeof img.naturalWidth
  != 'undefined' && img.naturalWidth
  == 0) {
  return false;
}
return true;
}
