var ZOBRAZENI_FOTKY=8; //Jak dlouho je zobrazena fotka(vcetne zatmivacky), v sekundach
var ZATMENI=800 //jak dlouho trva zatmivacka, v milisekundach
var NAHODNE=1; //1-nahodne poradi. Jinak - razeno dle jmena soubouru

                                     
var PRVEK='slideshow_img_a' //sem patri id tagu obrázku <img>
var SEZNAM='slideshow/slides_a.txt'




/* Initials */
                                    
document.getElementById(PRVEK).style.opacity='0';
document.getElementById(PRVEK).style.filter='alpha(opacity=0)';
var slideshow_array=new Array();
var IMG_PRELOAD= new Image();
var xmlHttp;
var SLOZKA;
AjaxDownload(SEZNAM,NAHODNE);



function zobraz(fotka){
fade(-1,PRVEK);
setTimeout("document.getElementById('"+PRVEK+"').src='"+SLOZKA+"/"+fotka+"'",ZATMENI);      
setTimeout("fade(1,'"+PRVEK+"')",ZATMENI+200);
}

function prefetch(fotka){
var url=slideshow_array[1]+"/"+fotka;
IMG_PRELOAD.src=url;
}


function naplanuj_slideshow(pole){
  if(pole[0]!="SLIDESHOWDATA"){return;}
SLOZKA=pole[1];
var i=2;
  for(;true;i++){
    var next="";
    if(i>=pole.length){break;}
      if(i+1>=pole.length){
        next=pole[2];
      }else{
        next=pole[i+1];
      }
    setTimeout("zobraz('"+pole[i]+"')", (i-2)*ZOBRAZENI_FOTKY*1000);
    setTimeout("prefetch('"+next+"')", ((i-2)*ZOBRAZENI_FOTKY*1000)+20);
    
    
  }
setTimeout("naplanuj_slideshow(slideshow_array)", (i-2)*ZOBRAZENI_FOTKY*1000);
  

}



function fade(inout,element){
if(inout==1){var numero=0.05;}else{var numero=-0.05;}
for (var i=1;i<21;i++){
 setTimeout("addFade('"+numero+"','"+element+"')",i * (ZATMENI/20));
 }
}

function addFade(value,element) {
 document.getElementById(PRVEK).style.opacity = parseFloat(document.getElementById(element).style.opacity) + parseFloat(value);
 
 var cislo=1;
 if(typeof(document.getElementById(element).style.filter) != "undefined"){
 cislo=parseFloat(document.getElementById(element).style.filter.replace("alpha(opacity=","").replace(")",""));
 }
 

 document.getElementById(PRVEK).style.filter ="alpha(opacity="+(cislo+parseFloat(value)*100) +")"; //IE8 
}
         

function AjaxDownload(adresa,rand){

xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
return;
}                            
var url=adresa;
  if(parseInt(rand)==1){
  url+="?random=1&t="+new Date().getTime();
  }

xmlHttp.onreadystatechange=AjaxDownloadStateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}


function AjaxDownloadStateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
slideshow_array=xmlHttp.responseText.split("\n");
  //trim
  for(var i=0;i<slideshow_array.length;i++){
  slideshow_array[i]=slideshow_array[i].replace(/^\s+|\s+$/g,"");

  }
document.getElementById(PRVEK).src=slideshow_array[2];
fade(1,PRVEK);


naplanuj_slideshow(slideshow_array);
} 
}



function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
  {
  objXMLHttp=new XMLHttpRequest()
  }
else if (window.ActiveXObject)
  {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
return objXMLHttp
}

