/*
 © Copyright (c) 2006-2008 Apple Inc.  All rights reserved.
 
 IMPORTANT:  This Apple software ("Apple Software") is supplied to you in consideration of your agreement to the following terms. Your use, installation and/or redistribution of this Apple Software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, or redistribute this Apple Software.
 
 Provided you comply with all of the following terms, Apple grants you a personal, non-exclusive license, under Apple’s copyrights in the Apple Software, to use, reproduce, and redistribute the Apple Software for the sole purpose of creating Dashboard widgets for Mac OS X. If you redistribute the Apple Software, you must retain this entire notice in all such redistributions.
 
 You may not use the name, trademarks, service marks or logos of Apple to endorse or promote products that include the Apple Software without the prior written permission of Apple. Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your products that incorporate the Apple Software or by other works in which the Apple Software may be incorporated.
 
 The Apple Software is provided on an "AS IS" basis.  APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
 
 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// Note: Properties and methods beginning with underbar ("_") are considered private and subject to change in future Dashcode releases.

function CreateList(elementOrID, spec)
{
    var listElement = elementOrID;
    if (elementOrID.nodeType != Node.ELEMENT_NODE) {
        listElement = document.getElementById(elementOrID);
    }

	if (listElement && !listElement.loaded) {
		listElement.loaded = true;
		listElement.object = new List(listElement, spec);
		
		return listElement.object;
	}
}

function List(element, spec)
{
	this.element = element;
    
    try {
        this._listStyle = eval(spec.listStyle);
    } catch (e) {
        this._listStyle = this.EDGE_TO_EDGE;
    }
    this._labelElementId = spec.labelElementId || 'label';
    var dataArray = spec.dataArray || null;
    var dataSourceName = spec.dataSourceName || null;
    this._useDataSource = spec.useDataSource || false;
    this._sampleRows = spec.sampleRows || 0;
        
    // Find the row template.
    for (var child = element.firstChild; child != null; child = child.nextSibling) {
        if (child.nodeType == Node.ELEMENT_NODE) {
            this.templateRowElement = child;
            break;
        }
    }

    // determine where to get the data from
    if (this._useDataSource) {
        var dataSource = window[dataSourceName];
        if (dataSource) {
            this.dataSource = dataSource;
        }
    } else if (dataArray) {
        if (dataArray.indexOf("[") == 0) dataArray = eval(dataArray);
        else dataArray = window[dataArray];
        if (dataArray) {
            this.dataArray = dataArray;
        }
    }
}

// Constants for the list types
List.EDGE_TO_EDGE = 1;
List.ROUNDED_RECTANGLE = 2;

//
// Set the data source object that will insert the data and format each row for dynamic lists.
// dataSource must implement two required callback methods: numberOfRows() and prepareRow(rowElement, rowIndex, templateElements).
// dataSource.numberOfRows() is called while the list is initializing and it should return the total number of rows in the list.
// dataSource.prepareRow() is called once for each row. It is passed 3 parameters:
//     rowElement: The HTML DOM element that represents the row being prepared. Typically an <li> element.
//     rowIndex: The position of the row in the list, starting at 0.
//     templateElements: A JavaScript object that contains a reference to each cloned element that had an ID in the template row. For example, if the template row had an element with id="label", the cloned element can be obtained using templateElements.label. These elements are descendants of rowElement.
//
List.prototype.setDataSource = function(dataSource)
{
    this._useDataSource = true;
    this.dataSource = dataSource;
    this.reloadData();
}

//
// Set the data array to use in static lists.
// dataArray should be an Array of HTML strings and a row will be created for each item. The HTML string will be inserted as the innerHTML of the element with the ID specified in the part spec as labelElementId.
//
List.prototype.setDataArray = function(dataArray)
{
    this._useDataSource = false;
    this.dataArray = dataArray;
    this.reloadData();
}

//
// This function is called by setupParts after all the parts have been set up.  
// We do not initially load our data until all setup is complete.
//
List.prototype.finishLoading = function()
{
    this.reloadData();
}

//
// Reload the data from the data source (for dynamic lists) or from the data array (for static lists).
// The list is regenerated by removing all the rows and cloning the template row. Parts inside each cloned row are also initialized.
//
List.prototype.reloadData = function()
{
    var templateRow = this.templateRowElement;
	if (!templateRow) return;
    var useTemplateAsRow = dashcode.inDesign;
    
    if (!useTemplateAsRow) {
        templateRow.style.visibility = "hidden";
        templateRow.style.display = "";
    }

    // remove all children except for the template row
	var list = this.element;
    var child = list.firstChild;
    while (child) {
        var nextChild = child.nextSibling;
        if (child != templateRow) {
            list.removeChild(child);
        }
        child = nextChild;
    }

    var cloneAndStyleRow = function(rowIndex, numRows, listStyle) {
        // clone the template DOM node
        var newRowElement = dashcode.cloneTemplateElement(templateRow, useTemplateAsRow && rowIndex == 0);
        // style the new row
        if (newRowElement.style.visibility == "hidden") {
            newRowElement.style.visibility = "visible";
        }
        if (listStyle == List.ROUNDED_RECTANGLE) {
            newRowElement.style.borderTopWidth = rowIndex > 0 ? "1px" : "0px";
        }
        return newRowElement;
    }

    var insertRow = function(rowElement, rowIndex, value) {
        // save index and value in row's object
        if (rowElement != templateRow && newRowElement.object) {
            rowElement.object.index = rowIndex;
            rowElement.object.value = value;
        }        
        // insert into the DOM
        if (!useTemplateAsRow) {
            list.insertBefore(rowElement, templateRow);
        } else {
            list.appendChild(rowElement);
        }
    }
    
    var dataSource = this.dataSource;
    var dataArray = this.dataArray;
    if (this._useDataSource) {
        // using dynamic data from a datasource
        var numRows = dashcode.inDesign ? this._sampleRows : 0;        
        if (dataSource && dataSource.numberOfRows) {
            numRows = dataSource.numberOfRows();
        }
        for (var rowIndex=0; rowIndex<numRows; rowIndex++) {
            var newRowElement = cloneAndStyleRow(rowIndex, numRows, this._listStyle);
            // hand the new row to the data source to process it and insert the data
            if (dataSource && dataSource.prepareRow) {
                dataSource.prepareRow(newRowElement, rowIndex, newRowElement.object.templateElements);
            }
            insertRow(newRowElement, rowIndex);
        }
    } else if(dataArray) {
        // using static data from a data array
        var numRows = this.dataArray.length;
        for (var rowIndex=0; rowIndex<numRows; rowIndex++) {
            var newRowElement = cloneAndStyleRow(rowIndex, numRows, this._listStyle);
            // process the new row to insert the data
            var templateElements = newRowElement.object.templateElements;
            var labelElement = null;
            for (var key in templateElements) {
                // try to find the label element
                if (this._labelElementId && this._labelElementId.length > 0 && key.indexOf(this._labelElementId) == 0) { 
                    labelElement = templateElements[key];
                    break;
                }
            }
            // get the text and value
            var itemLabel = '';
            var itemValue = '';
            if ((dataArray[rowIndex]) instanceof Array) {
                if (dataArray[rowIndex].length > 0) {
                    itemLabel = dataArray[rowIndex][0];
                    if (dataArray[rowIndex].length > 1) {
                        itemValue = dataArray[rowIndex][1];
                    }
                }
            }
            else {
                itemLabel = dataArray[rowIndex];
                itemValue = itemLabel;
            }
            // assign the label and insert it
            if (labelElement) {
                labelElement.innerHTML = itemLabel;
            }
            insertRow(newRowElement, rowIndex, itemValue);
        }
    }
    
    if (!useTemplateAsRow) {
        templateRow.style.display = "none";
    }
}

List.prototype._setDataArrayStr = function(dataString)
{
	if (dataString.indexOf("[") == 0) this.dataArray = eval(dataString);
	this.reloadData();
}

List.prototype._setSampleRows = function(numberOfRows)
{
    this._sampleRows = numberOfRows;
    this.reloadData();
}

List.prototype._setUseDataSource = function(useDataSource)
{
    this._useDataSource = useDataSource;
    this.reloadData();
}

List.prototype._setLabelElementId = function(newId)
{
    this._labelElementId = newId;
    this.reloadData();
}
