var slideShowSpeed = 5000 // (milliseconds)
var Pic = new Array() 
var Title = new Array()
var Date = new Array()
var PicIndex = new Array()
var index = 0
var initindex = 0
var preThumb = 0
var cacheIndex = 1;
var playing = false
var caching = false
var timeId
var prevId
var nextId
var preId
var checkupId
var hash = ""
var CurrentPic = new Array()
var PreviousPic = new Array()
var NextPic = new Array()
var Thumbs = new Array()
var ThumbOK = new Array()
var nextOK = false;
var prevOK = false;
var thumbsOK = false;
var numPreThumbsOK = 0;
var p = 0;
var numThumbs = 3;
var firstVisibleThumb = 0;
var sliderIndex=0;
var last_ww = 0;
var last_wh = 0;
var verbose = false;
var minsize = 500;
var showingAll = false;

var exifDataScript

setrollover('/scripts/previous.off.gif', 'previous_slide');
setrollover('/scripts/next.off.gif', 'next_slide');
setrollover('/scripts/next.over.gif', 'next_thumb');
setrollover('/scripts/ff.over.gif', 'ff_thumb');
setrollover('/scripts/end.over.gif', 'end_thumb');
setrollover('/scripts/previous.off.gif', 'previous_thumb');
setrollover('/scripts/play.over.gif', 'start_stop');
setrollover('/scripts/info.over.gif', 'info');

function getWindowWidth(){
  var ww = 0;
  d = document;
   if ( typeof window.innerWidth != 'undefined' )
     ww = window.innerWidth;  // NN and Opera version
   else
   {
     if ( d.documentElement
       && typeof d.documentElement.clientWidth!='undefined'
       && d.documentElement.clientWidth != 0 )
       ww = d.documentElement.clientWidth;
     else
       if ( d.body
         && typeof d.body.clientWidth != 'undefined' )
         ww = d.body.clientWidth;
     else alert ("Can't identify window width - please tell me which browser you are using.")
   }
   if (document.getElementById('sidebar')) ww = ww - document.getElementById('sidebar').offsetWidth	
   return ww;
}


function getWindowHeight(){
  var wh = 0;
  d = document;
   if ( typeof window.innerHeight != 'undefined' )
     wh = window.innerHeight;  // NN and Opera version
   else
   {
     if ( d.documentElement
       && typeof d.documentElement.clientHeight!='undefined'
       && d.documentElement.clientHeight != 0 )
       wh = d.documentElement.clientHeight;
     else
       if ( d.body
         && typeof d.body.clientHeight != 'undefined' )
         wh = d.body.clientHeight;
     else alert ("Can't identify window width - please tell me which browser you are using.")
   }
   return wh;
}

function getWindowYOffset(){
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function getWindowXOffset(){
  var scrOfX = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfX;
}

function startStopShow(){
   if (playing) {
	 rollOverArr['start_stop'].outImg.src = '/scripts/play.gif';
	 rollOverArr['start_stop'].overImg.src = '/scripts/play.over.gif';
	 document.images.start_stop.src = '/scripts/play.gif';
	 playing = false;
	 clearTimeout(timeId);
	 NextPic['Image'].onload = nextReady
   } else {
	 document.images.start_stop.src = '/scripts/pause.gif';
	 showImage();
	 rollOverArr['start_stop'].outImg.src = '/scripts/pause.gif';
	 rollOverArr['start_stop'].overImg.src = '/scripts/pause.over.gif';
	 document.images.start_stop.src = '/scripts/pause.gif';
	 playing = true;
     timeId = setTimeout('nextSlideShow()', slideShowSpeed);
	}
}

function nextSlideShow(){
   if (nextOK) {
		nextSlide();
		timeId = setTimeout('nextSlideShow()', slideShowSpeed)
		}
	else {
		NextPic['Image'].onload = nextSlideshowReady
		}
}

function nextSlide(){
   if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>nextSlide()'
   if (nextOK) 
	{
    	if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>nextSlide(): NextOK'

	index = index + 1
	if (index > (p-1)) index=0
	PreviousPic = new clonePic(CurrentPic)
	CurrentPic = new clonePic(NextPic)
	prevReady()
    	if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>nextSlide(): ImagesCloned'

	nextId = setTimeout('preloadNextImage()', 1)
    	if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>nextSlide(): TimerSet'

	showImage();
   	}
}

function previousSlide(){
   if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>previousSlide()';
   if (prevOK)
	{
	if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>previousSlide(): prevOK';

	index = index - 1
	if (index < 0) index=p-1
	NextPic = new clonePic(CurrentPic)
	CurrentPic = new clonePic(PreviousPic)
	nextReady()
    	if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>previousSlide(): ImagesCloned'

	prevId = setTimeout('preloadPreviousImage()', 1)
	if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>previousSlide(): TimerSet';

	showImage();
	}
}
function startThumb()
{
	//alert('previousThumb')
	if (firstVisibleThumb > 0)
		{
		firstVisibleThumb = 0

		// Set the slider to the right place
		displayObj.value = 0
		pos = (displayObj.value - sliderObj.fromVal)/(sliderObj.scale)
		carpeLeft(sliderObj.id, pos)

		// Update the thumbs nav icons
		setThumbIcons()

		// Update the displayed thumbnails
		showThumbs()
		}
}

function rwThumb()
{
	//alert('previousThumb')
	if (firstVisibleThumb > 0)
		{
		firstVisibleThumb = firstVisibleThumb - numThumbs
		if (firstVisibleThumb < 0) firstVisibleThumb = 0

		// Set the slider to the right place
		sliderIndex = firstVisibleThumb + Math.floor(numThumbs/2) - 1
		if (sliderIndex > p) sliderIndex = p
		if (sliderIndex < 0) sliderIndex = 0
		displayObj.value = sliderIndex
		pos = (displayObj.value - sliderObj.fromVal)/(sliderObj.scale)
		carpeLeft(sliderObj.id, pos)

		// Update the thumbs nav icons
		setThumbIcons()

		// Update the displayed thumbnails
		showThumbs()
		}
}

function previousThumb()
{
	//alert('previousThumb')
	if (firstVisibleThumb > 0)
		{
		firstVisibleThumb--

		// Set the slider to the right place
		sliderIndex = firstVisibleThumb + Math.floor(numThumbs/2) - 1
		if (sliderIndex > p) sliderIndex = p
		if (sliderIndex < 0) sliderIndex = 0
		displayObj.value = sliderIndex
		pos = (displayObj.value - sliderObj.fromVal)/(sliderObj.scale)
		carpeLeft(sliderObj.id, pos)

		// Update the thumbs nav icons
		setThumbIcons()

		// Update the displayed thumbnails
		showThumbs()
		}
}


function nextThumb()
{
	//alert('nextThumb')
	if (firstVisibleThumb < p-numThumbs)
		{
		firstVisibleThumb++

		// Set the slider to the right place
		sliderIndex = firstVisibleThumb + Math.floor(numThumbs/2) - 1
		if (sliderIndex > p) sliderIndex = p
		if (sliderIndex < 0) sliderIndex = 0
		displayObj.value = sliderIndex
		pos = (displayObj.value - sliderObj.fromVal)/(sliderObj.scale)
		carpeLeft(sliderObj.id, pos)

		// Update the thumbs nav icons
		setThumbIcons()

		// Update the displayed thumbnails
		showThumbs()
		}
}

function ffThumb()
{
	//alert('nextThumb')
	if (firstVisibleThumb < p-numThumbs)
		{
		firstVisibleThumb = firstVisibleThumb+numThumbs
		if (firstVisibleThumb+numThumbs > p) firstVisibleThumb = p-numThumbs

		// Set the slider to the right place
		sliderIndex = firstVisibleThumb + Math.floor(numThumbs/2) - 1
		if (sliderIndex > p) sliderIndex = p
		if (sliderIndex < 0) sliderIndex = 0
		displayObj.value = sliderIndex
		pos = (displayObj.value - sliderObj.fromVal)/(sliderObj.scale)
		carpeLeft(sliderObj.id, pos)

		// Update the thumbs nav icons
		setThumbIcons()

		// Update the displayed thumbnails
		showThumbs()
		}
}

function endThumb()
{
	//alert('nextThumb')
	if (firstVisibleThumb < p-numThumbs)
		{
		firstVisibleThumb = p-numThumbs
		// Set the slider to the right place
		displayObj.value = p
		pos = (displayObj.value - sliderObj.fromVal)/(sliderObj.scale)
		carpeLeft(sliderObj.id, pos)
		
		// Update the icons		
		setThumbIcons()

		// Show the thumbnails
		showThumbs()
		}
}

function thumbSlider(sliderValue)
{
	if (sliderIndex != sliderValue)
		{
		sliderIndex = sliderValue
		firstVisibleThumb = sliderIndex - Math.floor(numThumbs/2) - 1
		if (firstVisibleThumb > p-numThumbs) firstVisibleThumb = p-numThumbs
		if (firstVisibleThumb < 0) firstVisibleThumb = 0
		setThumbIcons()
		showThumbs()
		}
}

function setThumbIcons()
{
	// If there's a previous one - allow it
	if (firstVisibleThumb > 0)
		{
		if (document.getElementById('previous_thumb'))
			{
			document.images.previous_thumb.src="/scripts/previous.gif"
			setrollover('/scripts/previous.over.gif', 'previous_thumb');
			}

		if (document.getElementById('rw_thumb'))
			{	
			document.images.rw_thumb.src="/scripts/rw.gif"
			setrollover('/scripts/rw.over.gif', 'rw_thumb');
			}

		if (document.getElementById('start_thumb'))
			{		
			document.images.start_thumb.src="/scripts/start.gif"
			setrollover('/scripts/start.over.gif', 'start_thumb');
			}
		}
	else 
		{
		if (document.getElementById('previous_thumb'))
			{	
			document.images.previous_thumb.src="/scripts/previous.off.gif"
			setrollover('/scripts/previous.off.gif', 'previous_thumb');
			}

		if (document.getElementById('rw_thumb'))
			{	
			document.images.rw_thumb.src="/scripts/rw.off.gif"
			setrollover('/scripts/rw.off.gif', 'rw_thumb');
			}

		if (document.getElementById('start_thumb'))
			{	
			document.images.start_thumb.src="/scripts/start.off.gif"
			setrollover('/scripts/start.off.gif', 'start_thumb');
			}
		}	
		
	// If there's a next one - allow it
	if (firstVisibleThumb < p-numThumbs)
		{
		if (document.getElementById('next_thumb'))
			{	
			document.images.next_thumb.src="/scripts/next.gif"
			setrollover('/scripts/next.over.gif', 'next_thumb');
			}

		if (document.getElementById('ff_thumb'))
			{	
			document.images.ff_thumb.src="/scripts/ff.gif"
			setrollover('/scripts/ff.over.gif', 'ff_thumb');
			}

		if (document.getElementById('end_thumb'))
			{	
			document.images.end_thumb.src="/scripts/end.gif"
			setrollover('/scripts/end.over.gif', 'end_thumb');
			}
		}
	else
		{
		if (document.getElementById('next_thumb'))
			{
			document.images.next_thumb.src="/scripts/next.off.gif"
			setrollover('/scripts/next.off.gif', 'next_thumb');
			}

		if (document.getElementById('ff_thumb'))
			{	
			document.images.ff_thumb.src="/scripts/ff.off.gif"
			setrollover('/scripts/ff.off.gif', 'ff_thumb');
			}

		if (document.getElementById('end_thumb'))
			{	
			document.images.end_thumb.src="/scripts/end.off.gif"
			setrollover('/scripts/end.off.gif', 'end_thumb');
			}
		}
}

function thumbExpand(thumbNum)
{
	expandPhotoWindow()
	gotoSlide(thumbNum)
}

function thumbSlide(thumbNum)
{
	gotoSlide(thumbNum+firstVisibleThumb)
}

function gotoSlide(slideNum)
{
	//alert(slideNum+' - '+Title[slideNum])
	if (slideNum == index)
		{
		showImage();
		}
	else if (slideNum == index+1)
		{
		nextSlide();
		}
	else if (slideNum == index-1)
		{
		previousSlide();
		}
	else if (slideNum >= 0 && slideNum < p)
		{
		index = slideNum
		
		// Temporarily show a "loading" icon
		document.images.SlideShow.width = 24
		document.images.SlideShow.height = 24
		document.images.SlideShow.alt = Title[index] + ' - ' + Date[index];
		document.images.SlideShow.title = Title[index] + ' - ' + Date[index];
		document.images.SlideShow.src = "/scripts/loading.gif";
		document.getElementById('title').innerHTML = Title[index];
		
		// Now load the image
		preloadFirstImage()
		}
	else 
		{
		showImage()
		}
}

function nextSlideshowReady() {
	nextReady();
	nextSlideShow();
}

function nextReady() {
   if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>nextReady()';
   nextOK = true;
   document.images.next_slide.src="/scripts/next.gif"
   setrollover('/scripts/next.over.gif', 'next_slide');
   showThumbs()
   if (prevOK && thumbsOK && !caching) browserCache()
}

function prevReady() {
   if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>prevReady()';
   prevOK = true;
   document.images.previous_slide.src="/scripts/previous.gif"
   setrollover('/scripts/previous.over.gif', 'previous_slide');
   showThumbs()
   if (nextOK && thumbsOK && !caching) browserCache()
}

function showThumbs()
{
	if (!document.getElementById('thumbs_table')) return
	for (var i=0; i<numThumbs; i++)
		{
		var thumb = i+firstVisibleThumb
		document.getElementById('thumb_img'+i).alt = "Loading...";
		document.getElementById('thumb_img'+i).title = "Loading...";
		document.getElementById('thumb_img'+i).src = "/scripts/loading.gif";
		document.getElementById('thumb_caption'+i).innerHTML = "Loading...";
		if ((typeof( Thumbs[thumb] ) != "undefined") && ThumbOK[thumb])
			{
			if (verbose && i==2) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>showThumb(): '+thumb+'\nsrc='+Thumbs[thumb].src;
			document.getElementById('thumb_img'+i).alt = Title[thumb] + ' - ' + Date[thumb];
			document.getElementById('thumb_img'+i).title = Title[thumb] + ' - ' + Date[thumb];
			if (Thumbs[thumb].src) document.getElementById('thumb_img'+i).src = Thumbs[thumb].src;
			else document.getElementById('thumb_img'+i).src = "/scripts/exif/get_exif_thumb.php?filename=../.."+Pic[thumb];
			document.getElementById('thumb_caption'+i).innerHTML = Title[thumb];
			}
		else if (verbose && i==2)
			{
			document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>showThumb(): Not available '+thumb+' '
			if (typeof( Thumbs[thumb] ) != "undefined") document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'Defined=true src='+Thumbs[thumb].src
			else document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'Defined=false'
			document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+' OK='+ThumbOK[thumb]
			}		
		}
}

function showImage() {
   if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>showImage()'
   // Turn off the showing of EXIF data by pretending that it's on and then toggling it
   showing = true
   toggleInfo()
   
   // Calculate the photo dimensions to scale it by
   var origw = CurrentPic['Image'].width 
   var origh = CurrentPic['Image'].height
   //var windw=document.getElementById('gallery').offsetWidth
   var windw = getWindowWidth()
   var windh = getWindowHeight()
   if (document.getElementById('thumbs_table') && (typeof document.getElementById('expandphoto') == 'undefined')) windh = windh - document.getElementById('thumbs_table').offsetHeight
   var otoww = origw / windw
   var otowh = origh / windh
   var scale = 1
   // If this is a regular page with 3 thumbs and a slide:
   if (typeof document.getElementById('expandphoto') == 'undefined')   {
	if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>showImage(): Regular Page with slider'
   		if ((otoww > 0.7) || (otowh > 0.8)) {
	  		if (otoww < otowh) scale = 0.8 / otowh;
	  		else scale = 0.7 / otoww;
      	}
    } else {
    // Otherwise if this a big page with all thumbs and an expanding black box:
   	if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>showImage(): Table page with expander'
    	windh = windh - 85; // for the bottom bar
    	windw = windw - 120; // for the close button (x2 for neatness of centering)
    	otoww = origw / windw;
    	otowh = origh / windh;
    	if ((otoww > 0.98) || (otowh > 0.98)) {
	  		if (otoww < otowh) scale = 0.98 / otowh;
	  		else scale = 0.98 / otoww;
      	}
    } 
    
    
   // Make sure that the largest dimension is at least a minimum size (~500px) or otherwise it's just too small to be useful
   if (origw > origh)
	{
	if ((origw * scale) < minsize) scale = minsize / origw
	}
   else
	{
	if ((origh * scale) < minsize) scale = minsize / origh
	}
   if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>showImage(): scale='+scale	
   //alert('Image: ' + index + ' Orig: ' + origw + 'x' + origh +' Wind: ' + windw + 'x' + windh + ' Scale: ' + scale)
   document.images.SlideShow.width = origw * scale
   document.images.SlideShow.height = origh * scale 
   document.images.SlideShow.alt = Title[index] + ' - ' + Date[index];
   document.images.SlideShow.title = Title[index] + ' - ' + Date[index];
   document.images.SlideShow.src = CurrentPic['Image'].src
   document.getElementById('title').innerHTML = Title[index];
   
   // If we're using the version 2 gallery, make sure the slide is in view
   if (typeof document.getElementById('expandphoto') == 'undefined')   { 
		var galAnchor = getAnchorPosition('gallery')
		scrollTo(galAnchor.x, galAnchor.y)
   }
   window.location.hash = "photo="+index
   
   // Setup checkup variables
   hash = window.location.hash
   last_ww = getWindowWidth()
   last_wh = getWindowHeight()
}

// getAnchorPosition(anchorname) from http://www.mattkruse.com/javascript/anchorposition/
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}
	
// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}	
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}


function preloadFirstImage(){	
	if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>preloadFirstImage()'
	CurrentPic['Image'] = new Image()
	CurrentPic['Image'].onload = showImage
	CurrentPic['Image'].src = Pic[index]
	CurrentPic['ID'] = PicIndex[index]

	// Now load the additional EXIF data
	exifDataScript = document.createElement('script')
	exifDataScript.setAttribute('src','/photobase/scripts/exifBoxInfoLoader.php?id='+PicIndex[index]+'&verbose='+verbose)
    exifDataScript.setAttribute('type','text/javascript')
    document.getElementsByTagName('head')[0].appendChild(exifDataScript)

	// Now trigger the loading of the next & previous images ready for showing
	nextId = setTimeout('preloadNextImage()', 10)
	prevId = setTimeout('preloadPreviousImage()', 20)	
}

function preloadNextImage(){	
	// Make sure we can't move the next photo till it's loaded
	if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>preloadNextImage(): In'
	nextOK=false;
	document.images.next_slide.src="/scripts/next.off.gif"
	setrollover('/scripts/next.off.gif', 'next_slide');
	
	
	// Load the next photo
	NextPic['Image'] = new Image()
	NextPic['Image'].onload = nextReady
	NextPic['Image'].src = Pic[(index+1)%p]
	NextPic['ID'] = PicIndex[(index+1)%p]

	// Now load the additional EXIF data
	exifDataScript = document.createElement('script')
	exifDataScript.setAttribute('src','/photobase/scripts/exifBoxInfoLoader.php?id='+PicIndex[(index+1)%p]+'&verbose='+verbose+'&next')
    	exifDataScript.setAttribute('type','text/javascript')
    	document.getElementsByTagName('head')[0].appendChild(exifDataScript)
	if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>preloadNextImage(): Out'
}

function preloadPreviousImage(){
	// Make sure we can't move the previous photo till it's loaded
	prevOK=false;
	document.images.previous_slide.src="/scripts/previous.off.gif"	
	setrollover('/scripts/previous.off.gif', 'previous_slide');

	// Load the previous photo
	var prevIndex = index - 1
	if (prevIndex < 0) prevIndex = p - 1
	PreviousPic['Image'] = new Image()
	PreviousPic['Image'].onload = prevReady
	PreviousPic['Image'].src = Pic[prevIndex]
	PreviousPic['ID'] = PicIndex[prevIndex]

	// Now load the additional EXIF data
	exifDataScript = document.createElement('script')
	exifDataScript.setAttribute('src','/photobase/scripts/exifBoxInfoLoader.php?id='+PicIndex[prevIndex]+'&verbose='+verbose+'&previous')
    	exifDataScript.setAttribute('type','text/javascript')
    	document.getElementsByTagName('head')[0].appendChild(exifDataScript)
}

function browserCache()
{
	// Get the browser to load the images one at a time, but don't necessarily keep them in memory
	// Basically let the browser's image caching handle this as it knows best.
	cacheIndex++
	caching = true;
	if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>browserCache(): '+cacheIndex
	if (cacheIndex < p)
		{
		//alert('Caching '+cacheIndex)
		cacheImage = new Image()
		cacheImage.onload = browserCache
		cacheImage.src = Pic[cacheIndex]
		}
}

function preloadThumbOK(thumb)
{
	if (verbose && (thumb)==2) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>preloadThumbOK(): '+thumb+ ' src='+Thumbs[thumb].src
	ThumbOK[thumb] = true
	if ((thumb >= firstVisibleThumb) && (thumb < firstVisibleThumb+numThumbs)) showThumbs()
	numPreThumbsOK++
	
	if (numPreThumbsOK == p)
		{
		if (verbose) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>preloadThumbOK(): Start Caching';
		thumbsOK = true		
		if (nextOK && prevOK && !caching) browserCache()
		}
}

function preloadThumbs()
{
	if (preThumb < p)
		{
		if (verbose && preThumb==2) document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML+'<br>preloadThumbs(): '+preThumb+' - '+Pic[(preThumb+initindex)%p];
		Thumbs[(preThumb+initindex)%p] = new Image()
		ThumbOK[(preThumb+initindex)%p] = false
		Thumbs[(preThumb+initindex)%p].onLoad = preloadThumbOK((preThumb+initindex)%p);
		Thumbs[(preThumb+initindex)%p].src = "/scripts/exif/get_exif_thumb.php?filename=../.."+Pic[(preThumb+initindex)%p]
				
		preThumb = preThumb + 1
		preID = setTimeout('preloadThumbs()', 10)
		}
}

function setupThumbs()
{	
	// First - if we've not got a thumbnails table then don't bother loading them
	if (!document.getElementById('thumbs_table')) 
		{
		thumbsOK = true;
		return
		}
	
	// Setup some default values for the slider object
	displayObj = new invalidobject(0,0);
	sliderSetup('thumb_slider', 'horizontal', 200, 1, p, p, 0, '');	

	// Now first the Thumbs
	preloadThumbs()

	// Setup the thumbs icons
	setThumbIcons()
		
	// Here we might look if we can show more thumbs on the page.
}

function init(){
	hash = window.location.hash
	if (hash.indexOf('#photo=') == 0) {
		// We've been passed in a specific photo to start at.
		initindex = parseInt(hash.substr(7))
		if (document.getElementById('thumbs_table')) thumbSlider(initindex)
	}
	index = initindex
	if (!showingAll) firstVisibleThumb = initindex;
	else initindex = 0;

	setupThumbs()
	preloadFirstImage()
	showImage()

	document.onkeyup = keyPressHandler

	checkupId = setTimeout('checkup()', 1000)
}

function checkup() {
	if (hash != window.location.hash)
		{
		// The hash has changed
		hash = window.location.hash
		if (hash.indexOf('#photo=') == 0) 
			{
			// Hash has changed to a new photo index
			gotoSlide(parseInt(hash.substr(7)))
			}
		else
			{
			// Hash is not a valid hash.
			hash = "#photo="+index
			window.location.hash = hash
			}
		showThumbs()
		}
	else if (last_ww != getWindowWidth() || last_wh != getWindowHeight())
		{
		// Window has changed size
		if (typeof document.getElementById('expandphoto') != 'undefined') resizeExpanderBoxes();
		showImage()
		showThumbs()
		}
				
	checkupId = setTimeout('checkup()', 1000)
}

function toggleSlideshowInfo()
  {
  if (showing == false)
	{
	// Setup the data we already know about the photo
	document.getElementById('exifIndex').innerHTML = "<a href=/photobase/PhotoSearch.php?photo_id="+PicIndex[index]+">"+PicIndex[index]
	document.getElementById('exifTitle').innerHTML = Title[index]
	document.getElementById('exifDate').innerHTML = Date[index]
	
	// These items will have been to be loaded separately, so fill them in from the CurrentPic array
	document.getElementById('exifDate').innerHTML =  CurrentPic['Date'] 
	document.getElementById('exifLocation').innerHTML =  CurrentPic['Location'] 
	document.getElementById('exifOwner').innerHTML =  CurrentPic['Owner']
	document.getElementById('exifCamera').innerHTML =  CurrentPic['Camera']
	document.getElementById('exifShutterSpeed').innerHTML =  CurrentPic['ShutterSpeed'] 
	document.getElementById('exifFnum').innerHTML =  CurrentPic['Fnum']
	document.getElementById('exifFlength').innerHTML =  CurrentPic['Flength'] 
	document.getElementById('exifLightsource').innerHTML =  CurrentPic['Lightsource']
	document.getElementById('exifKeywordList').innerHTML =  CurrentPic['KeywordList']
	}
	
  toggleInfo();
  }	

function clonePic(source) {
    this['Image'] = source['Image']
	this['ID'] = source['ID']	
	this['Date'] = source['Date']
	this['Location'] = source['Location'] 
	this['Owner'] = source['Owner']
	this['Camera'] = source['Camera']
	this['ShutterSpeed'] = source['ShutterSpeed'] 
	this['Fnum'] = source['Fnum']
	this['Flength'] = source['Flength'] 
	this['Lightsource'] = source['Lightsource']
	this['KeywordList'] = source['KeywordList']
}

function keyPressHandler(event)
{
  if (!event) event=window.event;

  var code;
  if ((event.charCode) && (event.keyCode==0)) code = event.charCode
  else code = event.keyCode;

  switch (code)
	{
  case 27:
    // Escape key - close the black expander box if it's a) present b) open
    if (typeof document.getElementById('expandphoto') != 'undefined') 
	{
	if (document.getElementById('expanderphoto').style.visibility == "visible") closePhotoWindow();
	}	
    break;
  case 37:
    // Key left - move to the previous photo
    if (typeof document.getElementById('expandphoto') != 'undefined') 
	{
	if (document.getElementById('expanderphoto').style.visibility == "visible") previousSlide();
	}
    else previousSlide();
    break;
  case 39:
    // Key right - move to the next photo
    if (typeof document.getElementById('expandphoto') != 'undefined') 
	{
	if (document.getElementById('expanderphoto').style.visibility == "visible") nextSlide();
	}
    else nextSlide();
    break;
	}

}

