﻿$.fn.Articles = 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 art = new Articles(data, opts);

            var html = "<div class=\"block\" id=\"articles\">";
            html += art.Print();
            html += "</div>";
            $(container).append(html);
        }
    });
};

function Articles(xml, opts) {
    this.xml = xml;
    var data;
    
    $(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;
                }
            });
        }       
    });

    this.Print = function() {
        var html = "";
        var ary = [];

        if ($(data)[0].nodeName == "Season") {

            //find each 'item' in the file and parse it
            $(data).find('Tournament').each(function() {

                var $item = $(this);
                var article = ""

                // Heading
                article += "<h3><a href=\"#\">";
                article += $item.attr("Name");
                article += "</a></h3>";
                article += "<h4></h4>";

                // Date
                article += "<p class=\"meta\">" + $item.attr("Date") + "</p>";

                nocomment = true;
                children = $item[0].childNodes;
                var cnt = children.length;
                for (var i = 0; i < cnt; i++) {
                    if (children[i].nodeName == "Comment") {

                        //                        $(this).find('Image').each(function() {
                        //                            article += "<a href=\"#\" class=\"image\"><img src=\"images/" + children[i].text + "\" width=\"60\" height=\"60\" alt=\"photo\" /></a>";
                        //                        });

                        article += "<p>" + children[i].text + "</p>";
                        nocomment = false;
                    }
//                    else if (children[i].nodeName == "Image") {
//                        article += "<a href=\"#\" class=\"image\"><img src=\"images/" + children[i].text + "\" width=\"60\" height=\"60\" alt=\"photo\" /></a>";
//                    }
                }

                ary[ary.length] = article;
            });
        }
        else if ($(data)[0].nodeName == "Tournament") {
            if ($(data).attr("Type") == "Cup") {
                $(data).find('Round').each(function() {
                    //article = RoundComment($(this));
                    if (article != null)
                        ary[ary.length] = article;

                    //find each 'item' in the file and parse it
                    $(this).find('Game').each(function() {

                        article = GameComment($(this));
                        if (article != null)
                            ary[ary.length] = article;
                    });
                });
            }
            else {
                //find each 'item' in the file and parse it
                $(data).find('Game').each(function() {

                    article = GameComment($(this));
                    if (article != null)
                        ary[ary.length] = article;
                });
            }
        }

        for (i = ary.length - 1; i >= 0; i--) {
            if (i == ary.length)
                html += "<div class=\"first article\">";
            else
                html += "<div class=\"article\">";
            html += ary[i];
            html += "</div>";
        }

        return html;
    }
}

function RoundComment( obj) {
    var article = "";
    var heading = "";
    nocomment = true;
    
    // Heading
    heading += "<h3><a href=\"#\">Round ";
    heading += obj.attr("No");
    heading += "<br /> ";
    heading += "</a></h3>";
    heading += "<h4></h4>";
    heading += "<p class=\"meta\"></p>";
    
    obj.find('Comment').each(function() {
        nocomment = false;
        article += heading;

        $(this).find('Image').each(function() {
            article += "<span style=\"float:left;\">"
            article += "<a href=\"#\" class=\"image\"><img src=\"images/" + $(this).attr("Name") + "\" width=\"60\" height=\"60\" alt=\"photo\" /></a>";
            article += "</span>";
        });

        article += "<p>" + obj.text() + "</p>";
    });
    
    if( nocomment == false)
        return article;
    return null;
}

function GameComment(obj) {

    var $item = obj;
    var date = $item.attr("Date");
    var article = ""
    var heading = "";
    
    // Heading
    heading += "<h3><a href=\"#\">";
    $item.find('Team').each(function() {
        heading += $(this).attr("Name");
        heading += " ";
        heading += $(this).attr("Goals");
        heading += "<br /> ";
    });
    heading += "</a></h3>";

    // Date
    heading += "<p class=\"meta\">" + date + "</p>";

    nocomment = true;
    $item.find('Comment').each(function() {
        nocomment = false;
        article += heading;

        $(this).find('Image').each(function() {
            article += "<a href=\"#\" class=\"image\"><img src=\"img/photo_60x60.jpg\" width=\"60\" height=\"60\" alt=\"photo\" /></a>";
        });

        $item.find('Goal').each(function() {
            article += $(this).attr("Name") + " " + $(this).attr("No") + "<br />";
        });

        article += "<p>" + $(this).text() + "</p>";
    });    

    if (nocomment == false)
        return article;
    return null;
}
