////////////////////////////////////////////////////////////////////////////////
// Navigation Library
////////////////////////////////////////////////////////////////////////////////

// <script language="javascript">
// Copyright (c) Home Automation, Inc. All rights reserved.          
// Copyright (c) HomeRun Software Systems LLC. All rights reserved.

////////////////////////////////////////////////////////////////////////////////
// Globals
////////////////////////////////////////////////////////////////////////////////
var g_CurrentFocus = 0;		// Element on page that has the current focus
var g_aryFocusTable;		// Focus table array for tracking navigation between elements
var g_iCurrentPage = 0;		// Currently displayed page
var g_szCurrentPage = ""	// Current Page File
var g_iSetActiveControl = null	// Used to specify control to be given default focus when issuing dim/brighten commands
var g_NavHistory = new Array();
var g_ShowDemoOption = false;
var g_szStartupPage = 91;	// Default page to load

////////////////////////////////////////////////////////////////////////////////
// SetFocus: Sets focus on an item
////////////////////////////////////////////////////////////////////////////////
function SetFocus(nKey)
{
		
    try
    {
        // Track current focus in case it gets broken along the way
        var iPrevFocus = g_CurrentFocus;

        if (nKey >= 0)
        {
            g_CurrentFocus = g_aryFocusTable[g_CurrentFocus][nKey];
        }

        // If user tried an illegal move, reset Current Focus to known state
        if (g_CurrentFocus == -1)
        {
            g_CurrentFocus = iPrevFocus;
            return;
        }

        // Remove focus from the previous item if different
        if (iPrevFocus != g_CurrentFocus)
        {
			eval(g_aryFocusTable[iPrevFocus][5].substr(1,g_aryFocusTable[iPrevFocus][5].length-2));
        }

		// Finally we set the focus
		eval(g_aryFocusTable[g_CurrentFocus][4].substr(1,g_aryFocusTable[g_CurrentFocus][4].length-2));
		
    }
    catch(oError)
    {
		ErrorHandler(oError);
    }
}

////////////////////////////////////////////////////////////////////////////////
// ShowPage: Display the specified page
////////////////////////////////////////////////////////////////////////////////
function ShowPage
(
	iPage,
	iItemID
)
{
	var szPageName;
	var iPreviousPage = g_iCurrentPage;

	// Display the processing animation
	// ShowProcessWait();

	// Need to enable a 'back' button for the demo
	divNavBack.style.display = "inline";
	
	switch (iPage)
	{
		case PAGE_WELCOME:
			szPageName = "Welcome";
			divNavBack.style.display = "none";	// disable back button on welcome page for demo
			break;
		case PAGE_HOME:
			szPageName = "Home";
			break;
		case PAGE_CONTROL:
			szPageName = "Control";
			break;
		case PAGE_CONTROL_OUTPUTS:
			szPageName = "Control_Outputs";
			break;
		case PAGE_SECURITY:
			var iNumAreas = 1;
			if (!DEMO_MODE)
				iNumAreas = g_oHAI.Areas.Count;
			if (iNumAreas == 1)
			{
				szPageName = "Security_Zones";
				iPage = PAGE_SECURITY_ZONES;
				iItemID = 1;
			}
			else
				szPageName = "Security";
			break;
		case PAGE_SECURITY_ZONE:
			szPageName = "Security_Zone";
			break;
		case PAGE_SECURITY_ZONES:
			szPageName = "Security_Zones";
			break;
		case PAGE_TEMPERATURE:
			szPageName = "Temperature";
			break;
		case PAGE_TEMPERATURE_PESM:
			szPageName = "Temp_PESM";
			break;
		case PAGE_TEMPERATURE_TEMP:
			szPageName = "Temp_TempSensor";
			break;
		case PAGE_TEMPERATURE_HUMIDITY:
			szPageName = "Temp_Humidity";
			break;
		case PAGE_TEMPERATURE_THERMOSTAT:
			szPageName = "Temp_Thermostat";
			break;
		case PAGE_BUTTON:
			szPageName = "Button";
			break;
		case PAGE_EVENT:
			szPageName = "Event";
			break;
		case PAGE_SETTINGS_MAIN:
			szPageName = "Settings_Main";
			break;
		case PAGE_SETTINGS_CONTROLLER:
			szPageName = "Settings_Controller";
			break;
		case PAGE_STATUS:
			szPageName = "Status";
			break;
		case PAGE_UPDATES:
			szPageName = "Updates";
			break;
		case PAGE_CONTROL_COMMAND:
			szPageName = "Control_Command";
			break;
		case PAGE_CONTROL_COMMAND_APLNC:
			szPageName = "Control_Command_Aplnc";
			break;
		case PAGE_LOGIN:
			szPageName = "Login";
			break;
		case PAGE_CTRLR_WIZ_CONNECTION:
			szPageName = "Wiz_Ctrlr_Connection";
			break;
		case PAGE_CTRLR_WIZ_ADDRESS_PORT:
			szPageName = "Wiz_Ctrlr_Address_Port";
			break;
		case PAGE_CTRLR_WIZ_SERIAL:
			szPageName = "Wiz_Ctrlr_Serial";
			break;
		case PAGE_CTRLR_WIZ_NWRK_ENCRYPT:
			szPageName = "Wiz_Ctrlr_Nwrk_Encrypt";
			break;
		case PAGE_CTRLR_WIZ_CODE:
			szPageName = "Wiz_Ctrlr_Code";
			break;
		case PAGE_SYNC_PREFERENCES:
			szPageName = "Settings_Sync";
			break;
		case PAGE_CTRLR_WIZ_FINISH:
			szPageName = "Wiz_Ctrlr_Finish";
			break;
		case PAGE_NO_CONTROLLER:
			szPageName = "No_Controller";
			break;
		case PAGE_HELP:
			szPageName = "Help";
			break;
		case PAGE_RULES_LIST:
			szPageName = "Rule_List";
			break;
		case PAGE_RULES_CONDITION:
			szPageName = "Wiz_Rule_Condition";
			break;
		case PAGE_RULES_ALARM:
			szPageName = "Wiz_Rule_Alarm";
			break;
		case PAGE_RULES_SECURITY:
			szPageName = "Wiz_Rule_Security";
			break;
		case PAGE_RULES_UNIT:
			szPageName = "Wiz_Rule_Unit";
			break;
		case PAGE_RULES_ZONE:
			szPageName = "Wiz_Rule_Zone";
			break;
		case PAGE_RULES_VIDEO_PROPERTIES:
			szPageName = "Wiz_Rule_Video_Properties";
			break;
		case PAGE_RULES_NAME:
			szPageName = "Wiz_Rule_Name";
			break;
		case PAGE_SRVRLOG:
			MessageHandler("The System Log is not available when running in demo mode.");
			return;
			szPageName = "ServerLog";
			break;
		case PAGE_RULES_EDIT:
			// If in demo mode then prevent page from displaying
			if (DEMO_MODE)
			{
				ErrorHandler(null, ERR_DEMO_RULE_EDIT);
				return;
			}
			szPageName = "Rule_Edit"
			break;
		default:
			ErrorHandler(null, ERR_INVALID_PAGE);
			return;
	}
	
	// Delete controls from previous page if it exists
	g_VM_VerticalMenu = null;
	g_EM_ExpandMenu   = null;
		
	// Abort any current requests since the new one has precedence
	oXMLDoc.abort();
		
	g_iCurrentPage = iPage;	// Set the current page. Required for page refreshes
	
	// Add the page to the navigation history
	g_NavHistory.push(new Array(g_iCurrentPage, iItemID));
	
	if (szPageName != g_szCurrentPage)	
	{
		try
		{
			var hResult = oXSLDoc.load("xsl_" + szPageName + ".xsl");
			g_szCurrentPage = szPageName;
			
			if (!hResult)	
			{
				ErrorHandler(null, ERR_XSL_LOAD_FAILURE);
				CommandStatus.style.display = "none";  // Hide XML load eye candy
				return;
			}
		}
		catch (oError)
		{
			ErrorHandler(oError);
		}
	}

	// Play sound. If this is the first time loading then play welcome sound
	if (g_NavHistory.length == 1 && iPreviousPage == 0)
		PlaySound(SOUND_WELCOME);
	else
		PlaySound(SOUND_NAVIGATE);

	// Load the XML data
	GetPageData(szPageName, iItemID);		
}

////////////////////////////////////////////////////////////////////////////////
// RefreshCurrentPage: Reload the currently displayed page
////////////////////////////////////////////////////////////////////////////////
function RefreshCurrentPage()
{
	var aryPageInfo = g_NavHistory.pop();

	ShowPage(aryPageInfo[0], aryPageInfo[1]);
}

////////////////////////////////////////////////////////////////////////////////
// ShowPreviousPage: Display the previous page
////////////////////////////////////////////////////////////////////////////////
function ShowPreviousPage()
{
	// Remove the current page from the history and discard it
	g_NavHistory.pop();
	
	// Navigate to the previous page
	var aryPageInfo = g_NavHistory.pop();
	//alert("Page=" + aryPageInfo[0] + ", Data=" + aryPageInfo[1]);
	ShowPage(aryPageInfo[0], aryPageInfo[1]);
}

////////////////////////////////////////////////////////////////////////////////
// PH_EnabledItem: Check to see if the item with the current focus is disabled
// and if it is then move to the next item.
////////////////////////////////////////////////////////////////////////////////
function Nav_VerifyEnabled
(
	iFocusTableIndex
)
{
	var iMoveTo = g_aryFocusTable[iFocusTableIndex][9];

	if ( (g_aryFocusTable[iFocusTableIndex][8] != 0) || (iMoveTo == -1) )
		return true;
	else
	{
		eval(g_aryFocusTable[iMoveTo][4].substr(1,g_aryFocusTable[iMoveTo][4].length-2));
		return false;
	}
}

////////////////////////////////////////////////////////////////////////////////
// Nav_MarkAsDisabled: Mark the specified item as disabled
////////////////////////////////////////////////////////////////////////////////
function Nav_MarkAsDisabled
(
	iFocusTableIndex
)
{
	g_aryFocusTable[iFocusTableIndex][8] = 0;
}

////////////////////////////////////////////////////////////////////////////////
// Nav_MarkAsEnabled: Mark the specified item as enabled
////////////////////////////////////////////////////////////////////////////////
function Nav_MarkAsEnabled
(
	iFocusTableIndex
)
{
	g_aryFocusTable[iFocusTableIndex][8] = 1;
}

