
/* ImageSwapper class */

// constructor
function ImageSwapper(filenames, ext, button_names)
{
	// initialize arrays
	this.button_out = new Array();		// array of out-button images
	this.button_in = new Array();			// array of in-button images
	this.button_click = new Array();		// array of clicked-button images

	this.clicked = new Array();			// clicked property for each button
	this.names = button_names;			// array of document.button names


	if (filenames != null && button_names != null)
	{
		// set max values
		this.max_buttons = filenames.length;	// number of buttons

		// load the buttons
		this.loadButtons(filenames, ext);

		// set specified button as clicked by default
		this.setClicked(0);
	}
	else
		return false;
}


/* methods */

// loads buttons in filenames array as images
ImageSwapper.prototype.loadButtons = function (filenames, ext)
{
	var types = new Array("out", "in", "click");
									// array of button types
	var typename;						// full name of button type
	var max_types = types.length;			// number of types


	// loop for each button type
	for (j = 0; j < max_types; j++)
	{
		// build button typename
		// (eg "button_out" for type[0] = "out")
		typename = "button_" + types[j];

		// initialize as an empty array
		eval(typename + " = new Array()");

		// loop for each button file in this type
		for (i = 0; i < this.max_buttons; i++)
		{
			// set button as image object (so it preloads)
			eval(typename + '[i] = new Image()');

			// set button filename
			// (eg file1.jpg, file2.jpg, file3.jpg ...)
			eval(typename + '[i].src = "' + filenames[i] + (j+1)
				+ ext + '"');
		}
	}

	return;
}

// returns number of button
ImageSwapper.prototype.getButtonNum = function (button_name)
{
	var found_button = false;			// found button flag
	var i;							// loop counter


	// loop through each button name
	for (i = 0; i < this.max_buttons; i++)
	{
		// compare with supplied button name
		if (this.names[i] == button_name)
		{
			// it is found; break from loop
			found_button = true;
			break;
		}
	}

	// if not found, set i to -1
	if (! found_button)
		i = -1;

	return i;
}

// this function swaps the graphic and sets the statbar
ImageSwapper.prototype.buttonSet = function (button_type, button_name)
{
	var num;						// number of button
	var swapped;					// swapped flag


	// get number from name
	num = this.getButtonNum(button_name);

	// check if button num found and if not clicked
	if (num != -1 && this.clicked[num] == false)
	{
		// set button
		switch (button_type)
		{
		  case 'in':
			this.buttonIn(num);
			break;
		  case 'out':
			this.buttonOut(num);
			break;
		  case 'click':
			// reset all buttons to "unclicked" with proper image
			for (i = 0; i < this.max_buttons; i++)
				this.buttonOut(i);

			// click this button
			this.buttonClick(num);

			// set clicked flags
			this.setClicked(num);
		}

		// let browser know it's okay now to continue it's action
		swapped = true;
	}
	else
		// could not swap
		swapped = false;

	return swapped;

}

ImageSwapper.prototype.buttonOut = function (item)
{
	eval('document.' + this.names[item] + '.src = button_out[item].src');

	return true;
}

ImageSwapper.prototype.buttonIn = function (item)
{
	eval('document.' + this.names[item] + '.src = button_in[item].src');

	return true;
}

ImageSwapper.prototype.buttonClick = function (item)
{
	eval('document.' + this.names[item] + '.src = button_click[item].src');

	return true;
}

ImageSwapper.prototype.setClicked = function (item)
{
	// loop for all buttons
	for (i = 0; i < this.max_buttons; i++)
	{
		// check if we are not item
		if (i != item)
			// all buttons get false by degault
			this.clicked[i] = false;
		else
			// this button is clicked, sir
			this.clicked[i] = true;
	}
}

ImageSwapper.prototype.disableClicks = function ()
{
	// this button is _never_ clicked
	this.setClicked(-1);
}
