//	Calendar Javascript - Copyright (C) 2007-2010 Charles A Upsdell, All Rights Reserved; www.upsdell.com
//
//  For pages with game schedules
//
aModule.uses( 'cCalendar_gs', 'cCalendar', 'cal_config' );


// Method to insert HTML for list of games
//
var calDoR = 'R';
var calDoT = 'T';
var calDoX = 'X';
//
cCalendar.prototype.toGamesHTML = function ( id, option, tournament )
{
var $name = 'toGamesHTML';
option = option || (calDoR + calDoT + calDoX);
if ( tournament instanceof Array )
  {
	if ( id instanceof Array )
	  {
		for ( var i = 0; i < tournament.length; ++i )
			this.toGamesHTML( id[i], option, tournament[i] );
	  }
	else
	  {
		var sHTML = kal.games.topHTML;
		for ( var i = 0; i < tournament.length; ++i )
		  {
			var sHeader = '';
			if ( typeof(kal.games.tournamentGapTopHTML) != 'undefined' )
				sHeader = kal.games.tournamentGapTopHTML.blend( tournament[i] );
			var sFooter = '';
			if ( typeof(kal.games.tournamentGapHTML) != 'undefined' )
				sFooter = kal.games.tournamentGapHTML;
			sHTML += this.toHTML( 'games', kal.period, option, tournament[i], sHeader, sFooter );
		  }
		sHTML += kal.games.bottomHTML;
		var o = getElement( id );
		if ( o )
			o.innerHTML = sHTML;
	  }
  }
else
  {
	id = id || 'ins_games';
	var o = getElement( id );
	if ( o )
	  {
		var sHTML = this.toHTML( 'games', kal.period, option, tournament );
		if ( sHTML.length == 0 )
			alert( 'no games in schedule' );
		else
			o.innerHTML = sHTML;
	  }
  }
myScrollToAnchor();
return false;
}
//
function calendarGamesSwitch( change, type )
{
if ( change === undefined )
	change = '+';
if ( type === undefined )
	type = 'RTX';
if ( type.indexOf('R') != -1 )
	calDoR = ( change == '+') ? 'R' : '' ;
if ( type.indexOf('T') != -1 )
	calDoT = ( change == '+') ? 'T' : '' ;
if ( type.indexOf('X') != -1 )
	calDoX = ( change == '+') ? 'X' : '' ;
if ( type.indexOf('P') != -1 )
	kal.games.isShowPast = ( change == '+') ? true : false ;
cal.toGamesHTML();
return false;
}

function calGamesInit ( sd, sOption, sOption3 )
{
var sHTML = '';
sd.doR = ( sOption.indexOf('R') != -1 );
sd.doX = ( sOption.indexOf('X') != -1 );
sd.doT = ( sOption.indexOf('T') != -1 );
sd.doP = kal.games.isShowPast;
sd.nLines = 0;
sd.year = -1;
sd.month = -1;
if ( sd.tournament === undefined )
	sHTML += calGamesButtonsInit ( sd );
sHTML += (typeof(sOption3) == 'undefined') ? kal.games.topHTML : sOption3;
return sHTML;
}

function calGamesButtonsInit ( sd )
{
var sHTML = '';
if ( typeof(kal.games.headerHTML) == 'undefined' )
	sHTML += kal.games.topHTML;
else
	sHTML += '<table summary="Games" class="calendar_games" cellspacing="0" cellpadding="0">' + kal.games.headerHTML;
if ( (typeof(calendarGamesSwitch) != 'undefined') && (sd.tournament === undefined) )
  {
	var sButtons = '';
	sButtons += ( typeof(kal.games.buttonStartHTML) == 'undefined' ) ? '<p class="noprint">' : kal.games.buttonStartHTML;
	sButtons += ( sd.doR ) ? kal.games.hideR : kal.games.showR;
	sButtons += ( sd.doT ) ? kal.games.hideT : kal.games.showT;
	sButtons += ( sd.doX ) ? kal.games.hideX : kal.games.showX;
	sButtons += ( sd.doP ) ? kal.games.hideP : kal.games.showP;
	sButtons += ( typeof(kal.games.buttonEndHTML) == 'undefined' ) ? '</p>\n' : kal.games.buttonEndHTML;
	if ( typeof(kal.games.buttonInsert) != 'undefined' )
	  {
		var oButtons = getElement( kal.games.buttonInsert );
		if ( oButtons )
		  {
			oButtons.innerHTML = sButtons;
			sButtons = '';
		  }
	  }
	sHTML += sButtons;
  }
return sHTML;
}


// Method to return HTML with the season record
cEventGames.prototype.toHTML = function ( isntIE50x, nEstimatedGames )
{
var $name = 'toHTML';
if ( isntIE50x === undefined )
	isntIE50x = true;
if ( nEstimatedGames >= 0 )
	nEstimatedGames += this.nGames;
else
	nEstimatedGames = -nEstimatedGames;
var sHTML = '';
sHTML += '<p class="game_record">' + this.sTitle + ' (' + nEstimatedGames + '): <strong>';
sHTML += '<span class="win">' + this.nWin + '</span> &#8211; ';
sHTML += '<span class="lose">' + this.nLose + '</span> &#8211; ';
sHTML += '<span class="tie">' + this.nTie + '</span>';
sHTML += '</strong></p>\n';
sHTML += '<p class="game_barchart">';
sHTML += this.toHTML1( this.nWin, 'w', isntIE50x );
sHTML += this.toHTML1( this.nLose, 'l', isntIE50x );
sHTML += this.toHTML1( this.nTie, 't', isntIE50x );
sHTML += this.toHTML1( this.nNoScoreYet, 'r nsy', isntIE50x );
sHTML += this.toHTML1( nEstimatedGames-this.nWin-this.nLose-this.nTie-this.nNoScoreYet, 'r', isntIE50x );
sHTML += '</p>\n';
sHTML += '</p>\n';
return sHTML;
}
cEventGames.prototype.toHTML1 = function ( n, c, isntIE50x )
{
var $name = 'toHTML1';
var sHTML = '';
var sAdd = '';
if ( !isntIE50x )
	sAdd = '<span style="font-size:0.5ex;"> </span>';
sHTML += '<span class="' + c + '">';
for ( var i = 0; i < n; i++ )
	sHTML += '<i>&nbsp;</i>' + sAdd;
sHTML += '</span>'
return sHTML;
}


// Method to return HTML with a list of game information
cEventYear.prototype.toGamesHTML = function ( y, m, r, x, t, s )
{
var $name = 'toGamesHTML';
s.month = -1;

var nGames = r.nGames + x.nGames + t.nGames;
var sHTML = '';
var nDayMonthStarts = this.aYearDays[m];
var nDaysInMonth = this.aMonthDays[m];
var sVs = '<span class="narrow">vs</span> ';
if ( s.isMobileDevice )
	sVs = '';
for ( var d = 0; d < nDaysInMonth; ++d )
  {
	var yd = d + nDayMonthStarts;
	var isHidePast = false;
	if ( ! s.doP )
	  {
		if ( (y < s.this_year) || ((y == s.this_year) && (m < s.this_month)) || ((y == s.this_year) && (m == s.this_month) && (d < s.this_day)) )
			isHidePast = true;
	  }
	if ( s.isMarkDate )
	  {
		if ( (y > s.this_year) || (((y == s.this_year) && (m > s.this_month))) || ((y == s.this_year) && (m == s.this_month) && (d >= s.this_day)) )
		  {
			if ( s.nLines > 0 )
				sHTML += kal.games.header2HTML + kal.games.headerHTML;
			s.isMarkDate = false;
		  }
	  }
	if ( s.tournament !== undefined )
	  {
		var isNotSeries = true;
		for ( var e = 0; isNotSeries && (e < this.aDay[yd].aEvent.length); ++e )
		  {
			if ( this.aDay[yd].aEvent[e].isSeries && (!this.aDay[yd].aEvent[e].isGame) && (this.aDay[yd].aEvent[e].sEvent == s.tournament) )
				isNotSeries = false;
		  }
		if ( isNotSeries )
			continue;
	  }
	for ( var e = 0; e < this.aDay[yd].aEvent.length; ++e )
	  {
		if ( ! this.aDay[yd].aEvent[e].isGame )
			continue;
		var isExhibitionGame = this.aDay[yd].aEvent[e].isExhibitionGame;
		var isTournamentGame = this.aDay[yd].aEvent[e].isTournamentGame;
		var isRegularGame = this.aDay[yd].aEvent[e].isRegularGame;
		var isForSeasonRecord = true;
		if ( typeof(this.aDay[yd].aEvent[e].oType.isForSeasonRecord) != 'undefined' )
 			isForSeasonRecord = this.aDay[yd].aEvent[e].oType.isForSeasonRecord;
		var s1 = this.aDay[yd].aEvent[e].sScore1;
		var s2 = this.aDay[yd].aEvent[e].sScore2;
		var isVisible = ((isRegularGame && s.doR) || (isExhibitionGame && s.doX) || (isTournamentGame && s.doT)) && !isHidePast;
		if ( isVisible && (s.month != m) )
		  {
			s.month = m;
			sHTML += '<tr><th class="year" colspan="6">' + kal.aMonthName[m] + ' ' + y + '</td></tr>\n';
		  }
		sHTML += '<tr class="' + 'oe'.charAt(s.nLines%2) + '"';
		if ( isVisible )
		  {
			sHTML += '>\n';
			s.nLines += 1;
		  }
		else
			sHTML += ' style="display:none;">\n';
		sHTML += '<td class="day">' + (d+1) + '</td>\n';
		sHTML += '<td class="weekday"><span class="screen">' + kal.aWeekDayNameSmall[this.aDay[yd].nWeekDay] + '</span></td>\n';
		sHTML += '<td class="time">' + ((this.aDay[yd].aEvent[e].sTime != '') ? (this.aDay[yd].aEvent[e].oTime.toHTML( '', '-', true, 'TBA', 'span class=\"ampm\"', true )) : '&nbsp;') + '</td>\n';
		sHTML += '<td class="type';
		if ( isExhibitionGame )
		  {
			if ( (this.aDay[yd].sTournament!='') && !s.isMarkDate && (this.aDay[yd].aEvent[e].oType.t4 != '!') )
				sHTML += ' typeX conflict"><span title="Conflict">X</span>';
			else
				sHTML += ' typeX">X';
			if ( s1 >= 0 )
			  {
				++nGames;
				if ( isForSeasonRecord )
					++x.nGames;
			  }
		  }
		else if ( isTournamentGame )
		  {
			var sConflict = '';
			if ( this.aDay[yd].sTournament=='' )
				sConflict = ' conflict';
			var nIndexEndTag = this.aDay[yd].sTournament.indexOf('>');
			if ( nIndexEndTag == -1 )
				sHTML += ' typeT' + sConflict + '">T';
			else
				sHTML += ' typeT' + sConflict + '">' + this.aDay[yd].sTournament.substring(0,nIndexEndTag) + '">T</a>';
			if ( s1 >= 0 )
			  {
				++nGames;
				if ( isForSeasonRecord )
					++t.nGames;
			  }
		  }
		else
		  {
			if ( (this.aDay[yd].isSeriesDay) && !s.isMarkDate && (this.aDay[yd].aEvent[e].oType.t4 != '!') )
				sHTML += ' typeR conflict"><span title="Conflict">R</span>';
			else
				sHTML += ' typeR">&nbsp;';
			if ( s1 >= 0 )
			  {
				++nGames;
				if ( isForSeasonRecord )
					++r.nGames;
			  }
		  }
		sHTML += '</td>\n';

		var sLocation = this.aDay[yd].aEvent[e].sLocation;
		if ( isTournamentGame && (s.tournament == undefined) && ((sLocation == 'TBA')||(sLocation == '')) )
			sLocation = this.aDay[yd].sTournament;
		var sScore1 = this.aDay[yd].aEvent[e].sScore1;
		var sScore2 = this.aDay[yd].aEvent[e].sScore2;
		var isCan1 = '';
		var isCan2 = '';
		var isScoreUnreported = false;
		var sUseResultComment = this.aDay[yd].aEvent[e].result_comment;
		var isUseResultComment = (sUseResultComment !== undefined);

		var sScoreHTML = '<div class="game_score';
		if ( (this.aDay[yd].aEvent[e].isCancelled) && (this.aDay[yd].aEvent[e].sWhyCancelled != 'Forfeit') )
		  {
			sScoreHTML = '';
			isCan1 = '<del>';
			isCan2 = '</del>';
		  }
		else if ( s1 === '' )
		  {
			if ( s.isMarkDate )
			  {
				isScoreUnreported = true;
				sScoreHTML += '">&nbsp;<span class="dim">?-?</span>';
				if ( isForSeasonRecord )
				  {
					if ( isExhibitionGame )
						++x.nNoScoreYet;
					else if ( isTournamentGame )
						++t.nNoScoreYet;
					else
						++r.nNoScoreYet;
				  }
			  }
			else
				sScoreHTML = '';
		  }
		else
		  {
			if ( s1 > s2 )
			  {
				sScoreHTML += ' result_w">';
				if ( typeof(kal.games.IEnobrHTML) != 'undefined' )
					sScoreHTML += kal.games.IEnobrHTML[0];
				if ( isUseResultComment )
					sScoreHTML += sUseResultComment;
				else
					sScoreHTML += 'W<span class="screen">in</span>';
				if ( isForSeasonRecord )
				  {
					if ( isExhibitionGame )
						++x.nWin;
					else if ( isTournamentGame )
						++t.nWin;
					else
						++r.nWin;
				  }
			  }
			else if ( s1 < s2 )
			  {
				sScoreHTML += ' result_l">';
				if ( typeof(kal.games.IEnobrHTML) != 'undefined' )
					sScoreHTML += kal.games.IEnobrHTML[0];
				if ( isUseResultComment )
					sScoreHTML += sUseResultComment;
				else
					sScoreHTML += 'L<span class="screen">ose</span>';
				if ( isForSeasonRecord )
				  {
					if ( isExhibitionGame )
						++x.nLose;
					else if ( isTournamentGame )
						++t.nLose;
					else
						++r.nLose;
				  }
			  }
			else if ( s1 == s2 )
			  {
				sScoreHTML += ' result_t">';
				if ( typeof(kal.games.IEnobrHTML) != 'undefined' )
					sScoreHTML += kal.games.IEnobrHTML[0];
				if ( isUseResultComment )
					sScoreHTML += sUseResultComment;
				else
					sScoreHTML += 'T<span class="screen">ie</span>';
				if ( isForSeasonRecord )
				  {
					if ( isExhibitionGame )
						++x.nTie;
					else if ( isTournamentGame )
						++t.nTie;
					else
						++r.nTie;
				  }
			  }
			if ( !isUseResultComment && (s1!=99) && (s2!=99) )
				sScoreHTML += ' ' + calScore( s1, s2, '&#8211;' );
			if ( typeof(kal.games.IEnobrHTML) != 'undefined' )
				sScoreHTML += kal.games.IEnobrHTML[1];
		  }
		if ( sScoreHTML != '' )
			sScoreHTML += '</div>\n';

		var sCompetitor = this.aDay[yd].aEvent[e].sTeam2;
		if ( (sCompetitor == '') || (sCompetitor == 'TBA') )
			sCompetitor = '? team';
		else
			sCompetitor = '<b>' + sCompetitor + '</b>';
		if ( this.aDay[yd].aEvent[e].oType.t3 == 'A' )
			sCompetitor = '<i>' + sCompetitor + '</i>';
		sHTML += '<td class="detail_type"'
		if ( sScoreHTML == '' )
			sHTML += ' colspan="2"';
		sHTML += '>' + '<div>' + isCan1 + sVs + sCompetitor;
		if ( isCan1 != '' )
		  {
			sHTML += isCan2 + ' ' + this.aDay[yd].aEvent[e].sWhyCancelled;
		  }
		else if ( !s.isMarkDate && (s1==='') )
		  {
			sHTML += '<span class="handheld"><br /></span> at ' + sLocation + '&nbsp;';
			if ( this.aDay[yd].aEvent[e].sEvent != '' )
				sHTML += '<br />\n' + this.aDay[yd].aEvent[e].sEvent;
		  }
		if ( (this.aDay[yd].aEvent[e].oType.games_comment !== undefined) && !s.isMarkDate )
			sHTML += '<br />' + this.aDay[yd].aEvent[e].oType.games_comment;
		sHTML += '</div>\n</td>';
		if ( sScoreHTML != '' )
			sHTML += '<td class="detail_result">' + sScoreHTML + '</td>';
		sHTML += '\n</tr>\n';
	  }
  }
return sHTML;
}


