﻿$.fn.Cup = 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 cup = new Cup(data, opts);
            var html = cup.Print();
            $(container).append(html);
        }
    });
};

function Cup(xml, opts) {
    this.xml = xml;
    var data;
    var roundList = new RoundList();

    $(this.xml.responseXML).find('Season').each(function() {
        var $item = $(this);

        if ($item.attr("Name") == opts.season) {
            $item.find('Tournament').each(function() {
                var $tournament = $(this);
                if ($tournament.attr("Name") == opts.tournament && $tournament.attr("Type") == "Cup") {
                    data = $tournament;
                }
                else if ($tournament.attr("Name") == opts.tournament && $tournament.attr("Type") == "Tour") {
                    data = $tournament;
                }
            });
        }
    });

    if ($(data)[0].nodeName == "Tournament") {

        //find each 'item' in the file and parse it
        $(data).find('Round').each(function() {
            roundList.Add(new Round($(this)));
        });
    }
    
    this.Print = function() {
        var html = roundList.Print();
        html += roundList.PrintGames();
        return html;
    }
    
}

function Round(round) {
    var data = round;
    var gameList = new GameList(data.attr("No"));

    if ($(data)[0].nodeName == "Round") {

        //find each 'item' in the file and parse it
        $(data).find('Game').each(function() {

            var game = new Game($(this).attr("No"));
            $(this).find('Team').each(function() {
                game.AddTeam($(this).attr("Name"), $(this).attr("Goals"));
            });

            gameList.Add(game);
        });

    }
    
    this.Print = function() {
        var html = "";
        html = "<tr id='" + data.attr("No") + "'>";
        if( data.attr("Name") != null)
            html += "<td><img src=\"images/plus1.gif\" /> " + data.attr("Name") + "</td>";
        else
            html += "<td><img src=\"images/plus1.gif\" /> " + data.attr("No") + "</td>";
        html += "</tr>";
        return html;
    }

    this.PrintGames = function() {
        var html = "";
        html += gameList.Print();
        return html;
    }    

    this.PrintTable = function() {
        var html = "";
        html += gameList.PrintTable();
        return html;
    }

    this.Goals = function() {
        return gameList.GetGoals();
    }

    this.Games = function() {
        return gameList;
    }
}

function RoundList() {
    this.ary = [];
//    this.goalList = new GoalList();
//    this.bttree = new btTree();

    this.Get = function() {
        if (this.ary.length > 0)
            return this.ary;
        return null;
    }

//    this.GetGoals = function() {
//        return this.goalList;
//    }

    this.Add = function(obj) {
        if (obj instanceof Round && obj != null) {
            var i = this.Exist(obj);
            if (i == -1) {
                this.ary[this.ary.length] = obj;
//                this.goalList.AddList(obj.Goals());

//                var games = obj.Games().ary;
//                for (var j = 0; j < games.length; j++) {
//                    this.bttree.Add(new Node(games[j]));
//                }
            }
        }
    }

    this.Exist = function(obj) {
        if (obj instanceof Round && obj != null) {
            for (var i = 0; i < this.ary.length; i++) {
                //                    if (this.ary[i].squadNo == obj.squadNo)
                //                        return i;
            }
        }
        return -1;
    }

    this.Print = function() {
        var html = "";
        html += "<table id=\"league\" >";
        html += "<thead>";
        html += "<tr>";
        html += "<th>Round</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.PrintGames = function() {
        var html = "";
        for (var i = 0; i < this.ary.length; i++) {
            html += this.ary[i].PrintGames();
        }
        return html;
    }    

    this.PrintTable = function() {
        var html = "<li style='width:100em'><a href='#'>";
        html += this.bttree.Print();
        //html += this.goalList.Print();
        html += "</a></li>";
        return html;
    }
}
