﻿///<reference path="jquery-1.2.6.pack-vsdoc.js" />


function showUserLeagueTable(btnShow) {
    var table = $("#userTable");
    var teams = new Array();
    var userMatchesById = new Array();

    $.getJSON("../js_css/" + liga + "ligaTymy.txt", function(jsonTeams) {

        $.each(userMatches, function(i, match) {
            userMatchesById[match.id] = { "GD": match.GD, "GH": match.GH };
        });

        $.each(jsonTeams, function(i, team) {
            teams[team.tymId] = { "tymId": team.tymId, "nazev": team.nazev, "V": 0, "R": 0, "P": 0, "GV": 0, "GO": 0, "B": 0 };
        });

        $.getJSON("../js_css/" + liga + "ligaVysl.txt", function(data) {
            $.each(data, function(i, match) {

                var domaci = teams[match.D];
                var hoste = teams[match.H];

                var GD = match.GD;
                var GH = match.GH;

                if (match.D == userTymId || match.H == userTymId) {
                    if (userMatchesById[match.id].GD != null) {
                        GD = userMatchesById[match.id].GD;
                        GH = userMatchesById[match.id].GH;
                    }
                }

                //goly
                domaci.GV += GD;
                domaci.GO += GH;
                hoste.GV += GH;
                hoste.GO += GD;

                if (GD == GH) {
                    //body
                    domaci.B++;
                    hoste.B++;

                    //V R P
                    domaci.R++;
                    hoste.R++;
                }
                else if (GD > GH) {
                    //body
                    domaci.B += 3;

                    //V R P
                    domaci.V++;
                    hoste.P++;
                }
                else if (GD < GH) {
                    //body
                    hoste.B += 3;

                    //V R P
                    domaci.P++;
                    hoste.V++;
                }
            });

            teams.sort(seraditPodleBodu);
            var i = 1;
            var userTable = $("#userTableRows");

            for (var teamId in teams) {
                var team = teams[teamId];
                var cssClass = i < 3 ? " class=\"postup\"" : (i > 14 ? " class=\"sestup\"" : "");
                if (team.tymId == userTymId)
                    cssClass = " class=\"vybranyKlub\"";

                userTable.append("<tr" + cssClass + "><td>" + (i++) + ". </td>" +
                "<td style=\"text-align:left;\"><div style=\"padding:2px 5px; float:left;\">" +
                "<img src='../images/miniZnaky/" + team.tymId + ".jpg' /></div>" +
                "<div style=\"padding:2px;\">" + team.nazev + "</div></td>" +
                "<td>" + (team.V + team.R + team.P) + "</td>" +
                "<td>" + team.V + "</td>" +
                "<td>" + team.R + "</td>" +
                "<td>" + team.P + "</td>" +
                "<td>" + team.GV + ":" + team.GO + "</td>" +
                "<td>" + team.B + "</td></tr>");
            }

            $("#userTable").show();
            $(btnShow).hide();
        });
    });
}

function seraditPodleBodu(a, b){
    return b.B - a.B;
}


function ukaz(element, x) {
    var el = document.getElementById(element);
    var el2 = document.getElementById(element + '_');
    el.style.display = "block";
    el2.style.backgroundPosition = x + "px bottom";

}
function skri(element, x) {
    var el = document.getElementById(element);
    var el2 = document.getElementById(element + '_');
    el.style.display = "none";
    el2.style.backgroundPosition = x + "px top";
}

function sw(obj) {
    var obj = document.getElementById(obj);
    var el = document.getElementById('prvaliga');
    var el2 = document.getElementById('druhaliga');
    var table = document.getElementById('prvaliga_');
    var table2 = document.getElementById('druhaliga_');

    if (obj == el) {
        el2.className = 'al';
        el.className = 'ac';
        table.style.display = 'block';
        table2.style.display = 'none';
    } else if (obj == el2) {
        el.className = 'al';
        el2.className = 'ac';
        table2.style.display = 'block';
        table.style.display = 'none';
    }
}


function pollSubmit(id) {
    $.ajax({
        type: "POST",
        url: "http://www.fotbaltip.cz/services/poll.asmx/GetPollAnswerService",
        data: "{'answerID': " + id + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result, context) {
            var data = eval('(' + result.d + ')');
            loadPollResults(data);
        }
    });
}

function loadPollResults(data) {
    var total_votes = parseInt(data.TotalVotes);
    var results_html = "";
                
    for (var i = 0; i < data.Answers.length; i++) {
        percent = Math.round((parseInt(data.Answers[i].VoteCount) / parseInt(total_votes)) * 100);
        results_html += "<li>" + data.Answers[i].Answer + "<br /><div class=\"ind\" style=\"width:" + percent + "%;\"><div></div></div><span class=\"p\">" + percent + "%</span></li>\n";
    }
    //results_html += "</dl><p>Celkem hlasovalo: " + total_votes + "</p>\n";

    $("#pollId ul").empty().append(results_html);
    animateResults();

    $("#TotalVotesSpan").fadeOut("slow", function() {
        $(this).text("");
        $(this).text(total_votes).fadeIn("slow");
    });
}

function animateResults() {
    $("#pollId div.ind").each(function() {
        var percentage = $(this).next().text();
        $(this).css({ width: "0%" }).animate({
            width: Math.round((parseInt(percentage) * 0.85)) + "%"
        }, 'slow');
    });
}