function MyImage(src,linksrc,name,index) {
  this.img = new Image();
  this.src = src;
  this.linksrc = linksrc;
  this.name = name;
  this.index = index;

  this.img.name = name;

  this.loaded = false;

  if (this.index == theImageNumber) {
    // prefetch first image
    this.img.src = this.src;
    this.loaded = true;
  }

  this.getImage = function() {
    if (!this.loaded || this.img.src == "" || this.img.src == null) {
      this.img.src = this.src;
      this.loaded = true;
    }
    return this.img;
  }
}

function update() {
  var cell = document.getElementById("headerString");
  cell.innerHTML = images[theImageNumber].name;

  var action = document.getElementById("imageAction");
  action.href = images[theImageNumber].linksrc;

  document.title = images[theImageNumber].name;

  var nextButton = document.getElementById("nextButton");
  var previousButton = document.getElementById("previousButton");

  nextButton.disabled = false;
  previousButton.disabled = false;
  if (theImageNumber == 0) {
    previousButton.disabled = true;
  } else if (theImageNumber == images.length-1) {
    nextButton.disabled = true;
  }

}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")+1);
    strQueryString = "&" + strQueryString;
    if ( strQueryString.indexOf("&") > -1 ){
      var aQueryString = strQueryString.split("&");
      for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
        if (
          aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
          var aParam = aQueryString[iParam].split("=");
          strReturn = aParam[1];
          break;
        }
      }
    }
  }
  return strReturn;
}

function loadImage(direction) {
  // direction: 0 = forward, 1 = backward, 2 = both
  var image = images[theImageNumber].getImage();
  document.images.SLIDESIMG.src = image.src;
  document.images.onLoad = onImageLoad(image, direction);
}

function onImageLoad(image, direction) {
  if (image.height > image.width) {
    var tmp = image.height;
    image.height = 480;
    // scale width
    image.width = (image.width * image.height) / tmp;
  } else {
    image.width = 640;
    image.height = 480;
  }
  document.images.SLIDESIMG.width = image.width;
  document.images.SLIDESIMG.height = image.height;
  update();

  if (direction == 0) {
    prefetchAhead();
  } else if (direction == 1) {
    prefetchBehind();
  } else {
    prefetch();
  }

}

function prefetch() {

  //alert("prefetch");

  var PREFETCH_AMOUNT = 10;

  var fetchedBehind = 0;
  var fetchedAhead = 0;

  // fetch behind
    var timage;
    for (i=0;i<PREFETCH_AMOUNT;i++) {
	var x = theImageNumber-(i+1);
      if (x >= 0 && x < images.length-1 && !images[x].loaded) {
        timage = images[x].getImage();
        fetchedBehind++;
      } else if (x >= 0 && x < images.length-1 && images[x].loaded) {
        PREFETCH_AMOUNT++;
      }
	x = parseInt(theImageNumber)+parseInt(i)+1;
      if (x >= 0 && x < images.length-1 && !images[x].loaded) {
        timage = images[x].getImage();
        fetchedAhead++;
      } else if (x >= 0 && x < images.length-1 && images[x].loaded) {
        PREFETCH_AMOUNT++;
      }
    }

   //alert("fetchedBehind = " + fetchedBehind);
   //alert("fetchedAhead = " + fetchedAhead);
  
}

function prefetchAhead() {

  //alert("prefetchAhead");

  var PREFETCH_AMOUNT = 2;

  var fetchedAhead = 0;

    var timage;
    for (i=0;i<PREFETCH_AMOUNT;i++) {
	var x = parseInt(theImageNumber)+parseInt(i)+1;
      if (x >= 0 && x < images.length-1 && !images[x].loaded) {
        timage = images[x].getImage();
        fetchedAhead++;
      } else if (x >= 0 && x < images.length-1 && images[x].loaded) {
        PREFETCH_AMOUNT++;
      }
    }

   //alert("fetchedAhead = " + fetchedAhead);  
}

function prefetchBehind() {
  //alert("prefetchBehind");

  var PREFETCH_AMOUNT = 2;

  var fetchedBehind = 0;

  // fetch behind
    var timage;
    for (i=0;i<PREFETCH_AMOUNT;i++) {
	var x = theImageNumber-parseInt(i)+1;
      if (x >= 0 && x < images.length-1 && !images[x].loaded) {
        timage = images[x].getImage();
        fetchedBehind++;
      } else if (x >= 0 && x < images.length-1 && images[x].loaded) {
        PREFETCH_AMOUNT++;
      }
    }

   //alert("fetchedBehind = " + fetchedBehind);
  
}

function next() {
  if (theImageNumber < images.length-1) {
    theImageNumber++;
  }
  loadImage(0);
}

function previous() {
  if (theImageNumber > 0) {
    theImageNumber--;
  }
  loadImage(1);
}


