// set the starting image.
var i = 0;			
var current_slide = 0;
// The array of div names which will hold the images.
//var image_slide = new Array('image0', 'image1', 'image2', 'image3');
var image_slide = new Array();
for(m=0;m<total_slides;m++){
   image_slide[m] = 'image'+m;
}

// The number of images in the array.
var NumOfImages = image_slide.length;

// The time to wait before moving to the next image. Set to 3 seconds by default.
var wait = 4000;

// The Fade Function
function SwapImage(x,y) {
	$(image_slide[x]).appear({ duration: 0.65 });
	$(image_slide[y]).fade({ duration: 0.15 });
}

// the onload event handler that starts the fading.
function StartSlideShow() {
	play = setInterval('Play()',wait);
   writeTitle(document.getElementById('image'+current_slide));
   document.getElementById('thumb'+current_slide).className = 'current';
}

function Pause(id) {
   var thisDiv = 'imagelink' + id;
   var href = document.getElementById(thisDiv).getAttribute('href');
   var thisHref = href.substr(11,4);
   if(thisHref == 'Paus'){
      document.getElementById(thisDiv).href = 'javascript:Play();';
   }else if(thisHref == 'Play'){
      document.getElementById(thisDiv).href = 'javascript:Pause(' + id + ');';   
   }
	clearInterval(play);
}

function Play() {
	var imageShow, imageHide;

	imageShow = i+1;
	imageHide = i;
   
	if (imageShow == NumOfImages) {
      imageShow = 0;
		SwapImage(imageShow,imageHide);	
		i = 0;					
	} else {
		SwapImage(imageShow,imageHide);			
		i++;
	}
   
   writeTitle(document.getElementById('image'+imageShow));
   document.getElementById('thumb'+current_slide).className = 'off';
   document.getElementById('thumb'+imageShow).className = 'current';
         
   current_slide = imageShow;
}

function seek(pos) {
   if(pos!=i){
      clearInterval(play);

      var imageShow = pos;
      var imageHide = i;
         
      if (imageShow == NumOfImages) {
         imageShow = 0;
         SwapImage(imageShow,imageHide);
         i = 0;					
      } else {
         SwapImage(imageShow,imageHide);
         i = pos;
      }
   
      writeTitle(document.getElementById('image'+imageShow));
      document.getElementById('thumb'+current_slide).className = 'off';
      document.getElementById('thumb'+pos).className = 'current';
      
      current_slide = pos;
   }
}

function writeTitle(_thumb){
   var titleVars = new Array();
   var theTitle = _thumb.getAttribute('title');
   titleVars = theTitle.split("|");
   var theseDivs = '<div class="p-title">' + titleVars[0] + '</div>';
   if(titleVars[1])
      theseDivs += '<div class="p-quote">"' + titleVars[1] + '"</div>';
   if(titleVars[2])
      theseDivs += '<div class="p-source">&mdash; ' + titleVars[2] + '</div>';

   document.getElementById('caption').innerHTML = theseDivs;
}
