/* Cancel these functions that are called from global.js */
function findFullLeaderBoard () {}
function initHomeLeaderBoard () {}
iframeAdFactory.pcupcounter = 0;
/* AJAX section */
var handler = function (ajaxContent)
{
	var delay = setTimeout (function ()
	{
		var tourney = new Tournament (ajaxContent);
		tourney.init ();
		var roundNumber = 1;
		var query = location.search;
		var roundIndex = query.indexOf ('round');
		if (roundIndex > -1)
		{
			var roundStart = roundIndex + 'round='.length;
			roundNumber = parseInt (query.substring (roundStart, roundStart + 1));
		}
		else
		{
			var startDay = 27;
			var tstart = tourney.tourneyStartMMDDYY;
			var tmonth = parseInt (tstart.substring (0, 2));
			tmonth = isNaN (tmonth)? 9 : tmonth;
			var tday = tstart.substring (2, 4);
			tday = (tday.indexOf('0') > -1) ? parseInt (tstart.substring (3, 4)) : parseInt (tstart.substring (2, 4));
			//tday = 08//isNaN (tday)? 27 : tday;
			var tyear = parseInt (tstart.substring (4, 8));
			tyear = isNaN (tyear)? 2007 : tyear;
			var matchDays = new Array (tday, tday + 1, tday + 2, tday + 3);
			var matchDaysLength = matchDays.length;	
			for (var n = 0; n < matchDaysLength; n++)
			{
				var deduction = 0;
				var matchDay = matchDays [n];
				if (matchDay > 28)
				{
					if (tmonth == 2)
					{
						if (tyear % 4 == 0)
						{
							deduction = 29;
						}
						else
						{
							deduction = 28
						}
					}
					else if (tmonth == 4 || tmonth == 6 || tmonth == 9 || tmonth == 11)
					{
						deduction = 30;
					}
					else
					{
						deduction = 31;
					}
				}
				if (matchDay > deduction)
				{
					matchDays [n] = matchDay - deduction;
				}
			}
			var today = new Date ();
			var date = today.getDate ();
			var hours = today.getHours () + Math.floor (today.getTimezoneOffset () / 60) - 4; /* Set to Eastern Time hour */
			var minutes = today.getMinutes ();
			
			if(tourney.tournId == "500"){ // President's Cup
				if (date == matchDays [0])
				{
					roundNumber = 1;
				}
				else if (date == matchDays [1])
				{
					roundNumber = 2;
				}
				else if (date == matchDays [2])
				{
					roundNumber = 3;
					if ((hours > 12) || (hours == 12 && minutes > 45)) 
					{
						roundNumber = 4;
					}
	
				}
				else if (date == matchDays [3])
				{
					roundNumber = 5;
				}
			}
			else { // Ryder Cup
				if (date == matchDays [0])
				{
					roundNumber = 1;
					if ((hours > 12) || (hours == 12 && minutes > 45)){
						roundNumber = 2;
					}
				}
				else if (date == matchDays [1])
				{
					roundNumber = 3;
					if ((hours > 12) || (hours == 12 && minutes > 45))
					{
						roundNumber = 4;
					}
				}
				else if (date == matchDays [2])
				{
					roundNumber = 5;
				}	
			}
		}
		tourney.setRound (roundNumber);		
		tourney.getRows ();
		tourney.insertLBAd ();
	}, 1);
	
	
	if(iframeAdFactory.pcupcounter == 0){
		iframeAdFactory.pcupcounter++;
	} else {
		iframeAdFactory.refresh();
	}
};

var ajax = new ajaxObj ();
ajax.setMimeType ('text/xml');
var golf_lBoardXMLUrl = 'http://' + location.host + '/golf/static/xml/leaderboard/'+leaderboardID+'/ModifiedLeaderboard.xml';
//var golf_lBoardXMLUrl = 'http://' + location.host + '/golf/test/ModifiedLeaderboard.xml';


tii_callFunctionOnElementLoad ('leaderboard_container', function () {
	//alert(golf_lBoardXMLUrl);
	loadNewData();
	//if (typeof tii_useJSRefresh != 'undefined' && tii_useJSRefresh)
	//{
		//var interval = setInterval (function () { ajax.doGet (golf_lBoardXMLUrl, handler, 'xml'); }, 5000);
		//setTimeout(function(){ ajax.doGet (golf_lBoardXMLUrl, handler, 'xml'); alert('do it');},3000);
	//}
});
	
var loadNewData = function() {
	ajax.doGet (golf_lBoardXMLUrl, handler, 'xml');
	setTimeout(function(){ loadNewData(); },45000);
};

/* Tournament objects */


function Tournament (content)
{
	this.init = function ()
	{
		var tourney = content.getElementsByTagName ('Tourn').item (0);
		this.tourneyName = tourney.getAttribute ('Name');
		var tourn_name = document.getElementById ('tourn_name');
		if (tourn_name)
		{
			tourn_name.innerHTML = this.tourneyName;
		}
		this.tournId = tourney.getAttribute ('TournId');
		this.tourneyLoc = tourney.getAttribute ('Loc') + ', ' +  tourney.getAttribute ('localCity') + ', ' + tourney.getAttribute ('LocState');
		var tourn_loc = document.getElementById ('tourn_loc');
		if (tourn_loc)
		{
			tourn_loc.innerHTML = this.tourneyLoc;
		}
		this.tourneyStart = tourney.getAttribute ('TournStartDate');
		this.tourneyStartMMDDYY = tourney.getAttribute ('TournStartDate2');
		var tourn_start = document.getElementById ('tourn_start');
		if (tourn_start)
		{
			tourn_start.innerHTML = this.tourneyStart + " <span>(All Times Eastern)</span>";
		}
		this.rounds = tourney.getElementsByTagName ('Round');
		var pcup_rounds = document.getElementById ('pcup_rounds');
		if (pcup_rounds)
		{
			var roundsHTML = '';
			var roundLis = pcup_rounds.getElementsByTagName ('li');
			var roundsLength = this.rounds.length;
			for (var m = 0; m < roundsLength; m++)
			{
				this.round = this.rounds.item (m);
				var roundLi = roundLis.item (m);
				if (this.round && roundLi)
				{
					if (m === roundsLength -1) var cls = "class='last-child'";
					roundsHTML += '<li '+cls+'><a href="?round=' + (m + 1) + '">' + this.round.getAttribute ('RoundName') + '</a></li>\n';
				}
			}
			pcup_rounds.innerHTML = roundsHTML;
		}
		this.team1 = tourney.getAttribute ('Team1');
		this.team1 = !this.team1? '' : this.team1;
		this.team1Abbr = tourney.getAttribute ('Team1Abbr');
		this.team1Abbr = !this.team1Abbr? '' : this.team1Abbr;
		this.team2 = tourney.getAttribute ('Team2');
		this.team2 = !this.team2? '' : this.team2;
		this.team2Abbr = tourney.getAttribute ('Team2Abbr');
		this.team2Abbr = !this.team2Abbr? '' : this.team2Abbr;
		var totalscore = document.getElementById ('totalscore');
		if (totalscore)
		{
			var usPoints = tourney.getAttribute ('USTotalPoints');
			var intPoints = tourney.getAttribute ('InternationalTotalPoints');
			
			var r_us = Math.round(tourney.getAttribute ('USTotalPoints'));
			var r_int = Math.round(tourney.getAttribute ('InternationalTotalPoints'));
						
			if (usPoints == '' && intPoints == '')
			{
				totalscore.innerHTML = '<strong>Total Score:</strong>';
			}
			else if (r_us > r_int)
			{
				totalscore.innerHTML = '<strong>Total Score:</strong> <span class="team_us"><span>' + this.team1 + '</span> ' + usPoints + '</span>, <span class="team_int"><span>' + this.team2 + '</span> ' + intPoints + '</span>';
			}
			else if (r_us == r_int)
			{
				totalscore.innerHTML = '<strong>Total Score:</strong> <span class="team_us"><span>' + this.team1 + '</span> ' + usPoints + '</span>, <span class="team_int"><span>' + this.team2 + '</span> ' + intPoints + '</span>';
			}
			else if (r_us < r_int)
			{
				totalscore.innerHTML = '<strong>Total Score:</strong> </span> <span class="team_int"><span>' + this.team2 + '</span> ' + intPoints + '</span>, <span class="team_us"><span>' + this.team1 + '</span> ' + usPoints;
			}
		}
	}
	this.setRound = function (roundNumber)
	{
		if (roundNumber > 0)
		{
			this.round = this.rounds.item (roundNumber - 1);
		}
		var pcup_rounds = document.getElementById ('pcup_rounds');
		if (pcup_rounds)
		{
			var selectedRoundLi = pcup_rounds.getElementsByTagName ('li').item (roundNumber - 1);
			if (typeof selectedRoundLi != 'undefined' && selectedRoundLi)
			{
				selectedRoundLi.className = 'selected';
			}
		}
	}
	
	
	this.insertLBAd = function() {
		/* This portion adds the container for the Ad ten rows down within the leaderboard */
		var diffName = document.getElementById('leaderboard_container');	
		var div = document.createElement("div");
		div.className = "insertLBAdRow";
		div.setAttribute("id", "lb-ad");
		div.style.height = "51px";
		div.style.backgroundColor = "white";
		iframeAdFactory.getAd('i_640x50', 640, 50, false, false, true, div);		
		var prevChild = diffName.getElementsByTagName('table')[0];		
		diffName.insertBefore(div, prevChild);
	}
	
	this.getRows = function ()
	{
		var tableDiv = document.getElementById ('leaderboard_container');
		if (!tableDiv || !this.round)
		{
			return;
		}
			
		var tableDivHTML = '<table>\n<colgroup class="left team_us"></colgroup>\n<colgroup span="5"></colgroup>\n<colgroup class="right"></colgroup>\n<thead><tr class="main"><th rowspan="2">' + this.team1 + '</th><th rowspan="2">&nbsp;</th><th rowspan="2">' + this.team2 + '</th>';
		tableDivHTML += '<th rowspan="2">Thru</th><th rowspan="2">Score</th><th colspan="2" class="right">Points</th></tr><tr><th>' + this.team1Abbr + '</th><th>' + this.team2Abbr + '</th></tr></thead>\n';
		var round = this.round;
		var usRoundPoints = '';
		var intRoundPoints = '';
		usRoundPoints = round.getAttribute ('USRoundPoints');
		if (usRoundPoints != '' && Math.floor (usRoundPoints) == usRoundPoints)
		{
			usRoundPoints = parseInt (usRoundPoints);
		}
		intRoundPoints = round.getAttribute ('InternationalRoundPoints');
		if (intRoundPoints != '' && Math.floor (intRoundPoints) == intRoundPoints)
		{
			intRoundPoints = parseInt (intRoundPoints);
		}
		var groups = round.getElementsByTagName ('Group');
		var groupsLength = groups.length;
		for (var i = 0; i < groupsLength; i++)
		{
			var group = groups.item (i);
			var leaderId = group.getAttribute ('Leader');
			var matchNumber = group.getAttribute ('MatchNum');
			var usMatchPoints = group.getAttribute ('USMatchPoints');
			if (usMatchPoints == '1.0')
			{
				usMatchPoints = '1';
			}
			var intMatchPoints = group.getAttribute ('InternationalMatchPoints');
			if (intMatchPoints == '1.0')
			{
				intMatchPoints = '1';
			}
			var matchScore = group.getAttribute ('MatchScore');
			var players = group.getElementsByTagName ('Player');
			var player1 = new Player (players.item (0), matchNumber, matchScore, leaderId);
			var player2 = new Player (players.item (1), matchNumber, matchScore, leaderId);
			var usPlayer;
			var intPlayer;
			if (player1.isUS)
			{
				usPlayer = player1;
				intPlayer = player2;
			}
			else
			{
				usPlayer = player2;
				intPlayer = player1;
			}
			tableDivHTML += '<tr class="' + (i % 2 == 0? 'odd' : 'even') + '"><td class="team_us">' + usPlayer.name + '</td><td>' +  (intPlayer.name == ''? '' : 'vs') + '</td><td class="team_int">' + intPlayer.name + '</td><td>' + usPlayer.thru + '</td><td class="' + (usPlayer.isLeader? 'team_us' : (intPlayer.isLeader? 'team_int' : '')) + '">' + usPlayer.matchScore + '</td><td class="team_us">' + usMatchPoints + '</td><td class="team_int">' + intMatchPoints + '</td></tr>\n';
		}
		tableDivHTML += '<tr class="bottom"><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td class="team_us">' + usRoundPoints + '</td><td class="team_int">' + intRoundPoints + '</td></tr>\n</table>\n';
		tableDiv.innerHTML = tableDivHTML;
	}
}
	function Player (playerElement, matchNumber, matchScr, leaderId)
	{
		var pEl = playerElement;
		this.isUS = pEl.getAttribute ('isUS') == 'Yes'? true : false;
		var playerId = pEl.getAttribute ('PID');
		if (!leaderId)
		{
			leaderId = '';
		}
		this.isLeader = playerId == leaderId? true : false;
		var firstName = pEl.getAttribute ('Fname');
		var lastName = pEl.getAttribute ('Lname');
		this.name = firstName + ((firstName == '')? '' : ' ') + lastName;
		if (this.name == '' && this.isUS)
		{
			this.name = 'Match ' + matchNumber;
		}
		var teeTime = pEl.getAttribute ('TTime');
		if (matchScr == '' && teeTime != '' && this.isUS)
		{
			this.matchScore = teeTime;
		}
		else
		{
			this.matchScore = matchScr;
		}
		this.matchScore = matchScr;
		this.isHalved = this.matchScore == 'Halved'? true: false;
		var thru = pEl.getAttribute ('Thru');
		/*if (thru == '' && teeTime != '' && this.isUS)
		{
			this.thru = teeTime;
		}
		else
		{*/
			this.thru = thru;
		//}
		
		this.matchFinished = this.thru == 'F'? true : false;
		this.isMatchWinner = pEl.getAttribute ('MatchWinner') == 'Yes'? true : false;
	}
