var xmlHttpImageSize;
var oldCellContent;
var oldCellHeight;
var oldCellWidth;
var currentCell;
var showCaption;
var loading = false;
var cancel;

function vytvorXMLHttpRequestImageSize() {
  if (window.ActiveXObject) {
    xmlHttpImageSize = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpImageSize = new XMLHttpRequest();
  }
}

function showImageSize(photoid, isSearch) {
  if(loading == false)
  {
    vytvorXMLHttpRequestImageSize();
    xmlHttpImageSize.onreadystatechange = zpracujZmenuStavuImageSize;
    showCaption = isSearch;
    var url = "include/getimagesize.php" + "?id=" + photoid;
    xmlHttpImageSize.open("GET", url, true);
    xmlHttpImageSize.send(null);
    loading = true;
    cancel = false;
    return true;
  }
}

function hideImageSize() {
  if(loading == true)cancel = true;
  if(currentCell)
  {
    currentCell.innerHTML = oldCellContent;
    currentCell.height = oldCellHeight;
  }
}

function zpracujZmenuStavuImageSize() {
  if(xmlHttpImageSize.readyState == 4) {
    if(xmlHttpImageSize.status == 200) {
      zpracujVysledkyImageSize();
    }
  }
}

function zpracujVysledkyImageSize() 
{
   if(document.getElementsByTagName && (cancel == false))
   {
     var vysledek = xmlHttpImageSize.responseXML;
     var id = vysledek.getElementsByTagName("id")[0].firstChild.nodeValue;
     var type = vysledek.getElementsByTagName("type")[0].firstChild.nodeValue;
     var width = vysledek.getElementsByTagName("width")[0].firstChild.nodeValue;
     var caption = vysledek.getElementsByTagName("caption")[0].firstChild.nodeValue;
     var height = vysledek.getElementsByTagName("height")[0].firstChild.nodeValue;
     var filesize = vysledek.getElementsByTagName("filesize")[0].firstChild.nodeValue;
     var commentcount = vysledek.getElementsByTagName("commentcount")[0].firstChild.nodeValue;
     var shown = vysledek.getElementsByTagName("shown")[0].firstChild.nodeValue;
     currentCell = document.getElementById("Image" + id); 
  
     if(currentCell)
     {
       oldCellContent = currentCell.innerHTML;
       oldCellHeight = currentCell.clientHeight;
       oldCellWidth = currentCell.clientWidth;
       var content = '<div class="arialtextanketa" style="text-align: left;">';
       if(showCaption)content += '<b>' + caption + '</b><br>';
       content += '<b>Typ:</b> ' + type + '<br>';
       if((width > 0) && (height > 0))
       content += '<b>Rozlišení:</b> ' + width + ' x ' + height + '<br>';
       content += '<b>Velikost:</b> ' + filesize + ' KB<br>';
       content += '<b>Komentářů:</b> ' + commentcount + '<br>';
       content += '<b>Zhlédnuto:</b> ' + shown + '<br>';
       content += '</div>';
       currentCell.innerHTML = content;
       currentCell.style.height = oldCellHeight;
       currentCell.style.width = oldCellWidth;
     }     
   }
   loading = false;
   cancel = false;
}

