/* 
 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 topStories = parseInt(attributes.topStories);       // The number of stories featured in the top section

//
// Function: load()
// Called by HTML body element's onload event when the web application is ready to start
//
function load()
{
    rssLoad();
    dashcode.setupParts();
    
    var todaysDate = document.getElementById("todaysDate");
    todaysDate.innerText = createDateStr(new Date()).toUpperCase();
    
}


// This object implements the dataSource methods for the list.
var headlineList = {
    
    // The List calls this method to find out how many rows should be in the list.
numberOfRows: function() {
    if (feedResults == null)
        return 0;           // Return 0 if there are no results to load 
    else
        if (feedResults.length > topStories)
            return topStories;          // Else return the number of headlined items (default: 3)
        else
            return feedResults.length;
},
    
    // The List calls this method once for every row.
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.headlineTitle) {
        templateElements.headlineTitle.innerText = extractText(feedResults[rowIndex].title);
    }
    
    if (templateElements.headlineDescription) {
        templateElements.headlineDescription.innerText = extractText(feedResults[rowIndex].description);
    }
    
    // We also assign an onclick handler that will cause the browser to go to the detail page.
    var self = this;
    var handler = function() {
        detailController.setRepresentedObject(rowIndex);
        document.getElementById("StackLayout").object.setCurrentView("articlePage", false, true);
    };
    rowElement.onclick = handler;
}
};


// This object implements the dataSource methods for the list.
var secondHeadlineList = {
    
    // The List calls this method to find out how many rows should be in the list.
numberOfRows: function() {
    if (feedResults == null)
        return 0;                       // Return 0 if there are no results to load 
    else
        if (feedResults.length > topStories)
            return (feedResults.length-topStories);         // Else return the number of headlined items (default: 3)
        else
            return 0;
},
    
    // 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".
    var tempRow = rowIndex + topStories;
    if (templateElements.secondHeadlineTitle) {
        templateElements.secondHeadlineTitle.innerText = extractText(feedResults[tempRow].title);
    }
    
    // We also assign an onclick handler that will cause the browser to go to the detail page.
    var self = this;
    var handler = function() {
        detailController.setRepresentedObject(tempRow);
        document.getElementById("StackLayout").object.setCurrentView("articlePage", false, true);
    };
    rowElement.onclick = handler;
}
};

var detailController = {
    // This object acts as a controller for the detail UI.
    
setRepresentedObject: function(representedObject) {
    // To start, the represented object of our detail controller is simply a string, the title of the list row that the user chose.  You may want to make the represented object any kind of object when you customize the template.
    this._representedObject = representedObject;
    
    // When the represented object 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 title = document.getElementById('articleTitle');
    title.innerText = extractText( feedResults[this._representedObject].title );
    var article = document.getElementById('articleDescription');
    article.innerHTML = extractHTML( feedResults[this._representedObject].description );
    var date = document.getElementById('articleDate');
    date.innerText = "Published on " + createDateStr(feedResults[this._representedObject].date);
    
    var self = this;
    var moreLink = document.getElementById('readMore');
    moreLink.object.onclick = function() {
        window.location = feedResults[self._representedObject].link;
    };
    var backLink = document.getElementById('backToHeadlines');
    backLink.object.onclick = function() {
        document.getElementById("StackLayout").object.setCurrentView("frontPage", true);
    };
}
};




