	
$(document).ready(function() {
	if ($('.liveticker').length) {
		refreshStatus = 'go';
		liveAutoUpdate.sport_go();
	} else {
		return false;
	}
})
	  
var liveAutoUpdate = {

	existingFirstItem: 0,
	 
	 sport_go: function() {
		
		$.ajax({url: "/assets/js/footyresults.js?type=json", cache: false, dataType: "json", success: function(data) {
		//$.ajax({url: "footyresults.js?type=json", cache: false, dataType: "json", success: function(data) {
				var htmlString = "";
	
					// Display default text until there is a value in the JSON
					if ($(results.livedata).length == 0) {
						return false;
					} else {
						$('#goalrushlist #0').hide()
					}

						for (var i = 0; i < 1; i++) {
								
								if (Number(results.livedata[i].id) > liveAutoUpdate.existingFirstItem) {
									//console.log(results.livedata[i].teamAffected)
									var teamHighlight = results.livedata[i].teamAffected
									switch(teamHighlight) {
									case results.livedata[i].homeTeam:
									  var teamScored = "homescore"
									  break;
									case results.livedata[i].awayTeam:
									  var teamScored = "awayscore"
									  break;
									default:
									  var teamScored = ""
									}

									var teamScore = "<li id='" + results.livedata[i].id + "' class='latestresult clearing "+teamScored+"'><div class='clearing' id='lscontent'><dl><dt class='when'><span>" + results.livedata[i].timeStamp + "</span></dt>"
									+ "<dd class='happened " + results.livedata[i].eventCode + "'>" + results.livedata[i].type + "</dd>"
									+ "<dd class='game'><strong class='team'>"
									+ "<span class='hometeam'>" + results.livedata[i].homeTeam + " " + results.livedata[i].homeScore + "</span> - <span class='awayteam'>" + results.livedata[i].awayScore + " " + results.livedata[i].awayTeam + "</span></strong>" 
									var hasScorer = ""
									if(results.livedata[i].player) {
										hasScorer = "<span class='scorer'>" + results.livedata[i].player + "</span>, <span class='time'>" + results.livedata[i].gameTime + " mins</span>"
									}
									var divisionLevel = "</dd><dd class='division'>" + results.livedata[i].division + "</dd></dl></div></li>"	
									if (hasScorer.length > 0) {
										var combinedOutput = teamScore + hasScorer + divisionLevel;
									} else {
										var combinedOutput = teamScore + divisionLevel;
									}
									htmlString += combinedOutput;
									
								} 
							}
					/* If there has been an addition to the JSON file, the htmlString will have a value */						
						if (htmlString.length != 0){
							liveAutoUpdate.existingFirstItem = Number($(results.livedata)[0].id); // Make sure the new first Items value is stored as this is used as a condition to enter the building list item loop
							if ($("#livescores ol li").length > 0) {
								$('#livescores ol').html(htmlString)
								$('#lscontent').children().hide().fadeIn(3000);
							}
						}		
									
			}});
			callback(); 
	 }
};


/* Auto refresh page every minute */
function callback(state) {
	if (refreshStatus == 'go') {
		//console.log('refresh')
		setTimeout("liveAutoUpdate.sport_go();", 60000); // Refresh every minute
	}
}	 	

