function genA(page, txt) { return '<a href="http://wwos.ninemsn.com.au/cricket/' + page + '">' + txt + '</a>'; }
function genA2(page, txt) { return '<a href="http://wwos.ninemsn.com.au/cricket/' + page + '" style="color:white; text-decoration:none;">' + txt + '</a>'; }
function gen(dvd, befTtl, btwBannerLive, btwBannerLatest, btwTtlTtr, btwTtrDat, btwDatVnu, btwVnuRst, aftRst, mth, hme, awy, stt, cmt) {
    $.getJSON('http://data.ninemsn.com.au/Services/Service.axd?ServiceName=Cricket&ServiceFormat=JSON&callback=?&ServiceAction=GetMatchListLive&Params=%3FseriesId=', null, function(data) {
        var btwBannerRender;
        var live = -1;
        var latest = -1;
        var upcoming = -1;
        var mths = data.MatchList.Match, div = $('#cricketLiveMatch');
        div.empty();
for (var i = mths.length - 1; i >= 0; i--) {
            mth = mths[i], stt = mth.Result.status;
            cmt = mth.hasCommentary == 'true'; awy = mth.AwayTeam.value; hme = mth.HomeTeam.value;
            if (hme == 'AUS' || awy == 'AUS') {
                if (stt == 'L' || stt == 'P') {
                    if (live == -1) {
                        live = i;
                    }
                } else if (stt == 'F') {
                    if (latest == -1) {
                        latest = i;
                    }
                }
            }
        }
        if (live > -1) {
            mth = mths[live]
            btwBannerRender = btwBannerLive
        } else if (latest > -1) {
            mth = mths[latest]
            btwBannerRender = btwBannerLatest
        };
        var date = mth.DateTime.substring(0, mth.DateTime.indexOf('T')).split('-');
        div.html(
        btwBannerRender +
        befTtl +
        genA2((cmt ? 'live_comment.aspx' : 'scorecard.aspx') + '?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + cmt.toString(), mth.HomeTeam.fullName + ' v ' + mth.AwayTeam.fullName + ' - ' + mth.matchName) +
        btwTtlTtr +
        (cmt ? genA('live_comment.aspx?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + (cmt ? 'true' : 'false'), 'Live Commentary') + dvd : '') +
        genA('scorecard.aspx?matchid=' + mth.matchId + '&seriesid=' + mth.seriesId + '&livematch=true&comment=' + (cmt ? 'true' : 'false') + '&livematch=true', 'Scorecard') + dvd +
        (cmt ? genA('matchlet.aspx?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + (cmt ? 'true' : 'false'), 'Matchlet') + dvd : '') +
        (cmt ? genA('crystal-ball.aspx?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + (cmt ? 'true' : 'false'), 'Crystal Ball') + dvd : '') +
        (cmt ? genA('looking-glass.aspx?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + (cmt ? 'true' : 'false'), 'Looking Glass') + dvd : '') +
        genA('flash-scoreboard.aspx?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + (cmt ? 'true' : 'false'), 'Flash Scoreboard') + dvd +
        genA('seriesstats.aspx?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + (cmt ? 'true' : 'false'), 'Series Stats') +
        btwTtrDat + date[2] + '/' + date[1] + '/' + date[0] + btwDatVnu + mth.Venue + btwVnuRst + mth.Result.value + aftRst);

        setTimeout('gen(\'' + dvd + '\',\'' + befTtl + '\',\'' + btwTtlTtr + '\',\'' + btwTtrDat + '\',\'' + btwDatVnu + '\',\'' + btwVnuRst + '\',\'' + aftRst + '\',\'' + mth + '\',\'' + hme + '\',\'' + awy + '\',\'' + stt + '\',' + cmt + ')', 60000);
    });
}
jQuery.fn.genCricketLiveMatch = function() {
    var dvd = '<span class="cricket_divider">|</span>',
  befTtl =
    '<style type="text/css">' +
      '.cricket_divider{' +
        'color: #D1D1D1;' +
        'margin:0 6px;' +
      '}' +
      '#cricket_tertiary{' +
        'background: #FFFFFF url(http://wwos.ninemsn.com.au/img/cricket/v2/livescores_tertiary_bg.gif) repeat-x scroll left top;' +
        'border-bottom: 1px solid #478AB5;' +
        'color: #478AB5;' +
        'line-height: 25px;' +
        'max-width: 100%;' +
        'text-align: center;' +
        'margin-left: 0px;' +
        'margin-right: 0px;' +
      '}' +
      '#cricket_tertiary a{' +
        'color: #478AB5;' +
        'font-family: Arial;' +
        'font-size: 12px;' +
        'font-size-adjust: none;' +
        'font-stretch: normal;' +
        'font-style: normal;' +
        'font-variant: normal;' +
        'line-height: normal;' +
        'text-decoration: none;' +
      '}' +
    '</style>',
  btwBannerLive =
    '<div style="background: #2a4d66;height: 26px">' +
      '<div style="background: #2a4d66 url(http://wwos.ninemsn.com.au/img/live_score.gif) no-repeat scroll left top;width: 111px; height: 24px; float: left; margin-top: 2px;">' +
      '</div>' +
      '<span style="font-family: Arial,Helvetica,sans-serif; color: #FFFFFF; font-size: 12px; font-weight: bold; left: 4px; position: relative; top: 5px;">',
  btwBannerLatest =
    '<div style="background: #2a4d66;height: 26px">' +
      '<div style="background: #2a4d66 url(http://wwos.ninemsn.com.au/img/latest.gif) no-repeat scroll left top;width: 111px; height: 24px; float: left;">' +
      '</div>' +
      '<span style="font-family: Arial,Helvetica,sans-serif; color: #FFFFFF; font-size: 12px; font-weight: bold; left: 4px; position: relative; top: 5px;">',
  btwTtlTtr =
      '</span>' +
    '</div>' +
    '<div id="cricket_tertiary">',
  btwTtrDat =
    '</div>' +
    '<div style="padding: 10px 0px 7px 145px; color: #666666;font-size: 12px">' +
      '<div id="live_match_played_date" style="float: left">',
  btwDatVnu =
      '</div>' +
      '<div>' +
        '<span style="font-weight: normal; padding-left: 14px">Played at: </span>',
  btwVnuRst =
      '</div>' +
      '<div style="font-weight: bold">' +
        '<span style="font-weight: normal">Status: </span>',
  aftRst =
      '</div>' +
    '</div>',
  mth, hme, awy, stt, cmt;
    gen(dvd, befTtl, btwBannerLive, btwBannerLatest, btwTtlTtr, btwTtrDat, btwDatVnu, btwVnuRst, aftRst, mth, hme, awy, stt, cmt);
}




