////////////////////////////////////////////////////////////////////////////////
// Expansion Menu Library
//   Notes: The menu items must be included in a span called 'spnExpandMenu'
////////////////////////////////////////////////////////////////////////////////

// <script language="javascript">
// Copyright (c) Home Automation, Inc. All rights reserved.          
// Copyright (c) HomeRun Software Systems LLC. All rights reserved.

////////////////////////////////////////////////////////////////////////////////
// Globals
////////////////////////////////////////////////////////////////////////////////
var g_EM_ExpandMenu;			// Reference to control

////////////////////////////////////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////////////////////////////////////
var EM_DIRECTION_UP    = 1;		// Scroll Upward
var EM_DIRECTION_DOWN  = 2;		// Scroll Downward

////////////////////////////////////////////////////////////////////////////////
// ExpansionList: Create a new ExpansionList Object
////////////////////////////////////////////////////////////////////////////////
function ExpansionList()
{
	// Functions
	this.Initialize				= EM_Init;
	this.Scroll					= EM_Scroll;
	this.SelectItem				= EM_SelectItem;
	this.SelectItemRemote		= EM_SelectItemRemote;
	this.DeselectItem			= EM_DeselectItem;
	this.ChooseItem				= EM_ChooseItem;
	this.Add					= EM_AddItem;
	this.Expand					= EM_Expand;
	this.Collapse				= EM_Collapse;
		
	// Properties	
	this.List = new Array();
	this.SelectedIndex = 0;
	this.Value = "";
	this.Command = "";
	this.Length = 0;
	this.Expanded = false;
}

////////////////////////////////////////////////////////////////////////////////
// EM_Init: Initialize Expansion Menu
////////////////////////////////////////////////////////////////////////////////
function EM_Init()
{
	if ( this.Length > 0 )
	{
		// Build the HTML
		var szMenuHTML = "<TABLE id='tblEMMenu' class='EMMenu' cellpadding=0 cellspacing=0>";
		var DQ = new String();

		for (idx = 0; idx < this.Length; idx++)
		{
			var szOnMouseOver = " OnMouseOver=\"g_EM_ExpandMenu.SelectItem(this.menuitem)\"";
			var szOnMouseOut  = " OnMouseOut=\"g_EM_ExpandMenu.DeselectItem()\"";
			var szOnClick  = " OnClick=\"g_EM_ExpandMenu.ChooseItem(this.menuitem)\"";
			szMenuHTML += "<TR id='trEMMenu' class='EMMenuItem'><TD class='EMMenuItem' menuitem='" + idx + "'" + szOnMouseOver + szOnMouseOut + szOnClick + ">" + this.List[idx][0] + "</TD></TR>";
		}
		
		szMenuHTML+= "</TABLE>";
		
		// Add HTML to document
		spnExpandMenu.innerHTML = szMenuHTML;
	}
}

////////////////////////////////////////////////////////////////////////////////
// EM_Scroll: Scroll Expansion Menu
////////////////////////////////////////////////////////////////////////////////
function EM_Scroll
(
	iDirection
)
{
	// Determine item to select
	switch (iDirection)
	{
		case EM_DIRECTION_UP:
			if (this.SelectedIndex > 0)
				this.SelectedIndex--;
			break;
		case EM_DIRECTION_DOWN:
			if (this.SelectedIndex < (this.Length - 1) )
				this.SelectedIndex++;
			break;
		default:
			return;
	}
	
	// Deselect old and then Select the new item
	this.EM_DeselectItem();
	this.EM_SelectItem(this.SelectedIndex);	
}

////////////////////////////////////////////////////////////////////////////////
// EM_SelectItem: Select Expansion Menu
////////////////////////////////////////////////////////////////////////////////
function EM_SelectItem
(
	iItem
)
{
	if ( this.Expanded )
	{
		// Deselect Current Item
		this.DeselectItem();
		
		trEMMenu[iItem].className = "EMMenuItemSelect";
		this.SelectedIndex = iItem;
	}
}

////////////////////////////////////////////////////////////////////////////////
// EM_SelectItemRemote: Select Expansion Menu Item With Remote
////////////////////////////////////////////////////////////////////////////////
function EM_SelectItemRemote()
{
	var iNewRow = this.SelectedIndex;

	switch (g_LastKeyChar)
	{
        case 0x26:				// Up button selected
        case 0x38:
			if (this.SelectedIndex > 0)
				iNewRow = (this.SelectedIndex-0) - 1;
			break;
        case 0x28:				// Down button selected
        case 0x40:
			if (this.SelectedIndex < (this.Length - 1) )
				iNewRow = (this.SelectedIndex-0) + 1;
		default:
			if (this.SelectedIndex > 0)
			if (null == iNewRow)
				iNewRow = this.SelectedIndex;
	}
	
	// Expand Menu if it isn't already
	if (!this.Expanded)
	{
		this.Expand();
		iNewRow = 0;	// Default to first item in menu
	}
		
	// If the new row is valid then select/deselect rows
	if ( (iNewRow >= 0) && (iNewRow < this.Length) )
	{
		// Select the new row
		this.SelectItem(iNewRow);
		
		// Set the new row
		this.SelectedIndex = iNewRow;	
	}
}

////////////////////////////////////////////////////////////////////////////////
// EM_DeselectItem: Deselect Expansion Menu Item
////////////////////////////////////////////////////////////////////////////////
function EM_DeselectItem()
{
	if ( this.Expanded )
	{
		trEMMenu[this.SelectedIndex].className = "EMMenuItem";
	}
}

////////////////////////////////////////////////////////////////////////////////
// EM_ChooseItem: Choose Expansion Menu Item
////////////////////////////////////////////////////////////////////////////////
function EM_ChooseItem
(
	iItem
)
{
	// If no item is specified then use the currently selected item
	if (null == iItem)
		iItem = this.SelectedIndex;
		
	if (iItem >= 0 && iItem < this.Length)
	{
		eval(this.List[iItem][1]);
		RefreshCurrentPage();
	}
}

////////////////////////////////////////////////////////////////////////////////
// EM_AddItem: Add Expansion Menu Item
////////////////////////////////////////////////////////////////////////////////
function EM_AddItem
(
	szDisplayValue,
	szCommand
)
{
	if ( szDisplayValue != "" && szCommand != "" )
	{
		this.List[this.Length] =  new Array(szDisplayValue, szCommand);
		
		this.Length = this.List.length;
	}
}

////////////////////////////////////////////////////////////////////////////////
// EM_Expand: Expand the Expansion Menu 
////////////////////////////////////////////////////////////////////////////////
function EM_Expand()
{
	spnExpandMenu.style.display = "inline";
	this.Expanded = true;
}

////////////////////////////////////////////////////////////////////////////////
// EM_Collapse: Collapse Expansion Menu
////////////////////////////////////////////////////////////////////////////////
function EM_Collapse()
{
	spnExpandMenu.style.display = "none";
	this.Expanded = false;
}
