var current_pos = 0;
var filmstrip_size = 0; // how many thumbnails showing
var images_size = images.length; // how many images there are



function setDescription(pos)
{
  /*  Get the Description of the image selected  */ 
	var lgimage_desc = document.getElementById("lgimage_desc");
	
	var string = descriptions[pos].split("|");
	var text = "<strong>"+ string[0] +"</strong><br />\n" + string[1];
	
	lgimage_desc.innerHTML = text;
}
function setFilmstripSize (size) {filmstrip_size = size;}

function initiate_gallery( fs_size )
{
  setFilmstripSize(fs_size);
	
	/* Get the first 5 images from the images Array */
	for (var x=0; x<filmstrip_size; x++)
	{
		// get each thumbnail
		var tempimage = document.getElementById("image" + (x+1));
    tempimage.src= "photos/photoAlbum/thumbnails/" + images[x];
    tempimage.alt = alts[x];

		// set large image to the 3rd item.
		if ((filmstrip_size == 5 && x == 2) || (filmstrip_size == 3 && x == 1) || (filmstrip_size <= 2 && x == 0))	
		  { set_large_image(x); setDescription(x); }
  }
	//getImageArray();
}

function centerImage(clickedImagePos)
{
  /* center the clicked image */
	

	if (filmstrip_size == 5)
	{
		switch(clickedImagePos)
    {
			case 1: current_pos=current_pos-2; break;
			case 2: current_pos=current_pos-1; break;
			case 3: current_pos=current_pos; break;
			case 4: current_pos=current_pos+1; break;
			case 5: current_pos=current_pos+2; break;
    }
	}
	else if (filmstrip_size == 3)
	{
		switch(clickedImagePos)
		{
		  case 1: current_pos = current_pos - 1; break;
			case 2: current_pos = current_pos; break;
			case 3: current_pos = current_pos + 1; break;
		}
	}
	else if (filmstrip_size == 2)
	{
		switch(clickedImagePos)
		{
			case 1: current_pos = current_pos; break;
			case 2: current_pos = current_pos + 1; break;
		}
	}

  if (current_pos > images.length) {current_pos = current_pos - images.length;}
	else if (current_pos < 0) {current_pos = current_pos + images.length;}
	
	var subcounter = 1;
	for (var x=current_pos; x<current_pos+filmstrip_size; x++)
	{
		var tempimage = document.getElementById("image"+subcounter);
		if (x >= images.length)
		{
			// we have reached the end of the array
			tempimage.src="photos/photoAlbum/thumbnails/" +images[(x - images.length)];
			tempimage.alt = alts[(x - images.length)];
			if ((filmstrip_size == 5 && subcounter == 3) || (filmstrip_size == 3 && subcounter == 2) || (filmstrip_size == 2 && subcounter == 1))
			  { set_large_image((x-images.length)); setDescription(x-images.length); }
		}
		else
		{
			tempimage.src="photos/photoAlbum/thumbnails/" +images[x];
			tempimage.alt = alts[x];
			if ((filmstrip_size == 5 && subcounter == 3) || (filmstrip_size == 3 && subcounter == 2) || (filmstrip_size == 2 && subcounter == 1))
			  { set_large_image(x); setDescription(x);}
		}

		subcounter++;
  }
	
	//getImageArray();
}

function set_large_image(position)
{
	var largeimage = document.getElementById("lgimage");
	largeimage.src= "photos/photoAlbum/" + images[position];	
	largeimage.alt=alts[position];
}

function next_image()
{	
  if (current_pos == images_size)  current_pos = 1;
	else  ++current_pos;
	
	// Move all the images to the left.
	var subcounter = 1;
	for (var x=current_pos; x<current_pos+filmstrip_size; x++)
	{
		var tempimage = document.getElementById("image"+subcounter);
		
		// determine if we are showing a placeholder or an image.
		if (x >= images.length)
		{
			// we have reached the end of the array
			if (images[(x - images.length)] == 'placeholder.jpg'){
				 tempimage.src="photoAlbum/images/placeholder.jpg";
				 tempimage.alt = "placeholder";
				}
			else 
				{
					tempimage.src="photos/photoAlbum/thumbnails/" +images[(x - images.length)];
					tempimage.alt = alts[(x - images.length)];
				}
			
			if ((filmstrip_size == 5 && subcounter == 3) || (filmstrip_size == 3 && subcounter == 2) || (filmstrip_size == 2 && subcounter == 1))
      { set_large_image((x-images.length)); setDescription(x-images.length); }
		}
		else
		{
			if (images[x] == 'placeholder.jpg')
			{
				 tempimage.src="photoAlbum/images/placeholder.jpg";
				 tempimage.alt="placeholder";
			}
			else {
				tempimage.src="photos/photoAlbum/thumbnails/" +images[x];
				tempimage.alt = alts[x];
			}
			
			if ((filmstrip_size == 5 && subcounter == 3) || (filmstrip_size == 3 && subcounter == 2) || (filmstrip_size == 2 && subcounter == 1))
       { set_large_image(x); setDescription(x);}
		}
		
		subcounter++;
	}
	//getImageArray();
}

function prev_image()
{	
	if (current_pos == 0) current_pos = images_size-1;
	else --current_pos;	

	// move all images to the right.
	var subcounter = 1; 
	for (var x=current_pos; x <= current_pos+(filmstrip_size-1); x++)
	{
		var tempimage = document.getElementById("image"+subcounter);
		if (x < 0)
		{
			// we have reached the end of the array
			tempimage.src="photos/photoAlbum/thumbnails/" +images[(images.length + x)];
			tempimage.alt = alts[(x + images.length)];
			
			if ((filmstrip_size == 5 && subcounter == 3) || (filmstrip_size == 3 && subcounter == 2) || (filmstrip_size == 2 && subcounter == 1))
			  {set_large_image((images.length +x)); setDescription(images.length +x);}
			
		}
		else if (x >= images.length)
		{
			// we have reached the end of the array
			tempimage.src="photos/photoAlbum/thumbnails/" +images[(x - images.length)];
			tempimage.alt = alts[(x - images.length)];
  		if ((filmstrip_size == 5 && subcounter == 3) || (filmstrip_size == 3 && subcounter == 2) || (filmstrip_size == 2 && subcounter == 1))
	      { set_large_image((x-images.length)); setDescription(x-images.length); }
		}			
		else
		{
			tempimage.src="photos/photoAlbum/thumbnails/" +images[x];
			tempimage.alt = alts[x];
			if ((filmstrip_size == 5 && subcounter == 3) || (filmstrip_size == 3 && subcounter == 2) || (filmstrip_size == 2 && subcounter == 1))
        {set_large_image(x); setDescription(x);}
		}

		subcounter++;
	}
	//getImageArray();
}

function displayMessage(string)
{
	  var displayimage = document.getElementById("output");
		displayimage.innerHTML = string;
}

function getImageArray()
{
	// Display Image src of the images showing.
	 var displayimage = document.getElementById("output");
	 var image1 = document.getElementById("image1");
 	 var image2 = document.getElementById("image2");
	 var image3 = document.getElementById("image3");
	 var image4 = document.getElementById("image4");
	 var image5 = document.getElementById("image5");

   var text;
	 text = "image 1: " + image1.src + " ( "+(current_pos)+" )<br>";
	 
	 if (current_pos+1 >= images.length) text +="image 2: " + image2.src + " ( "+(current_pos+1-images.length)+" )<br>";
	 else text +="image 2: " + image2.src + " ( "+(current_pos+1)+" )<br>";
	 
	 if (current_pos+2 >= images.length) text +="<span style=\'font-weight:bold\'>image 3: " + image3.src + "</span> ( "+(current_pos+2-images.length)+" )<br>";
   else text +="<span style=\'font-weight:bold\'>image 3: " + image3.src + "</span> ( "+(current_pos+2)+" )<br>";

	 if (current_pos+3 >= images.length) text +="image 4: " + image4.src + " ( "+(current_pos +3-images.length)+" )<br>";
   else text +="image 4: " + image4.src + " ( "+(current_pos +3)+" )<br>";
   
	 if (current_pos+4 >= images.length) text +="image 4: " + image4.src + " ( "+(current_pos +4-images.length)+" )<br>";
	 else text +="image 5: " + image5.src + " ( "+(current_pos +4)+" )<br>";
	 

   displayimage.innerHTML = text;
}


/* SLIDESHOW FUNCTIONS - this are the same as the ones already created, but useable without opening a new window.*/

NewImg = new Array ("http://65.109.70.70/photos/photoAlbum/113.jpg","http://65.109.70.70/photos/photoAlbum/112.jpg","http://65.109.70.70/photos/photoAlbum/111.jpg","http://65.109.70.70/photos/photoAlbum/110.jpg","http://65.109.70.70/photos/photoAlbum/109.jpg","http://65.109.70.70/photos/photoAlbum/108.jpg");
ImgTitle = new Array ("Photo 6","Photo 5","Photo 4","Photo 3","Photo 2","Photo 1");
var ImgNum = 0;
var ImgLength = NewImg.length - 1;
//Time delay between Slides in milliseconds
var delay = 5000;
var lock = false;
var run;
function toggle() 
{ 
  if(document.getElementById("slide").innerHTML=="Stop")
	{ 
    document.getElementById("slide").innerHTML = "Start"; auto();
  }
  else { document.getElementById("slide").innerHTML = "Stop"; auto(); }
}

function setSpeed(speed)
{
  delay = speed;
	if (document.getElementById("slide").innerHTML=="Stop")
	{
   window.clearInterval(run);
   run = setInterval("next_image()", delay);
	}
}

function auto() 
{
  if (lock == true) 
	{
    lock = false;
    window.clearInterval(run); 
	}
  else if (lock == false) 
	{
    lock = true;
    run = setInterval("next_image()", delay);
	} 
}

