﻿function TeamList() {
    this.ary = [];

    this.Get = function(idx) {
        if (idx > this.ary.length)
            return null;
        return this.ary[idx];
    }

    this.Count = function() {
        return this.ary.length;
    }
    
    this.Exist = function(obj) {
        if (obj instanceof Team && obj != null) {
            for (var i = 0; i < this.ary.length; i++) {
                if (this.ary[i].Equals(obj))
                    return i;
            }
        }
        return -1;
    }

    this.Add = function(obj) {
        if (obj instanceof Team && obj != null) {
            this.ary[this.ary.length] = obj;
        }
    }

    this.AddGame = function(obj) {
        if (obj instanceof Game && obj != null) {

            var idx = this.Exist(obj.HomeTeam());
            if (idx != -1) {
                this.ary[idx].AddGame(obj);
            }
            else {
                this.ary[this.ary.length] = obj.HomeTeam();
                this.ary[this.ary.length - 1].AddGame(obj);
            }

            idx = this.Exist(obj.AwayTeam());
            if (idx != -1) {
                this.ary[idx].AddGame(obj);
            }
            else {
                this.ary[this.ary.length] = obj.AwayTeam();
                this.ary[this.ary.length - 1].AddGame(obj);
            }
        }
    }

    this.Print = function() {
        var html = "";
        this.ary.sort(this.sortTeams);

        html += "<table id=\"league\" >";
        html += "<thead>";
        html += "<tr>";
        html += "<th>Name</th>";
        html += "<th>pld</th>";
        html += "<th>w</th>";
        html += "<th>d</th>";
        html += "<th>l</th>";
        html += "<th>f</th>";
        html += "<th>a</th>";
        html += "<th>gd</th>";
        html += "<th>pts</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;
    }

    this.sortTeams = function(a, b) {
        if (b.points == a.points) {
            if (b.gd == a.gd) {
                if (b.won == a.won) {
                    // result between each team
                    return b.beat( a);
                }
                else
                    return b.won - a.won;
            }
            else
                return b.gd - a.gd;
        }
        
        return b.points - a.points;
    }
}

function Team(name, goals) {
    this.name = name;
    this.goals = goals;
    this.games = new GameList(this.name);
    this.pld = 0;
    this.won = 0;
    this.drawn = 0;
    this.lost = 0;
    this.fore = 0;
    this.against = 0;
    this.points = 0;
    this.gd = 0;

    this.GetGames = function() {
        return this.games;
    }
    
    this.AddGame = function(obj) {
        if (obj instanceof Game && obj != null) {
            if (this.name == obj.HomeTeam().name) {
                this.UpdateStats(obj);
                this.games.Add(obj);
                return true;
            }
            else if (this.name == obj.AwayTeam().name) {
                this.UpdateStats(obj);
                this.games.Add(obj);
                return true;
            }
            return false;
        }
    }

    this.UpdateStats = function(obj) {

        var htGoals = Number(obj.HomeTeam().goals);
        if (htGoals != Number.NaN && obj.HomeTeam().goals.length > 0) {
            //    if (obj.HomeTeam().goals != "P" || obj.HomeTeam().goals != "p" || obj.HomeTeam().goals != "?") {
            if (this.name == obj.Winner())
                this.won = Number(this.won) + 1;
            else if (this.name == obj.Looser())
                this.lost = Number(this.lost) + 1;
            else
                this.drawn = Number(this.drawn) + 1;

            if (this.name == obj.HomeTeam().name) {
                this.fore += Number(obj.HomeTeam().goals);
                this.against += Number(obj.AwayTeam().goals);
            }
            else if (this.name == obj.AwayTeam().name) {
                this.fore += Number(obj.AwayTeam().goals);
                this.against += Number(obj.HomeTeam().goals);
            }

            this.gd = Number(this.fore) - Number(this.against);
            this.points = Number(this.won) * 3 + Number(this.drawn);

            this.pld = Number(this.pld) + 1;
        }
    }

    this.Equals = function(obj) {
        if (obj instanceof Team && obj != null) {
            if (this.name == obj.name)
                return true;
        }
        return false;
    }

    this.beat = function(obj) {
        if (obj instanceof Team && obj != null) {
            // find the games where a played b

            if (this.games.beat(obj) > 0)
                return 1;
            else if (this.games.beat(obj) < 0)
                return -1;
        }
        return 0;
    }
    
    this.Print = function() {

        var temp = new Array();
        temp = this.name.split(' ');

        var str = "";
        for (i = 0; i < temp.length; i++) {
            str += temp[i];
        }

        html = "<tr id='" + str + "'>";
        html += "<td class='name'><img src=\"images/plus1.gif\" /> " + this.name + "</td>";
        html += "<td>" + this.pld + "</td>";
        html += "<td>" + this.won + "</td>";
        html += "<td>" + this.drawn + "</td>";
        html += "<td>" + this.lost + "</td>";
        html += "<td>" + this.fore + "</td>";
        html += "<td>" + this.against + "</td>";
        html += "<td class='gd'>" + this.gd + "</td>";
        html += "<td>" + this.points + "</td>";
        html += "</tr>";

        return html;
    }
}