/**
 * @author Scott A. Miller
 * Copyright 2007 The Tonic Group.
 * All rights reserved.
 */
function ImageViewer(imageUrls, imageCaptions) {
	this.imageUrls = imageUrls;
	this.imageCaptions = imageCaptions;
	this.imageIndex = -1;
}

ImageViewer.prototype.registerHandlers = function(captionId, imageId, forwardButtonId, backwardButtonId) {
	this.captionElement = document.getElementById(captionId);
	this.imageElement = document.getElementById(imageId);
	this.forwardButton = document.getElementById(forwardButtonId);
	this.backwardButton = document.getElementById(backwardButtonId); 
	
	this.updateButtons(false, false);
	
	var thisObj = this;
	
	this.forwardButton.onmouseover = function() {thisObj.updateButtons(true, false)};
	this.forwardButton.onmouseout = function() {thisObj.updateButtons(false, false)};
	this.forwardButton.onclick = function() {thisObj.moveForward()};
	
	this.backwardButton.onmouseover = function() {thisObj.updateButtons(false, true)};
	this.backwardButton.onmouseout = function() {thisObj.updateButtons(false, false)};
	this.backwardButton.onclick = function() {thisObj.moveBackward()};
	
  this.imageIndex = 0;
}

ImageViewer.prototype.updateProductSelection = function(selection) {
  var id = selection.options[selection.options.selectedIndex].value;
  location='product_detail.php?id=' + id;
}

ImageViewer.prototype.preLoadImages = function() {
	var count = imageUrls.length;
	for (var i = 0; i < count; i++) {
		var tempImage = new Image;
		tempImage.src = imageUrls[i];
	}
}

ImageViewer.prototype.updateButtons = function(mouse_in_forward, mouse_in_backward) {
  if (mouse_in_forward) {
    this.forwardButton.src = "images/forward_on.gif";
  } 
  else {
    this.forwardButton.src = "images/forward_off.gif";
  }
  if (mouse_in_backward) {
		this.backwardButton.src = "images/backward_on.gif";
  } 
  else {
    this.backwardButton.src = "images/backward_off.gif";
  }
}

ImageViewer.prototype.moveForward = function() {
  this.imageIndex++
  if (this.imageIndex >= this.imageUrls.length) {
    this.imageIndex = 0
  }
  this.captionElement.innerHTML = this.imageCaptions[this.imageIndex]
  this.imageElement.src = this.imageUrls[this.imageIndex]  		
}

ImageViewer.prototype.moveBackward = function() {
  this.imageIndex--
  if (this.imageIndex < 0) {
    this.imageIndex = this.imageUrls.length - 1
  }
  this.captionElement.innerHTML = this.imageCaptions[this.imageIndex]
  this.imageElement.src = this.imageUrls[this.imageIndex]  		
}


