var imgDiv = null;
var transparentDiv = null;
var dialogWidth = 300;
var dialogHeight = 200;

function createDialog()
{
  imgDiv = document.createElement("div");

  imgDiv.style.display = "none";
  imgDiv.style.width = dialogWidth + "px";
  imgDiv.style.height = dialogHeight + "px";
  imgDiv.className = "imgDiv";
  imgDiv.id = "imgDiv";

  transparentDiv = document.createElement("div");

  transparentDiv.style.display = "none";
  transparentDiv.className = "transparentDiv";
  transparentDiv.id = "transparentDiv";

  docWidth = parseInt(document.documentElement.clientWidth);
  docHeight = parseInt(document.documentElement.clientHeight);

  left1 = Math.floor((docWidth - dialogWidth) / 2);
  top1 = Math.floor((docHeight - dialogHeight) / 2);

  imgDiv.style.top = top1 + "px";
  imgDiv.style.left = left1 + "px";

  document.body.appendChild(imgDiv);
  document.body.appendChild(transparentDiv);
}

function showImg(iId)
{
  if(!imgDiv) createDialog();

  url = "http://localhost/galeria.php?iid=" + iId;
  startGETRequest(url, onImgComplete, onEnd);

  var img1 = document.getElementById('img1');
  if(img1) img1.style.display = "none";
  
  imgDiv.style.display = "block";
  transparentDiv.style.display = "block";
}

function closeImg()
{
  if(!imgDiv) return;

  imgDiv.style.display = "none";
  transparentDiv.style.display = "none";
}

function changeThPage(pId)
{
  url = "http://localhost/galeria.php?pid=" + pId;
  url = encodeURI(url);
  startGETRequest(url, onThComplete, onEnd);
}

function onImgComplete(text, xml)
{
  if(text.substr(0, 5) == 'error'){
    closeImg();
    arr = text.split("\n");
    if(arr.length == 2){
      if(arr[0] == 'error')
        alert(arr[1]);
      else
        alert("Nieprawidłowa odpowiedź serwera.");
    }
  }
  else{
    imgDiv.innerHTML = text;
  }
}

function onThComplete(text, xml)
{
  if(text.substr(0, 5) == 'error'){
    arr = text.split("\n");
    if(arr.length == 2){
      if(arr[0] == 'error')
        alert(arr[1]);
      else
        alert("Nieprawidłowa odpowiedź serwera.");
    }
  }
  else{
    var thDiv = document.getElementById('thDiv');
    thDiv.innerHTML = text;
  }
}

function onEnd()
{
}