// processFeeds.js
// ===============

var fieldMap = {};

function ajaxGetNewFeed(path) 
{
   var buster = (function(){
	var unixTS = Math.round((new Date()).getTime()/1000);
	return(10 * Math.floor(unixTS/10));
   })();

   var jsonPath = 'http://data.sportsillustrated.cnn.com/jsonp'+path+'scoreboard.json';
      $.jsonp({
        url: jsonPath,
        callbackParameter: 'callback',
        callback:'callbackWrapper',
	cache:true,
	data: (!_ISWEBKIT)? "" :{ts:buster},
        success: function(data, msg) { 
            sendGamesToController(data.scoreboard, true);
        },
        error: function(data, msg) {
        }
    });
}

function stripInvalidRows(feedDataArray)
{
   var feedRow;
    var newArray = new Array();

    ////log.write("The feedDataArray has a length of " + feedDataArray.length + " (includes MAP rows and Data rows)");

    for (i = 0; i < feedDataArray.length; i++)
    {
        feedRow = trimString(feedDataArray[i]);

        if (feedRow.indexOf("|") > -1) 
        {
            newArray[newArray.length] = feedRow;
        }
    }
    return newArray;
}


function processMappingRows(feedDataArray)
{
    var feedRow;
    var indexStartOfGameData = 0;        // important to initialize

    ////log.write("The feedDataArray has a length of " + feedDataArray.length + " (includes MAP rows and Data rows)");
        
    for (i = 0; i < feedDataArray.length; i++)
    {
        feedRow = trimString(feedDataArray[i]);

        if (indexStartOfGameData == 0)
            indexStartOfGameData = i;

        if (feedRow.length == 0) 
        {            
            ////log.write("[processing feedDataArray] Removing extra row of empty data at position " + i);
            feedDataArray.splice(i);
        }
    }

    ////log.write("[processing feedDataArray] Game data started at row " + indexStartOfGameData);

    if (isValidfieldMap() == false) 
    {
        // Doing nothing with this Failed validation right now.   This validation result is not important
        // (it's a problem with associative arrays)
        
        ////log.write("Warning [very low severity]: the Master Feed File did not have valid MAP entries that described the data fields");
    }        
    // return the array index of the first row where the game data started
    return indexStartOfGameData;
}

function isValidfieldMap() {

    var fieldPosition;
    var passValidation = true;
    var key;

    for(key in fieldMap) {
    
        // the map contains array positions, so we use that variable name
        fieldPosition = fieldMap[key];

        // //log.write("inside first loop   " + key + "---->" + fieldMap[key]);


        if (fieldPosition == 'undefined') {
            ////log.write("*** undefined map entry found");
            passValidation = false;
            break;            
        }

        // Each value in the map was already converted to a number (attempted conversion)
        // before it was added to the map.   We test the result of the conversion here.
        //
        // *************** use utility function **************************************
        
        if (isNaN(fieldPosition) == true) {
            ////log.write("*** isNaN map entry found");
            passValidation = false;
            break;            
        }
        
        // --------------------------------------------------------------------------------
        // OPTIONAL - Can add a test to verify that there is only one ZERO value in the map,
        //                or any other test.
    }
    
    
    ////log.write("---------------------------");
    ////log.write("dump of all properties in the fieldMap object:");
    
    for(key in fieldMap) {
      ////log.write(key + "---->" + fieldMap[key]);
    }
    
    ////log.write("isValidfieldMap() Validation function returning " + passValidation);
    
    return passValidation;
}
    

function lookupFieldPosition(fieldIdentifier) {

    return fieldMap[fieldIdentifier];
}
 

function processMetaData(metaStr)
{
    var metaArray;
    metaArray = metaStr.split(TOP_LEVEL_DELIMITER);
    var newTimestamp = metaArray[TIMESTAMP];
    var newInterval = metaArray[SETINTERVAL];

    if (parseInt(newInterval) != parseInt(INTERVAL))
    {
        setupPollingTimer(newInterval);
        INTERVAL = newInterval;
    }

    if (parseInt(newTimestamp) > parseInt(currTimestamp))
    {
        currTimestamp = newTimestamp;
        return true;
    }
    else
        return false;
}


