//=============================================================================================================
// File:		ucbInstruments.js
// Purpose: 	Implements an instrument and tuning selection mechanism.
// Author:		JDH/1999-2002

//=============================================================================================================
// Global variables
//=============================================================================================================

	// The list of objects attached to particular HTML tag names
var InstControllers = [];

//=============================================================================================================
// Global functions
//=============================================================================================================

// Function:	InstGetController
// Purpose:		Gets the object that controls a particular HTML object.
// Arguments:	htmlObjectName - The HTML object
//
function InstGetController( htmlObjectName )
{
	return InstControllers[ htmlObjectName ];
}

// Method:		InstSetController
// Purpose:		Sets the controller that controls a given HTML object.
// Arguments:	htmlObjectName - The HTML object name
//				object - The JavaScript controller object
//
function InstSetController( htmlObjectName, object )
{
	InstControllers[ htmlObjectName ] = object;
}

//=============================================================================================================
// The tuning combo box
//=============================================================================================================

// Class:		TuningCombo
// Purpose:		A tuning combo box.
// Arguments:	comboName - The name of the combo
//				instrumentList - The list of instruments
//				instrumentIndex - The index of the instrument for this combo
//
function TuningCombo( comboName, instrumentList, instrumentIndex, selectedTuning )
{
	this.comboName 			= comboName;
	this.instrumentList 	= instrumentList;
	this.instrumentIndex 	= instrumentIndex;
	this.selectedTuning		= selectedTuning;
}

// Method:		GetHTML
// Purpose:		Gets the HTML to build the combo box.
// Arguments:	None
//
TuningCombo.prototype.GetHTML = function()
{
	s = "<select name=\"Select" + this.comboName + "\" size=1>";
	for( var tuning = 0; tuning < this.instrumentList[ this.instrumentIndex ].length; tuning++ )
	{
		if ( this.selectedTuning == this.instrumentList[ this.instrumentIndex ][ tuning ] )
			s += "<option selected value=\"" + this.instrumentList[ this.instrumentIndex ][ tuning ] + "\">" + this.instrumentList[ this.instrumentIndex ][ tuning ];
		else
			s += "<option value=\"" + this.instrumentList[ this.instrumentIndex ][ tuning ] + "\">" + this.instrumentList[ this.instrumentIndex ][ tuning ];
	}
	s += "</option></select>"
	return s;
}

// Method:		GetTuning
// Purpose:		Gets the current tuning.
// Arguments:	None
//
TuningCombo.prototype.GetTuning = function()
{
	return document.MainForm[ "Select" + this.comboName ].options[ document.MainForm[ "Select" + this.comboName ].selectedIndex ].value;
}

//=============================================================================================================
// The Instrument group object
//=============================================================================================================

// Class:		InstrumentGroup
// Purpose:		Implements a group of controls that makes up an instrument/tuning selector.
// Arguments:	parentObjectName - The name of the parent object to notify when an attempt
//					to change instrument or tuning occurs.
//				instrumentList - The list of instruments
//				instrumentIndex - The index of the instrument for this combo
//				selectedInstrument - The currently selected instrument
//
function InstrumentGroup( parentObjectName, instrumentList, instrumentIndex, selectedInstrument, selectedTuning )
{
	this.goImage = new Image( 33, 20);
	this.goImage.src = "images/go.gif";

	if ( instrumentIndex == 0 )
	{
		this.onImage = new Image( 100, 31 );
		this.onImage.src = "images/guitar_button_on.gif";
		this.offImage = new Image( 100, 31 );
		this.offImage.src = "images/guitar_button.gif";
		this.tuningCombo = new TuningCombo( "GuitarCombo", instrumentList, instrumentIndex, selectedTuning );
		this.baseObjectName = "guitar";
	}
	else
	{
		this.onImage = new Image( 100, 31 );
		this.onImage.src = "images/bass_button_on.gif";
		this.offImage = new Image( 100, 31 );
		this.offImage.src = "images/bass_button.gif";
		this.tuningCombo = new TuningCombo( "BassCombo", instrumentList, instrumentIndex, selectedTuning );
		this.baseObjectName = "bass";
	}
	
	this.parentObjectName 	= parentObjectName;
	this.instrumentList 	= instrumentList;
	this.instrumentIndex 	= instrumentIndex;
	this.selectedInstrument	= selectedInstrument;
}

// Method:		onImageMouseOver
// Purpose:		Responds to the mouse going over the instrument image.
// Arguments:	None
//
InstrumentGroup.prototype.onImageMouseOver = function()
{
	window.status = "Click to use a " + this.baseObjectName;
	if ( this.instrumentIndex != this.selectedInstrument )
		UCBGetBodyDocument()[ this.baseObjectName + "_image" ].src = this.onImage.src;
	return true;
}

// Method:		onImageMouseOut
// Purpose:		Responds to the mouse leaving the instrument image.
// Arguments:	None
//
InstrumentGroup.prototype.onImageMouseOut = function()
{
	window.status = "";
	if ( this.instrumentIndex != this.selectedInstrument )
		UCBGetBodyDocument()[ this.baseObjectName + "_image" ].src = this.offImage.src;
	return true;
}

// Method:		onGoMouseOver
// Purpose:		Responds to the mouse going over the go image.
// Arguments:	None
//
InstrumentGroup.prototype.onGoMouseOver = function()
{
	window.status = "Click to use a " + this.baseObjectName;
	return true;
}

// Method:		onGoMouseOut
// Purpose:		Responds to the mouse leaving the go image.
// Arguments:	None
//
InstrumentGroup.prototype.onGoMouseOut = function()
{
	window.status = "";
	return true;
}

// Method:		Place
// Purpose:		Builds HTML to represent the group and writes it to the document.
// Arguments:	None
//
InstrumentGroup.prototype.Place = function()
{
	var instrumentImage = this.offImage.src;
	if ( this.instrumentIndex == this.selectedInstrument )
		instrumentImage = this.onImage.src;

	s = "<a name=\"" + this.baseObjectName + "_image_link\" href=\"javascript:" + this.parentObjectName + ".onRequestInstrument(" + this.instrumentIndex + ");\" onMouseOver=\"return InstGetController('" + this.baseObjectName + "_image_link').onImageMouseOver();\" onMouseOut=\"return InstGetController('" + this.baseObjectName + "_image_link').onImageMouseOut();\">";
	s += "<img name=\"" + this.baseObjectName + "_image\" src=\"" + instrumentImage + "\" width=100 height=31 border=0></a><br>";
	s += this.tuningCombo.GetHTML();
	s += "<br> &nbsp &nbsp ";
	s += "<a name=\"" + this.baseObjectName + "_go\" href=\"javascript:" + this.parentObjectName + ".onRequestInstrument(" + this.instrumentIndex + ");\" onMouseOver=\"return InstGetController('" + this.baseObjectName + "_go').onGoMouseOver();\" onMouseOut=\"return InstGetController('" + this.baseObjectName + "_go').onGoMouseOut();\">";
	s += "<img src=\"" + this.goImage.src + "\" width=33 height=20 border=0></a>";
	document.write( s );

	InstSetController( this.baseObjectName + "_image_link", this );
	InstSetController( this.baseObjectName + "_go", this );
}

// Method:		GetTuning
// Purpose:		Gets the current tuning.
// Arguments:	None
//
InstrumentGroup.prototype.GetTuning = function()
{
	return this.tuningCombo.GetTuning();
}

//=============================================================================================================
// The Instrument selector object
//=============================================================================================================

// Class:		InstrumentSelector
// Purpose:		Builds an instrument selector (which is a number of instrument groups).
// Arguments:	thisObjectName - The name of this object
//				instrumentList - The list of instrument tunings
//				formName - The name of the encapsulating form
//				selectedInstrument - The index of the selected instrument
//
function InstrumentSelector( thisObjectName, instrumentList, formName, selectedInstrument, selectedTuning )
{
	this.thisObjectName 	= thisObjectName;
	this.instrumentList 	= instrumentList;
	this.formName 			= formName;
	this.selectedInstrument	= selectedInstrument;
	this.selectedTuning		= selectedTuning;

	var bassTuning = selectedTuning;
	var guitarTuning = selectedTuning;
	if ( selectedInstrument == 0 )
		bassTuning = "Bass Standard";
	if ( selectedInstrument == 1 )
		guitarTuning = "Guitar Standard";

	this.guitarGroup = new InstrumentGroup( this.thisObjectName,
											instrumentList, 0,
											selectedInstrument,
											guitarTuning );
	this.bassGroup = new InstrumentGroup( this.thisObjectName,
										  instrumentList, 1,
										  selectedInstrument,
										  bassTuning );
}

// Method:		Place
// Purpose:		Builds HTML to represent the selector and writes it to the document.
// Arguments:	None
//
InstrumentSelector.prototype.Place = function()
{
	document.write( "<table border=0 cellpadding=0 cellspacing=0 hspace=0 vspace=0><tr><td width=20>&nbsp</td><td>" );
	this.guitarGroup.Place();
	document.write( "</td><td width=20>&nbsp</td><td>" );
	this.bassGroup.Place();
	document.write( "</td></tr></table>" );
}

// Method:		onRequestInstrument
// Purpose:		Responds to the request to move to a new instrument or tuning.
// Arguments:	instrumentIndex - The selected instrument index
//
InstrumentSelector.prototype.onRequestInstrument = function( instrumentIndex )
{
	document[ this.formName ].formInstrument.value = ( instrumentIndex == 0 ) ? "Guitar" : "Bass";
	document[ this.formName ].formTuning.value = ( instrumentIndex == 0 ) ? this.guitarGroup.GetTuning() : this.bassGroup.GetTuning();
	document[ this.formName ].submit();
}
