/* 
 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.
 */

var listController = {
    // This object acts as a controller for the list UI.
    // It implements the dataSource methods for the list.
    
    numberOfRows: function() {
        // The List calls this dataSource method to find out how many rows should be in the list.
        return mainActivities.length;
    },
    
    prepareRow: function(rowElement, rowIndex, templateElements) {
        // The List calls this dataSource method for every row.  templateElements contains references to all elements inside the template that have an id. We use it to fill in the text of the rowTitle element.
        if (templateElements.rowTitle) {
            templateElements.rowTitle.innerText = mainActivities[rowIndex].name;
        }

        // We also assign an onclick handler that will cause the browser to go to the next level.
        var self = this;
        var handler = function() {
            var activity = mainActivities[rowIndex];
        //    detailController.setActivity(activity);
            var browser = document.getElementById('browser').object;
            // The Browser's goForward method is used to make the browser push down to a new level.  Going back to previous levels is handled automatically.
            browser.goForward(document.getElementById(activity.subItemID), activity.name);
        };
        rowElement.onclick = handler;
    }
};

var listX10Controller = {
    // This object acts as a controller for the list UI.
    // It implements the dataSource methods for the list.
    
    numberOfRows: function() {
        // The List calls this dataSource method to find out how many rows should be in the list.
        return devicesX10.length;
    },
    
    prepareRow: function(rowElement, rowIndex, templateElements) {
        // The List calls this dataSource method for every row.  templateElements contains references to all elements inside the template that have an id. We use it to fill in the text of the rowTitle element.
        if (templateElements.rowTitleX10) {
            templateElements.rowTitleX10.innerText = devicesX10[rowIndex].name;
        }

        // We also assign an onclick handler that will cause the browser to go to the detail page.
        var self = this;
        var handlerX10 = function() {
            var device = devicesX10[rowIndex];
            detailControllerX10.setActivity(device);
            var browser = document.getElementById('browser').object;
            // The Browser's goForward method is used to make the browser push down to a new level.  Going back to previous levels is handled automatically.
            browser.goForward(document.getElementById('detailLevelX10'), device.name);
        };
        rowElement.onclick = handlerX10;
    }
};

var detailControllerX10 = {
    // This object acts as a controller for the detail UI.
    
    setActivity: function(device) {
        this._device = device;
        this._representedObject = device.name;
        
        // When the device is set, this controller also updates the DOM for the detail page appropriately.  As you customize the design for the detail page, you will want to extend this code to make sure that the correct information is populated into the detail UI.
        var detailTitle = document.getElementById('detailTitle');
        detailTitleX10.innerHTML = this._device.name;
        var detailDescriptionX10 = document.getElementById('detailDescriptionX10');
        detailDescriptionX10.innerHTML = this._device.description;

        // Set the Handler for the On and Off buttons
        buttonOnX10.m_Subject = "X10%20ON%20" + this._device.name;
        buttonOnX10.m_Address = "control@platkus.com";
        buttonOnX10.onclick = sendEmail;

        buttonOffX10.m_Subject = "X10%20OFF%20" + this._device.name;
        buttonOffX10.m_Address = "control@platkus.com";
        buttonOffX10.onclick = sendEmail;

    }
};

// This object implements the dataSource methods for the list.
var listLevelFavsController = {
	
	// 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() {
		return this._rowData.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 = this._rowData[rowIndex];
		}

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


// This object implements the dataSource methods for the list.
var listLevelXControlController = {
    // This object acts as a controller for the list UI.
    // It implements the dataSource methods for the list.
    
    numberOfRows: function() {
        // The List calls this dataSource method to find out how many rows should be in the list.
        return commandsXControl.length;
    },
    
    prepareRow: function(rowElement, rowIndex, templateElements) {
        // The List calls this dataSource method for every row.  templateElements contains references to all elements inside the template that have an id. We use it to fill in the text of the rowTitle element.
        if (templateElements.rowTitleXControl) {
            templateElements.rowTitleXControl.innerText = commandsXControl[rowIndex].name;
        }

        // We also assign an onclick handler that will cause the browser to go to the detail page.
        var self = this;
        var handlerXControl = function() {
            var commands = commandsXControl[rowIndex];
            detailControllerX10.setActivity(commands);
            var browser = document.getElementById('browser').object;
            // The Browser's goForward method is used to make the browser push down to a new level.  Going back to previous levels is handled automatically.
            browser.goForward(document.getElementById('detailLevelX10'), commands.name);
        };
        rowElement.onclick = handlerXControl;
    }
};


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

// The List data.
var mainActivities = [
    { name: "X10 Remote", subItemID: "listLevelX10" },
    { name: "X Control", subItemID: "listLevelXControl" },
    { name: "Favorite Contacts", subItemID: "listLevelFavs" },
    { name: "---", subItemID: "listLevelX10" },
    { name: "---", subItemID: "listLevelX10" },
    { name: "---", subItemID: "listLevelX10" },
    { name: "---", subItemID: "listLevelX10" },
    { name: "---", subItemID: "listLevelX10" }
];

var devicesX10 = [
    { name: "P1", description: "Controller Computer" },
    { name: "P2", description: "Computer Room Lamp" },
    { name: "P3", description: "Bed Room Lamp" },
    { name: "P4", description: "Family Room Lamp" },
    { name: "P5", description: "Garage Lights" },
    { name: "P6", description: "Front Door Lights" },
    { name: "P7", description: "Unused" },
    { name: "P8", description: "Basement Lights" },
    { name: "P9", description: "Computer Room Motion" },
    { name: "P10", description: "Computer Room Ambient" },
    { name: "P11", description: "Hallway Motion" },
    { name: "P12", description: "Angel Light" },
    { name: "P13", description: "Dining Room Motion" },
    { name: "P14", description: "Unused" },
    { name: "P15", description: "Driveway Motion" },
    { name: "P16", description: "Dawn / Dusk Detector" }
];

var commandsXControl = [
    { name: "Power Commands", description: "Power" },
    { name: "FTP Commands", description: "FTP" },
    { name: "Placeholder", description: "Test" }
];
