/**
 * Constants to identify the array of races and current race
 */
var raceArray = new Array();
var currentRace = -1;

/*
 *  Function:   yesterday
 *
 *  Purpose:    Returns yesterday's date
 *
 *  Parameters: y - the year i.e. 2008
 *              m - the month i.e. 03 = March
 *              d - the day of the month i.e 30
 *            
 *  Returns:    The day before the given date i.e. 20080329
 */
function yesterday(y, m, d) {
  
  var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

  dim[1]=(((y%100!=0)&&(y%4==0))||(y%400==0))?29:28;

  if(m == 1 && d == 1) {
    d=31;
    m=12;
    y--;
  } else if(d == 1) {
    d=dim[m-2];
    m--;
  } else {
    d--;
  }

  if(m < 10) {
    m = "0" + m;
  }

  if(d < 10) {
    d = "0" + d;
  }

  return y + "" + m + "" + d;
}

/*
 *  Function:   getRaceDetails
 *
 *  Purpose:    Populates the runners and riders in the given div
 *
 *  Parameters: raceId - the id of the race that you want the riders for
 *              divName - the div name to populate
 *              init - whether this is the first time that the method has been called
 *            
 *  Returns:    Nothing
 */
function getRaceDetails(raceId, divName, init) {

  if(currentRace != raceId || init) {

    var race = raceArray[raceId];
    
    var riderString = "<table class='runnerTable' cellpadding='0' cellspacing='0'>";

    for(i=0;i<raceArray[raceId].length;i++) {
      riderString += "<tr class='runnerRow'><td class='silk'><img class='silkImg' src='" + race[i].silk + "'></img></td>";
      riderString += "<td class='horseAndJockey'>" + race[i].horsename + "<br>" + race[i].jockey + "</td></tr>";  
    }

    riderString += "</table>";

    $("div[class='runnersSection']").hide();

    $("#" + divName).show().html(riderString);

    currentRace = raceId;

  } else {
    currentRace = -1;
    $("div[class='runnersSection']").hide();
  }

  return;
}

/*
 *  Function:   initRaceDetails
 *
 *  Purpose:    Uses Ajax to populate all the runners and riders of todays races
 *
 *  Parameters: firstRaceId - the id of the first race of the day
 *              y - the year i.e. 2008
 *              m - the month i.e. 03 = March
 *              d - the day of the month i.e 30#
 *              divName - the div name to populate
 *            
 *  Returns:    Nothing
 */
function initRaceDetails(firstRaceId, y, m, d, divName) {
  
  $.ajax({
    url: "/dynamicxml/racing/rcp_stats/Declarations_" + yesterday(y, m, d) + "_Overnight.xml",
    type: 'GET',
    dataType: 'xml',
    cache: false,
    success: function(msg){

      var count = 0;
      var raceId = 0;
      
      $("Race", msg).each(function() {
        count = 0;
        raceId = $(this).attr('uid');
        raceArray[raceId] = new Array();
        $("Runner", this).each(function() {
          var silk = $(this).attr('silkname');
          var silkImage = "http://www.racingpost.co.uk/images/silks/";
          silkImage += silk.substring(silk.length-5, silk.length-4) + "/";
          silkImage += silk.substring(silk.length-6, silk.length-5) + "/";
          silkImage += silk.substring(silk.length-7, silk.length-6) + "/" + silk;

          raceArray[raceId][count] = new Object();
          raceArray[raceId][count].horsename = $(this).attr('horsename');
          raceArray[raceId][count].jockey = $(this).attr('jockey')
          raceArray[raceId][count].silk = silkImage;

          count++;
        });

      });

      getRaceDetails(firstRaceId, divName, true);
    }     
  });

  return;
}