/* 
 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: createDateStr(date, includeYear)
// Creates a readable date string from the provided Date object.
//
// date: JavaScript Date object to format
//
function createDateStr(date, includeYear)
{
   if (!date) {
       return "??/??" + (includeYear?"/??":"");
   }

   var day = date.getDate();
   var month = (date.getMonth()) + 1; // add one since Date returns 0-11 month values
   var year = date.getFullYear();

   if (day == "" && month == "") {
       return "??/??" + (includeYear?"/??":"");
   }
   else {
       var dateStr = month + "/" + day + (includeYear?"/"+year:"");
       return dateStr;
   }
}

//
// Function: filterItemsWithMedia(podcastList)
// Sets up filters on the podcasts to only show items with media
//
// podcastList: name of bound list with podcasts
// 
function filterItemsWithMedia(podcastList)
{
    var list = dashcode.getDataSource(podcastList);
    
    // Sets up a filter predicate function on the list's data source
    list.setFilterPredicate(function(object){
        var media = object.valueForKeyPath('enclosure.$url');
        
        // Allow any item with media
        if (media)
            return true;
            
        return false;
    });
}

//
// Function: handleCommonErrors(dataSourceName)
// Handles common errors with a feed data source.  Will display an error in these cases.
//
// dataSourceName: name of the main feed data source
//
//
function handleCommonErrors(dataSourceName, errorHandler)
{
    var feedDataSource = dashcode.getDataSource(dataSourceName);
    
    // Is the data source not defined. Only happens if developer deletes the data source manually.
    if (!feedDataSource) {
        errorHandler("No Podcast data source specified");
        
        return false;
    }
    
    if (!feedDataSource.valueForKey('url')) {
        errorHandler("No Podcast specified\nProvide a Podcast URL in Application Attributes");
        
        return false;
    }
    
    // Watch the errorMessage property of the data source, if it changes, check the error
    // and display something.
    feedDataSource.addObserverForKeyPath({}, function(changeNotification, keyPath){
        var errorStatus = feedDataSource.valueForKey('statusCode');
        var errorMessage = feedDataSource.valueForKey('errorMessage');
        
        if (errorMessage) {
            if (errorStatus < 0) { 
                errorHandler("Feed not available: " + errorMessage);
            } else {
                errorHandler("Error status " + errorStatus);
            }
        }
    }, "errorMessage", null);

    // Check to make sure this is RSS or ATOM and that there is at least one article
    feedDataSource.addObserverForKeyPath({}, function(changeNotification, keyPath){
        var entries = feedDataSource.valueForKeyPath('content.channel.item');
        var foundCount = 0;
        
        if (entries && (DC.typeOf(entries) == 'array')) {
            foundCount = entries.length;
        } else {
            errorHandler(feedDataSource.fullURL() + ' does not appear to be a valid Podcast');
            return;
        }
        
        if (!foundCount) {
            errorHandler("Podcast not available.  The Podcast URL must be from the same source as your widget.");
        }
        
    }, "content", null);    
}

//
// Function: showError(errorString)
// Show an error in a red box
//
// errorString: string to be displayed
// divtoHide: element that needs to be hidden when error is shown.
function showError(errorString)
{
    var errorDiv = document.createElement("div");
    errorDiv.innerText = errorString;
    errorDiv.setAttribute("style", "position: absolute; border-style: solid; border-width: 1px; right: 10px; left: 10px; top: 120px; background-color: rgb(32, 32, 32); border-color: rgb(0, 0, 0); -webkit-border-radius: 10px; -moz-border-radius: 10px; text-align: center; padding: 15px; font-family: Helvetica; font-weight: bold; color: rgb(255, 255, 255); font-size: 15px; text-shadow: rgb(0, 0, 0) 0px -1px 0px;");
    
    document.getElementById("content").appendChild(errorDiv);
}

//
// Transformer: articleDateTransformer
// Parses the date from the podcast and prefixes with the prefix defined.
//
//  value: Date string from the podcast
//
//
articleDateTransformer = Class.create(DC.ValueTransformer,{
   constructor: function(prefix, includeYear){
       this.prefix = prefix;
       this.includeYear = includeYear;
   },
   transformedValue: function(value){
       var string = this.prefix + " ";
       var dateString = null;
       var date = new Date(Date.parse(value));

       if (date) {
           string += createDateStr(date, this.includeYear);
       } else {
           string = "N/A";
       }

		return string;
   }
});
