'use strict';
/**
 * Created by chregi on 2/28/15.
 */




 // parse a date in yyyy-mm-dd format
function parseDate(input) {
  var parts = input.match(/(\d+)/g);
  // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
  return new Date(parts[0], parts[1]-1, parts[2], parts[3], parts[4]); // months are 0-based
}



var getSchedule = function() {

	var id = jQuery('#srs-schedule').attr('data-teamid');

	jQuery.ajax({
	    url: "http://api.swissrugbystats.ch/teams/"+id+"/games.json",
	    success: function(data){
            
            /* build schedule table
             * TODO: 
             * - set links
            */
            var tableStr = "";
            tableStr = "<table class='srs-scheduletable'><thead><tr><th>Datum</th><th>Host</th><th>Guest</th><th>Kickoff / Result</th></tr></thead><tbody>";
            for (var i = 0; i < data.length; i++) {
                // parse the date
                var d = parseDate(data[i].date);
                var dStr = ("0" + d.getDate()).slice(-2) +"."+ ("0"+(d.getMonth()+1)).slice(-2) +"."+ d.getFullYear();

                // game result
                if ((data[i].host.score != null) && (data[i].guest.score != null)) {
                    var result = data[i].host.score + " : " + data[i].guest.score;
                } else {
                    var result = d.getHours() + ":"+ ("0" + d.getMinutes()).slice(-2) + " Uhr";
                }

                tableStr += "<tr><td>" + dStr + "</td><td>" + data[i].host.team.name + "</td><td>" + data[i].guest.team.name + "</td><td>" + result + "</td></tr>";
            }
            
            tableStr += "</tbody></table>";
            jQuery("#srs-schedule").html(tableStr);

            jQuery("#srs-schedule").append("<br/><p>All game data provided by <a href='http://swissrugbystats.ch' target='_blank'>Swiss Rugby Stats</a>");
	    }
	});

}

var getNextGame = function() {

	var id = jQuery('#srs-nextGame').attr('data-teamid');

	jQuery.ajax({
	    url: "http://api.swissrugbystats.ch/nextGameByTeamId/"+id+"/.json",
	    success: function(data){
	      jQuery('#srs-nextGame').html("Host: " + data.host.team.name + "<br>Guest: " + data.guest.team.name);
	    }
	});
}

/* execute as soon as DOM is ready
 * TODO: check for existing tags first to reduce ajax calls to minimum
 */
window.onload = function() {

	getSchedule();
	getNextGame();

};
@@ -358,32 +358,35 @@
 ion(
+id
 ) %7B%0A
-%0A%09var id = jQuery('#
+    var selector = '.
 srs-
@@ -393,25 +393,17 @@
 schedule
-').attr('
+%5B
 data-tea
@@ -397,34 +397,41 @@
 dule%5Bdata-teamid
+='+id+'%5D
 '
-)
 ;%0A%0A%09jQuery.ajax(
@@ -1678,31 +1678,24 @@
  jQuery(
-%22#srs-schedule%22
+selector
 ).html(t
@@ -1728,23 +1728,16 @@
 ery(
-%22#srs-schedule%22
+selector
 ).ap
@@ -1865,16 +1865,246 @@
  %7D%0A%09%7D);%0A
+%7D%0A%0Avar getSchedules = function() %7B%0A    %0A    var scheduleTags = jQuery('.srs-schedule');%0A    jQuery('.srs-schedule').each(function( index ) %7B%0A        var id = jQuery( this ).attr('data-teamid');%0A        getSchedule(id);%0A    %7D);    
 %0A%7D%0A%0Avar 
@@ -2580,16 +2580,17 @@
 Schedule
+s
 ();%0A%09get

support multiple game schedules