var FADE_DURATION_MS = 800;
var FADE_STEP_MS = 80;
var DAYS_OF_WEEK = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
		    'Thursday', 'Friday', 'Saturday' ];
var MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug',
	      'Sept', 'Oct', 'Nov', 'Dec' ];

function Gallery(id1, id2, idPrev, idNext, idCap, photos) {
  this.backBuffer = document.getElementById(id1);
  this.frontBuffer = document.getElementById(id2);
  var prev = document.getElementById(idPrev);
  var next = document.getElementById(idNext);
  this.prev = prev;
  this.next = next;
  this.caption = document.getElementById(idCap);
  this.photos = photos;
  this.curIdx = 0;
  this.curOpacity = 0;
  this.prefetchNext = new Image();
  this.prefetchPrev = new Image();

  // Set up the buttons
  var g = this;
  setOpacity(prev, 0.75);
  prev.onmouseover = function() {
    setOpacity(prev, 1);
    prev.style.cursor = 'pointer';
  }
  prev.onmouseout = function() {
    setOpacity(prev, 0.75);
    prev.style.cursor = 'auto';
  }
  prev.onclick = function() {
    g.loadPhoto(g.curIdx - 1);
  }

  setOpacity(next, 0.75);
  next.onmouseover = function() {
    setOpacity(next, 1);
    next.style.cursor = 'pointer';
  }
  next.onmouseout = function() {
    setOpacity(next, 0.75);
    next.style.cursor = 'auto';
  }
  next.onclick = function() {
    g.loadPhoto(g.curIdx + 1);
  }
}

Gallery.prototype.numPhotos = function() {
  return this.photos.length;
}

Gallery.prototype.getCurId = function() {
  return this.photos[this.curIdx].id;
}

Gallery.prototype.loadPhotoById = function(id) {
  for (var i = 0; i < this.photos.length; i++) {
    if (this.photos[i].id == id) {
      this.loadPhoto(i);
      return;
    }
  }
}

Gallery.prototype.loadPhoto = function(idx) {
  if (idx < 0) idx = 0;
  if (idx >= this.photos.length) idx = this.photos.length - 1;

  // Clear any pending fades.
  if (this.fadeTimeout) {
    window.clearTimeout(this.fadeTimeout);
    this.fadeTimeout = undefined;
    this.backBuffer.src = this.frontBuffer.src;
  }

  this.curIdx = idx;
  this.populateCaption();

  // Load the new image into the front buffer (async to avoid flashing)
  setOpacity(this.frontBuffer, 0);
  this.curOpacity = 0;
  var me = this;
  setTimeout(function() { me.frontBuffer.src = getUrl(me.photos[idx].id); }, 0);

  // Fade in the image in the front buffer.
  this.fadeTimeout = setTimeout(function() { fadeStep(me) }, FADE_STEP_MS);

  if (idx < this.photos.length - 1) {
    this.next.style.visibility = 'visible';
    this.prefetchNext.src = getUrl(this.photos[idx + 1].id);
  } else {
    this.next.style.visibility = 'hidden';
  }
  if (idx > 0) {
    this.prev.style.visibility = 'visible';
    this.prefetchPrev.src = getUrl(this.photos[idx - 1].id);
  } else {
    this.prev.style.visibility = 'hidden';
  }
}

Gallery.prototype.populateCaption = function() {
  var photo = this.photos[this.curIdx];
  var date = new Date();
  date.setTime(photo.id * 1000);
  var ampm = 'am';
  var hour = date.getUTCHours();
  if (hour >= 12) {
    ampm = 'pm';
    hour -= 12;
  }
  if (hour == 0) {
    hour = 12;
  }
  var minute = date.getUTCMinutes();
  if (minute < 10) {
    minute = '0' + minute;
  }
  var dateStr = DAYS_OF_WEEK[date.getDay()] + '<br/>' + MONTHS[date.getMonth()] +
      ' ' + date.getDate() + ', ' + date.getFullYear() + '<br/>' +
      hour + ':' + minute + ' ' + ampm;

  var caption = '';
  caption += '<div class="date">' + dateStr + '</div>';
  if (photo.title) {
    caption += '<b>' + photo.title + '</b><br/>';
  }
  caption += photo.text;
  this.caption.innerHTML = caption;
}

function fadeStep(g) {
  g.curOpacity = g.curOpacity + (FADE_STEP_MS / FADE_DURATION_MS);
  if (g.curOpacity >= 1) {
    setOpacity(g.frontBuffer, 1);
    g.curOpacity = 1;
    g.backBuffer.src = g.frontBuffer.src;
    g.fadeTimeout = undefined;
  } else {
    setOpacity(g.frontBuffer, g.curOpacity);
    g.fadeTimeout = setTimeout(function() { fadeStep(g) }, FADE_STEP_MS);
  }  
}

function setOpacity(elem, opacity) {
  elem.style.opacity = opacity;
  elem.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';
}

function getUrl(id) {
  return 'p/' + id + '.jpg';
}

function isOldIE() {
  if (navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null) {
      return parseFloat(RegExp.$1) < 7;
    }
  }
  return false;
}
