//*********************************************************************************************************
// BagOfBits Rotator (c) Brian Budd, 2001
// This software is supplied without warranty or support, and I accept no liability for any problems
// or data loss it may cause you.  Please feel free to use it on your websites, all I ask is that you
// credit BagOfBits where it is used by using the logo/link available from www.bagofbits.com/netbits.html
//
//  v003 - 9th December 2001    Fixes for Netscape
//*********************************************************************************************************

// Global Variables
var BOBR_CurrentImageX=0, BOBR_CurrentImageY=0;
var BOBR_MaxX, BOBR_MaxY;
var BOBR_RoImages;
var BOBR_MainImage, BOBR_Loaded;				// Image number of main display image and load status

// Initialisation function, called on page load
function RotatorInit(Ximages, Yimages)
{
// General Variables
	var WaitLoopX, WaitLoopY, BaseImage, ScanLoop;

// Validate the parameters before anything else
	if (Ximages < 1)
	{
		alert("RotatorInit was called with an invalid Ximages value.\nPlease refer to the documentation for information");
		return;
	}
	if (Yimages < 1)
	{
		alert("RotatorInit was called with an invalid Yimages value.\nPlease refer to the documentation for information");
		return;
	}

// Set the globals for the array sizes
	BOBR_MaxX=Ximages-1;
	BOBR_MaxY=Yimages-1;
	BOBR_RoImages = new Array(Ximages);

// Find the first image in the sequence to load from
	BOBR_Loaded = 0;				// Default status variable to "failed load"
	BaseImage = -1;
	BOBR_MainImage = -1;
	ScanLoop = 0;
	while (ScanLoop < window.document.images.length)
	{
		if (window.document.images[ScanLoop].name=="BOBR_IMAGE1")
		{
			BaseImage=ScanLoop;
		}

		if (window.document.images[ScanLoop].name=="BOBR_MAIN")
		{
			BOBR_MainImage=ScanLoop;
		}
		ScanLoop++;
	}

// Validate that a "main" image was specified
	if (BOBR_MainImage < 0)
	{
		alert("BOBR_MAIN not found\nPlease correct your HTML file and try again");
		return;
	}

// Validate that an image list was specified
	if (BaseImage < 0)
	{
		alert("BOBR_IMAGE1 not found\nPlease correct your HTML file and try again");
		return;
	}
// Validate that there are enough images to load the array
	if (BaseImage+((BOBR_MaxX+1)*(BOBR_MaxY+1)) > window.document.images.length)
	{
		alert("There are not enough images on the page to complete a load of the image rotator\nThe load has been aborted");
		return;
	}


// Create the table of images
	for (WaitLoopX=0; WaitLoopX<=BOBR_MaxX;WaitLoopX++)
	{
		BOBR_RoImages[WaitLoopX] = new Array(Yimages);
		for (WaitLoopY=0; WaitLoopY<=BOBR_MaxY;WaitLoopY++)
		{
			BOBR_RoImages[WaitLoopX][WaitLoopY] = new Image;
			BOBR_RoImages[WaitLoopX][WaitLoopY].src = BOBR_RoImages[WaitLoopX][WaitLoopY].name = window.document.images[BaseImage+WaitLoopX+(WaitLoopY*(BOBR_MaxX+1))].src;
		}

	}


// Position the 'cursor' in the image table
	BOBR_CurrentImageX = 0; BOBR_CurrentImageY = 0;
	SetImage(BOBR_CurrentImageX, BOBR_CurrentImageY);
	BOBR_Loaded=1;					// Set status to denote that load was successful
//		alert("Rotator Init Done");

		
}


//=======================================================================================================================
// RotateImage - Make the necessary image adjustments based on a button click and initiate image update
//
function RotateImage(kpress)
{
	var a_string;			// String for alert message creation

// Check that everything was loaded correctly
	if (BOBR_Loaded==0)
	{
		alert("The image rotator did not load successfully and so cannot be used");
		return;
	}

// Check the operation
	if (kpress=='0') 
	{
		BOBR_CurrentImageX = 0; BOBR_CurrentImageY = 0;
	}
	if (kpress=='1')
		BOBR_CurrentImageY+=1;
	if (kpress=='2')
		BOBR_CurrentImageX+=1;
	if (kpress=='3')
		BOBR_CurrentImageX-=1;
	if (kpress=='4')
		BOBR_CurrentImageY-=1;

// Check the boundaries
	if (BOBR_CurrentImageY < 0) BOBR_CurrentImageY=BOBR_MaxY;
	if (BOBR_CurrentImageY > BOBR_MaxY) BOBR_CurrentImageY=0; 
	if (BOBR_CurrentImageX < 0) BOBR_CurrentImageX=BOBR_MaxX; 
	if (BOBR_CurrentImageX > BOBR_MaxX) BOBR_CurrentImageX=0; 

// Set the Image
	SetImage(BOBR_CurrentImageX, BOBR_CurrentImageY);
}


//=======================================================================================================================
// SetImage - Display the new image and set all supporting variables correctly
//
function SetImage(CurrentImageX, CurrentImageY)
{

// Variables used for checking validity of movement
	var CheckL, CheckR, CheckU, CheckD;

// Set the check variables
	CheckL=CurrentImageX-1; CheckR=CurrentImageX+1; CheckU=CurrentImageY-1;	CheckD=CurrentImageY+1;
	if (CheckD < 0) CheckD=BOBR_MaxY;
	if (CheckU > BOBR_MaxY) CheckU=0; 
	if (CheckR > BOBR_MaxX) CheckR=0; 
	if (CheckL < 0) CheckL=BOBR_MaxX; 

// Set the image
	if (BOBR_RoImages[CurrentImageX][CurrentImageY].name != "BLANK")
	{
		window.document.images[BOBR_MainImage].width=400;
		window.document.images[BOBR_MainImage].height=300;
		window.document.images[BOBR_MainImage].src = BOBR_RoImages[CurrentImageX][CurrentImageY].src;
	}
}
