jQuery.fn.reverse = [].reverse;

function displayCSBGames() {
	
	xml_file_name = "/celtics/hub.xml";
	
	$(document).ready(function(){
	
		var rnd = Math.round(Math.random() * 100000);
		file_name = xml_file_name +'?random='+rnd;
		
		$.get(file_name,{},function(xml){
			
			gameCounter = 0;
			recentGamesCounter = 0;
			liveGamesCounter = 0;
			upcomingGamesCounter = 0;
			gamePreviewBool = 0;
			
			liveGamesTotal = $('game[gstat="2"]',xml).length;
			recentGamesTotal = $('game[gstat="3"]',xml).length;
			upcomingGamesTotal = $('game[gstat="1"]',xml).length;
			
			//START LIVE GAMES LOOP */
			$('game[gstat="2"]',xml).each(function(i) {
			
				if (liveGamesCounter < 1) {
				
					liveGameData = '';
					ls_file_name = '/games/game_component/dynamic/simple_scoreboard.xml?random='+rnd;
					
					$.get(ls_file_name,{},function(xmllive){
						
						//Loop the XML to find the Celtics game.
						$('game[gcd*="BOS"]',xmllive).each(function(i) {
						gameCode = $(this).attr("gcd");
						liveGameClock = $(this).attr("clk");
						
						liveGameQuarter = $(this).attr("gstattxt");
						liveGameStatusNumber = $(this).attr("gstat");
						
						liveGameVtmTcd = $(this).find("vtm").attr("tcd");
						liveGameHtmTcd = $(this).find("htm").attr("tcd");
						
						liveGameVtmScoreData = $(this).find("vtm").attr("scr");
						
						liveGameVtmScoreArray = liveGameVtmScoreData.split("|");
						liveGameVtmScore = liveGameVtmScoreArray[8];
						
						liveGameHtmScoreData = $(this).find("htm").attr("scr");
						liveGameHtmScoreArray = liveGameHtmScoreData.split("|");
						liveGameHtmScore = liveGameHtmScoreArray[8];
						
						});
						
						liveGameData = BuildCelticsLiveGame(liveGameVtmTcd,liveGameHtmTcd,liveGameVtmScore,liveGameHtmScore,liveGameClock,liveGameQuarter);
						$("#live_game").append(liveGameData);
						return liveGameData;
					});
					liveGamesCounter++;
				}
				
			});
			
			
			if (liveGamesTotal == 1 && recentGamesTotal == 0) {
				recentGamesVariable = 0;
				upcomingGamesVariable = 2;
			}
			
			if (liveGamesTotal == 1 && recentGamesTotal > 0) {
				recentGamesVariable = 1;
				upcomingGamesVariable = 1;
			}
			
			if (liveGamesTotal == 0 && recentGamesTotal == 0) {
				recentGamesVariable = 0;
				upcomingGamesVariable = 3;	
			}
			
			if (liveGamesTotal == 0 && recentGamesTotal > 0) {
				recentGamesVariable = 1;
				upcomingGamesVariable = 2;
			}
						
			//START RECENT GAMES LOOP */
			$('game[gstat="3"]',xml).reverse().each(function(i) {
			
				if (i < recentGamesVariable && recentGamesTotal != 0) {
			
				gameDate = $(this).attr("gcd");
				recentGameGamecode = $(this).attr("gcd");
				gameOpponent = $(this).attr("opponent");
				recentGameTipoffMilitary = $(this).attr("timet");
				gameURL = $(this).attr("tkt");
				gameStatus = $(this).attr("gstat");
				recentGameVideoLink = $(this).attr("vid");
			
				recentGameVtmTcd = $(this).find("vtm").attr("tcd");
				recentGameVtmScore = $(this).find("vtm").attr("scr");
				recentGameHtmTcd = $(this).find("htm").attr("tcd");
				recentGameHtmScore = $(this).find("htm").attr("scr");
				
				year = gameDate.slice(0,4);
				mon = gameDate.slice(4,6);
				day = gameDate.slice(6,8);
				hour = recentGameTipoffMilitary.slice(0,2);
				minutes = recentGameTipoffMilitary.slice(2,4);
				
				/* START MOST recentGame GAME */
				recentGameData = BuildCelticsRecentGame(recentGameVtmTcd,recentGameHtmTcd,recentGameVtmScore,recentGameHtmScore,recentGameGamecode,recentGameVideoLink);
				
				if (recentGamesCounter == 0) 
				{
					$("#recent_game3").append(recentGameData);
				}
				
				if (recentGamesCounter == 1) 
				{
					$("#recent_game2").append(recentGameData);
				}
				
				if (recentGamesCounter == 2) 
				{
					$("#recent_game1").append(recentGameData);
				}
				
				/* END MOST recentGame GAME */
				recentGamesCounter++;
				}
					
			});	
			
			/* Grab Live Game Data from Simple Scoreboard */
			//POPULATE MOST recentGame GAME DATA
			
			$('game[gstat=1]',xml).each(function(i) {

				if (i < upcomingGamesVariable) {
					
					upcomingGameVtmTcd = $(this).find("vtm").attr("tcd");
					upcomingGameHtmTcd = $(this).find("htm").attr("tcd");
					upcomingGameGamecode = $(this).attr("gcd");
					upcomingGameTixLink = $(this).attr("tkt");
					year = upcomingGameGamecode.slice(0,4);
					mon = upcomingGameGamecode.slice(4,6);
					day = upcomingGameGamecode.slice(6,8);
					upcomingGameTipoffMilitary = $(this).attr("timet");
					hour = upcomingGameTipoffMilitary.slice(0,2);
					minutes = upcomingGameTipoffMilitary.slice(2,4);
					
					if (day.indexOf("0") != -1) {
						switch (day) {
						   case '01': upcomingGameDay = "1"; break;
						   case '02': upcomingGameDay = "2"; break;
						   case '03': upcomingGameDay = "3"; break;
						   case '04': upcomingGameDay = "4"; break;
						   case '05': upcomingGameDay = "5"; break;
						   case '06': upcomingGameDay = "6"; break;
						   case '07': upcomingGameDay = "7"; break;
						   case '08': upcomingGameDay = "8"; break;
						   case '09': upcomingGameDay = "9"; break;
						   case '10': upcomingGameDay = "10"; break;
						   case '20': upcomingGameDay = "20"; break;
						   case '30': upcomingGameDay = "30"; break;
						}
					}
					else {
					upcomingGameDay = day;
					}
					
					switch (mon) {
					   case '01': upcomingGameMonth = "Jan"; break;
					   case '02': upcomingGameMonth = "Feb"; break;
					   case '03': upcomingGameMonth = "Mar"; break;
					   case '04': upcomingGameMonth = "Apr"; break;
					   case '05': upcomingGameMonth = "May"; break;
					   case '06': upcomingGameMonth = "Jun"; break;
					   case '07': upcomingGameMonth = "Jul"; break;
					   case '08': upcomingGameMonth = "Aug"; break;
					   case '09': upcomingGameMonth = "Sep"; break;
					   case '10': upcomingGameMonth = "Oct"; break;
					   case '11': upcomingGameMonth = "Nov"; break;
					   case '12': upcomingGameMonth = "Dec"; break;
					}
					
					upcomingGameDayString = upcomingGameMonth+' '+upcomingGameDay;
					upcomingGameDate = upcomingGameDayString;
					
					/* START PREVIEW LOGIC - ONLY DISPLAY GAME PREVIEW IF GAME IS TODAY */
					
					if (year < 4) {
					year += 2000;
					}
														
					else if (year < 100) {
					year += 1900;		
					}	
					
					var useDate = new Date(year, mon-1, day, 0, 0, 0);
					var today = new Date();
					var todayYear = today.getFullYear();
					
					if (todayYear < 4) {
					todayYear += 2000;
					}
														
					else if (todayYear < 100) {
					todayYear += 1900;		
					}	
					
					var todayDate = today.getDate();
					var todayMonth = today.getMonth();
					var newToday = new Date(todayYear,todayMonth,todayDate);
					
					if (useDate.valueOf() == newToday.valueOf()) {
					gamePreviewBool = 1;
					}
					else {
					gamePreviewBool = 0;
					}
					
					/* END PREVIEW LOGIC */
					
					
					upcomingGameData = BuildCelticsUpcomingGame(upcomingGameVtmTcd,upcomingGameHtmTcd,upcomingGameDate,upcomingGameGamecode,upcomingGameTixLink,gamePreviewBool);
					
						if (upcomingGamesCounter == 0) 
						{
							$("#upcoming_game1").append(upcomingGameData);
						}
						
						if (upcomingGamesCounter == 1) 
						{
							$("#upcoming_game2").append(upcomingGameData);
						}
						
						if (upcomingGamesCounter == 2) 
						{
							$("#upcoming_game3").append(upcomingGameData);
						}
						upcomingGamesCounter++;	
					}
				
				});
			
			});
		});
	}
	
function BuildCelticsRecentGame(recentGameVtmTcd,recentGameHtmTcd,recentGameVtmScore,recentGameHtmScore,recentGameGamecode,recentGameVideoLink) {
			/* START MOST recentGame GAME */
			myRecentGameOutput = '';
			myRecentGameOutput += '<table cellpadding="0" cellspacing="0" class="csb_header recent">';
			myRecentGameOutput += '<tr class="logos">';
			myRecentGameOutput += '<td class="left"><img src="/celtics/media/csb_'+recentGameVtmTcd+'_logo3530.gif" width="35" height="30" alt="'+recentGameVtmTcd+'" /></td>';
			myRecentGameOutput += '<td class="right"><img src="/celtics/media/csb_'+recentGameHtmTcd+'_logo3530.gif" width="35" height="30" alt="'+recentGameHtmTcd+'" /></td>';
			myRecentGameOutput += '</tr>';
			myRecentGameOutput += '<tr class="scores">';
			myRecentGameOutput += '<td class="left"><span>'+recentGameVtmScore+'</span></td>';
			myRecentGameOutput += '<td class="right"><span>'+recentGameHtmScore+'</span></td>';
			myRecentGameOutput += '</tr>';
			myRecentGameOutput += '<tr class="info">';
			myRecentGameOutput += '<td colspan="2">';
			myRecentGameOutput += '<a href="/games/'+recentGameGamecode+'/boxscore.html">BOX</a> ';
			myRecentGameOutput += '<a href="/games/'+recentGameGamecode+'/recap.html">RECAP</a> ';
			myRecentGameOutput += '<a href="'+recentGameVideoLink+'">VIDEO</a>';
			myRecentGameOutput += '</td>';
			myRecentGameOutput += '</tr>';
			myRecentGameOutput += '</table>';
			return myRecentGameOutput;
}

function BuildCelticsLiveGame(liveGameVtmTcd,liveGameHtmTcd,liveGameVtmScore,liveGameHtmScore,liveGameTime,liveGameQuarter) {
			/* START LIVE GAME */
			myLiveGameOutput = '';
			myLiveGameOutput += '<table cellpadding="0" cellspacing="0" class="csb_header live">';
			myLiveGameOutput += '<tr class="logos">';
			myLiveGameOutput += '<td class="left"><img src="/celtics/media/csb_'+liveGameVtmTcd+'_logo3530.gif" width="35" height="30" alt="'+liveGameVtmTcd+'" /></td>';
			myLiveGameOutput += '<td class="right"><img src="/celtics/media/csb_'+liveGameHtmTcd+'_logo3530.gif" width="35" height="30" alt="'+liveGameHtmTcd+'" /></td>';
			myLiveGameOutput += '</tr>';
			myLiveGameOutput += '<tr class="scores">';
			myLiveGameOutput += '<td class="left"><span>'+liveGameVtmScore+'</span></td>';
			myLiveGameOutput += '<td class="right"><span>'+liveGameHtmScore+'</span></td>';
			myLiveGameOutput += '</tr>';
			myLiveGameOutput += '<tr class="info">';
			myLiveGameOutput += '<td colspan="2">'+liveGameTime+'/'+liveGameQuarter+' - <span class="live">LIVE!</span></td>';
			myLiveGameOutput += '</tr>';
			myLiveGameOutput += '</table>';
			return myLiveGameOutput;
}

function BuildCelticsUpcomingGame(upcomingGameVtmTcd,upcomingGameHtmTcd,upcomingGameDate,upcomingGameGamecode,upcomingGameTixLink,gamePreviewBool) {

			upcomingGameTixLinkEdit = upcomingGameTixLink.replace("brand=celtics&CAMEFROM=NBACELTICS_CAL","camefrom=NBACELTICS_WEBHPCSB&amp;brand=celtics");

			/* START UPCOMING GAME */
			myUpcomingGameOutput = '';
			myUpcomingGameOutput += '<table cellpadding="0" cellspacing="0" class="csb_header upcoming">';
			myUpcomingGameOutput += '<tr class="logos">';
			myUpcomingGameOutput += '<td class="left"><img src="/celtics/media/csb_'+upcomingGameVtmTcd+'_logo3530.gif" width="35" height="30" alt="'+upcomingGameVtmTcd+'" /></td>';
			myUpcomingGameOutput += '<td class="right"><img src="/celtics/media/csb_'+upcomingGameHtmTcd+'_logo3530.gif" width="35" height="30" alt="'+upcomingGameHtmTcd+'" /></td>';
			myUpcomingGameOutput += '</tr>';
			myUpcomingGameOutput += '<tr class="date">';
			myUpcomingGameOutput += '<td colspan="2">'+upcomingGameDate+'</td>';
			myUpcomingGameOutput += '</tr>';
			myUpcomingGameOutput += '<tr class="info">';
			myUpcomingGameOutput += '<td colspan="2">';
			if (gamePreviewBool == 1) 
			{
			myUpcomingGameOutput += '<a href="/games/'+upcomingGameGamecode+'/gameinfo.html">Preview</a> ';
			}
			if (gamePreviewBool == 0) {
			myUpcomingGameOutput += '<a href="/celtics/schedule/2011-2012-regular-season-schedule.html">Schedule</a> ';
			}
			if (upcomingGameHtmTcd == "celtics") {
				myUpcomingGameOutput += '<a href="'+upcomingGameTixLinkEdit+'">Tickets</a>';
			}
			myUpcomingGameOutput += '</td>';
			myUpcomingGameOutput += '</tr>';
			myUpcomingGameOutput += '</table>';
			return myUpcomingGameOutput;
}
