/* 
 This file was generated by Dashcode.  
 You may edit this file to customize your widget or web page 
 according to the license.txt file included in the project.
 */

//
// Function: load()
// Called by HTML body element's onload event when the web application is ready to start
//
function load()
{
    dashcode.setupParts();
}

var restaurantRow;
var itemRow;

// This object implements the dataSource methods for the list.
var restaurantsListDataSource = {
	
	// Sample data for the content of the list. 
	// Your application may also fetch this data remotely via XMLHttpRequest.
	_rowData: ["Restaurant 1", "Item 2", "Item 3"],
	
	// The List calls this method to find out how many rows should be in the list.
	numberOfRows: function() {
		return restaurants.length;
	},
	
	// The List calls this method once for every row.
	prepareRow: function(rowElement, rowIndex, templateElements) {
		// templateElements contains references to all elements that have an id in the template row.
		// Ex: set the value of an element with id="label".
		if (templateElements.label) {
			templateElements.label.innerText = restaurants[rowIndex].name;
		}

		// Assign a click event handler for the row.
		rowElement.onclick = function(event) {
			// Do something interesting
            restaurantRow = rowIndex;
            document.getElementById('itemsList').object.reloadData();
			// Go forward in a Browser
            var browser = document.getElementById('browser').object; // Replace with id of Browser
            browser.goForward('itemsListView', restaurants[rowIndex].name);  // Replace with id of next level's view (or the view itself and the header title for the next level.

		};
	}
};

// This object implements the dataSource methods for the list.
var itemsListDataSource = {
	
	// Sample data for the content of the list. 
	// Your application may also fetch this data remotely via XMLHttpRequest.
	_rowData: ["Item 1", "Item 2", "Item 3"],
	
	// The List calls this method to find out how many rows should be in the list.
	numberOfRows: function() {
        if (typeof(restaurantRow) == 'undefined') {
            return this._rowData.length;
        } else {
            return restaurants[restaurantRow].items.length;
        }
	},
	
	// The List calls this method once for every row.
	prepareRow: function(rowElement, rowIndex, templateElements) {
		// templateElements contains references to all elements that have an id in the template row.
		// Ex: set the value of an element with id="label".
		if (templateElements.label1) {
            if (typeof(restaurantRow) == 'undefined') {
                templateElements.label1.innerText = this._rowData[rowIndex];
            } else {
                templateElements.label1.innerText = restaurants[restaurantRow].items[rowIndex].name;
            }
		}

		// Assign a click event handler for the row.
		rowElement.onclick = function(event) {
			// Do something interesting
            itemRow = rowIndex;
            document.getElementById('itemDetailList').object.reloadData();
            // Go forward in a Browser
var browser = document.getElementById('browser').object; // Replace with id of Browser
browser.goForward('itemDetailListView', restaurants[restaurantRow].items[rowIndex].name);  // Replace with id of next level's view (or the view itself and the header title for the next level.

		};
	}
};

// This object implements the dataSource methods for the list.
var itemDetailListDataSource = {
	
	// Sample data for the content of the list. 
	// Your application may also fetch this data remotely via XMLHttpRequest.
	_rowData: ["Item 1", "Item 2", "Item 3", "Item 4"],
	
	// The List calls this method to find out how many rows should be in the list.
	numberOfRows: function() {
		return 4;
	},
	
	// The List calls this method once for every row.
	prepareRow: function(rowElement, rowIndex, templateElements) {
		// templateElements contains references to all elements that have an id in the template row.
		// Ex: set the value of an element with id="label".
		if (templateElements.label2) {
            if (typeof(itemRow) == 'undefined') {
                switch(rowIndex) {
                    case 0:
                        templateElements.label2.innerText = "Calories: ";
                    break;
                    case 1:
                        templateElements.label2.innerText = "Fat: ";
                    break;
                    case 2:
                        templateElements.label2.innerText = "Carbs: ";
                    break;
                    case 3:
                        templateElements.label2.innerText = "Fiber: ";
                    break;
                }
            } else {
                switch(rowIndex) {
                    case 0:
                        templateElements.label2.innerText = "Calories: " + restaurants[restaurantRow].items[itemRow].calories;
                    break;
                    case 1:
                        templateElements.label2.innerText = "Fat: " + restaurants[restaurantRow].items[itemRow].fat;
                    break;
                    case 2:
                        templateElements.label2.innerText = "Carbs: " + restaurants[restaurantRow].items[itemRow].carbs;
                    break;
                    case 3:
                        templateElements.label2.innerText = "Fiber: " + restaurants[restaurantRow].items[itemRow].fiber;
                    break;
                }
            }
		}

		// Assign a click event handler for the row.
		rowElement.onclick = function(event) {
			// Do something interesting
		};
	}
};

