﻿function GameList( name) {
    this.ary = [];
    this.name = name;

    this.beat = function(obj) {
        var gd = 0;
        if (obj instanceof Team && obj != null) {
            for (var i = 0; i < this.ary.length; i++) {

                if (this.ary[i].HomeTeam == obj.name) {
                    gd -= this.ary[i].GoalDiff();
                }
                else if (this.ary[i].AwayTeam == obj.name) {
                    gd += this.ary[i].GoalDiff();
                }
            }
        }
        return gd;
    }
    
    this.Get = function() {
        if (this.ary.length > 0)
            return this.ary;
        return null;
    }

    this.Add = function(obj) {
        if (obj instanceof Game && obj != null) {
            var i = this.Exist(obj);
            if (i == -1) {
                this.ary[this.ary.length] = obj;
            }
        }
    }

    this.Exist = function(obj) {
        if (obj instanceof Game && obj != null) {
            for (var i = 0; i < this.ary.length; i++) {
            }
        }
        return -1;
    }

    this.Print = function() {
        if (this.ary.length != 0) {
            var html = "";

            var temp = new Array();
            temp = this.name.split(' ');

            var str = "";
            for (i = 0; i < temp.length; i++) {
                str += temp[i];
            }

            html += "<table id='" + str + "Games" + "' class='gamesK' >";
            html += "<thead>";
            html += "<tr>";
            html += "<th></th>";
            html += "<th>Home</th>";
            html += "<th>&nbsp;</th>";
            html += "<th>&nbsp;</th>";
            html += "<th>Away</th>";
            html += "</tr>";
            html += "</thead>";
            html += "<tbody>";

            for (var i = 0; i < this.ary.length; i++) {
                html += this.ary[i].Print();
            }

            html += "</tbody>";
            html += "</table>";
            return html;
        }
        return "";
    }
}

function Game(no) {

    var No = no;
    this.teamList = new TeamList();

    this.No = function() {
        if (Number(No) > 0)
            return No;
        return '';
    }
    
    this.HomeTeam = function() {
        return this.teamList.Get(0);
    }

    this.AwayTeam = function() {
        return this.teamList.Get(1);
    }

    this.AddTeam = function(name, goals) {
        this.teamList.Add(new Team(name, goals));
    }

    this.Winner = function() {
    if (this.HomeTeam().goals == "Bye" || this.HomeTeam().goals == "HW-W")
            return this.HomeTeam().name;
        else if (this.AwayTeam().goals == "Bye" || this.HomeTeam().goals == "AW-W")
            return this.AwayTeam().name;

        if (Number(this.HomeTeam().goals) > Number(this.AwayTeam().goals))
            return this.HomeTeam().name;
        else if (Number(this.HomeTeam().goals) < Number(this.AwayTeam().goals))
            return this.AwayTeam().name;
    }

    this.Looser = function() {
    if (this.HomeTeam().goals == "Bye" || this.HomeTeam().goals == "HW-W")
            return this.AwayTeam().name;
        else if (this.AwayTeam().goals == "Bye" || this.HomeTeam().goals == "AW-W")
            return this.HomeTeam().name;

        if (Number(this.HomeTeam().goals) > Number(this.AwayTeam().goals))
            return this.AwayTeam().name;
        else if (Number(this.HomeTeam().goals) < Number(this.AwayTeam().goals))
            return this.HomeTeam().name;
    }

    this.GoalDiff = function() {
        return Number(this.HomeTeam().goals) - Number(this.AwayTeam().goals);
    }
    
    this.Print = function() {
        var html = "";
        html += "<tr><td>" + this.No() + "</td><td>" + this.HomeTeam().name + "</td><td>" + this.HomeTeam().goals + "</td>";
        html += "<td>" + this.AwayTeam().goals + "</td><td>" + this.AwayTeam().name + "</td></tr>";
        return html;
    }

}