// nova galerie

var galSizeH = 0;
var galSizeW = 0;
var galScroll = 0;
var galMiniH = 0;
var galTopDivH = 0;
var galId = 0;

var aktualFoto = 0;
var predesleFoto = 0;
var dalsiFoto = 0;

var casovacBloku;
var casovacPrehravani;
var gallang = 'cs';


function galWinDim(){
  
	if (window.innerHeight){
    galSizeH = window.innerHeight;
    galSizeW = window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientHeight){
		galSizeH = document.documentElement.clientHeight;
		galSizeW = document.documentElement.clientWidth;
	} else if (document.body && document.body.clientHeight){
		galSizeH = document.body.clientHeight;
		galSizeW = document.body.clientWidth;
  }
  if(galSizeH == 0){
    alert('Error');
  } else {
    document.getElementById('galery_view_transparent').style.height = (galSizeH) + 'px';
    document.getElementById('galery_view').style.height = (galSizeH) + 'px';
    document.getElementById('galery_view_transparent').style.width = (galSizeW) + 'px';
    document.getElementById('galery_view').style.width = (galSizeW) + 'px';
    casovacBloku = setTimeout('galWinDim()',100);
  }

  if (document.documentElement && document.documentElement.scrollTop) {
    galScroll = document.documentElement.scrollTop;
  } else if (document.body) {
    galScroll = document.body.scrollTop;
  } else {
    galScroll = 0;
  }
  document.getElementById('galery_view_transparent').style.top = (galScroll) + 'px';
  document.getElementById('galery_view').style.top = (galScroll) + 'px';
  
  if(document.getElementById('gal_minidiv')){
    galTopDivH = galSizeH - galMiniH ;
    document.getElementById('gal_topdiv').style.height = (galTopDivH) + 'px';
    document.getElementById('gal_topdiv').style.width = (galSizeW) + 'px';
    document.getElementById('gal_minidiv').style.width = (galSizeW) + 'px';
  }
  if(document.getElementById('gal_miniobal')){
    document.getElementById('gal_miniobal').style.width = (galSizeW) + 'px';
  }  
}

function showgalery(obr, gal, minisize, lang){
  var galeryDiv;
  minisize = 120;
  galMiniH = minisize;
  galId = gal;
  gallang = lang;
  document.getElementById('galery_view').style.display = '';
  document.getElementById('galery_view_transparent').style.display = '';
  //document.getElementById('galery_view').focus();
  galeryDiv = '<div style="width: 0px; height: ' + (minisize) + 'px; padding: 0px;	margin: 0px;" id="gal_minidiv"></div>';
  galeryDiv += '<div style="width: 0px; padding: 0px; margin: 0px; height: 0px;" id="gal_topdiv"></div>';
  
  document.getElementById('galery_view').innerHTML = galeryDiv;

  galWinDim();  
  showObrGal(obr);
  setTimeout('loadMiniWait()',100);
  
}

function loadMiniWait(){
  if(document.getElementById('gal_topdiv').innerHTML != ''){
    loadMini();
  } else {
    setTimeout('loadMiniWait()',100);
  }
}



function showObrGal(obr){
  if(aktualFoto != obr){
    if(document.getElementById('igal4u' + aktualFoto)){
      document.getElementById('igal4u' + aktualFoto).style.border = '2px solid black';
    }
    if(document.getElementById('igal4u' + obr)){
      document.getElementById('igal4u' + obr).style.border = '2px solid white';
    }
    
    aktualFoto = obr;
    myDiv = document.getElementById('gal_topdiv');
    var params = "&subakce=obrshow" +
                 "&obr=" + obr + 
                 "&galTopDivH=" + galTopDivH + 
                 "&galSizeW=" + galSizeW;
                 
    if (xmlHttp)
    {
      try
      {
        xmlHttp.open('GET', '/galerie/ajax.php?akce=gal_mini&lanG=' + gallang + params, true);
        xmlHttp.onreadystatechange = handleRequestStateChange;
        xmlHttp.send(null);
      }
      catch (e)
      {
        alert("Can't connect to server 2:\n" + e.toString());
      }
    }
    afterLoadMini();
  }
}

function loadMini(){

  myDiv = document.getElementById('gal_minidiv');
  var params = "&subakce=mini" +
               "&gal=" + galId + 
               "&galSizeW=" + galSizeW +
               "&minih=" + galMiniH;
               
  if (xmlHttp)
  {
    try
    {
      xmlHttp.open('GET', '/galerie/ajax.php?akce=gal_mini&lanG=' + gallang + params, true);
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(null);
    }
    catch (e)
    {
      alert("Can't connect to server 2:\n" + e.toString());
    }
  }
  
  setTimeout('loadAfterMiniWait()',100);
  
}

function loadAfterMiniWait(){
  if(document.getElementById('seznam_fotek')){
    afterLoadMini();
  } else {
    setTimeout('loadAfterMiniWait()',100);
  }
}

function afterLoadMini(){
  if(document.getElementById('seznam_fotek')){
    var minuleVpoli = 0;
    var nastavDalsi = 0;
    
    document.getElementById('agal4u' + aktualFoto).focus();
    
    document.getElementById('igal4u' + aktualFoto).style.border = '2px solid white'; // style=\"border: 2px solid black;\"
  
    var sznFoto = document.getElementById('seznam_fotek').innerHTML;
    var sznFotoArr = sznFoto.split(',');
    
    predesleFoto = 0;
    dalsiFoto = 0;
    
    for (i=0;i<sznFotoArr.length;i++){
      
      if(aktualFoto == sznFotoArr[i]){
        predesleFoto = minuleVpoli;
        nastavDalsi = 1;
      } else if (nastavDalsi == 1){
        dalsiFoto = sznFotoArr[i];
        break;
      }
      minuleVpoli = sznFotoArr[i];
    }
    setTimeout('waitLoadPopis()',100);
  }
}

function waitLoadPopis(){
  if(document.getElementById('galIdPopisObr')){
    if(document.getElementById('galIdPopisObr').innerHTML == aktualFoto){

      var widthPop = document.getElementById('gal_popis').offsetWidth;
      var heightPop = document.getElementById('gal_popis').offsetHeight;

      document.getElementById('gal_popis_transparent').style.width = widthPop + 'px';
      document.getElementById('gal_popis_transparent').style.height = heightPop + 'px';
    
    } else {
      setTimeout('waitLoadPopis()',100);
    }
  } else {
    setTimeout('waitLoadPopis()',100);
  }
}

function galPopisClose(){
  document.getElementById('gal_popis').style.display = 'none';
  document.getElementById('gal_popis_transparent').style.display = 'none';
}


function prevFoto(){
  if(predesleFoto > 0){
    showObrGal(predesleFoto);
  }
}

function nextFoto(){
  if(dalsiFoto > 0){
    showObrGal(dalsiFoto);
  }
}

function galPlay(){
  if(dalsiFoto > 0){
    nextFoto();
    casovacPrehravani = setTimeout('galPlay()', 5000);
  } else {
    clearTimeout(casovacPrehravani);
  }
}

function galPlayStop(){
  clearTimeout(casovacPrehravani);
}

function closeGalery(){
  clearTimeout(casovacPrehravani);
  document.getElementById('galery_view').style.display = 'none';
  document.getElementById('galery_view').style.height = '0px';
  document.getElementById('galery_view').style.width = '0px';

  document.getElementById('galery_view_transparent').style.display = 'none';
  document.getElementById('galery_view_transparent').style.height = '0px';
  document.getElementById('galery_view_transparent').style.width = '0px';


  document.getElementById('galery_view').style.innerHTML = '';
}

function checkKey(evt){

  var charCode = (evt.which) ? evt.which : event.keyCode

  if(charCode == 37){
    prevFoto();
  } else if(charCode == 39){
    nextFoto();
  } else if(charCode == 80){
    galPlay();
  } else if(charCode == 83){
    galPlayStop();
  } else if(charCode == 27){
    closeGalery();
  } 

}
// konec nove galsrie

var neww = false;
function openw(co,okno,jak,nazev){
  var verze = parseInt(navigator.appVersion);
  var wi = 'width=';
  var hi = 'height=';
  var res = 'resizable=';
  var scr = 'scrollbars=';

  if (verze >= 4){

    sirkao = screen.width;
    vyskao = screen.height;

    var sirkaz = co.indexOf('x=') + 2;
    var sirkat = co.substr(sirkaz);
    var sirkak = sirkat.indexOf('&');
    if(sirkak > 0){
      var sirka = sirkat.substr(0, sirkak);
    }
    else{
      var sirka = sirkat;
    }

    var vyskaz = co.indexOf('y=') + 2;
    var vyskat = co.substr(vyskaz);
    var vyskak = vyskat.indexOf('&');

    if(vyskak > 0){
      var vyska = vyskat.substr(0, vyskak);
    }
    else{
      var vyska = vyskat;
    }

    sirka = sirka * 1;
    vyska = vyska * 1;

    if((isNaN(sirka) == true) && (isNaN(vyska) == true)){
      var res2cd1 = co.split('_');
      sirka = res2cd1[1];
      var res2cd2 = res2cd1[2].split('.');
      vyska = res2cd2[0];
    }

//    vyska = (vyska * 1) + 30;

  	if(sirka > sirkao){
  	  wih = sirkao;
      resh = 1;
      scrh = 1;
    }
    else{
      wih = sirka;
      resh = 0;
      scrh = 0;
    }

  	if(vyska > (vyskao - 30)){
  	  hih = vyskao;
      resh = 1;
      scrh = 1;
    }
    else{
      hih = vyska;
      if(scrh == 0){
        resh = 0;
        scrh = 0;
      }
    }

    wih = wih * 1;
    hih = hih * 1;

    if(scrh == 1){
      if((wih + 19) < sirkao){
        wih = wih + 19;
      }
      if((hih + 19) < vyskao){
        hih = hih + 19;
      }
    }

    var odshora = (vyskao - hih) / 2;
    if(odshora < 0){
      odshora = 0;
    }

    odboku = (sirkao - wih) / 2;
    if(odboku < 0){
      odboku = 0;
    }

    jak = wi + wih + ', ' + hi + hih + ', ' + res + resh + ', ' + scr + scrh + ', top=' + odshora + ', left=' + odboku;
  }

  if(neww != false){
	  if (typeof(neww.document) == "object"){
		  neww.window.close();
		}
	}

	neww=window.open('',okno,jak);
	neww.document.write("<html>\n<head>\n");
	neww.document.write("<title>"+nazev+"</title>\n</head>\n");
	neww.document.write("<body style=\"margin: 0;\">\n");
	neww.document.write("<div style=\"width: 100%; position: absolute; top: 40%; left: 0px; text-align: center; z-index: 1;\">Please wait ...</div>\n");
	neww.document.write("<a href=\"javascript:window.close()\" style=\"position: absolute; top: 0px; left: 0px; z-index: 2;\"><img src=\""+co+"\" border=\"0\" alt=\""+nazev+"\"></a>\n");
	neww.document.write("</body>\n");
	neww.document.write("</html>");
}

function prihlaseni(url) {
	var w=370;
	var h=280;
	var windowfeatures='toolbar=no, scrollbars=yes, resizable=yes, menubar=no';
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl;
	win = window.open(url, "prihlaseni", winprops+windowfeatures)
	if(parseInt(navigator.appVersion) >= 4){
		win.window.focus();
	}
}

function regwindow(url){
	var w=600;
	var h=400;
	var windowfeatures='toolbar=0, scrollbars=1, resizable=1, menubar=0';
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl;
	win = window.open(url, "registrace", winprops+windowfeatures)
	if(parseInt(navigator.appVersion) >= 4){
		win.window.focus();
	}
}

function appwindow(url, wu, hu, wname){

	var w;
	var h;

  sirkao = screen.width;
  vyskao = screen.height;

  if(wu >= sirkao){wu = (sirkao - 40)}
  if(hu >= vyskao){hu = (vyskao - 50)}

	if(wu > 0){
    w = wu;
  }
  else{
    w = 600;
  }
	if(hu > 0){
    h = hu;
  }
  else{
    h = 400;
  }

  if(wname == ''){
    wname = 'registrace';
  }

  var windowfeatures='toolbar=0, scrollbars=1, resizable=1, menubar=0';
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl;
	win = window.open(url, "registrace", winprops+windowfeatures)
	if(parseInt(navigator.appVersion) >= 4){
		win.window.focus();
	}
}

function appopen(url){
  if(opener == '[object]'){
    window.open(url);
    window.close();
  }
  else{
    parent.location.href=url;
  }
}

function datetoform(datum, kam){
  window.opener.document.getElementById(kam).value = datum;
  window.close();
}


function confirmLink(theLink, theMsg){
    var is_confirmed = confirm(theMsg);
    if (is_confirmed) {
    }
    return is_confirmed;
}
