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 = null;
		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
		};

		if (latest >= 0 || live >= 0) {
			var url = [];
			var i = 0;
			var date = mth.DateTime.substring(0, mth.DateTime.indexOf('T')).split('-');

			url[i++] = (btwBannerRender ? btwBannerRender : '') + befTtl;
			url[i++] = 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);
			url[i++] = btwTtlTtr;
			url[i++] = (cmt ? genA('live_comment.aspx?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + (cmt ? 'true' : 'false'), 'Live Commentary') + dvd : '');
			url[i++] = genA('scorecard.aspx?matchid=' + mth.matchId + '&seriesid=' + mth.seriesId + '&livematch=true&comment=' + (cmt ? 'true' : 'false') + '&livematch=true', 'Scorecard');
			url[i++] = dvd;
			url[i++] = (cmt ? genA('matchlet.aspx?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + (cmt ? 'true' : 'false'), 'Matchlet') + dvd : '');
			url[i++] = (cmt ? genA('crystal-ball.aspx?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + (cmt ? 'true' : 'false'), 'Crystal Ball') + dvd : '');
			url[i++] = (cmt ? genA('looking-glass.aspx?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + (cmt ? 'true' : 'false'), 'Looking Glass') + dvd : '');
			url[i++] = genA('flash-scoreboard.aspx?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + (cmt ? 'true' : 'false'), 'Flash Scoreboard');
			url[i++] = dvd;
			url[i++] = genA('seriesstats.aspx?seriesid=' + mth.seriesId + '&matchid=' + mth.matchId + '&livematch=true&comment=' + (cmt ? 'true' : 'false'), 'Series Stats');
			url[i++] = btwTtrDat;
			url[i++] = date[2] + '/' + date[1] + '/' + date[0];
			url[i++] = btwDatVnu + mth.Venue + btwVnuRst + mth.Result.value + aftRst;

			div.html(url.join(''));
		}

		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);
}

GetDateObjFromServiceDate = function(serviceDate)
{
	//2010-07-20T17:57:01.0000000
	var year = serviceDate.substr(0,4);
	var month = serviceDate.substr(5,2);
	if(month.substr(0,1) == '0')
	{
		month = parseInt(month.substr(1,1)); 
	}
	
	var day = serviceDate.substr(8,2);
	if(day.substr(0,1) == '0')
	{
		day = parseInt(day.substr(1,1));
	} 
	var hours = serviceDate.substr(11,2);
	if(hours.substr(0,1) == '0')
	{
		hours = parseInt(hours.substr(1,1)); 
	}
	var minutes = serviceDate.substr(14,2);
	if(minutes.substr(0,1) == '0')
	{
		minutes = parseInt(minutes.substr(1,1)); 
	}
	var seconds = serviceDate.substr(17,2);
	if(seconds.substr(0,1) == '0')
	{
		seconds = parseInt(seconds.substr(1,1)); 
	}
	
	return new Date(year, month - 1, day, hours, minutes, seconds, 0);	
};



function GenerateLiveScoresHeader() {
	var url = 'http://data.ninemsn.com.au/Services/Service.axd?ServiceName=Cricket&ServiceFormat=JSON&callback=?&ServiceAction=NewMatchListLive&Params=%3FclientId=82';

	$.getJSON(url, function (data) {
                                var found = false;
		if (data != null) {
			try {
				var matches = data.MatchList.Match;
				for (var i = 0; i < matches.length; i++) {
					var match = matches[i];

					// check to if the match was played by Australia and if the game is live/completed
					if ((match.HomeTeam.teamId == "1" || match.AwayTeam.teamId == "1") &&
						(match.Result.status == "L" || match.Result.status == "F")) {
                                                                                               found = true;
						var isLive = 'false';
						if (match.Result.status == "L") isLive = 'true';

						var seriesId = match.seriesId,
							matchId = match.matchId,
							matchTypeId = match.matchTypeId,
							teamIds = match.HomeTeam.teamId + ',' + match.AwayTeam.teamId,
							hasComments = 'true',
							hasSC = 'true',
							hasMatchlet = 'true',
							hasPredictor = 'true',
							gameName = match.HomeTeam.fullName + ' v ' + match.AwayTeam.fullName + ' - ' + match.matchName,
							matchDate = GetDateObjFromServiceDate(match.DateTime).format('d/m/Y'),
							matchVenue = match.Venue,
							matchStatus = match.Result.value;

					}
				}
                                                                if(found)
                                                                {
				
                                                                if (isLive == 'true') {$('#headerStrip').addClass('live'); }

				$('#gameName').html(gameName);
				$('#matchDate').html(matchDate);
				$('#matchVenue').html(matchVenue);
				$('#matchStatus').html(matchStatus);
				$('#ckt-links').html(GetMatchLinks(seriesId, matchId, matchTypeId, teamIds, isLive, hasComments, hasSC, hasMatchlet, hasPredictor));
                                                                 var mainDiv = $('#liveScoreHeader');
                                                                 if(mainDiv.length)
{
    mainDiv.css('display','block');
}
                                                                }
			}
			catch (err) {

			}
		}
	});
}




