/**
 * eSlides.js
 * Copyright (C) 2004 Edmond Lau <edmond@mit.edu>, <edmondlau@gmail.com>
 *
 * This eSlides.js javascript file was generated by the eSlides tool.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

images = new Array();
$IMAGE_ARRAY

var SLIDE_WIDTH = $SLIDE_WIDTH;
var TOTAL_IMAGES = $TOTAL_IMAGES;

var LEFT_SCROLL_HANDLE = null;
var RIGHT_SCROLL_HANDLE = null;

var SCROLL_INTERVAL = 25;

var LEFT_KEY = 63234;
var RIGHT_KEY = 63235;
var LEFT_KEY_MOZILLA = 37;
var RIGHT_KEY_MOZILLA = 39;

var SLOW_SCROLL_SPEED = 5;
var MEDIUM_SCROLL_SPEED = 10;
var FAST_SCROLL_SPEED = 20;

// state variables
var selectedSlide = 1;              // currently selected image
var curPosition = 0;                // CSS offset of slide
var scrollSpeed;                    // variable for timed events

// check if user has already selected a different slide before the page finished loading
function init() {
  if (selectedSlide == 1) {
    box(1);
  }
}

function select(target) {
  if (selectedSlide == target) return;

  // remove selection from current image
  unbox(selectedSlide);

  // set selected slide to target
  selectedSlide = target;

  // update links
  darkenLinks();

  var targetPosition = slideNumberToPosition(target);

  if (targetPosition > curPosition) { // need to scroll right
    if (LEFT_SCROLL_HANDLE != null) {
      clearInterval(LEFT_SCROLL_HANDLE);
      LEFT_SCROLL_HANDLE = null;
    }
    if (RIGHT_SCROLL_HANDLE == null) {
      scrollRight();
    }
  } else { // targetPosition < curPosition
    if (RIGHT_SCROLL_HANDLE != null) {
      clearInterval(RIGHT_SCROLL_HANDLE);
      RIGHT_SCROLL_HANDLE = null;
    }
    if (LEFT_SCROLL_HANDLE == null) {
      scrollLeft();
    }
  }

  // set the main focus image
  document.getElementById("focus").src = "images/" + images[target];

  // box the new image
  box(target);
}

function unbox(target) {
  // remove the selection box from the previous image
  var previousSlide = document.getElementById("slide_" + target);
  previousSlide.style.padding = "6px 6px 6px 6px";
  previousSlide.style.border = "0px";
}

function box(target) {
  // draw a selection box around the image selected
  var clickedSlide = document.getElementById("slide_" + target);
  var browserType = navigator.appName;
  if (browserType.search(/Microsoft Internet Explorer/) == -1) {
    // not IE
    clickedSlide.style.padding = "3px 3px 3px 3px";
  } else {
    // IE bug fix
    clickedSlide.style.padding = "3px 3px 0px 3px";
  }
  clickedSlide.style.border = "3px solid #3333CC";
}

// selected image is to the left
function scrollLeft() {
  // determine scroll speed
  scrollSpeed = SLOW_SCROLL_SPEED;
  LEFT_SCROLL_HANDLE = setInterval("_scrollLeft(scrollSpeed)", SCROLL_INTERVAL); 
}

function _scrollLeft(speed) {
  var cells = document.getElementById("cells");
  curPosition -= speed;
  cells.style.left = curPosition + "px";

  var selectedPosition = slideNumberToPosition(selectedSlide);
 
  if (selectedPosition == curPosition) {
    // reached the selectedPosition
    clearInterval(LEFT_SCROLL_HANDLE);
    LEFT_SCROLL_HANDLE = null;
  } else if (curPosition - speed < selectedPosition) { 
    // selectedPosition will overshoot curPosition in next call
    // need to slow it down
    clearInterval(LEFT_SCROLL_HANDLE);
    scrollSpeed = curPosition - selectedPosition;
    LEFT_SCROLL_HANDLE = setInterval("_scrollLeft(scrollSpeed)", SCROLL_INTERVAL);
  } else {
    // dynamically calibrate speed
    var speed = getScrollSpeed(curPosition - selectedPosition);
    if (scrollSpeed != speed) {
      clearInterval(LEFT_SCROLL_HANDLE);
      scrollSpeed = speed;
      LEFT_SCROLL_HANDLE = setInterval("_scrollLeft(scrollSpeed)", SCROLL_INTERVAL);
    }
  }
}

// selected image is to the right
function scrollRight() {
  // determine scroll speed
  scrollSpeed = SLOW_SCROLL_SPEED;
  RIGHT_SCROLL_HANDLE = setInterval("_scrollRight(scrollSpeed)", SCROLL_INTERVAL); 
}


function _scrollRight(speed) {
  var cells = document.getElementById("cells");
  curPosition += speed;
  cells.style.left = curPosition + "px";

  var selectedPosition = slideNumberToPosition(selectedSlide);
 
  if (selectedPosition == curPosition) {
    // reached the selectedPosition
    clearInterval(RIGHT_SCROLL_HANDLE);
    RIGHT_SCROLL_HANDLE = null;
  } else if (curPosition + speed > selectedPosition) { 
    // curPosition will overshoot selectedPosition in next call
    // need to slow it down
    clearInterval(RIGHT_SCROLL_HANDLE);
    scrollSpeed = selectedPosition - curPosition;
    RIGHT_SCROLL_HANDLE = setInterval("_scrollRight(scrollSpeed)", SCROLL_INTERVAL);
  } else {
    // dynamically calibrate speed
    var speed = getScrollSpeed(selectedPosition - curPosition);
    if (scrollSpeed != speed) {
      clearInterval(RIGHT_SCROLL_HANDLE);
      scrollSpeed = speed;
      RIGHT_SCROLL_HANDLE = setInterval("_scrollRight(scrollSpeed)", SCROLL_INTERVAL);
    }
  }
}

function shiftSelectionLeft() {
  if (selectedSlide > 1) {
    select(selectedSlide - 1);
  }
}

function shiftSelectionRight() {
  if (selectedSlide < TOTAL_IMAGES) {
    select(selectedSlide + 1);
  }
}

function shiftSelectionLeft5() {
  if (selectedSlide > 1) {
    select(max(selectedSlide - 5, 1));
  }
}

function shiftSelectionRight5() {
 if (selectedSlide < TOTAL_IMAGES) {
    select(min(selectedSlide + 5, TOTAL_IMAGES));
  }
}

// returns the value required by curPosition to reach target
function slideNumberToPosition(target) {
  // slide 1 => 0
  // slide 2 => -175
  // slide 3 => -350
  // etc.
  return (1 - target) * SLIDE_WIDTH;
}

function getScrollSpeed(distance) {
  if (distance <= 2 * SLIDE_WIDTH) {
    return SLOW_SCROLL_SPEED;
//  } else if (distance <= 3 * SLIDE_WIDTH) {
//    return MEDIUM_SCROLL_SPEED;
  } else {
    return FAST_SCROLL_SPEED;
  }
}

function parseKey(keyEvent) {
  var key = getKey(keyEvent);
  if ((key == LEFT_KEY || key == LEFT_KEY_MOZILLA) && 
      RIGHT_SCROLL_HANDLE == null) { // && LEFT_SCROLL_HANDLE == NULL) {
    shiftSelectionLeft();
  } else if ((key == RIGHT_KEY || key == RIGHT_KEY_MOZILLA) && 
             LEFT_SCROLL_HANDLE == null) {// && RIGHT_SCROLL_HANDLE == null) {
    shiftSelectionRight();
  }
}

// need to fix for windows firefox?
function getKey(keyEvent) {
  if (window.event) { // Internet Explorer & Safari
     return window.event.keyCode;
  } else if (keyEvent) {
     return keyEvent.keyCode;
  } else {
    return null;
  }
}

// prev and prev5 link are enabled if selectedSlide > 1
// next and next5 link is enabled if selectedSlide < TOTAL_IMAGES
function isEnabled(link) {
  return ((link == "prevlink" || link == "prev5link") && selectedSlide > 1) || 
  	 ((link == "nextlink" || link == "next5link") && selectedSlide < TOTAL_IMAGES);
}

// brightens the link if it is enabled; otherwise, leaves it black
// a link should have a light color when when the mouse hovers over it
function light(link) {
  if (isEnabled(link)) {
    document.getElementById(link).style.color = "#9999FF";
  } else {
    document.getElementById(link).style.color = "#000000";
  }
}

// darkens the link if it is enabled; otherwise, leaves it black
// a link should be darkened normally
function darken(link) {
  if (isEnabled(link)) {
    document.getElementById(link).style.color = "#3333CC";
  } else {
    document.getElementById(link).style.color = "#000000";
  }
}

function darkenLinks() {
  darken("prev5link");
  darken("prevlink");
  darken("nextlink");
  darken("next5link");
}

function max(a, b) {
  if (a > b) 
    return a;
  return b;
}

function min(a, b) {
  if (a < b)
    return a;
  return b;
}