//
// Copyright (c) 2005, Android Technologies, Inc.
//
// FILE: upcoming_page_switching_2.js
//
// Code related to managing the notebook tabs and sub-page switching.

var g_delayedAlertPosted = false;
var gLastActiveDivID = "atiDivMain";
var GRAPHICS_DIRECTORY = "./graphics/";
var NOTEBOOK_TABS_SUB_PAGES_PREFIX	= "notebookTabs_subPage_";

// A simple routine that pushes an error message to be displayed
//  with "alert()", off to a timer.  Useful for displaying errors
//  that occur on "timer()" functions (so the user does not get
//  bombarded with "alert()" boxes).
function showDelayedAlert(message)
{
	if (message)
		alert(message);
} // function showdelayedAlert()

// The setInterval() call variant that supports arguments is not
//  universally supported.  Embed them in the function call
//  parameter instead.
function delayedAlert(message, delayTimeMS)
{
	// Check repetitive alert prevention flag.
	if (g_delayedAlertPosted)
		return;
		
	if (!delayTimeMS)
		delayTimeMS = 500;  // Default delay.
		
	if (message)
		setTimeout("showDelayedAlert(" + "'" + message + "'" + ", " + delayTimeMS + ")");
	
	// Set flag to avoid repetitive alerts.
	g_delayedAlertPosted = true;
} // function delayedAlert()

// This really should be part of the logic of the code
//  rather than a hard-coded table, but under the gun
//  for demo deadline.
function divIDToColumnNumber(divID)
{
	if (divID == "atiDivMain")
		return 1;
	//if (divID == "atiDivResource")
	//	return 2;
	if (divID == "atiDivEvent")
		return 2;
	if (divID == "atiDivVenue")
		return 3;
	//if (divID == "atiDivExtraStuff")
	//	return 5;
		
	alert("(divIDToColumnNumber) Invalid div ID: " + divID);
	return -1;
} // function divIDToColumnNumber(divID)
	
function changeSubPage(tabClicked, subPageDoc, divID)
{
	// Make the given divID visible and all others 
	//  invisible.
    // var targetDivElement = document.getElementById(divID);
    
    //if (!targetDivElement
    var divElements = subPageDoc.getElementsByTagName("DIV");
    
    var numTabs = 0;
    if (divElements)
    {
	    if (divElements.length > 0)
	    {
		    for (var i = 0; i < divElements.length; i++)
		    {
			    // Is it one of our divs?
			    var divElem = divElements[i];

			    var prefix = divElem.id.substr(0, 3);
			    if (prefix == "ati")
			    {
				    numTabs++;
				    
				    // Yes, is it the selected div?
				    if (divElem.id == divID)
				    {
					    // Yes, make it visible.
					    divElem.style.display = "block";
					    
						
					}
					else
						// No, hide it.
					    divElem.style.display = "none";
				} // if (divElem.id.substr(3) == "ati")
				
		    } // for()
	    } // if (divElements.length > 0)
    } // if (divElements)
		
    // Highlight the correct notebook tab.
    columnNumber = divIDToColumnNumber(divID);
    if (numTabs > 0)
    	highlightSubPageNotebookTab(columnNumber, numTabs);
    
	if (!tabClicked) return true; else return;
} // function changeSubPage(virtualPageCode)
	
// Given a document containing a sub-pages notebook tabs bar,
//  Highlight the given column number and un-highlight all
//  the others.
// PARAMETERS:
//	columnNumber: The column number to "highlight".
//	numTabs		: The number of tabs in the notebook tabs bar.
// NOTE		: Tabs are numbered starting at base 1, NOT 0.
// TIMER BASED FUNCTION!
function highlightSubPageNotebookTab(columnNumber, numTabs)
{
	if (!columnNumber)
	{
		delayedAlert("(highlightSubPageNotebookTab) Column number is undefined.");
		return;
	} // if (!columnNumber)
	
	if (!numTabs)
	{
		delayedAlert("(highlightSubPageNotebookTab) Tab count is undefined.");
		return;
	} // if (!numTabs)

	var fullNameOfUnhighlightImage =
		GRAPHICS_DIRECTORY + "one-sub-page-notebook-tab-soft-blue.jpg";
	var fullNameOfHighlightImage =
		GRAPHICS_DIRECTORY + "one-sub-page-notebook-tab-yellow.jpg";
	
	for (var i = 1; i <= numTabs; i++)
	{
		// Find the table data element for the given column number.
		var tdName =
			NOTEBOOK_TABS_SUB_PAGES_PREFIX + i;	
		var tdElem =
			document.getElementById(tdName);
			
		if (!tdElem)
		{ 
			delayedAlert("(highlightSubPageNotebookTab) Unable to find the element named: " + tdName + ".");
			return;
		} // if (!tdElem)
		
		if (i == columnNumber)
		{
			if (tdElem.attributes.background.value != fullNameOfHighlightImage)
				tdElem.attributes.background.value = fullNameOfHighlightImage;
		}
		else
		{
			if (tdElem.attributes.background.value != fullNameOfUnhighlightImage)
				tdElem.attributes.background.value = fullNameOfUnhighlightImage;
		} // else - if (i == columnNumber)
	} // for()
} // function highlightSubPageNotebookTab() 		


