var tx_tinygallery = function() {
      
   var tx_tinygallery_currentindex;
   
   var showImage = function (index) {
   
      var source = tx_tinygallery_images[index];
      var target = document.getElementById("tx_tinygallery_bigimage");
      var parent = target.parentNode;
      
      // bild austauschen
      parent.replaceChild(source,target);
   
      // bild im container zentrieren
      source.style.marginTop = ((parseInt(source.parentNode.parentNode.style.height) - source.height) / 2) + "px";
      
      // id neu setzen (alte id ist verloren gegangen da ziel ueberschrieben wurde)
      source.setAttribute("id","tx_tinygallery_bigimage");
      
      // href des umgebenden a-tags auf neues originalbild setzen  
      parent.href = tx_tinygallery_masterimage_hrefs[index];
      
      // href des lupe-buttons ebenso  
      jQuery('.magnify-image a').each(function(){
         this.href = tx_tinygallery_masterimage_hrefs[index];
      });
   
      // thumbnails als aktiv markieren bzw. altes thumbnail als inaktiv setzen
      jQuery("#thumbnails-main img").parent().parent().removeClass("thumbnail-act");
      jQuery("#tx_tinygallery_thumb_" + index).parent().parent().addClass("thumbnail-act");
   
      // aktuellen index merken
      tx_tinygallery_currentindex = index;
   
   }
   
   var thumbClick = function (element) {
      var index = parseInt(element.id.substr(21));
      showImage(index);
   }
   
   var browseClick = function (direction) {
   
      var index = tx_tinygallery_currentindex + direction;
      var count = tx_tinygallery_images.length;
   
      if (index < 0) {
         index = count-1;
      } else if (index >= count) {
         index = 0;
      }
   
      showImage(index);
   
   }

   jQuery(document).ready(function(){
      
   });
   
   jQuery(window).load(function(){
   
      var max_height = 0;
      var bild;
      for (var i = 0; i < tx_tinygallery_images.length; i++) {
         bild = tx_tinygallery_images[i];
         if (bild.height > max_height) {
            max_height = bild.height;
         }
      }
         
      jQuery("#bigimg-main").css("height",(max_height+2) + "px");
      jQuery("#bigimg-main div").css("height",max_height + "px");
   
      showImage(0);
   
      jQuery(".prev-image a").click(function(){
         browseClick(-1);
         return false;
      });
   
      jQuery(".next-image a").click(function(){
         browseClick(1);
         return false;
      });
   
      jQuery("#thumbnails-main img").click(function(){
         thumbClick(this);
         this.blur(); // focus wieder entfernen, um haesslichen rand wegzubekommen
         return false;
      });
      
      jQuery("#browse-image-outer").show();
      
   });
   
};

var gal = new tx_tinygallery();

