﻿var typeOfPage = "";
var refreshInterval = 15000;
var sport = "football";
var sportTranslation = "";
var language = 5;
var sort = 1;
var interval = "";
var selectLeagueMenu = new Array();
var currentJSON;
var lastOpenID = null;
var date;
var contestGroupID;
var leagueID;
var countryID;
var currTimeZone = 0;
var teamID = 0;
var league = "";
var teams = new Array();
var lChoiceHead = "";

function setLinks(source) 
{
    jQuery("#results_top").html(source);
}

var siteURL = "";

function pageINIT() {
    $(document).click(function (e) {
        closeAll("");
    });

    var pathname = window.location.host;
    if (pathname.indexOf("www") < 0) {
        if (pathname.indexOf("localhost") < 0) {
            window.location = siteURL + window.location.pathname;
            return;
        }
    }
    setServerTime();
    alignPage();

    $("#language_hidden li h3").click(function () {
        //$("#language span").html($(this).text() + '<a onclick="showLanguages(); return false;" href="#"><img src="' + siteURL + '/Styles/images/dd_icon.png" alt="languages"/></a>');
        $("#language span").html('<a onclick="showTimezone(); return false;" href="#">' + $(this).text() + '&nbsp;<img src="' + siteURL + '/Styles/images/dd_icon.png" alt="languages"/></a>');
        $("#language span a").click(function (e) {
            e.stopPropagation();
        });
    });

    $("#timezone_hidden li h3").click(function () {
        //$("#timezone span").html($(this).text() + '<a onclick="showTimezone(); return false;" href="#"><img src="' + siteURL + '/Styles/images/dd_icon.png" alt="timezone"/></a>');
        $("#timezone span").html('<a onclick="showTimezone(); return false;" href="#">' + $(this).text() + '&nbsp;<img runat="server" src="' + siteURL + '/Styles/images/dd_icon.png" alt="time zones" /></a>');
        $("#timezone span a").click(function (e) {
            e.stopPropagation();
        });
    });

    $("#ddSports_hidden li h3").click(function () {
        //$("#ddSports span").html($(this).text() + '<a onclick="showSports(); return false;" href="#"><img src="' + siteURL + '/Styles/images/dd_icon.png" alt="sports"/></a>');
        $("#ddSports span").html('<a onclick="showSports(); return false;" href="#">' + $(this).text() + '&nbsp;<img src="' + siteURL + '/Styles/images/dd_icon.png" alt="sports"/></a>');
        lastOpenID = null;
        $("#ddSports span a").click(function (e) {
            e.stopPropagation();
        });
    });

    $("#ddPageType_hidden li h3").click(function () {
        //$("#ddPageType span").html($(this).text() + '<a onclick="showPageType(); return false;" href="#"><img src="' + siteURL + '/Styles/images/dd_icon.png" alt="page type"/></a>');
        $("#ddPageType span").html('<a onclick="showPageType(); return false;" href="#">' + $(this).text() + '&nbsp;<img src="' + siteURL + '/Styles/images/dd_icon.png" alt="page type"/></a>');
        $("#ddPageType span a").click(function (e) {
            e.stopPropagation();
        });
        //lastOpenID = null;
    });

    $("#ddLeagueChoice_hidden li h3").click(function () {
        //$("#ddLeagueChoice span").html($(this).text() + '<a onclick="showLeagueChoice(); return false;" href="#"><img src="' + siteURL + '/Styles/images/dd_icon.png" alt="league choice"/></a>');
        $("#ddLeagueChoice span").html('<a onclick="showLeagueChoice(); return false;" href="#">' + $(this).text() + '&nbsp;<img src="' + siteURL + '/Styles/images/dd_icon.png" alt="league choice"/></a>');
        $("#ddLeagueChoice span a").click(function (e) {
            e.stopPropagation();
        });
    });

    $("#ddDate_hidden li h3").click(function () {
        //$("#ddDate span").html($(this).text() + '<a onclick="showDate(); return false;" href="#"><img src="' + siteURL + '/Styles/images/dd_icon.png" alt="date"/></a>');
        $("#ddDate span").html('<a onclick="showDate(); return false;" href="#">' + $(this).text() + '&nbsp;<img src="' + siteURL + '/Styles/images/dd_icon.png" alt="date"/></a>');
        $("#ddDate span a").click(function (e) {
            e.stopPropagation();
        });
        //lastOpenID = null;
    });
    $('.panel').hide();

    $('.lm').collapser({
        target: 'next',
        effect: 'slide',
        changeText: 0,
        expandClass: 'expIco',
        collapseClass: 'collIco'
    }, function () {
        $('.panel').slideUp();
    });
    $("#language span a").click(function (e) {
        e.stopPropagation();
    });
    $("#timezone span a").click(function (e) {
        e.stopPropagation();
    });
    $("#ddSports span a").click(function (e) {
        e.stopPropagation();
    });
    $("#ddPageType span a").click(function (e) {
        e.stopPropagation();
    });
    $("#ddLeagueChoice span a").click(function (e) {
        e.stopPropagation();
    });
    $("#ddDate span a").click(function (e) {
        e.stopPropagation();
    });
} 

function alignPage() {
    var footer_top = $("#footer").offset().top;
    var footer_height = $("#footer").height();
    var c_bottom_top = $("#results_bottom").offset().top;
    var c_bottom_height = $("#results_bottom").height();
    var left_top = $("#list_all_leagues_1").offset().top;
    var left_height = $("#list_all_leagues_1").height();    
}

function showLanguages() {
    closeAll("language");
    $("#language_hidden").toggle();
}
function closeLanguages(val) {
    var loc = document.location.toString();
    if (loc.indexOf("?") >= 0) {
        loc = loc.substring(0, loc.indexOf("?"));
    }
    document.location = loc + "?lang=" + val;
}
function showTimezone() {
    closeAll("timezone");
    $("#timezone_hidden").toggle();
}
function closeTimezone(curr) {
    $("#timezone_hidden").hide();
    currTimeZone = curr;
    calcWithCurrentTimeZone();
}
function showSports() {
    closeAll("Sports");
    $("#ddSports_hidden").toggle();
}
function closeSports() {
    $("#ddSports_hidden").hide();
}
function showPageType() {
    closeAll("PageType");
    $("#ddPageType_hidden").toggle();
}
function closePageType() {
    $("#ddPageType_hidden").hide();
}
function showLeagueChoice() {
    closeAll("LeagueChoice");
    $("#ddLeagueChoice_hidden").toggle();
}
function closeLeagueChoice() {
    $("#ddLeagueChoice_hidden").hide();
}
function showDate() {
    closeAll("Date");
    $("#ddDate_hidden").toggle();
}
function closeDate() {
    $("#ddDate_hidden").hide();
} 

function setLanguage(value) {
    language = value;
}

function setSiteURL(value) {
    siteURL = value;
}

function setSportTRanslation(value) {
    sportTranslation = value;
}

function setSport(value) {
    sport = value;
}

function setType(value) {
    typeOfPage = value;
    if (typeOfPage == "live") {
        refreshInterval = 5000;
    } else if (typeOfPage == "forDay") {
        refreshInterval = 120000;
    } else if (typeOfPage == "stat_fixt") {
        if (currentJSON && currentJSON.length > 0) {
            jQuery.each(currentJSON, function () {
                if (this.ContestGroup == contestGroupID) {
                    leagueID = this.SubContestId;
                }
            });
        }
        refreshInterval = 120000;
    } else if (typeOfPage == "stat_results") {
        if (currentJSON && currentJSON.length > 0) {
            jQuery.each(currentJSON, function () {
                if (this.ContestGroup == contestGroupID) {
                    leagueID = this.SubContestId;
                }
            });
        }
        refreshInterval = 120000;
    } else if (typeOfPage == "stat_tables") {
        if (currentJSON && currentJSON.length > 0) {
            jQuery.each(currentJSON, function () {
                if (this.ContestGroup == contestGroupID) {
                    leagueID = this.SubContestId;
                }
            });
        }
        refreshInterval = 120000;
    } else if (typeOfPage == "stat_Statistics") {
        if (currentJSON && currentJSON.length > 0) {
            jQuery.each(currentJSON, function () {
                if (this.ContestGroup == contestGroupID) {
                    leagueID = this.SubContestId;
                }
            });
        }
        refreshInterval = 120000;
    } else if (typeOfPage == "league") {
        leagueID = contestGroupID;
        refreshInterval = 15000;
    } else {
        refreshInterval = 15000;
    }
    getData();
}

function getTeamsFromLeague(leagueJSON) {
    var team = new Object
    teams == new Array();
    var html_statTeams = '<span><a onclick="showDate(); return false;" href="#">' + langTeams + '&nbsp;<img runat="server" src="' + siteURL + '/Styles/images/dd_icon.png" alt="date" /></a></span>' +
                            '<ul style="display:none;" id="ddDate_hidden">';
    jQuery.each(leagueJSON.Event, function () {
        var homeTeam = new Object();
        homeTeam.ID = this.HomeID;
        homeTeam.Name = this.Home;
        var awayTeam = new Object();
        awayTeam.ID = this.AwayID;
        awayTeam.Name = this.Away;
        if (!isTeamInList(homeTeam, teams)) {
            html_statTeams += '<li><h3 onclick="closeDate(); setTypeTeam(' + homeTeam.ID + ',\'' + homeTeam.Name + '\');">' + homeTeam.Name + '</h3></li>';
            teams.push(homeTeam);
        }
        if (!isTeamInList(awayTeam, teams)) {
            html_statTeams += '<li><h3 onclick="closeDate(); setTypeTeam(' + awayTeam.ID + ',\'' + awayTeam.Name + '\');">' + awayTeam.Name + '</h3></li>';
            teams.push(awayTeam);
        }
    });
    html_statTeams += '</ul>';
    if (sport.toLowerCase() == "football") {
        jQuery("#ddDate").html(html_statTeams);
        $("#ddDate span a").click(function (e) {
            e.stopPropagation();
        });
        $("#ddDate_hidden li h3").click(function () {
            $("#ddDate span").html('<a onclick="showDate(); return false;" href="#">' + $(this).text() + '&nbsp;<img src="' + siteURL + '/Styles/images/dd_icon.png" alt="date"/></a>');
            lastOpenID = null;
            $("#ddDate span a").click(function (e) {
                e.stopPropagation();
            });
        });
    }
}

function isTeamInList(oTeam, listTeams) {
    var ima = false;
    jQuery.each(listTeams, function () {
        if (this.ID == oTeam.ID) {
            ima = true;
            return true;
        }
    });
    return ima;
}

function setTypeTeam(value, value1) {
    teamID = value;
    home = value1;
    typeOfPage = "stat_teams";
    refreshInterval = 1200000;
    if (currentJSON && currentJSON.length > 0) {
        leagueID = currentJSON[0].SubContestId;
    }
    getData();
}

function getData() {
    if (interval == "") {
        ajaxCall();
        interval = setInterval("ajaxCall()", refreshInterval);
    }
    else {
        stop_Int();
        ajaxCall();
        interval = setInterval("ajaxCall()", refreshInterval);
    }
}

function ajaxCall() {
    if (typeOfPage == "live" || typeOfPage == "all" || typeOfPage == "results" || typeOfPage == "upcoming") {
        $.ajax({
            //url: "http://livescowebservice.livescore.si/Service/Results.asmx/ResultsAll?type=" + typeOfPage + "&sport=" + sport + "&languageID=" + language + "&sort=" + sort,
            //url: "http://localhost:50780/LivescoWebService/Service/Results.asmx/ResultsAll?type=" + typeOfPage + "&sport=" + sport + "&languageID=" + language + "&sort=" + sort,
            url: siteURL + "/Service/Results.asmx/ResultsAll?type=" + typeOfPage + "&sport=" + sport + "&languageID=" + language + "&sort=" + sort,
            type: 'GET',
            success: function (res) {
                callback(res);
            }
        });
    } else if (typeOfPage == "forDay") {
        $.ajax({
            //url: "http://livescowebservice.livescore.si/Service/Results.asmx/ResultsForDay?sport=" + sport + "&languageID=" + language + "&sort=" + sort + "&date=" + date,
            //url: "http://localhost:50780/LivescoWebService/Service/Results.asmx/ResultsForDay?sport=" + sport + "&languageID=" + language + "&sort=" + sort + "&date=" + date,
            url: siteURL + "/Service/Results.asmx/ResultsForDay?sport=" + sport + "&languageID=" + language + "&sort=" + sort + "&date=" + date,
            type: 'GET',
            success: function (res) {
                callback(res);
            }
        });
    } else if (typeOfPage == "league") {
        if (countryID == "0") {
            $.ajax({
                //url: "http://livescowebservice.livescore.si/Service/Results.asmx/ResultsByLeague?sport=" + sport + "&languageID=" + language + "&sort=" + sort + "&contestGroupID=" + leagueID,
                //url: "http://localhost:50780/LivescoWebService/Service/Results.asmx/ResultsByLeague?sport=" + sport + "&languageID=" + language + "&sort=" + sort + "&contestGroupID=" + leagueID,
                url: siteURL + "/Service/Results.asmx/ResultsByLeague?sport=" + sport + "&languageID=" + language + "&sort=" + sort + "&contestGroupID=" + leagueID,
                type: 'GET',
                success: function (res) {
                    callback(res);
                }
            });
        } else {
            $.ajax({
                //url: "http://livescowebservice.livescore.si/Service/Results.asmx/ResultsByLeague?sport=" + sport + "&languageID=" + language + "&sort=" + sort + "&contestGroupID=" + leagueID,
                //url: "http://localhost:50780/LivescoWebService/Service/Results.asmx/ResultsByLeague?sport=" + sport + "&languageID=" + language + "&sort=" + sort + "&contestGroupID=" + leagueID,
                url: siteURL + "/Service/Results.asmx/ResultsByCountry?sport=" + sport + "&languageID=" + language + "&sort=" + sort + "&countryID=" + countryID,
                type: 'GET',
                success: function (res) {
                    callback(res);
                }
            });
        }
    } else if (typeOfPage == "stat_fixt" || typeOfPage == "stat_results") {
        $.ajax({
            //url: "http://livescowebservice.livescore.si/Service/Results.asmx/ResultsByLeagueNextLastMatches?sport=" + sport + "&languageID=" + language + "&sort=" + sort + "&contestGroupID=" + leagueID,
            //url: "http://localhost:50780/LivescoWebService/Service/Results.asmx/ResultsByLeagueNextLastMatches?sport=" + sport + "&languageID=" + language + "&sort=" + sort + "&subContestID=" + leagueID,
            url: siteURL + "/Service/Results.asmx/ResultsByLeagueNextLastMatches?sport=" + sport + "&languageID=" + language + "&sort=" + sort + "&subContestID=" + leagueID,
            type: 'GET',
            success: function (res) {
                callback(res);
            }
        });
    } else if (typeOfPage == "stat_tables") {
        $.ajax({
            //url: "http://livescowebservice.livescore.si/Service/Stats.asmx/LeagueTableStats?sport=" + sport + "&subcontestID=" + leagueID + "&languageID=" + language,
            //url: "http://localhost:50780/LivescoWebService/Service/Stats.asmx/LeagueTableStats?sport=" + sport + "&subcontestID=" + leagueID + "&languageID=" + language,
            url: siteURL + "/Service/StatsService.asmx/LeagueTableStats?sport=" + sport + "&subcontestID=" + leagueID + "&languageID=" + language,
            type: 'GET',
            success: function (res) {
                callbackStats(res);
            }
        });
    } else if (typeOfPage == "stat_teams") {
        $.ajax({
            //url: "http://livescowebservice.livescore.si/Service/Stats.asmx/TeamStats?sport=" + sport + "&teamID=" + homeTeamID + "&subcontestID=" + leagueID + "&languageID=" + language,
            //url: "http://localhost:50780/LivescoWebService/Service/Stats.asmx/TeamStats?sport=" + sport + "&teamID=" + teamID + "&subcontestID=" + leagueID + "&languageID=" + language,
            url: siteURL + "/Service/StatsService.asmx/TeamStats?sport=" + sport + "&teamID=" + teamID + "&subcontestID=" + leagueID + "&languageID=" + language,
            type: 'GET',
            success: function (res) {
                callbackStatsTeams(res);
            }
        });
    } else if (typeOfPage == "stat_Statistics") {
        $.ajax({
            //url: "http://livescowebservice.livescore.si/Service/Stats.asmx/TeamStats?sport=" + sport + "&teamID=" + homeTeamID + "&subcontestID=" + leagueID + "&languageID=" + language,
            //url: "http://localhost:50780/LivescoWebService/Service/Stats.asmx/TeamStats?sport=" + sport + "&teamID=" + teamID + "&subcontestID=" + leagueID + "&languageID=" + language,
            url: siteURL + "/Service/StatsService.asmx/SeasonStats?sport=" + sport + "&homeTeamID=0&awayTeamID=0&subcontestID=" + leagueID + "&languageID=" + language,
            type: 'GET',
            success: function (res) {
                callbackStatsStatistics(res);
            },
            error: function (a, b, c) {
                var abc = 0;
            }
        });
    }
}

function stop_Int() {
    if (interval != "") {
        window.clearInterval(interval)
        interval = ""
    }
}

jQuery.fn.sort = function () {
    return this.pushStack([].sort.apply(this, arguments), []);
};

function sortCountry(a, b) {
    if (a.Country == b.Country) {
        return 0;
    }
    return a.Country > b.Country ? -1 : 1;
};
function sortCountryAsc(a, b) {
    return sortCountry(a, b) * -1;
};

function callback(res) {
    var data = "";
    jQuery.each(jQuery(res.responseText), function () {
        if (this.nodeName == "P" || this.nodeName == "p") {
            if (this.innerHTML != "{}") {
                data = json_parse(this.innerHTML);
            }
        }
    });
    if (!res.responseText) {
        try {
            data = json_parse(res.text);
        } catch (err) {
            data = json_parse(res.childNodes[0].textContent);
        }
    }
    if (data.Error == undefined) {
//        var sortedJson = jQuery(data.contest).sort(sortCountryAsc);
        //        currentJSON = sortedJson;
        currentJSON = data.contest;
        jQuery("#results_top").empty();
        jQuery("#results_bottom").empty();
        jQuery("#results_top").empty();
        jQuery("#results_bottom").empty();
        var tempJson = currentJSON;
        if (lastOpenID != null && lastOpenID != -1) {
            tempJson = jQuery.grep(currentJSON, function (value) {
                return value.SubContestId == lastOpenID;
            });
        }
        if (typeOfPage == "league") {
            tempJson = jQuery.grep(currentJSON, function (value) {
                return value.ContestGroup == leagueID;
            });
        }
        createResultsTable(tempJson);
        if (tempJson.length == 0) {
            //jQuery("#results_top").append('Sorry, there are no matches for selected league!');
            jQuery("#results_top").append(getResultTableHeadTranslations("Sorry, there are no matches for selected league!"));
        }
        if (typeOfPage == "league") {
            selectLeagueMenu = new Array();
            jQuery.each(currentJSON, function () {
                var o = new Object();
                o.Name = this.ContestName;
                o.Country = this.Country;
                o.ID = this.SubContestId;
                o.SportTranslation = this.SportTranslation;
                o.NameURL = this.ContestName.replace(/ /g, '_');
                o.CountryURL = this.Country.replace(/ /g, '_');
                o.CountryID = countryID;
                o.ContestGroupID = this.ContestGroup;
                selectLeagueMenu.push(o);
            });
        }
        //createResultsTable(currentJSON);
        createSelectLeagueMenu();
        alignPage();
        if (typeOfPage == "league") {
            if (teams.length == 0) {
                if (tempJson.length != 0) {
                    getTeamsFromLeague(tempJson[0]);
                    if (tempJson.Tip == "kup") {
                        jQuery("#pg_stat_tables").remove();
                        jQuery("#pg_stat_Statistics").remove();
                    }
                }
            }
        }
//        if (typeOfPage == "league") {
//            $("#ddLeagueChoice span").html('<a onclick="showLeagueChoice(); return false;" href="#">' + $("#ddLeagueChoice ul li h3:first").text() + '&nbsp;<img src="' 
//                + siteURL + '/Styles/images/dd_icon.png" alt="league choice"/></a>');            
//        }
    } else {
        jQuery("#ddLeagueChoice_hidden").empty();
        jQuery("#ddLeagueChoice_hidden").append(lChoiceHead);
        switch (typeOfPage) {
            case "live":
                {
                    jQuery("#results_top").empty();
                    jQuery("#results_bottom").empty();
                    jQuery("#results_top").append(getResultTableHeadTranslations("Sorry, there are no live matches!"));
                    break;
                }
            case "all":
                {
                    jQuery("#results_top").empty();
                    jQuery("#results_bottom").empty();
                    jQuery("#results_top").append(getResultTableHeadTranslations("Sorry, no matches today!"));
                    break;
                }
            case "results":
                {
                    jQuery("#results_top").empty();
                    jQuery("#results_bottom").empty();
                    jQuery("#results_top").append(getResultTableHeadTranslations("Sorry, there are no finished matches!"));
                    break;
                }
            case "upcoming":
                {
                    jQuery("#results_top").empty();
                    jQuery("#results_bottom").empty();
                    jQuery("#results_top").append(getResultTableHeadTranslations("Sorry, there are no upcoming matches!"));
                    break;
                }
            case "forDay":
                {
                    jQuery("#results_top").empty();
                    jQuery("#results_bottom").empty();
                    jQuery("#results_top").append(getResultTableHeadTranslations("Sorry, there are no matches for selected date!"));
                    break;
                }
            case "league":
                {
                    jQuery("#results_top").empty();
                    jQuery("#results_bottom").empty();
                    //jQuery("#results_top").append('Sorry, there are no matches for selected league!');
                    jQuery("#results_top").append(getResultTableHeadTranslations("Sorry, there are no matches for selected league!"));
                    break;
                }
            case "stat_fixt":
                {
                    jQuery("#results_top").empty();
                    jQuery("#results_bottom").empty();
                    //jQuery("#results_top").append('Sorry, there are no matches for selected league!');
                    jQuery("#results_top").append(getResultTableHeadTranslations("Sorry, there are no matches for selected league!"));
                    break;
                }
            case "stat_results":
                {
                    jQuery("#results_top").empty();
                    jQuery("#results_bottom").empty();
                    //jQuery("#results_top").append('Sorry, there are no matches for selected league!');
                    jQuery("#results_top").append(getResultTableHeadTranslations("Sorry, there are no matches for selected league!"));
                    break;
                }
            default:
                {
                    jQuery("#results_top").empty();
                    jQuery("#results_bottom").empty();
                    jQuery("#results_top").append(getResultTableHeadTranslations("No data!"));
                    break;
                }
        }
    }
}

function isEven(value) {
    if (value % 2 == 0)
        return true;
    else
        return false;
}

function createSelectLeagueMenu() {
    if (lChoiceHead == "") {
        lChoiceHead = jQuery("#ddLeagueChoice_hidden").html();
    }
    if (jQuery("#ddLeagueChoice_hidden").length == 0) {
        jQuery("#ddLeagueChoice").append("<ul style='display:none;' id='ddLeagueChoice_hidden'></ul>");
    }
    jQuery("#ddLeagueChoice_hidden").empty();
    jQuery("#ddLeagueChoice_hidden").append(lChoiceHead);
    jQuery.each(selectLeagueMenu, function () {
        if (typeOfPage == "league") {
            if (countryID != "0") {
                var urlLink = siteURL + "/" + this.SportTranslation + "/" + this.CountryURL + "/" + this.NameURL + "/" + this.ContestGroupID + "/" + this.CountryID;
                jQuery("#ddLeagueChoice_hidden").append("<li><h3 onclick='window.location = \"" + urlLink + "\"' value=" + this.ID + ">" + this.Name + "</h3></li>");
            }
        } else {
            jQuery("#ddLeagueChoice_hidden").append("<li><h3 onclick='closeLeagueChoice(); return false;' value=" + this.ID + ">" + this.Name + "</h3></li>");
        }
    });
    var tempJson;
    $("#ddLeagueChoice_hidden li h3").click(function () {
        lastOpenID = $(this).attr("value");
        $("#ddLeagueChoice span").html('<a onclick="showLeagueChoice(); return false;" href="#">' + $(this).text() + '&nbsp;<img src="' + siteURL + '/Styles/images/dd_icon.png" alt="league choice"/></a>');
        $("#ddLeagueChoice span a").click(function (e) {
            e.stopPropagation();
        });
        var tempJson = currentJSON;
        if (lastOpenID != "-1") {
            tempJson = jQuery.grep(currentJSON, function (value) {
                return value.SubContestId == lastOpenID;
            });
        }
        jQuery("#results_top").empty();
        jQuery("#results_bottom").empty();
        setType(typeOfPage);
        //createResultsTable(tempJson);
        alignPage();

    });
}

function openInfo(id, home, away, league, country, homescore, awayscore, homescore1p, awayscore1p, datetime, datetime1, datetime2, comment) {
    openWin(siteURL + "/Info.aspx?id=" + id + "&home=" + home + "&away=" + away + "&league=" + league + "&country=" + country + "&homescore=" + homescore + "&awayscore=" + awayscore +
            "&homescore1p=" + homescore1p + "&awayscore1p=" + awayscore1p + "&date=" + datetime.replace(/%20/g, ' ') + " " + datetime1.replace(/%20/g, ' ') +
             " " + datetime2.replace(/%20/g, ' ') + "&comment=" + comment);
}

function openWin(pagename) {
    var microsite_window = window.open(pagename, "",
                'toolbar=no,location=no,borders=no,directories=no,status=no,menubar=no,scrollbars=no,top=50,left=50,resizable=no,width=500')
}

//function openInfo(eventID, home, away, league, country, hs, as, hs1, as1, date, comment) {
//    window.open("Info.htm?id=" + eventID + "&home=" + home + "&away=" + away + "&league=" + league + "&country=" + country + "&homescore=" + hs + "&awayscore=" + as +
//            "&homescore1p=" + hs1 + "&awayscore1p=" + as1 + "&date=" + date + "&comment=" + comment, "", 
//            "toolbar=no,location=no,borders=no,directories=no,status=no,menubar=no,scrollbars=no,top=50,left=50,resizable=no,width=500");

//}

function getMatchInfo(eventID, home, away, league, country, hs, as, hs1, as1, date, comment) {
    var rezultat = "";
    var pol = "";
    if (comment == "NSY") {
        rezultat = "- : -";
    } else {
        if (comment != "1st Half") {
            pol = unescape(hs1.toString()) + " - " + unescape(as1.toString());
        }
        rezultat = unescape(hs.toString()) + " - " + unescape(as.toString());
    }
    var scoreClass = "score";
    if (unescape(comment) == "NSY" || unescape(comment) == "1st Half" || unescape(comment) == "2nd Half" || unescape(comment) == "Half Time" || unescape(comment) == "ET 1st Half" || unescape(comment) == "ET 2nd Half") {
        scoreClass = "score";
    } else {
        scoreClass = "scoreFull";
    }
    var html = "<table width='100%' cellspacing='0' cellpadding='0'>" +
                        "<colgroup>" +
                            "<col width='40%' align='left'/>" +
                            "<col width='20%' />" +
                            "<col width='40%' align='right'/>" +
                        "</colgroup>" +
                "<tr>" +
                    "<td style='text-align:right;'><h1 class='team'>" + unescape(home) + "</h1></td><td><span class='" + scoreClass + "'>" + rezultat
                    + "</span></td><td><h1 class='team'>"
                        + unescape(away) + "</h1></td>" +
                "<tr>" +
                "<tr>" +
                    "<td colspan='3'><span class='comment' style='font-weight:normal;'>" + pol + "</span></td>" +
                "<tr>" +
                "<tr>" +
                    "<td colspan='3'><span class='date'>" + unescape(date.toString()) + "</span></td>" +
                "</tr>" +
                "<tr>" +
                    "<td colspan='3'><span class='comment'>" + unescape(comment) + "</span></td>" + 
                "</tr>";
    jQuery("#info_header").html(html);
    $.ajax({
        //url: "http://livescowebservice.livescore.si/Service/Results.asmx/MatchInfo?sport=" + sport + "&languageID=" + language + "&eventID=" + eventID,
        //url: "http://localhost:50780/LivescoWebService/Service/Results.asmx/MatchInfo?sport=" + sport + "&languageID=" + language + "&eventID=" + eventID,
        url: siteURL + "/Service/Results.asmx/MatchInfo?sport=" + sport + "&languageID=" + language + "&eventID=" + eventID,
        type: 'GET',
        success: function (res) {
            callbackMatchInfo(res);
        }
    });
}

function callbackMatchInfo(res) {
    var data = "";
    jQuery.each(jQuery(res.responseText), function () {
        if (this.nodeName == "P" || this.nodeName == "p") {
            if (this.innerHTML != "{}") {
                data = json_parse(this.innerHTML);
            }
        }
    });
    if (!res.responseText) {
        try {
            data = json_parse(res.text);
        } catch (err) {
            data = json_parse(res.childNodes[0].textContent);
        }
    }
    if (data.Error == undefined) {

        var counter = 0;
        var html = "<table width='100%' cellspacing='0' cellpadding='0'>" +
                        "<colgroup>" +
                            "<col width='45%' align='left'/>" +
                            "<col width='10%' />" +
                            "<col width='45%' align='right'/>" +
                        "</colgroup>";
        $.each(data, function () {
            var rezultat = "&nbsp;";
            var spanclass = "";
            if (this.goal_card == "goal") {
                rezultat = this.rezultat;
                spanclass = "icon soccer-ball";
            } else {
                if (this.card_type.indexOf("yellow") >= 0) {
                    spanclass = "icon y-card";
                } else {
                    spanclass = "icon r-card";
                }
            }
            counter++;
            if (counter % 2 == 0) {
                html += "<tr class='odd'>";
            } else {
                html += "<tr class='even'>";
            }
            if (this.uloga == "home") {
                html += "<td class='summary fl'>" +
                                    "<div class='time-box'>" + this.minute + "'  </div>" +
                                    "<div class='icon-box'><span class='" + spanclass + "'>&nbsp;</span></div>" +
                                     this.player + "&nbsp;" +
                                "</td>";
            } else {
                html += "<td class='summary fl'>&nbsp;</td>";
            }
            html += "<td class='score' rowspan='1'>" + rezultat + "</td>";
            if (this.uloga == "home") {
                html += "<td class='summary fr'>&nbsp;</td>";
            } else {
                html += "<td class='summary fr'>" +
                                    "<div class='time-box'>" + this.minute + "'</div>" +
                                    "<div class='icon-box'><span class='" + spanclass + "'>&nbsp;</span></div>" +
                                    "&nbsp;" + this.player +
                                "</td>";
            }
            html += "</tr>";
        });
        html += "</table>";
        $("#info_content").append(html);
    } else {
        jQuery("#results_top").empty();
        jQuery("#results_bottom").empty();
        jQuery("#results_top").append(getResultTableHeadTranslations("No data!"));
    }
}

function calcWithCurrentTimeZone() {
    try {
        createResultsTable(currentJSON);
    } catch (err) { }
}

var langFixtures = "";
var langResults = "";
var langTables = "";
var langTeams = "";
var langStats = "";
var langResFixt = "";

function resetMenuForStats(par) {
    if (sport.toLowerCase() == "football") {
        var firstElem = "";
        if (par == 0) {
            firstElem = langTables;
            var html_ddPageType = '<span><a onclick="showPageType(); return false;" href="#">' + firstElem + ' &nbsp;<img runat="server" src="' + siteURL + '/Styles/images/dd_icon.png" alt="page type" /></a></span>' +
                            '<ul style="display:none;" id="ddPageType_hidden">' +
                                '<li id="pg_stat_fixt"><h3 onclick="closePageType(); setType(\'stat_fixt\');">' + langFixtures + '</h3></li>' +
                                '<li id="pg_stat_results"><h3 onclick="closePageType(); setType(\'stat_results\');">' + langResults + '</h3></li>' +
                                '<li id="pg_stat_tables"><h3 onclick="closePageType(); setType(\'stat_tables\');">' + langTables + '</h3></li>' +
                                '<li id="pg_stat_Statistics"><h3 onclick="closePageType(); setType(\'stat_Statistics\');">' + langStats + '</h3></li>' +
                            '</ul>';
        } else if (par == 1) {
            firstElem = langResFixt;
            var html_ddPageType = '<span><a onclick="showPageType(); return false;" href="#">' + firstElem + ' &nbsp;<img runat="server" src="' + siteURL + '/Styles/images/dd_icon.png" alt="page type" /></a></span>' +
                            '<ul style="display:none;" id="ddPageType_hidden">' +
                                '<li id="pg_stat_fixt"><h3 onclick="closePageType(); setType(\'stat_fixt\');">' + langFixtures + '</h3></li>' +
                                '<li id="pg_stat_results"><h3 onclick="closePageType(); setType(\'stat_results\');">' + langResults + '</h3></li>' +
                                '<li id="pg_league"><h3 onclick="closePageType(); leagueID = contestGroupID; setType(\'league\');">' + langResFixt + '</h3></li>' +
                                '<li id="pg_stat_tables"><h3 onclick="closePageType(); setType(\'stat_tables\');">' + langTables + '</h3></li>' +
                                '<li id="pg_stat_Statistics"><h3 onclick="closePageType(); setType(\'stat_Statistics\');">' + langStats + '</h3></li>' +
                            '</ul>';
        }

        jQuery("#ddPageType").html(html_ddPageType);
        $("#ddPageType span a").click(function (e) {
            e.stopPropagation();
        });
    }

    var html_ddLeagueChoice = '<span><a onclick="showLeagueChoice(); return false;" href="#">' + league + '<img runat="server" src="' + siteURL + '/Styles/images/dd_icon.png" alt="league choice" /></a></span>';
    jQuery("#ddLeagueChoice").html(html_ddLeagueChoice);
    $("#ddLeagueChoice span a").click(function (e) {
        e.stopPropagation();
    });

    $("#ddPageType_hidden li h3").click(function () {
        //$("#ddPageType span").html($(this).text() + '<a onclick="showPageType(); return false;" href="#"><img src="' + siteURL + '/Styles/images/dd_icon.png" alt="page type"/></a>');
        $("#ddPageType span").html('<a onclick="showPageType(); return false;" href="#">' + $(this).text() + '&nbsp;<img src="' + siteURL + '/Styles/images/dd_icon.png" alt="page type"/></a>');
        //lastOpenID = null;
        $("#ddPageType span a").click(function (e) {
            e.stopPropagation();
        });
    });
}

function resetPageTypeMenu() {
    if (Date.parse(date) < Date.today()) {
        jQuery("#ddPageType span").html('<a onclick="showPageType(); return false;" href="#">' + jQuery("#lih3Results").text() + '&nbsp;<img src="' + siteURL + '/Styles/images/dd_icon.png" alt="page type"/></a>');
        jQuery("#lih3Results").hide();
        jQuery("#lih3Live").hide();
        jQuery("#lih3Upcoming").hide();
        jQuery("#lih3All").hide(); 
        $("#ddPageType span a").click(function (e) {
            e.stopPropagation();
        });
    } else if (Date.parse(date) > Date.today()) {
        jQuery("#ddPageType span").html('<a onclick="showPageType(); return false;" href="#">' + jQuery("#lih3Upcoming").text() + '&nbsp;<img src="' + siteURL + '/Styles/images/dd_icon.png" alt="page type"/></a>');
        jQuery("#lih3Results").hide();
        jQuery("#lih3Live").hide();
        jQuery("#lih3Upcoming").hide();
        jQuery("#lih3All").hide();
        $("#ddPageType span a").click(function (e) {
            e.stopPropagation();
        });
    } else {
        jQuery("#ddPageType span").html('<a onclick="showPageType(); return false;" href="#">' + jQuery("#lih3All").text() + '&nbsp;<img src="' + siteURL + '/Styles/images/dd_icon.png" alt="page type"/></a>');
        jQuery("#lih3Results").show();
        jQuery("#lih3Live").show();
        jQuery("#lih3Upcoming").show();
        jQuery("#lih3All").show();
        $("#ddPageType span a").click(function (e) {
            e.stopPropagation();
        });
    }
}

function closeAll(ddt) {
    if(ddt != "timezone"){
        $("#timezone_hidden").hide();
    }

    if (ddt != "language") {
        $("#language_hidden").hide();
    }

    if (ddt != "Sports") {
        $("#ddSports_hidden").hide();
    }

    if (ddt != "PageType") {
        $("#ddPageType_hidden").hide();
    }

    if (ddt != "LeagueChoice") {
        $("#ddLeagueChoice_hidden").hide();
    }

    if (ddt != "Date") {
        $("#ddDate_hidden").hide();
    }
}
