﻿var currentset;
var photocount;
var currentphoto;

function handleLoad() {
  // will only have xml responses
  xmlCB = true;
  
  var set_thumbs = document.getElementById("photosetslist").getElementsByTagName("img");
  
  thumb_click(0);
}

function showLoading() {
  var loader_div = document.getElementById("loaderdiv");
  loader_div.style.backgroundImage = "url(images/ajax-loader.gif)";
}

function hideLoading() {
  var loader_div = document.getElementById("loaderdiv");
  loader_div.style.backgroundImage = "";
}

function setInnerHTML(elementId, str) {
  var el = document.getElementById(elementId);
  el.innerHTML = str;
}

function showPhoto(url) {
  showLoading();
  var show_img = document.getElementById("show_image");
  show_img.innerHTML = "";
  var img = document.createElement("img");
  img.src = url;
  show_img.appendChild(img);
}

function fetchPhoto() {
  // get info on current photo index
  ajaxCall("flickrsetphoto.ashx?setsset=" + setsset + "&setindex=" + currentset + "&photoindex=" + currentphoto, fetchPhotoCallback);
}

function fetchPhotoCallback(photo) {
  //var title = photo.getElementsByTagName("title")[0].firstChild.nodeValue;
  //setInnerHTML("setshowphototitle", title);
  
  var mediumurl = photo.getElementsByTagName("mediumurl")[0].firstChild.nodeValue;
  showPhoto(mediumurl);
}

function updateButtons() {
  // previous button
  var prev = document.getElementById("prevbuttonimg");
  if (currentphoto < 1) {
    // no previous
    prev.style.cursor = "";
    prev.onclick = null;
    prev.src = "images/leftdis.jpg";
  }
  else {
    // we can go back
    prev.style.cursor = "pointer";
    prev.onclick = prevPhoto;
    prev.src = "images/leftpil.jpg";
  }
  
  // next button
  var next = document.getElementById("nextbuttonimg");
  if ((currentphoto+1) < photocount) {
    // we can go to next
    next.style.cursor = "pointer";
    next.onclick = nextPhoto;
    next.src = "images/rightpil.jpg";
  }
  else {
    // no next photo
    next.style.cursor = "";
    next.onclick = null;
    next.src = "images/rightdis.jpg";
  }
}

function nextPhoto() {
  currentphoto++;
  refreshPhotoPane();
}

function prevPhoto() {
  currentphoto--;
  refreshPhotoPane();
}

function refreshPhotoPane() {
  var show_img = document.getElementById("show_image");
  show_img.innerHTML = "";
  fetchPhoto();
  setInnerHTML("setshowcount", (currentphoto+1) + "/" + photocount);
  updateButtons();
}

function thumb_click(index) {
  currentset = index;
  // Call for info on clicked set
  ajaxCall("flickrsetinfo.ashx?setsset=" + setsset + "&setindex=" + index, fillPhotosetData);
}

function fillPhotosetData(setinfo) {
  var title = setinfo.getElementsByTagName("title")[0].firstChild.nodeValue;
  setInnerHTML("setshowtitle", title);
  
  setInnerHTML("setshowdescription", "");
  try {
    var description = setinfo.getElementsByTagName("description")[0].firstChild.nodeValue;
    setInnerHTML("setshowdescription", description);
  } catch(e) {}
  
  photocount = parseInt(setinfo.getElementsByTagName("photos")[0].firstChild.nodeValue);
  
  currentphoto = 0;
  refreshPhotoPane();
}

