var phototimeout_delay = 3000; // milliseconds
var currentphoto = 0;
var photoTimerId = 0;
var cachenos = -1;
var curWidth = 0;
var startDate = new Date();
var starttime = startDate.getTime();

var cache = new Array();

var photos =   ["images/Cornish Barn holiday accommodation - lounge.jpg",
		"images/Cornish Barn holiday accommodation - dining area.jpg",
		"images/Cornish Barn holiday accommodation - kitchen.jpg",
		"images/Cornish Barn holiday accommodation - main bedroom.jpg",
		"images/Cornish Barn holiday accommodation - middle bedroom.jpg",
		"images/Cornish Barn holiday accommodation - third bedroom.jpg",
		"images/Cornish Barn holiday accommodation - bathroom.jpg",
		"images/Cornish Barn holiday accommodation - secluded rear garden.jpg"];

var width = [608,608,608,608,608,345,345,608];

var titles =   ["1/8 The pleasant and spacious living area",
		"2/8 Dining area, plenty of room for a large family",
		"3/8 The fully equipped kitchen",
		"4/8 The main bedroom",
		"5/8 The second bedroom",
		"6/8 The third bedroom",
		"7/8 Bathroom, refurbished in 2009",
		"8/8 The private garden, perfect for bar-b-que's, and Boule"];



function cachephoto() {

   if (cachenos==0) { window.setTimeout('phototimeout()', phototimeout_delay);}

   cachenos = cachenos + 1;
   cache[cachenos] = new Image();
   if (cachenos < photos.length-1) {
     cache[cachenos].onload=function () { cachephoto(); }
   }
   cache[cachenos].src = photos[cachenos];
}



function nextphoto() {
 ++currentphoto;
 displayphoto();
}


function previousphoto() {
 --currentphoto;
 displayphoto();
}


function startstopphoto() {
 var text=document.getElementById('photosbutton').innerHTML
 document.getElementById('photosbutton').innerHTML = (text == "Stop") ? "Start" : "Stop";
 phototimeout(); //Call this because if stopped need to display next photo now!
}


function phototimeout() {
 if (document.getElementById('photosbutton').innerHTML == "Stop") {
  ++currentphoto;
  displayphoto();
 }
}


function resetphototimeout() {
 if (document.getElementById('photosbutton').innerHTML == "Stop") {
  window.clearTimeout(photoTimerId);
 }
}


function photoloaded() {
 if (document.getElementById('photosbutton').innerHTML == "Stop") {
          photoTimerId = window.setTimeout("phototimeout()", phototimeout_delay);
 }

  if (width[currentphoto] != curWidth) {
   curWidth = width[currentphoto];
   document.images.photoshow.width = curWidth;
  }
  document.getElementById('phototitle').innerHTML = titles[currentphoto];
}


function displayphoto() {

 resetphototimeout(); //incase here because user pushed prev or next 
 if (currentphoto > photos.length-1) {
  currentphoto = 0;
 } if (currentphoto < 0) {
  currentphoto = photos.length-1
 }

  if (  document.images.photoshow.onload == null) {
    document.images.photoshow.onload = function loaded() {photoloaded();};
  }

  //This bit probably not necessary but oh well!
  if (cache.length > currentphoto && cache[currentphoto].completed) {
    //Use image from cache if available
    document.images.photoshow.src = cache[currentphoto].src;
  } else {
    //Trigger 
    document.images.photoshow.src = photos[currentphoto];
  }
}
