////////////////////////////////////////////////////////////////////////////////
//   Spinner List Library
//   Notes: The menu items must be included in a span called 'spnSpinnerList'
////////////////////////////////////////////////////////////////////////////////

// <script language="javascript">
// Copyright (c) Home Automation, Inc. All rights reserved.          
// Copyright (c) HomeRun Software Systems LLC. All rights reserved.

////////////////////////////////////////////////////////////////////////////////
// Globals
////////////////////////////////////////////////////////////////////////////////
var g_oSpinner;

////////////////////////////////////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////////////////////////////////////
SL_MINUS = 0;
SL_PLUS  = 1;

////////////////////////////////////////////////////////////////////////////////
// SpinnerList: SpinnerList Object
////////////////////////////////////////////////////////////////////////////////
function SpinnerList
(
	oDisplaySpan,	// Span to display list item in
	oButtonMinus,	// Span containing minus button
	oButtonPlus		// Span containing plus button
)	
{
	this.Add		= SL_AddItem;
	this.Remove		= SL_RemoveItem;
	this.SpinDown	= SL_SpinDown;
	this.SpinUp		= SL_SpinUp;
	this.Display	= SL_DisplayItem;
	this.CheckButtonState = SL_CheckButtonState;
	this.Value		= "";
	this.DisplayValue = "";
	this.length		= 0;
	this.List		= new Array();
	this.LastAction = -1;
	this.SelectedIndex = 0;
	
	// User specified settings
	this.spnDisplay = oDisplaySpan;
	this.spnBtnMinus = oButtonMinus;
	this.spnBtnPlus  = oButtonPlus;
}

////////////////////////////////////////////////////////////////////////////////
// SL_AddItem: Add an item to the list
////////////////////////////////////////////////////////////////////////////////
function SL_AddItem
(
	vItem,
	vValue
)
{
	var bResult = false;
	
	try
	{
		if (null == vValue)
			vValue = vItem;
		this.List.push( new Array(vItem, vValue) );
		this.length++;
		bResult = true;
	}
	catch (oError)
	{
	}
	
	return bResult;
}

////////////////////////////////////////////////////////////////////////////////
// SL_RemoveItem: Remove an item to the list
////////////////////////////////////////////////////////////////////////////////
function SL_RemoveItem()
{
	var vResult;
	
	try
	{
		if (this.List.length > 0)
		{
			vResult = this.List.pop();
			this.length--;
		}	
	}
	catch (oError)
	{
		vResult = false;
	}
	
	return vResult;
}

////////////////////////////////////////////////////////////////////////////////
// SL_SpinDown: Scroll down one list item
////////////////////////////////////////////////////////////////////////////////
function SL_SpinDown()
{
	if (this.SelectedIndex > 0)
	{
		this.SelectedIndex--;
		this.LastAction = SL_MINUS;
		this.Display(this.SelectedIndex);
	}
	else
		PlaySound(SOUND_BONK);
}

////////////////////////////////////////////////////////////////////////////////
// SL_SpinUp: Scroll up one list item
////////////////////////////////////////////////////////////////////////////////
function SL_SpinUp()
{
	if (this.SelectedIndex < (this.length-1))
	{
		this.SelectedIndex++;
		this.LastAction = SL_PLUS;
		this.Display(this.SelectedIndex);	
	}
	else
		PlaySound(SOUND_BONK);
}

////////////////////////////////////////////////////////////////////////////////
// SL_DisplayItem: Display Item In List
////////////////////////////////////////////////////////////////////////////////
function SL_DisplayItem
(
	iItem
)
{
	var bResult = false;

	// Verify the specified item is valid before continuing
	if ( (iItem >= 0) && (iItem < this.length) )
	{
		this.spnDisplay.filters[0].Apply();		// Set state for beginning of transition
		
		// Display the item
		this.spnDisplay.innerText = this.List[iItem][0];
		this.SelectedIndex = iItem;
		this.DisplayValue = this.List[iItem][0];
		this.Value = this.List[iItem][1];
		
		this.spnDisplay.filters[0].Play();		// Do transition

		// Enable/Disable buttons
		this.CheckButtonState();
		
		bResult = true;
	}
	
	return bResult;
}

////////////////////////////////////////////////////////////////////////////////
// SL_CheckButtonState: Enables/Disables buttons as necessary
////////////////////////////////////////////////////////////////////////////////
function SL_CheckButtonState()
{
	// Reset button source to deselected state
	this.spnBtnMinus.filters(0).src = AIButtonItem["Spinner_Minus"].Deselect.src
	this.spnBtnPlus.filters(0).src = AIButtonItem["Spinner_Plus"].Deselect.src

	// Set state of Minus button
	if (this.SelectedIndex == 0)
	{
		AIButtonDisable(this.spnBtnMinus, 'Spinner_Minus');
		// Select Plus button if we're at the beginning of the list
		if (this.length > 1)
		{
			AIButtonSelect(this.spnBtnPlus, 'Spinner_Plus');
			PlaySound(SOUND_BONK);
			return;
		}
	}
	else
	{
		if (this.LastAction == SL_MINUS)
			AIButtonSelect(this.spnBtnMinus, 'Spinner_Minus');
		else
			AIButtonDeselect(this.spnBtnMinus, 'Spinner_Minus');
	}
		
	// Set state of Plus button
	if (this.SelectedIndex == (this.length-1))
	{
		AIButtonDisable(this.spnBtnPlus, 'Spinner_Plus');
		
		// Select Minus button if we're at the end of the list
		if (this.length > 1)
		{
			AIButtonSelect(this.spnBtnMinus, 'Spinner_Minus');
			PlaySound(SOUND_BONK);
		}
	}
	else
	{
		if (this.LastAction == SL_PLUS)
			AIButtonSelect(this.spnBtnPlus, 'Spinner_Plus');
		else
			AIButtonDeselect(this.spnBtnPlus, 'Spinner_Plus');
	}

}

