
function HeavyBox(gallery,cls){

  var doyle=document.getElementsByTagName("body");
  var bodie=doyle[0];
    
  var darkness=document.createElement("div");
  darkness.id="darkness";
  darkness.style.height=getViewportHeight()+"px";
   
  bodie.appendChild(darkness);
   
   
  var table_container=document.createElement("div");
  table_container.id="heavybox_table_container";
  table_container.innerHTML='<table id="heavybox_table" cellspacing="0" cellpadding="0"><tbody><tr><td><div id="heavybox"><img src="" alt="" /><div id="heavybox_description"><div id="heavybox_count"></div><div id="heavybox_close">'+(document.lang=="cs"?"zavřít":"close")+'</div><p><b>&nbsp;</b></p><p>&nbsp;</p></div><a id="heavybox_prev"></a><a id="heavybox_next"></a></div></td></tr></tbody></table>';
  var viewport_height=getViewportHeight();
  var viewport_width=getViewportWidth();
  table_container.firstChild.style.width=viewport_width+"px";
  table_container.firstChild.style.height=viewport_height+"px";
  table_container.style.top=bodie.scrollTop+"px";
  bodie.appendChild(table_container);

  var box=gE("heavybox");
   
  registerEvent(gE("heavybox_prev"),"mouseover",function(e){if(!browser.isIE && e || browser.isIE){e=getEvent(e);e.target.style.backgroundPosition="0 -800px";}});
  if(browser.isIE) registerEvent(gE("heavybox_prev"),"mousemove",function(e){e=getEvent(e);e.target.style.backgroundPosition="0 -800px";});
  registerEvent(gE("heavybox_prev"),"mouseout",function(e){if(!browser.isIE && e || browser.isIE){e=getEvent(e);e.target.style.backgroundPosition="0 0";}});
  registerEvent(gE("heavybox_next"),"mouseover",function(e){if(!browser.isIE && e || browser.isIE){e=getEvent(e);e.target.style.backgroundPosition="100% -800px";}});
  if(browser.isIE) registerEvent(gE("heavybox_next"),"mousemove",function(e){e=getEvent(e);e.target.style.backgroundPosition="100% -800px";});
  registerEvent(gE("heavybox_next"),"mouseout",function(e){if(!browser.isIE && e || browser.isIE){e=getEvent(e);e.target.style.backgroundPosition="100% 0";}});
   

   
  var onKey=function(e){
    e=getEvent(e);
    if(e.which==27)day();
    if(e.which==37 && current_index>1)prevImage();
    if(e.which==39 && current_index<images.length-1)nextImage();
    e.preventDefault();
  };
   
   
  var resz=function(){
    darkness.style.height=getElementHeight(bodie)+40+"px";
    table_container.style.height=getViewportHeight()+"px";
    table_container.firstChild.style.width=getViewportWidth()+"px";
    table_container.firstChild.style.height=getViewportHeight()+"px";
  };

   
  var scrl=function(){
    table_container.style.top=document.documentElement.scrollTop+"px";
  };
   
   
  var night=function(){
    resz();
    scrl();
    setDB(darkness,table_container);
    registerEvent(document,"keydown",onKey);
    registerEvent(window,"resize",resz);
    registerEvent(window,"scroll",scrl);
  };
   
  
  
  var day=function(){
    setDN(darkness,table_container);
    unregisterEvent(document,"keydown",onKey);
    unregisterEvent(window,"resize",resz);
    unregisterEvent(window,"scroll",scrl);
  };
   
   
  var hideButtons=function(){
    gE("heavybox_next").style.backgroundPosition=gE("heavybox_prev").style.backgroundPosition="0 0";
  };
   
   
   
   
  var thumbs_container=gE(gallery);
  var thumbs=isUndefined(cls)?thumbs_container.getElementsByTagNames("a"):thumbs_container.getElementsByClassName(cls);


  for(var i=0;i<thumbs.length;i++){

    var fc=function(e){
      e=getEvent(e);
      if(e.target.tagName.toLowerCase()=="img")  var image_id_match=e.target.parentNode.parentNode.id.match(/_(\d+)$/); else{
        
        if(e.target.tagName.toLowerCase()=="span") var image_id_match=e.target.parentNode.id.match(/_(\d+)$/); 
        
        else var image_id_match=e.target.id.match(/_(\d+)$/);
         
       }
      night();
     
     
      var href=e.target.tagName.toLowerCase()=="img"?e.target.parentNode.parentNode.href:e.target.href;
     if(e.target.tagName.toLowerCase()=="span")var href=e.target.parentNode.href;
     
      var path_matches=href.match(/^(.+)\/[^\/]+$/);
     
      path=path_matches[1]+"/";
   
      if(!initialized)init(image_id_match[1]); else{ 
        var ajajax=new AJAX();
        var getResponse=function(status){
         if(status!=200){
            alert("AJAX error (status:"+status+")");      
            return false;
          }
          var ro=ajajax.getResponseObject();
          var imgs=ro.getElementsByTagName("img");
   
          for(var i=0;i<imgs.length;i++){
            var ind=getTagText(imgs[i],"position");
            if(!images[ind].data_loaded){
              var img=imgs[i];
              images[ind].id=img.getAttribute("id");
              images[ind].src=img.getAttribute("src");
              images[ind].width=img.getAttribute("width");
              images[ind].height=img.getAttribute("height");
              images[ind].name=getTagText(img,"name");
              images[ind].description=getTagText(img,"description");
              images[ind].preload.src=path+images[ind].src;
              images[ind].data_loaded=true;
            }
            if(images[ind].id==image_id_match[1])current_index=ind;          
          }
          loadImage();
        };
       
        ajajax.setFinalMethod(getResponse);
        ajajax.request("GET","/kubure.php","action=photo_gallery&photo="+image_id_match[1]+"&presentation_id="+Page.presentation_id);
      }
       
      e.preventDefault();
      return false;
    };

    registerEvent(thumbs[i],"click",fc);
  }
 
   
  var images=[];
  var initialized=0;
  var current_index=0;
  var path="";
   
   
   
  var getTagText=function(el,tag_name){
    var els=el.getElementsByTagName(tag_name);
    if(els[0].firstChild)return els[0].firstChild.data;
    return ""; 
  };
   
   
  var getData=function(image_id){
    var ajajax=new AJAX();

    var getResponse=function(status){
      if(status!=200){
        alert("AJAX error (status:"+status+")");      
        return false;
      }
        
      var ro=ajajax.getResponseObject();
      var imgs=ro.getElementsByTagName("img");

      for(var i=0;i<imgs.length;i++){
  
        var ind=getTagText(imgs[i],"position");
        if(!images[ind].data_loaded){
          var img=imgs[i];
          
          images[ind].id=img.getAttribute("id");
          images[ind].src=img.getAttribute("src");
          images[ind].width=img.getAttribute("width");
          images[ind].height=img.getAttribute("height");
          images[ind].name=getTagText(img,"name");
          images[ind].description=getTagText(img,"description");
          
          images[ind].preload.src=path+images[ind].src;
          
          images[ind].data_loaded=true;
        }
      }
        
    };
     
    ajajax.setFinalMethod(getResponse);
    ajajax.request("GET","/kubure.php","action=photo_gallery&photo="+image_id+"&presentation_id="+Page.presentation_id);
  };
   
   
   
   
   
  var init=function(image_id){

    var ajajax=new AJAX();

    var getResponse=function(status){
      if(status!=200){
        alert("AJAX error (status:"+status+")");      
        return false;
      }
        
      var ro=ajajax.getResponseObject();
      var gals=ro.getElementsByTagName("photo_gallery");
      var lng=gals[0].getAttribute("count");

      for(var i=1;i<=lng;i++)images[i]={id:0,data_loaded:false,src:"",width:0,height:0,name:"",description:"",preload:new Image()};
        
      var imgs=ro.getElementsByTagName("img");

      for(var i=0;i<imgs.length;i++){
        var img=imgs[i];
        var ind=getTagText(img,"position");
        images[ind].id=img.getAttribute("id");
        images[ind].src=img.getAttribute("src");
        images[ind].width=img.getAttribute("width");
        images[ind].height=img.getAttribute("height");
        images[ind].name=getTagText(img,"name");
        images[ind].description=getTagText(img,"description");
        images[ind].preload.src=path+images[ind].src;
        images[ind].data_loaded=true;
        if(images[ind].id==image_id)current_index=ind;
      }
      loadImage();
    };
   
    ajajax.setFinalMethod(getResponse);
    ajajax.request("GET","/kubure.php","action=photo_gallery&photo="+image_id+"&presentation_id="+Page.presentation_id);
    initialized=1;
  };
   
   
   
   
  var noMoreWait=function(){
    box.style.backgroundImage="none";
  };
   
   
  var resizeBox=function(w,h,fc){
    var fc_resize_h=function(x){gE("heavybox").style.width=x+"px";};
    var fc_resize_h_final=function(x){fc_resize_h(x); window._heavybox_slider_v.run();};
    var fc_resize_v=function(y){gE("heavybox").style.height=y+"px";};
    var fc_resize_v_final=function(y){fc_resize_v(y);noMoreWait();if(isFunction(fc))fc();};
    window._heavybox_slider_h=new Slider("_heavybox_slider_h",getElementWidth(box),w,100,2,fc_resize_h,fc_resize_h_final);
    window._heavybox_slider_v=new Slider("_heavybox_slider_v",getElementHeight(box),h,100,2,fc_resize_v,fc_resize_v_final);
    window._heavybox_slider_h.run();
   };
   
   
  var showDescription=function(fc){
    var fc_resize_d=function(y){gE("heavybox_description").clipEl(0,0,0,y);};
    var fc_resize_d_final=function(y){fc_resize_d(y);if(isFunction(fc))fc();};
    window._heavybox_slider_d=new Slider("_heavybox_slider_d",40,0,1,10,fc_resize_d,fc_resize_d_final);
    window._heavybox_slider_d.run();
   };
   
   
  var hideDescription=function(){
    gE("heavybox_description").clipEl(0,0,0,40);
    setDN(box.firstChild);
  };
   
   
  var textDescription=function(txt){
    var cont=gE("heavybox_description");
    if(isString(txt))cont.lastChild.previousSibling.firstChild.firstChild.data=txt+"\u00A0";
    if(images.length>1) cont.lastChild.firstChild.data=(document.lang=="cs"?"Obrázek ":"Image ")+current_index+(document.lang=="cs"?" z":" of")+(((images.length<6 || images.length==8) && document.lang=="cs")?"e":"")+" "+(images.length-1);
  };
   
   
   
  var loadImage=function(){
    hideDescription();
    resizeBox(parseInt(images[current_index].width)+20,parseInt(images[current_index].height)+20,function(){textDescription(images[current_index].description);showDescription(showImage());});
   };
   
   
  var showImage=function(){
    box.firstChild.src=path+images[current_index].src;
    setDB(box.firstChild);   
    if(current_index>1){
      setVV("heavybox_prev");
      if(browser.isOldIE) gE("heavybox_prev").style.height=Math.round(0.9*images[current_index].height)+"px";
    }else setVH("heavybox_prev"); 
   
    if(current_index==images.length-1)setVH("heavybox_next");else{
      setVV("heavybox_next");
      if(browser.isOldIE) gE("heavybox_next").style.height=Math.round(0.9*images[current_index].height)+"px";
     }
   };
   
   
   
   
   
  var nextImage=function(){
    if(current_index<images.length-1)current_index++;

    if(current_index==1){
      if(!images[current_index].data_loaded || !images[current_index+1].data_loaded)getData(images[current_index].id);
    }else if(current_index==images.length-1){
      if(!images[current_index].data_loaded || !images[current_index-1].data_loaded)getData(images[current_index].id);
    }else{
      if(!images[current_index].data_loaded || !images[current_index+1].data_loaded || !images[current_index-1].data_loaded)getData(images[current_index].id);
    }

    setVH("heavybox_prev","heavybox_next");
    hideButtons();
    loadImage();
   };
   
   
   
  var prevImage=function(){
    if(current_index>1)current_index--;  
    if(current_index==1){
      if(!images[current_index].data_loaded || !images[current_index+1].data_loaded)getData(images[current_index].id);
    }else if(current_index==images.length-1){
      if(!images[current_index].data_loaded ||  !images[current_index-1].data_loaded)getData(images[current_index].id);
    }else{
      if(!images[current_index].data_loaded || !images[current_index+1].data_loaded || !images[current_index-1].data_loaded)getData(images[current_index].id);
    }

    setVH("heavybox_prev","heavybox_next");
    hideButtons();
    loadImage();
  };
   
   
   
  registerEvent(gE("heavybox_next"),"click",nextImage);
  registerEvent(gE("heavybox_prev"),"click",prevImage);
   
  registerEvent(gE("heavybox_close"),"click",day);
   


}






