// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/* key codes */
RET = 13;
LEFT = 37;
UP = 38;
RIGHT = 39;
DOWN = 40;

/* how many to prefetch before and after a given item */
PREFETCH_WINDOW = 3;

/**************************************************
 * The selection boxes are on hold for now. 
 * var boxIDs = null;
 * 
 * function changeSelection(code) {
 *  var len = boxIDs.length
 *  for (var i = 0; i < len; i++) {
 *    var boxID = boxIDs[i];
 *
 *    if (boxID == selected) {
 *      var box = document.getElementById(boxID);
 *      box.style.border = "thin solid gray";
 *      var j;
 *
 *      if (code == UP) {
 *        if (i == 0) j = len - 1;
 *        else j = i - 1;
 *      } else if (code == DOWN) {
 *        if (i == len - 1) j = 0;
 *        else j = i + 1;
 *      }
 *      selected = boxIDs[j];
 *      box = document.getElementById(selected);
 *      box.style.border = "thin solid black";
 *      break;
 *    }
 *  }
 *}
 ***************************************************/

function loadPage(code) {
  location.href = destinations[code];
}

function changeImage(code) {
  try {
    if (code == LEFT && currentPhoto > 0) {
      currentPhoto--;
      updatePhotos(currentPhoto);
    } else if (code == RIGHT && (currentPhoto+1 < photos.length)) {
      currentPhoto++;
      updatePhotos(currentPhoto);
    }
  } catch (e) {
    /* Looks like the preloader isn't going to work--change pages instead */
	loadPage(code);
  }
}

function handleKeyUp(e) {
  var code;

  if (!e) var e = window.event;
  if (e.keyCode) code = e.keyCode;
  else if (e.which) code = e.which;

  if (handlers[code]) handlers[code](code);

  return true;
}

function removeChildren(obj) {
  while (obj.firstChild) obj.removeChild(obj.firstChild);
}

function prependChild(parent, child) {
  parent.insertBefore(child, parent.firstChild);
}

function loadPhoto(photoIdx) {
  if (photos[photoIdx].img) return;

  photo = photos[photoIdx];
  photo.img = new Image();
  photo.img.src = photo.url;
  photo.img.id = "showPhoto";
  photos[photoIdx] = photo;
}

function addComment(comment) {
  var commentText = comment.name+": "+comment.comment;
  var div = document.createElement("div");
  div.className = "comment";
  div.appendChild(document.createTextNode(commentText));
  document.getElementById("comments").appendChild(div);
}

function newComment() {
  var comment = {
    "name": document.newCommentForm.name.value, 
    "comment": document.newCommentForm.comment.value
  };

  addComment(comment);
  photos[currentPhoto].comments.push(comment);
}

function showPhoto(photoIdx) {
  loadPhoto(photoIdx);
  photo = photos[photoIdx];

  var photoAnchor = document.getElementById("photoAnchor");
  photoAnchor.href = photo.origurl;
  removeChildren(photoAnchor);

  //FIXME: make sure photo.img has been loaded
  photoAnchor.appendChild(photo.img);

  var caption = document.getElementById("caption");
  removeChildren(caption);
  caption.appendChild(document.createTextNode(photo.caption));

  var showTitle = document.getElementById("showTitle");
  removeChildren(showTitle);
  showTitle.appendChild(document.createTextNode(photo.title));

  var showTags = document.getElementById("showTags");
  removeChildren(showTags);
  showTags.appendChild(document.createTextNode(photo.tags));

  var comments = document.getElementById("comments");
  removeChildren(comments);

  var len = photo.comments.length;
  for(var i = 0; i < len; i++) {
    addComment(photo.comments[i]);
  }

  photos[photoIdx] = photo;
}

// Called when a new photo is viewed.  
// * Verifies new photo has Image object (creates it if it doesn't)
// * Shows new photo
// * Adjusts prefetched photos
//   * Check for null srcs in N photos after new photo and N photos before 
//     new photo--if there is one, load the photo
//   * Check all other photos for non-null srcs and, if found, set them to null
function updatePhotos(photoIdx, dontShow) {
  if (!dontShow) showPhoto(photoIdx);

  if (photoIdx == 0) {
    document.getElementById("prevAnchor").style.display="none";
  } else if (photoIdx == 1) {
    document.getElementById("prevAnchor").style.display="inline";
  } else if (photoIdx == (photos.length-2)) {
    document.getElementById("nextAnchor").style.display="inline";
  } else if (photoIdx == (photos.length-1)) {
    document.getElementById("nextAnchor").style.display="none";
  }

  for(var i = 0; i < photos.length; i++) {
    if (i == photoIdx) continue;
    if (i < photoIdx) {
      if (i < photoIdx - PREFETCH_WINDOW) {
        photos[i].img = null;
      } else {
        loadPhoto(i);
      }
    } else {
      if (i > photoIdx + PREFETCH_WINDOW) {
        photos[i].img = null;
      } else {
        loadPhoto(i);
      }
    }
  }
}

function debug(msg) {
  if (window.console) {
    window.console.log(msg);
  } else {
    setTimeout(function() { throw new Error("[debug] " + msg); }, 0);
  }
}

function onLoad() {
  if (currentPhoto != -1) updatePhotos(currentPhoto, 1);
}

var handlers = new Object();
var destinations = new Object();
var photos = null;
var currentPhoto = -1;
var selected = null;

document.onkeyup = handleKeyUp;
