////////////////////////////////////////////////////////////////////////
// javascript/lib.js
// $Id$
//
// Dave Restall - October 2007
//
// General small javascript routines used to display the page.
////////////////////////////////////////////////////////////////////////

function Image_Slide()
	{
	// Slideshow of all the images on a page.

	// No vehicle images defined, do nothing.

	if (! D_Images.length) return;

	// All the VIMG_\d+ images will need to be processed.

	var D_Image;

	for (var D_Index in  D_Images)
		{
		D_Image = D_Images[D_Index];	// Get an image control record

		if (! D_Image.Images.length) continue; // No images

		Image_ID = D_Image.Base_ID;	// ID for img tag

		D_Image.Index ++;
		D_Image.Index = D_Image.Index % D_Image.Images.length;

		// Index now holds the subscript of the image to display
		// Get the image from its ID and change the image src to
		// the new image.

		New_Image_SRC = D_Image.Images[D_Image.Index];

		document.getElementById(Image_ID).src = New_Image_SRC;
		}
	}			// Image_Slide

function Slide_2()
	{
	// Two images appear on the screen [Left] and [Right'] when this
	// function is called, the D_Slide_2 object is processed.  This
	// replaces the image in Left with that in Right and shifts a
	// new image in Right.  When the images have completed, they
	// wrap round to the beginning.  There is also a caption field
	// that needs to be swapped to.  The caption for the image is
	// in the same element number in the captions array.

	ID_Left = D_Slide_2.ID[0];
	ID_Right = D_Slide_2.ID[1];

	// Get the next index ID.  This is the ID for the LEFT image

	D_Slide_2.Index ++;
	D_Slide_2.Index = D_Slide_2.Index % D_Slide_2.Images.length;

	Index_Left = D_Slide_2.Index;
	Index_Right = ( Index_Left + 1 ) % D_Slide_2.Images.length;

	// Swap the images.

	document.getElementById(ID_Left).src = D_Slide_2.Images[Index_Left];
	document.getElementById(ID_Right).src = D_Slide_2.Images[Index_Right];

	// Swap the caption

	ID_Left += '.Caption';
	ID_Right += '.Caption';

	document.getElementById(ID_Left).childNodes[0].nodeValue =
					D_Slide_2.Captions[Index_Left];
	document.getElementById(ID_Right).childNodes[0].nodeValue =
					D_Slide_2.Captions[Index_Right];
	}			// Slide_2

// Image scroll/marquee functions

function Scroll_Right()
	{
	// Shift the images right, a new image is placed on the left and
	// the final image drops of the end.

	// Scroll_Images.Index holds the index of the first image to be
	// displayed.  The next time the button is pressed, this will
	// need to a different number.

	N_Images = Scroll_Images.Thumbnails.length;
	Scroll_Images.Index = (Scroll_Images.Index + N_Images - 1) % N_Images;
	Index = Scroll_Images.Index;

	IMG_Index = 0;
	var Image;

	while (Image = document.getElementById('Scroll_' + IMG_Index))
		{
		Image.src = Scroll_Images.Thumbnails[Index];
		Index = (Index + 1) % N_Images;
		IMG_Index ++;
		}
	}			// Scroll_Right


function Scroll_Left()
	{
	// Shift the images left, lose the first image and pop a new
	// image in to the right end.

	// Index holds the index of the image to be put in the first
	// scroll window.  We count from this to the final window.
	// What's the final window ?  This is the number of img tags
	// with the name Scroll_\d+.


	Scroll_Images.Index = (Scroll_Images.Index + 1) %
					Scroll_Images.Thumbnails.length;

	Index = Scroll_Images.Index;

	IMG_Index = 0;
	var Image;

	while (Image = document.getElementById('Scroll_' + IMG_Index))
		{
		Image.src = Scroll_Images.Thumbnails[Index];
		Index = (Index + 1) % Scroll_Images.Thumbnails.length;
		IMG_Index ++;
		}
	}			// Scroll_Left

function Big_Swap(Index)
	{
	// This is in the detail window.  It swaps the big image with
	// one of the same size but selected from the scroll.

	Image_ID = Scroll_Images.Big_ID;
	var Image = document.getElementById(Image_ID);
	Index = (Scroll_Images.Index + Index ) % Scroll_Images.Images.length;
	Image.src = Scroll_Images.Images[Index];
	}			// Big_Swap

function Detail_Window(VID, Width, Height)
	{
	window.open('/view.php?&VID=' + VID, 'Detail_Window',
				'width=' + Width + ',height=' + Height);

	}			// Detail_Window

// We go to the main body here and set up everything that is required
// subsequently.

// Any dynamic images (i.e. vehicles that have multiple pictures have a
// record in this array which is processed in the slide show.

var D_Images = new Array();
// var Scroll_Images = new Array();

// And every INTERVAL seconds we update the images in the D_Images array.

INTERVAL = 4;		// 4 seconds

window.setInterval('Image_Slide()', INTERVAL * 1000);

// EOF javascript/lib.js
