
function Group(block, name)
{
	this.Block = block;
	this.Name = name;
	this.Teams = new Array();
	this.AddTeam = function(team)
	{
		this.Teams[this.Teams.length] = team;
		if (this.Block != null)
		{
			team.Group = this;
		}
	};
}
 var done = false;

function Team(name)
{
	this.Type = "Team";
	this.Name = name;
	this.Wins = 0;
	this.Losses = 0;
	// schedule contains byes and games
	this.Schedule = [];
	this.Games = [];
	this.Byes = [];
	this.HasSponsor = false;
	this.Sponsors = [];
	this.TeamUrl = "";
	this.HasTeamUrl = false;
	this.TeamPicUrl = "";
	this.HasTeamPic = false;
	this.Lead = null;
	this.Second = null;
	this.Third = null;
	this.Skip = null;
	this.Group = null;
	this.NewsEvents = new NewsEventsContainer();

	this.GetDisplayName = function()
	{
		var team = this.Name;
		if (QryString("team", "") != this.Name.replace(" ", ""))
		{
			team = "<a href='" + GetTeamPageLink(this.Name) + "' >" + this.Name + "</a>";
		}
		return team;
	};
	this.AddGame = function (game)
	{
		this.Games[this.Games.length] = game;
		this.Schedule[this.Schedule.length] = game;
	};
	this.AddBye = function (week)
	{
		this.Byes[this.Byes.length] = week;
		this.Schedule[this.Schedule.length] = week;
	};
	this.AddSponsor = function(sponsor)
	{
		this.Sponsors[this.Sponsors.length] = sponsor;
		this.HasSponsor = true;
	};
	this.AddNewsEvent = function(newsEvent)
	{
		this.NewsEvents.AddEvent( newsEvent );
		newsEvent.Team = this;
	};
	this.SetTeamUrl = function(teamUrl)
	{
		if (teamUrl.length > 0)
		{
			this.HasTeamUrl = true;
			this.TeamUrl = teamUrl;
		}
	};
	this.SetTeamPic = function(imgUrl)
	{
		if (imgUrl.length > 0)
		{
			this.HasTeamPic = true;
			this.TeamPicUrl = imgUrl;
		}
	};
	this.CompletedGamesCount = function()
	{
		return (this.Wins + this.Losses);
	};
	this.GetWinPct = function()
	{
		if (this.CompletedGamesCount() == 0)
			return -1;
		return (this.Wins) / (this.CompletedGamesCount());
	};
}

function TeamMember(firstName, lastName)
{
	this.FirstName = firstName;
	this.LastName = lastName;
	this.FullName = firstName + " " + lastName;
}

function Sponsor(name, url)
{
	this.Name = name;
	this.Url = url;
	this.HasUrl = (url.length > 0);
	this.Image = null;
	this.HasImage = false;
	this.GetDisplayName = function()
	{
		var name = this.Name;
		if (this.HasUrl)
		{
			name = "<a href='" + this.Url + "' class='Sponsor' target='" + this.Name + "'>" + this.Name + "</a>";
		}
		return name;
	};

	this.SetSponsorImage = function(sponsorImage)
	{
		this.Image = sponsorImage;
		this.HasImage = (sponsorImage.Url.length > 0);
	};

	this.GetSponsorImageLink = function()
	{
		if (this.HasImage)
		{
			var img = "<img src=\"" + this.Image.Url + "\" border='0' ";
			if (this.Image.Width > 0)
			{
				img += " width='" + this.image.Width + "' ";
			}
			if (this.Image.Height > 0)
			{
				img += " Height='" + this.image.Height + "' ";
			}
			img += " />";
			if (this.HasUrl)
			{
				img = "<a href='" + this.Url + "' target='" + this.Name + "'>" + img + "</a>";
			}
			return img;
		}
	};
}

function SponsorImage(url, width, height)
{
	this.Url = url;
	this.Width = width;
	this.Height = height;
}

function RockColour(name)
{
	this.Name = name;
}

Red = new RockColour("Red");
Blue = new RockColour("Blue");

function LineScore(endScores)
{
	this.EndScores = endScores;
	this.FinalScore = 0;
	for (var i = 0; i < endScores.length ; i++ )
	{
		this.FinalScore += endScores[i];
	}
}

function Bye(team)
{
	this.Type = "Bye";
	this.Team = team;
	this.Week = null;

	team.AddBye(this);
}

function Game(sheetNumber, redTeam, blueTeam, isPlayoffs)
{
	this.Type = "Game";
	this.IsBye = false;
	this.SheetNumber = sheetNumber;
	this.RedTeam = redTeam;
	this.RedLineScore = [];
	this.BlueTeam = blueTeam;
	this.BlueLineScore = [];
	this.IsFinished = false;
	this.WinningTeam = null;
	this.Week = null;
	if (isPlayoffs)
	{
		this.IsPlayoffs = isPlayoffs;
	}
	else
		this.IsPlayoffs = false;

	redTeam.AddGame(this);
	blueTeam.AddGame(this);

	this.SetScore = function (redLineScore, blueLineScore)
	{
		this.RedLineScore = redLineScore;
		this.BlueLineScore = blueLineScore;
		this.IsFinished = true;
		this.WinningTeam = (redLineScore.FinalScore > blueLineScore.FinalScore) ? this.RedTeam : this.BlueTeam;
		if (!this.IsPlayoffs)
		{
			if (this.RedTeam == this.WinningTeam)
			{
				this.RedTeam.Wins++;
				this.BlueTeam.Losses++;
			}
			else
			{
				this.RedTeam.Losses++;
				this.BlueTeam.Wins++;
			}
		}
	};

	this.DisplayAsTableRow = function()
	{
		var txt = "";
		txt += "<tr>";
		txt += "<td rowspan='2'>" + this.SheetNumber + "</td>";

		var winnerStyle = "style='font-weight:bold;'";

		var style = "";
		if (this.IsFinished && this.WinningTeam == this.RedTeam)
		{
			style = winnerStyle;
		}
		txt += "<td " + style + ">" + this.RedTeam.Name + "</td>";
		txt += "<td " + style + ">" + ((this.IsFinished) ? this.RedLineScore.FinalScore : "&nbsp;") + "</td>";
		txt += "</tr>";

		style = "";
		if (this.IsFinished && this.WinningTeam == this.BlueTeam)
		{
			style = winnerStyle;
		}
		txt += "<tr>";
		txt += "<td " + style + ">" + this.BlueTeam.Name + "</td>";
		txt += "<td " + style + ">" + ((this.IsFinished) ? this.BlueLineScore.FinalScore : "&nbsp;") + "</td>";
		txt += "</tr>";

		document.writeln(txt);
	};
}

function Week(name, date)
{
	this.Name = name;
	this.GameDate = date;
	this.Games = [];
	this.Byes = [];
	this.PrevWeek = null;
	this.NextWeek = null;
	this.RecapUrl = "";

	this.Events = [];

	this.AddGame = function(game)
	{
		this.Games[this.Games.length] = game;
		game.Week = this;
		this.Events[this.Events.length] = game;
	};

	this.AddBye = function(bye)
	{
		this.Byes[this.Byes.length] = bye;
		bye.Week= this;
		this.Events[this.Events.length] = bye;
	};
	this.DisplayGames = function()
	{
		document.writeln("<table>");
		for (var i = 0; i < this.Games.length; i++)
		{
			this.Games[i].DisplayAsTableRow();
		}
		document.writeln("</table>");
	};
	this.GetHasStarted = function()
	{
		for (var i = 0; i < this.Games.length; i++)
		{
			if (this.Games[i].IsFinished)
			{
				return true;
			}
		}
	};
}

function GameBlock(name)
{
	this.Name = name;
	this.GroupA = new Group("Group A");
	this.GroupB = new Group("Group B");
	this.Schedule = new Schedule();

	this.AddWeek = function(week)
	{
		this.Schedule.AddWeek(week);//s[this.Weeks.length] = week;
	};
}

function Schedule()
{
	this._CurrentWeek = null;
	this._NextWeek = null;
	this._LastWeek = null;

	this.Weeks = [];

	this.AddWeek = function(week)
	{
		this.Weeks[this.Weeks.length] = week;
	};

	this.GetCurrentWeek = function()
	{
		if (this._CurrentWeek == null)
		{
			this.PopulateWeeks();
		}
		return this._CurrentWeek;
	};
	this.GetNextWeek = function()
	{
		if (this._NextWeek == null)
		{
			this.PopulateWeeks();
		}
		return this._NextWeek;
	};
	this.GetLastWeek = function()
	{
		if (this._LastWeek == null)
		{
			this.PopulateWeeks();
		}
		return this._LastWeek;
	};

	this.PopulateWeeks = function()
	{
		var curr_date = new Date();
		var currWeek, nextWeek;
		var lastWeek = null;
		var week; 
		for (var i = 0; i < CurrentBlock.Schedule.Weeks.length ; i++ )
		{
			week = CurrentBlock.Schedule.Weeks[i];

			var daysApart = Math.round( (week.GameDate - curr_date) / 86400000);

			if (daysApart >= 0)// && daysApart < 7)
			{
				currWeek = week;
				if ((i + 1) < CurrentBlock.Schedule.Weeks.length)
					nextWeek = CurrentBlock.Schedule.Weeks[i + 1];
				else
					nextWeek = null;
				break;
			}
			lastWeek = week;
		}
		this._CurrentWeek = currWeek;
		this._LastWeek = lastWeek;
		this._NextWeek = nextWeek;
	};


}

function NewsEventHtml(date, subject, html)
{
	this.Type = "Html";
	this.Date = new Date(date);
	this.Subject = subject;
	this.Html = html;

	// Team's AddNews method
	// will set this association
	this.Team = null;

	this.ToString = function()
	{
		return this.Html;
	};
}

function NewsEventSpeil(date, team, result, speil, money)
{
	this.Type = "Speil";
	this.Date = new Date(date);
	this.Subject = team.Name;
	this.Result = result;
	this.Speil = speil;
	this.Team = team;
	if (money)
		this.Money = money;
	else
		this.Money = null;

	this.ToString = function()
	{
		var html = this.Team.GetDisplayName() + " ";
		if (result == "Winner")
			html += "<b>Winner</b>";
		else
			html += result;

		html += " " + this.Speil.GetDisplayHtml();
		return html;
	};
}

function Speil(startDate, endDate, name, url)
{
	this.StartDate = new Date(startDate);
	if (endDate != null)
		this.EndDate = this.StartDate;
	else
		this.EndDate = new Date(endDate);

	this.Name = name;

	if (url)
	{
		this.Url = url;
	}
	else
		this.Url = "";

	this.GetDisplayHtml = function()
	{
		if (this.Url.length > 0)
		{
			return "<a href='" + this.Url + "' target='Event'>" + this.Name + "</a>";
		}
		else
			return this.Name;
	};
}



function PCL()
{
	this.NewsEvents = new NewsEventsContainer();
	this.AddEvent = function(event)
	{
		this.NewsEvents.AddEvent(event);
	};
}

function NewsEventsContainer()
{
	this.Events = [];

	this.AddEvent = function(event)
	{
		this.Events[this.Events.length] = event;
	};
	this.Sort = function(ascending)
	{
		if (ascending)
			this.Events.sort(SortEventByDateAscending);
		else
			this.Events.sort(SortEventByDateDescending);
	};
}
/*
var bool = 0;
var doc = null;
var enabled = false;
function WriteToWin(txt)
{
	if (enabled)
	{
		if (doc == null)
		{
			var win = window.open("");
			doc = win.document;
			doc.open();
		}
		doc.writeln(txt + "<br/>");
	}
}
*/
function SortEventByDateDescending(eventA, eventB)
{
	var dA =eventA.Date;
	var dB = eventB.Date;
	//bool++;
	//	WriteToWin(dA + "," + dB + ", " + (dA > dB) + ", " + (dA < dB));

	if (dA > dB)
	{
		return -1;
	}
	else if (dA < dB)
	{
		return 1;
	}
	else
	{
		if (eventA.Subject > eventB.Subject)
			return 1;
		else
			return -1;
	}
};

function SortEventByDateAscending(eventA, eventB)
{
	var dA = eventA.Date;
	var dB = eventB.Date;
	if (dA > dB)
	{
		return 1;
	}
	else if (dA < dB)
	{
		return -1;
	}
	else
	{
		if (eventA.Subject > eventB.Subject)
			return 1;
		else
			return -1;
	}
};

// this is strange, but i need
// it for the display loop
function DatesEqual(dateA, dateB)
{
	dateA = new Date(dateA);
	dateB = new Date(dateB);
	if (dateA.getYear() == dateB.getYear() && dateA.getMonth() == dateB.getMonth() && dateA.getDate() == dateB.getDate())
	{
		return true;
	}
	return false;
}

function sortTeam(TeamA, TeamB)
{
	if (TeamA.GetWinPct() == TeamB.GetWinPct())
	{
		if (TeamA.Wins != TeamB.Wins)
		{
			return TeamB.Wins - TeamA.Wins;
		}
		if (TeamA.Losses != TeamB.Losses)
		{
			return TeamA.Losses - TeamB.Losses;
		}
		if (TeamA.Name > TeamB.Name)
			return 1;
		else
			return -1;
	}

	return TeamB.GetWinPct() - TeamA.GetWinPct();
}

function sortTeamByName(TeamA, TeamB)
{
	if (TeamA.Name > TeamB.Name)
		return 1;
	else
		return -1;
}


function QryString(name,defaultVal)
{
	var pattern="[?&]"+name+"=([^&]*)";
	var regex=new RegExp(pattern,"ig");
	var qry=location.search;
	var ret="";
	if(regex.test(qry))
	{
		var ret=regex.exec(qry);
		ret=RegExp.$1;
	}
	else
	{
		if(defaultVal!=null)
			ret=defaultVal;
	}
	return ret;
}

function GetSiteLink(page)
{
	var url = location.href;
	var path = "";
	if (url.indexOf("/html/") == -1)
	{
		if (page == "index.htm")
		{
			path = "";
		}
		else
			path = "./html/";
	}
	else
	{
		if (page == "index.htm")
		{
			path = "../";
		}
		else
			path = "";
	}
	return path + page;
}

function GetLastUpdated()
{
	var d1 = new Date(document.lastModified);
	var d2 = new Date(SCHEDULE_LAST_UPDATED);

	return (d1 > d2)
		? GetFriendlyDateTime(d1) : GetFriendlyDateTime(d2);
}

function GetTeamPageLink(teamName)
{
	//var url = GetSiteLink("teams.htm");
	var url = "/teams/team-" + teamName.toLowerCase();
	return url;
//	return url + "?team=" + teamName.replace(" ", "");
}

var month_names = new Array("Jan", "Feb", "Mar", 
"Apr", "May", "Jun", "Jul", "Aug", "Sep", 
"Oct", "Nov", "Dec");

var day_names = new Array("Sun", "Mon", "Tue",
"Wed", "Thu", "Fri", "Sat");

