﻿$(document).ready(function () {
    var includeMatches = $("#IncludeMatches").val() == "True";

    $("#SportsCombo").change(function () {
        ChangeSport();
    });


    $("#CountryCombo").change(function () {
        ChangeCountry();
    });


    $("#TournamentCombo").change(function () {
        ChangeTournament();
    });

    if (includeMatches == true) {
        var useMatchCode = $("#UseMatchCode").val();

        $("#MatchesCombo").change(function () {
            if ($(this).val() != -1) {
                var sportId = $("#SelectedSportId").val();
                var countryId = $("#SelectedCountryId").val();
                var tournamentId = $("#SelectedTournamentId").val();
                var languageId = $("#CurrentLanguage").val();
                var matchCodeId = $(this).val();

                //H2H içerisinde olacağını biliyoruz direk H2H yönlendirmesi yap
                var urlFormat = useMatchCode ? "/HeadToHead/Index.aspx?matchCode=[matchCode]&languageId=[languageId]" : "/HeadToHead/Index.aspx?matchId=[matchId]&languageId=[languageId]";
                urlFormat = urlFormat.replace("[matchId]", matchCodeId);
                urlFormat = urlFormat.replace("[matchCode]", matchCodeId);
                urlFormat = urlFormat.replace("[languageId]", languageId);
                window.location = urlFormat;
            }
        });
    }
});

function ChangeCountry() {
    var includeMatches = $("#IncludeMatches").val() == "True";

    if ($("#SportsCombo").val() != "-1" && $("#CountryCombo").val() != "-1") {
        $("#SelectedCountryId").val($("#CountryCombo").val());
        $("#SelectedTournamentId").val(null);

        var tour = document.getElementById("TournamentCombo");
        tour.innerHTML = "";
        tour.appendChild(CreateOptionElement($("#ChooseText").val(), "-1"));

        if (includeMatches == true) {
            var matches = document.getElementById("MatchesCombo");
            matches.innerHTML = "";
            matches.appendChild(CreateOptionElement($("#ChooseText").val(), "-1"));
        }

        $("#TournamentCombo").attr("disabled", "disabled");
        $("#MatchesCombo").attr("disabled", "disabled");
        $.ajax({
            url: "/Menu/GetComboTournaments.aspx?rnd=" + Math.random() + "&sportId=" + $("#SportsCombo").val() + "&countryId=" + $("#CountryCombo").val() + "&languageId=" + $("#CurrentLanguage").val() + "&includeMatches=" + includeMatches,
            type: "GET",
            dataType: "text/json",
            success: function (result) {
                result = eval(result);
                tour.innerHTML = "";

                for (var i = 0; i < result.length; i++) {
                    tour.appendChild(CreateOptionElement(result[i].name, result[i].id));
                }

                $("#TournamentCombo").attr("disabled", "");
            },
            error: function (xhr) {
            }
        });
    }
}

function ChangeTournament() {
    var includeMatches = $("#IncludeMatches").val() == "True";
    $("#SelectedTournamentId").val($("#TournamentCombo").val());

    if ($("#SportsCombo").val() != "-1" && $("#CountryCombo").val() != "-1" && $("#TournamentCombo").val() != "-1") {
        var sportId = $("#SelectedSportId").val();
        var countryId = $("#SelectedCountryId").val();
        var tournamentId = $("#SelectedTournamentId").val();
        var languageId = $("#CurrentLanguage").val();

        if (includeMatches == false) {
            //menu içerisindeki formatı kullanarak redirect url oluştur
            var urlFormat = $("#RedirectLocationForLeftMenu").val();
            urlFormat = urlFormat.replace("[sportId]", sportId);
            urlFormat = urlFormat.replace("[countryId]", countryId);
            urlFormat = urlFormat.replace("[tournamentId]", tournamentId);
            urlFormat = urlFormat.replace("[languageId]", languageId);
            window.location = urlFormat;
        }
        else {
            var matches = document.getElementById("MatchesCombo");
            matches.innerHTML = "";
            matches.appendChild(CreateOptionElement($("#ChooseText").val(), "-1"));

            $("#MatchesCombo").attr("disabled", "disabled");
            $.ajax({
                url: "/Menu/GetComboMatches.aspx?rnd=" + Math.random() + "&sportId=" + $("#SportsCombo").val() + "&countryId=" + $("#CountryCombo").val() + "&tournamentId=" + $("#TournamentCombo").val() + "&languageId=" + $("#CurrentLanguage").val() + "&includeMatches=" + includeMatches,
                type: "GET",
                dataType: "text/json",
                success: function (result) {
                    result = eval(result);
                    matches.innerHTML = "";

                    for (var i = 0; i < result.length; i++) {
                        matches.appendChild(CreateOptionElement(result[i].name, result[i].id));
                    }

                    $("#MatchesCombo").attr("disabled", "");
                },
                error: function (xhr) {
                }
            });
        }
    }
}

function ChangeSport() {
    var includeMatches = $("#IncludeMatches").val() == "True";
    if ($("#SportsCombo").val() != "-1") {
        $("#SelectedSportId").val($("#SportsCombo").val());
        $("#SelectedCountryId").val(null);
        $("#SelectedTournamentId").val(null);

        var countries = document.getElementById("CountryCombo");
        countries.innerHTML = "";
        if ($("#SelectedSportId").val() != "22" || $("#SelectedSportId").val() == "4") {
            countries.appendChild(CreateOptionElement($("#ChooseText").val(), "-1"));
        }

        var tour = document.getElementById("TournamentCombo");
        tour.innerHTML = "";
        tour.appendChild(CreateOptionElement($("#ChooseText").val(), "-1"));

        if (includeMatches == true) {
            var matches = document.getElementById("MatchesCombo");
            matches.innerHTML = "";
            matches.appendChild(CreateOptionElement($("#ChooseText").val(), "-1"));
        }

        if ($("#SelectedSportId").val() == "22" || $("#SelectedSportId").val() == "4") {
            $("#countryContainer").hide();
        }
        else
            $("#countryContainer").show();

        $("#CountryCombo").attr("disabled", "disabled");
        $.ajax({
            url: "/Menu/GetComboCountries.aspx?rnd=" + Math.random() + "&sportId=" + $("#SportsCombo").val() + "&languageId=" + $("#CurrentLanguage").val() + "&includeMatches=" + includeMatches,
            type: "GET",
            dataType: "text/json",
            success: function (result) {
                result = eval(result);
                countries.innerHTML = "";

                for (var i = 0; i < result.length; i++) {
                    countries.appendChild(CreateOptionElement(result[i].name, result[i].id));
                }
                $("#CountryCombo").attr("disabled", "");

                ChangeCountry();
            },
            error: function (xhr) {
            }
        });
    }
}

function CreateOptionElement(text, value, selected) {
    var element = document.createElement("option");
    element.setAttribute("value", value);

    //if (selected == true)
        //element.setAttribute("selected", "selected");

    var elTxt = document.createTextNode(text);
    element.appendChild(elTxt);

    return element;
}
