﻿$.fn.Goals = function(options) {
    var defaults = {
        targeturl: "./season20078.xml",
        season: '2009/10',
        tournament: 'Wsyl League'
    }

    var opts = $.extend(defaults, options);
    var container = this;
    var _currentSeason;
    $(container).empty();

    $.ajax({
        type: "GET",
        url: opts.targeturl, //Requesting simple.xml
        dataType: "xml", //Make sure that you specify the type of file you expecting (XML)
        complete: function(data) {

            var goals = new Goals(data, opts);
            var html = "";
            html += goals.Print();

            $(container).append(html);
        }
    });
};

function Goals(xml, opts) {
    this.xml = xml;
    var data;
    var goallist = new GoalList();

    $(this.xml.responseXML).find('Season').each(function() {
        var $item = $(this);

        if ($item.attr("Name") == opts.season) {
            data = $item;

            $item.find('Tournament').each(function() {
                var $tournament = $(this);
                if ($tournament.attr("Name") == opts.tournament) {
                    data = $tournament;
                }
            });
        }
    });


    //find each 'item' in the file and parse it
    $(data).find('Goal').each(function() {
        goallist.Add(new Goal($(this).attr("SquadNo"), $(this).attr("Name"), $(this).attr("No")));
    });


    this.Print = function() {
        return goallist.Print();
    }
}

function GoalList() {
    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 Goal && 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 Goal && obj != null) {

            var idx = this.Exist(obj);
            if (idx != -1) {
                this.ary[idx].Update(obj);
            }
            else {
                this.ary[this.ary.length] = obj;
            }

        }
    }

    this.Print = function() {
        var html = "";
        this.ary.sort(this.sortGoals);

        html += "<table id=\"goal\" >";
        html += "<thead>";
        html += "<tr>";
        html += "<th>Squad No.</th>";
        html += "<th>Name</th>";
        html += "<th>goals</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.sortGoals = function(a, b) {
        return b.goals - a.goals;
    }
}

function Goal(squadno, name, goals) {
    this.squadno = squadno;
    this.name = name;
    this.goals = goals;

    this.Update = function(obj) {
        if (obj instanceof Goal && obj != null) {
            if (this.name == obj.name)
            {
                this.goals = Number(this.goals) + Number(obj.goals);
            }
        }
    }

    this.Equals = function(obj) {
        if (obj instanceof Goal && obj != null) {
            if (this.name == obj.name)
                return true;
        }
        return false;
    }

    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>" + this.squadno + "</td>";
        html += "<td>" + this.name + "</td>";
        html += "<td>" + this.goals + "</td>";

        html += "</tr>";

        return html;
    }
}