﻿function createResultsTable(json) {
    selectLeagueMenu = new Array();
    var htmlTop = "";
    var htmlBottom = "";
    var sumBrojMeceva = 0;
    var currBrojMeceva = 0;
    var lastLeagueHeader = "";
    var poceoBottomDiv = false;
    htmlTop += "<table cellpadding='0' cellspacing='0'>";
    htmlTop += "<colgroup>" +
                "<col width='37' />" +
                "<col width='130' />" +
                "<col width='130' />" +
                "<col width='88' />" +
                "<col width='32' />" +
                "<col width='28' />" +
                "<col width='22' />" +
                "<col width='25' />" +
                "<col width='45' />" +
                "<col width='120' />" +
            "</colgroup>";
    htmlTop += "<tr class='t_head'>" +
                "<td><span>&nbsp;</span></td>" +
                "<td><span>" + getResultTableHeadTranslations("Home") + "</span></td>" +
                "<td><span>" + getResultTableHeadTranslations("Away") + "</span></td>" +
                "<td><span>" + getResultTableHeadTranslations("Score") + "</span></td>" +
                "<td><span>" + getResultTableHeadTranslations("Stats") + "</span></td>" +
                "<td><span>" + getResultTableHeadTranslations("Info") + "</span></td>" +
                "<td><span>" + getResultTableHeadTranslations("Tv") + "</span></td>" +
                "<td><span>" + getResultTableHeadTranslations("Live") + "</span></td>" +
                "<td><span>" + getResultTableHeadTranslations("Odds") + "</span></td>" +
                "<td><span>" + getResultTableHeadTranslations("Comment") + "</span></td>" +
            "</tr>";
    htmlBottom = htmlTop;
    jQuery.each(json, function () {
        var dodajHeader = true;
        var country = this.Country;
        var league = this.ContestName;
        var o = new Object();
        o.Name = this.ContestName;
        o.Country = this.Country;
        o.ID = this.SubContestId;
        selectLeagueMenu.push(o);
        if (lastOpenID != null && lastOpenID != "-1") {
            if (this.SubContestId != lastOpenID) {
                return true;
            }
        }
        sumBrojMeceva += this.Event.length
        lastLeagueHeader = "";
        lastLeagueHeader = createLeagueHeader(this);
//        if (currBrojMeceva <= 10) {
//            htmlTop += lastLeagueHeader;
//        } else {
//            htmlBottom += lastLeagueHeader;
//            poceoBottomDiv = true;
//        }
        jQuery.each(this.Event, function () {
            if (typeOfPage == "live" || typeOfPage == "all" || typeOfPage == "results" || typeOfPage == "upcoming" || typeOfPage == "forDay" || typeOfPage == "league" ||
                (typeOfPage == "stat_fixt" && this.Comment == "NSY") || (typeOfPage == "stat_results" && this.Comment != "NSY")) {
                if (currBrojMeceva <= 10) {
                    if (dodajHeader) {
                        htmlTop += lastLeagueHeader;
                        dodajHeader = false;
                    }
                } else {
                    if (dodajHeader) {
                        htmlBottom += lastLeagueHeader;
                        poceoBottomDiv = true;
                        dodajHeader = false;
                    }
                }
                var htmlEvent = createEvent(this, currBrojMeceva, o.ID, country, league);
                if (currBrojMeceva <= 10) {
                    htmlTop += htmlEvent;
                } else if (currBrojMeceva == 11) {
                    if (!poceoBottomDiv) {
                        htmlBottom += lastLeagueHeader;
                        poceoBottomDiv = true;
                    }
                    htmlBottom += htmlEvent;
                } else {
                    htmlBottom += htmlEvent;
                }
                currBrojMeceva++;
            }
        });
    });
    htmlTop += "</table>";
    if (poceoBottomDiv)
        htmlBottom += "</table>";
    else
        htmlBottom = "";
    if (!poceoBottomDiv && currBrojMeceva == 0) {
        if (typeOfPage == "stat_fixt" || typeOfPage == "stat_results") {
            //jQuery("#results_top").append('Sorry, there are no matches for selected league!');
            jQuery("#results_top").append('Keine Spiele für ausgewählte Liga!');
        }
    } else {
        jQuery("#results_top").html(htmlTop);
        jQuery("#results_bottom").html(htmlBottom);
    }
}

function createLeagueHeader(jsonContest) {
    var html = "";
    html += "<tr class='t_league'>" +
                "<td class='flag'><img width='16px' height='11px' src='" + siteURL + "/Styles/images/flags/" + jsonContest.CountryEng.replace(/ /g, '') + ".png' alt='" 
                + jsonContest.Country + "' /></td>" +
                "<td colspan='9'><a href='" + siteURL + "/" + sportTranslation + "/" + jQuery.trim(jsonContest.Country).replace(/ /g, '_') + "/"
                    + jQuery.trim(jsonContest.ContestName).replace(/ /g, '_') + "/" + getResultTableHeadTranslations("urlLeagueTable") + "/" +
                    + jsonContest.SubContestId + "/0'>" + jsonContest.Country + " - " + jsonContest.ContestName + "</a></td>" +
            "</tr>";
    return html;
}

function createEvent(jsonEvent, currPosition, subConID, country, league) {
    var html = "";
    var trClass = "odd";
    var minuteClass = "t_minute";
    var scoreClass = "t_result";
    var h = jsonEvent.HomeScore;
    var a = jsonEvent.AwayScore;
    var rezultat = h + ":" + a;
    var tv = "";
    var liveTvStream = "";
    var odds = "";
    var datum = jsonEvent.Date;
    if (isEven(currPosition)) {
        trClass = "even";
    }
    var minute = jsonEvent.Minute;
    if (!jsonEvent.Live) {
        try {
            var tArray = minute.toString().split(':');
            var cas = tArray[0];
            var min = tArray[1];
            var dat = Date.today().set({ hour: parseInt(cas), minute: parseInt(min) });
            dat.add({ hour: currTimeZone });
            minute = dat.toString("HH:mm");
        } catch (er) { }
        if (jsonEvent.Live) {
            minute = minute + "'";
            minuteClass = "t_minute live";
            scoreClass = "t_result live";
        }
    } else {
        minute = minute + "'";
        minuteClass = "t_minute live";
        scoreClass = "t_result live";
    }
    if (h == "" && a == "") {
        rezultat = "-:-";
    } else {
        rezultat = jsonEvent.HomeScoreRegular + ":" + jsonEvent.AwayScoreRegular;
    }
    var poluvreme = "";
    if (jsonEvent.HomeScore1P != "" && jsonEvent.AwayScore1P != "") {
        poluvreme = "(" + jsonEvent.HomeScore1P + ":" + jsonEvent.AwayScore1P + ")";
    } else {
        poluvreme = "(-:-)";
    }
    var statsHtml = "<a href='" + siteURL + "/" + sportTranslation + "/" + jQuery.trim(country).replace(/ /g, '_').replace(/\./g, '') + "/"
                    + jQuery.trim(league).replace(/ /g, '_').replace(/\./g, '') + "/" + getResultTableHeadTranslations("urlMatchStats") + "/" + jQuery.trim(jsonEvent.Home).replace(/ /g, '_').replace(/\./g, '').replace(/\//g, '-') + "/"
                    + jQuery.trim(jsonEvent.Away).replace(/ /g, '_').replace(/\./g, '').replace(/\//g, '-') + "/"
                    + subConID + "/" + jsonEvent.HomeID + "/" + jsonEvent.AwayID + "'><img src='" + siteURL + "/Styles/images/stats.png' alt='stats' /></a>";
    var homeTeam = "<a href='" + siteURL + "/" + sportTranslation + "/" + jQuery.trim(country).replace(/ /g, '_').replace(/\./g, '') + "/"
                    + jQuery.trim(league).replace(/ /g, '_').replace(/\./g, '') + "/" + getResultTableHeadTranslations("urlTeamStats") + "/"
                    + subConID + "/" + jQuery.trim(jsonEvent.Home).replace(/ /g, '_').replace(/\./g, '').replace(/\//g, '-') + "/" + jsonEvent.HomeID + "'>"
                    + jsonEvent.Home + "</a>";
    var awayTeam = "<a href='" + siteURL + "/" + sportTranslation + "/" + jQuery.trim(country).replace(/ /g, '_').replace(/\./g, '') + "/"
                    + jQuery.trim(league).replace(/ /g, '_').replace(/\./g, '') + "/" + getResultTableHeadTranslations("urlTeamStats") + "/"
                    + subConID + "/" + jQuery.trim(jsonEvent.Away).replace(/ /g, '_').replace(/\./g, '').replace(/\//g, '-') + "/" + jsonEvent.AwayID + "'>"
                    + jsonEvent.Away + "</a>";
    if (jsonEvent.Tv == "1") {
        tv = "<a href='" + siteURL + "/" + sportTranslation + "/" + jQuery.trim(country).replace(/ /g, '_').replace(/\./g, '') + "/"
                    + jQuery.trim(league).replace(/ /g, '_').replace(/\./g, '') + "/" + getResultTableHeadTranslations("urlTv") + "/" + jQuery.trim(jsonEvent.Home).replace(/ /g, '_').replace(/\./g, '').replace(/\//g, '-') + "/"
                    + jQuery.trim(jsonEvent.Away).replace(/ /g, '_').replace(/\./g, '').replace(/\//g, '-') + "/"
                    + jsonEvent.EventId + "'><img src='" + siteURL + "/Styles/images/tv.png' alt='tv' /></a>";
    }
    if (jsonEvent.LiveStream == "1" || jsonEvent.LiveStream == "True") {
        liveTvStream = "<a href='" + siteURL + "/" + sportTranslation + "/" + jQuery.trim(country).replace(/ /g, '_').replace(/\./g, '') + "/"
                    + jQuery.trim(league).replace(/ /g, '_').replace(/\./g, '') + "/" + getResultTableHeadTranslations("urlLiveStream") + "/" + jQuery.trim(jsonEvent.Home).replace(/ /g, '_').replace(/\./g, '').replace(/\//g, '-') + "/"
                    + jQuery.trim(jsonEvent.Away).replace(/ /g, '_').replace(/\./g, '').replace(/\//g, '-') + "/"
                    + jsonEvent.EventId + "'><img src='" + siteURL + "/Styles/images/live-stream.png' alt='Live' /></a>";
    }
    if (jsonEvent.FinishedFull == "0") {
        odds = "<a href='" + siteURL + "/" + sportTranslation + "/" + jQuery.trim(country).replace(/ /g, '_').replace(/\./g, '') + "/"
                    + jQuery.trim(league).replace(/ /g, '_').replace(/\./g, '') + "/" + getResultTableHeadTranslations("urlOdds") + "/" + jQuery.trim(jsonEvent.Home).replace(/ /g, '_').replace(/\./g, '').replace(/\//g, '-') + "/"
                    + jQuery.trim(jsonEvent.Away).replace(/ /g, '_').replace(/\./g, '').replace(/\//g, '-') + "/"
                    + jsonEvent.EventId + "'><img src='" + siteURL + "/Styles/images/odds.png' alt='odds' /></a>";
    }
//    var clickEvent = "openInfo(\"" + jsonEvent.EventId + "\"," +
//                            "\"" + jsonEvent.Home + "\"," + 
//                            "\"" + jsonEvent.Away + "\"," +
//                            "\"" + league + "\"," +
//                            "\"" + country + "\"," +
//                            "\"" + h + "\"," + 
//                            "\"" + a + "\"," +
//                            "\"" + jsonEvent.HomeScore1P + "\"," +
//                            "\"" + jsonEvent.AwayScore1P + "\"," +
//                            "\"" + jsonEvent.Date + "\"," +
    //                            "\"" + jsonEvent.CommentTranslation + "\")";
//    var clickEvent = "openInfo(\"" + jsonEvent.EventId + "\",\"" + jsonEvent.Home.replace(/ /g, "%20").replace(/\n/g, "%20") + "\",\"" +
//                jsonEvent.Away.replace(/ /g, "%20").replace(/\n/g, "%20") +
//                "\",\"" + league.replace(/ /g, "%20").replace(/\n/g, "%20") + "\",\"" + country.replace(/ /g, "%20").replace(/\n/g, "%20") + "\",\"" + h + "\",\"" + a + "\",\"" +
//                jsonEvent.HomeScore1P + "\",\"" + jsonEvent.AwayScore1P +
//                            "\",\"" + $.trim(jsonEvent.Date.split("\n")[0]) + "\",\"" + $.trim(jsonEvent.Date.split("\n")[1]) + "\",\"" + $.trim(jsonEvent.StartTime)
    //                            + "\",\"" + jsonEvent.Comment.replace(/ /g, "%20").replace(/\n/g, "%20") + "\");";
    var clickEvent = "openInfo(\"" + jsonEvent.EventId + "\",\"" + escape(jsonEvent.Home) + "\",\"" +
                escape(jsonEvent.Away) +
                "\",\"" + escape(league) + "\",\"" + escape(country) + "\",\"" + h + "\",\"" + a + "\",\"" +
                jsonEvent.HomeScore1P + "\",\"" + jsonEvent.AwayScore1P +
                            "\",\"" + $.trim(jsonEvent.DateTranslated.split("\n")[0]) + "\",\"" + $.trim(jsonEvent.DateTranslated.split("\n")[1]) + "\",\"" + $.trim(jsonEvent.StartTime)
                            + "\",\"" + escape(jsonEvent.CommentTranslation) + "\");";

    html +="<tr class='" + trClass + "'>" +
                "<td class='" + minuteClass + "' title='" + datum + "'><span>" + minute + "</span></td>" +
                "<td class='t_home_team'>" + homeTeam + "</td>" +
                "<td class='t_away_team'>" + awayTeam + "</td>" +
                "<td class='" + scoreClass + "' onclick='" + clickEvent + "' style='cursor:pointer;'><span>" + rezultat + poluvreme + "</span></td>" +
                "<td class='t_stats'>" + statsHtml + "</td>" +
                "<td class='t_info' onclick='" + clickEvent + "'><img src='" + siteURL + "/Styles/images/info.png' alt='info' /></a></td>" +
                "<td class='t_tv'>" + tv + "</td>" +
                "<td class='t_tv'>" + liveTvStream + "</td>" + 
                "<td class='t_odds'>" + odds + "</td>" +
                "<td class='t_comment'><span>" + jsonEvent.CommentTranslation + "</span></td>" + 
            "</tr>";
    return html;
}
