
var ImageViewer;

// TODO: Find a better location for the blank gif file?
var blankImageLocation = "../image/blank.html";




/*Encapsulation*/ (function() {

// --------
// Image viewer
// --------

// constructor
//

ImageViewer = function(popupUrl) {
  this.imageUrl = "";
  this.imageDesc = "";
  this.popupUrl = popupUrl;
  this.imageList = [];
  this.descList = [];
  this.imageWindow = null;
  this.nextImage = new Image();
  this.prevImage = new Image();
  this.blankImage = new Image();
  this.blankImage.src = blankImageLocation;
}



// loadImage
//

ImageViewer.prototype.addImage = function(url, desc) {
  this.imageList.push(url);
  this.descList.push(desc);
}



// showImage
//

ImageViewer.prototype.showImage = function(url, desc) {
  if (url) this.imageUrl = url;
  if (url) this.imageDesc = desc;

  // Get the window and document
  var win = this.getWindow();
  var doc = win.document;

  // Update the image
  var imageObj = doc.getElementById("photoImage");
  if (!imageObj) return;

  if (imageObj.src != this.imageUrl) {
    imageObj.src = blankImageLocation; // Safari image resize bug fix
    imageObj.src = this.imageUrl;
  }

  // Update the description
  var descObj = doc.getElementById("photoDescription");
  if (descObj) {
    descObj.firstChild.nodeValue = this.imageDesc;
  }

  // Update the button states
  var index = this.getImageIndex();
  var nextVisible = true, prevVisible = true;

  if (typeof(index) != "number") {
    nextVisible = false;
    prevVisible = false;
  }
  else {
    if (index >= (this.imageList.length - 1)) nextVisible = false;
    if (!index) prevVisible = false;
  }

  var nextObj = doc.getElementById("next");
  if (nextObj) {
    if (nextVisible) {
      nextObj.style.display = "block";
      this.nextImage.src = this.imageList[index + 1];
    }
    else nextObj.style.display = "none";
  }

  var prevObj = doc.getElementById("previous");
  if (prevObj) {
    if (prevVisible) {
      prevObj.style.display = "block";
      this.prevImage.src = this.imageList[index - 1];
    }
    else prevObj.style.display = "none";
  }

  win.focus();
}



// showNextImage
//

ImageViewer.prototype.showNextImage = function() {
  var index = this.getImageIndex();
  if (typeof(index) != "number") return;
  var url = this.imageList[index+1];
  var desc = this.descList[index+1];
  if (url) this.showImage(url, desc);
}



// showPreviousImage
//

ImageViewer.prototype.showPreviousImage = function() {
  var index = this.getImageIndex();
  if (typeof(index) != "number") return;
  var url = this.imageList[index-1];
  var desc = this.descList[index-1];
  if (url) this.showImage(url, desc);
}



// getWindow
//

ImageViewer.prototype.getWindow = function() {
  var loaded;

  // Try to access the image window.  If we can't get to it then it needs to
  // be reloaded.
  try {loaded = !!this.imageWindow.document} catch(e) {loaded = false}

  if (!loaded) {
    this.imageWindow = window.open(this.popupUrl);
    pollLoadState(this);
  }

  return this.imageWindow;
}



// getImageIndex
//

ImageViewer.prototype.getImageIndex = function() {
  var comp, index, i=-1;
  while (comp = this.imageList[++i]) {
    if (this.imageUrl == comp) {
      index = i;
      break;
    }
  }
  return index;
}






// --------
// Image window
// --------

// onNext
//

function onNext() {
  this.imageViewer.showNextImage();
  return false;
}



// onPrevious
//

function onPrevious() {
  this.imageViewer.showPreviousImage();
  return false;
}



// pollLoadState

function pollLoadState(imageViewer) {
  var timeout;
  function pollFunc() {
    if (checkLoadState(imageViewer)) clearTimeout(timeout);
  };
  timeout = setInterval(pollFunc, 100);
}



// checkLoadState
//

function checkLoadState(imageViewer) {
  var win = imageViewer.imageWindow;
  if (!win || win.isViewerLoaded) return true;

  // Check whether the document is done loading
  var imageObj = win.document.getElementById("photoImage");
  var nextObj = win.document.getElementById("next");
  var prevObj = win.document.getElementById("previous");
  if (!imageObj || !nextObj || !prevObj) return;

  // Assign events and members
  nextObj.imageViewer = imageViewer;
  nextObj.onclick = onNext;

  prevObj.imageViewer = imageViewer;
  prevObj.onclick = onPrevious;

  win.isViewerLoaded = true;

  // Display the image
  imageViewer.showImage();

  return true;
}



/*Encapsulation*/})();
