// nastaveni parametru provozu galerie
var appearstep=0.10;
var appeartime=50;
var appearblocked=false;// blokace postupneho mizeni a objevovani se
var appearbg=true;  // zapnuti pozadi pod obrazkem
var appearnav=true; // zapnuti navigace mezi obrazky
var slideshowtime=5;
var bgalpha=0.75;
var titleheight=32;

// globalni promenne a inicializace hodnot
var imagelist=new Array();
var imagealpha=0;
var imageindex=0;
var appearaction=0;
var slideshow=0;
var slideshowindex=0;
var actualindex=0;
var img;
img = new Image () ;

function loadimage(url){
 var i=document.getElementById('image');
 i.style.display='none';
 img.name=url;
 img.onload=loadedimageobj;
 img.src=url;
}
function startslide(){
 slideshow=1;
 slideshowindex=actualindex+1;
 loadslide();
}
function stopslide(){
 slideshow=0;
 createnavig();
}
function loadslide(){
 if(slideshowindex>=imageindex)slideshow=0;
 if(slideshow==0&&slideshowindex==imageindex){hideimage();}
 if(slideshow==0){return;}
 loadimage(imagelist[slideshowindex][0]);
 window.setTimeout('loadslide();',slideshowtime*1000);
 slideshowindex++;
}
function buildimagelinks(){
 var i;
 var x=document.getElementById('imagefield').childNodes;
 var h;var t;
 for(i=0;x[i];i++){
  if(x[i].nodeName!='A')continue;
  h='javascript:loadimage(\''+x[i].href+'\');';
  addimage(x[i].href,x[i].title);
  //alert('addimage('+x[i].href+','+x[i].title);
  x[i].href=h;
 }
 document.getElementById('imageshow').innerHTML='<a href="javascript:hideimage();"><img src="" id="image" style="display:none;border:none;" /></a><div id="imagebg"></div><div id="imagenav"></div>';
//alert('imageindex: '+imageindex);
}
function addimage(url,txt){
 imagelist[imageindex]=new Array(url,txt);
 imageindex++;
}
function loadedimageobj(){
 var i=document.getElementById('image');
 i.style.display='none';
 i.src=this.src;
 resizeimage(this.width,this.height);
 return true;
}
function createimagebg(){
 var b=document.getElementById('imagebg');
 b.style.position='absolute';
 b.style.top='0px';
 b.style.left='0px';
 //b.style.width='100%';
 b.style.width=(document.body.scrollWidth)+'px';
 b.style.height=(document.body.scrollHeight)+'px';
 setalpha(b,bgalpha);
 b.style.display='block';
 b.style.zIndex=1000;
}
function setalpha(i,a){
 if(a>1||a<0.001)return;
 i.style.opacity=a;
 i.style.mozOpacity=a;
 i.style.filter='alpha(opacity='+(a*100)+')';
}
function appearimage(){
 if(appearblocked)return;
 if(appearaction!=1)return;
 var i=document.getElementById('image');
 setalpha(i,imagealpha);
 imagealpha+=appearstep;
 if(imagealpha>1){setalpha(i,1);appearaction=0;return;}
 window.setTimeout('appearimage();',appeartime);
}
function disappearimage(){
 var i=document.getElementById('image');
 if(imagealpha<=0||appearblocked){
  if(appearbg){document.getElementById('imagebg').style.display='none';}
  if(appearnav){document.getElementById('imagenav').style.display='none';}
 }
 if(appearblocked){i.style.display='none';return;}
 if(appearaction!=2)return;
 if(imagealpha<=0){
  i.style.display='none';appearaction=0;return;
 }
 setalpha(i,imagealpha);
 imagealpha-=appearstep;
 window.setTimeout('disappearimage();',appeartime);
}
function resizeimage(owidth,oheight){
 var i=document.getElementById('image');
 i.style.position='absolute';
 if(appearbg)createimagebg();
 if(typeof(window.innerWidth)=='number'){
  iWidth=window.innerWidth;
  iHeight=window.innerHeight;
 }else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){
  iWidth = document.documentElement.clientWidth;
  iHeight = document.documentElement.clientHeight;
 }else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){
  iWidth = document.body.clientWidth;
  iHeight = document.body.clientHeight;
 }
 if(typeof(window.pageXOffset)=='number'){
  iScrollX = window.pageXOffset ;
  iScrollY = window.pageYOffset ;
 }else if(typeof(document.body.scrollTop)=='number'){
  iScrollX = document.body.scrollLeft ;
  iScrollY = document.body.scrollTop ;
  if(iScrollX==0&&iScrollY==0&&(document.documentElement.scrollTop>0||document.documentElement.scrollLeft>0)){
   iScrollX=document.documentElement.scrollLeft;
   iScrollY=document.documentElement.scrollTop;
  }
 }else if(document.all){
  iScrollX = document.body.scrollLeft ;
  iScrollY = document.body.scrollTop ;
 }
 iLeft = iWidth/2-owidth/2+iScrollX ;
 iTop  = iHeight/2-oheight/2+iScrollY ;
//alert('scroll: '+iScrollX+'x'+iScrollY+'\niWH: '+iWidth+'x'+iHeight+'\noWH: '+owidth+'x'+oheight+'\npage offset: '+iLeft+'x'+iTop);// TODO
 if(iLeft<0)iLeft=0;
 if(iTop<titleheight)iTop=titleheight;
 i.style.left=iLeft+'px';
 i.style.top=iTop+'px';
 i.style.display='block';
 imagealpha=appearstep;
 appearaction=1;
//alert('document body: '+document.body.scrollHeight);
 appearimage();
 if(appearnav)createnavig(iLeft,iTop-titleheight,owidth,titleheight);
 i.style.zIndex=1001;
}
function createnavig(l,t,w,h){
 var n=document.getElementById('imagenav');
 n.style.zIndex=1002;
 var i;
 var nt='';
 nt=nt+'<table cellspacing="0px"><tr>';
 nt=nt+'<td class="imagebar-left">&nbsp;</td>';
 printindex=0;
 for(i=0;i<imageindex;i++){
  if(imagelist[i][0]==img.name){printindex=i+1;break;}
 }
 nt=nt+'<td class="imagebar-leftbox">'+printindex+'/'+imageindex+'</td>';
 for(i=0;i<imageindex;i++){
  //nt=nt+' <a href="javascript:loadimage(\''+imagelist[i][0]+'\');">'+(i+1)+'</a>'
  if(imagelist[i][0]==img.name){
   nt=nt+'<td class="imagebar-middlebox">';
   if(i>0)nt=nt+' <a href="javascript:loadimage(\''+imagelist[i-1][0]+'\');" title="předchozí">'+'&nbsp;&nbsp;&lt;&nbsp;&nbsp;'+'</a>';
   //nt=nt+' | ';
   nt=nt+'<span class="imagename">'+imagelist[i][1]+'</span>';
   if(i<imageindex-1)nt=nt+' <a href="javascript:loadimage(\''+imagelist[i+1][0]+'\');" title="následující">'+'&nbsp;&nbsp;&gt;&nbsp;&nbsp;'+'</a>';
   
   nt=nt+'</td>'; // middlebox
   nt=nt+'<td class="imagebar-rightbox-slide">';
   if(i<imageindex-2){
    if(slideshow==0)nt=nt+' <a href="javascript:startslide();" title="spustit promítání">'+'&nbsp;&gt;&gt;&gt;&nbsp;'+'</a>';
   }
   if(slideshow==1)nt=nt+'<a href="javascript:stopslide();" title="zastavit promítání">'+'&nbsp;|&nbsp;|&nbsp;'+'</a>';
   nt=nt+'</td>'; // rightbox-slide
   actualindex=i;
   break;
  }
 }
 nt=nt+'<td class="imagebar-rightbox-close">';
 nt=nt+'<span class="imagebar-close"><a href="javascript:hideimage();" title="zavřít náhled">'+'&nbsp;&nbsp;X&nbsp;&nbsp;'+'</a></td>';
 nt=nt+'</td>'; // rightbox-close
 //nt=nt+' '+''+' ';
 nt=nt+'<td class="imagebar-right">&nbsp;</td>';
 nt=nt+'</tr></table>';
 n.innerHTML=''+nt;
 n.style.display='block';
 //n.style.background='#0ff';
 //n.style.color='#f00';
 n.style.position='absolute';
 n.style.top=t+'px';
 n.style.left=l+'px';
 n.style.width=w+'px';
 n.style.height=h+'px';
}
function hideimage(){
 var i=document.getElementById('image');
 appearaction=2;
 slideshow=0;
 disappearimage();
}

