﻿var arrayMyMatchesSoccer = new Array();
var arrayMyMatchesBasketball = new Array();
var arrayMyMatchesTennis = new Array();
var arrayMyMatchesHockey = new Array();
var arrayMyMatchesHandball = new Array();
var arrayMyMatchesVolleyball = new Array();
var arrayMyMatchesNFL = new Array();
var jsonMyMatchesSoccer;
var jsonMyMatchesBasketball;

function myMatches(idIN) {
    if ($("#star_" + idIN).hasClass("star")) {
        jQuery($("#star_" + idIN)).attr('class', "star_selected");
        $("#star_" + idIN).html("<img src='media/images/start_blue.png' alt='blue_star'>");
        if (sport == "football") {
            arrayMyMatchesSoccer.push(idIN);
        } else if (sport == "basketball") {
            arrayMyMatchesBasketball.push(idIN);
        } else if (sport == "tennis") {
            arrayMyMatchesTennis.push(idIN);
        } else if (sport == "hockey") {
            arrayMyMatchesHockey.push(idIN);
        } else if (sport == "handball") {
            arrayMyMatchesHandball.push(idIN);
        } else if (sport == "volleyball") {
            arrayMyMatchesVolleyball.push(idIN);
        } else if (sport == "nfl") {
            arrayMyMatchesNFL.push(idIN);
        }
    } else {
        jQuery($("#star_" + idIN)).attr('class', "star");
        $("#star_" + idIN).html("<img src='media/images/start_gray.png' alt='grey_star'>");
        if (sport == "football") {
            arrayMyMatchesSoccer = jQuery.grep(arrayMyMatchesSoccer, function(value) {                
                return value != idIN;
            });
            if (type == "mymatches") {
                showMyMatchesSoccer(null, false);
            }
        } else if (sport == "basketball") {
            arrayMyMatchesBasketball = jQuery.grep(arrayMyMatchesBasketball, function(value) {
                return value != idIN;
            });
            if (type == "mymatches") {
                showMyMatchesBasketball(null, false);
            }
        } else if (sport == "tennis") {
            arrayMyMatchesTennis = jQuery.grep(arrayMyMatchesTennis, function(value) {
                return value != idIN;
            });
        } else if (sport == "hockey") {
            arrayMyMatchesHockey = jQuery.grep(arrayMyMatchesHockey, function(value) {
                return value != idIN;
            });
        } else if (sport == "handball") {
            arrayMyMatchesHandball = jQuery.grep(arrayMyMatchesHandball, function(value) {
                return value != idIN;
            });
        } else if (sport == "volleyball") {
            arrayMyMatchesVolleyball = jQuery.grep(arrayMyMatchesVolleyball, function(value) {
                return value != idIN;
            });
        } else if (sport == "nfl") {
            arrayMyMatchesNFL = jQuery.grep(arrayMyMatchesNFL, function(value) {
                return value != idIN;
            });
        }
    }
}

function showMyMatchesSoccer(data, newFeed) {
    if (newFeed) {
        jsonMyMatchesSoccer = json_parse(data);
    }
    $("#accordians").empty();
    $.each(jsonMyMatchesSoccer, function() {
        var postoji = false;
        $.each(this.Event, function() {
            if (jQuery.inArray(this.EventId, arrayMyMatchesSoccer) > -1) {
                postoji = true;
            }
        });
        if (postoji) {
            var html = "";
            var cName = this.ContestName;
            html = html + "<div id='td_" + this.SubContestId + "' class='toggler_detail'>" +
                        "<table class='toggler_table'>" +
                            "<tr class='last'>" +
                                "<td class='toggler_nametd'>" + cName + "</td>" +
                                "<td class='toggler_icontd toggler_icontd_alignright'>" +
                                    "<a href='#'>" +
                                        "<img onclick='openStats(\"" + this.SubContestId + "\",\"" + cName + "\")' src='media/images/result_icon.gif'/>" +
                                    "</a>" +
                                "</td>" +
                                "<td class='toggler_icontd iconbrd'>" +
                                    "<a href='#'>" +
                                        "<img onclick='remove(\"" + this.SubContestId + "\")' src='media/images/cross_icon.gif'/>" +
                                    "</a>" +
                                "</td>" +
                                "<td width=30>&nbsp;</td>" +
                            "</tr>" +
                        "</table>" +
                      "</div>" +
                      "<div id='t_" + this.SubContestId + "' class='toggler'>" +
                        "<div id='swap'>" +
                            "<img id='" + this.SubContestId + "' onclick='resize(\"" + this.SubContestId + "\")' src='media/images/neg.gif'/>" +
                        "</div>" +
                      "</div>" +
                      "<div id='acc_" + this.SubContestId + "' class='accordion' style='overflow: hidden; visibility: visible; opacity: 1;'>" +
                        "<div>" +
                            "<div id='swap'></div>" +
                            "<div>" +
                                "<table id='tab_" + this.SubContestId + "' width='100%' cellspacing='0' cellpadding='0' border='0'>";
            $.each(this.Event, function() {
                if (jQuery.inArray(this.EventId, arrayMyMatchesSoccer) > -1) {
                    var teamHome = this.Home;
                    var teamAway = this.Away;
                    var tHome = this.Home;
                    var tAway = this.Away;
                    if (this.Pobednik == 1) {
                        teamHome = "<b>" + teamHome + "</b>";
                    } else if (this.Pobednik == 2) {
                        teamAway = "<b>" + teamAway + "</b>";
                    }
                    if (this.ImaPenala) {
                        if (int.Parse(this.ScoreHome) > int.Parse(this.ScoreAway)) {
                            teamHome = teamHome + "*";
                            tHome = tHome + "*";
                        } else if (int.Parse(this.ScoreHome) < int.Parse(this.ScoreAway)) {
                            teamAway = teamAway + "*";
                            tHome = tHome + "*";
                        }
                    }
                    if (this.CardHome != "0") {
                        teamHome = "<span class='" + this.CardHome + "'>" + teamHome + "</span>";
                    }
                    if (this.CardAway != "0") {
                        teamAway = "<span class='" + this.CardAway + "'>" + teamAway + "</span>";
                    }
                    html = html + "<tr class='" + this.cssClassMinute + "'>" +
                            "<td class='" + this.cssClassSignal + "' style='border-right: medium none; width: 20px; text-align: right;'>";
                    if (this.Live) {
                        html = html + "<!-- ImageReady Slices (Untitled-1) -->" +
                                                    "<img src='media/images/second.gif' width='8' height='8' alt='' class='blkdot'>" +
                                                    "<!-- End ImageReady Slices -->"
                    }
                    html = html + "</td><td class='" + this.cssClassSignal + "'>" + this.Comment + "</td>" +
                            "<td class='team1'>" + teamHome + "</td>" +
                            "<td class='" + this.cssClassScore + "'>" +
                                "<a href='info.aspx?subContestID=" + this.EventId + "&comm=" + this.Comment.replace("'", "") + "&score=" + this.Score +
                                    "&name=" + cName + "&home=" + tHome + "&away=" + tAway + "' " +
                                    "onclick='openWinSmall(this.href); return false;' title=''>" + this.Score +
                                "</a>" +
                            "</td>" +
                            "<td class='team2'>" + teamAway + "</td>" +
                            "<td class='tviconsoccer'>";
                    if (this.LineUp == 1) {
                        html = html + "<a style='float:left;' onclick='openWinLineUp(this.href); return false;' href='lineup.aspx?subContestID="
                                            + this.EventId + "&comm=" + this.Comment.replace("'", "") + "&score=" + this.Score + "&name="
                                            + cName + "'><img src='media/images/luIcon.png' alt='lineup '></a>";
                    }
                    if (this.Tv == 1) {
                        html = html + "<a style='float:right;' href='#' onclick=\"refreshPageTV('liveOnTv', '" + this.EventId
                                            + "', 'football');\"><img src='media/images/tvIcon.png' alt='tv'></a>";
                    }
                    //if (this.Tv == 1) {
                    //    html = html + "<a onclick='refreshPageTV(\"liveOnTv\", \"" + this.EventId + "\", \"football\");' href='#'>" +
                    //            "<img alt='tv' src='media/images/tvicon.gif'/>" +
                    //          "</a>";
                    //}
                    html = html + "</td><td title='My Matches' valign='middle' id='star_" + this.EventId + "' onclick='myMatches(" + this.EventId
                                    + ");' class='star_selected'><img src='media/images/start_blue.png' alt='grey_star' ></td></tr>";
                }
            });
            html = html + "</table></div><div class='bottomMargin'/></div></div>";
            $("#accordians").append(html);
        }
    });
    $("#accordians").append("<br /><div style='text-align:right'><a href='http://www.sport.cc' target='_blank'>" +
                                "<img height='44px' src='media/images/goal.cc-logo-add.png'>" +
                                "</a></div><br />");
    if (arrayMyMatchesSoccer.length == 0) {
        $("#accordians").empty();
        $("#accordians").append("<span style='font-size:14px'>No My Matches Selected</span>");
    }
}

function showMyMatchesBasketball(data, newFeed) {
    if (newFeed) {
        jsonMyMatchesBasketball = json_parse(data);
    }
    $("#accordians").empty();
    $.each(jsonMyMatchesBasketball, function() {
        var postoji = false;
        $.each(this.Event, function() {
            if (jQuery.inArray(this.EventId, arrayMyMatchesBasketball) > -1) {
                postoji = true;
            }
        });
        if (postoji) {
            var html = "";
            var cName = this.ContestName;
            html = html + "<div id='td_" + this.SubContestId + "' class='toggler_detail'>" +
                        "<table class='toggler_table'>" +
                            "<tr class='last'>" +
                                "<td class='toggler_nametd'>" + cName + "</td>" +
                                "<td class='toggler_icontd toggler_icontd_alignright'>" +
                                    "<a href='#'>" +
                                        "<img onclick='openStats(\"" + this.SubContestId + "\",\"" + cName + "\")' src='media/images/result_icon.gif'/>" +
                                    "</a>" +
                                "</td>" +
                                "<td class='toggler_icontd iconbrd'>" +
                                    "<a href='#'>" +
                                        "<img onclick='remove(\"" + this.SubContestId + "\")' src='media/images/cross_icon.gif'/>" +
                                    "</a>" +
                                "</td>" +
                                "<td width=30>&nbsp;</td>" +
                            "</tr>" +
                        "</table>" +
                      "</div>" +
                      "<div id='t_" + this.SubContestId + "' class='toggler'>" +
                        "<div id='swap'>" +
                            "<img id='" + this.SubContestId + "' onclick='resize(\"" + this.SubContestId + "\")' src='media/images/neg.gif'/>" +
                        "</div>" +
                      "</div>" +
                      "<div id='acc_" + this.SubContestId + "' class='accordion' style='overflow: hidden; visibility: visible; opacity: 1;'>" +
                        "<div>" +
                            "<div id='swap'></div>" +
                            "<div>" +
                                "<table id='tab_" + this.SubContestId + "' width='100%' cellspacing='0' cellpadding='0' border='0'>";
            $.each(this.Event, function() {
                if (jQuery.inArray(this.EventId, arrayMyMatchesBasketball) > -1) {
                    var teamHome = this.Home;
                    var teamAway = this.Away;
                    var tHome = this.Home;
                    var tAway = this.Away;
                    var m_firstqH, m_firstqA, m_secondqH, m_secondqA, m_thirdqH, m_thirdqA, m_fourthqH, m_fourthqA, m_fifthqH, m_fifthqA;
                    try {
                        if (int.Parse(this.ScoreHome1P) < 10)
                            m_firstqH = "&nbsp;&nbsp;" + this.ScoreHome1P;
                    } catch (e) { m_firstqH = this.ScoreHome1P }
                    try {
                        if (int.Parse(this.ScoreAway1P) < 10)
                            m_firstqA = "&nbsp;&nbsp;" + this.ScoreAway1P;
                    } catch (e) { m_firstqA = this.ScoreAway1P }
                    try {
                        if (int.Parse(this.ScoreHome2P) < 10)
                            m_secondqH = "&nbsp;&nbsp;" + this.ScoreHome2P;
                    } catch (e) { m_secondqH = this.ScoreHome2P }
                    try {
                        if (int.Parse(this.ScoreAway2P) < 10)
                            m_secondqA = "&nbsp;&nbsp;" + this.ScoreAway2P;
                    } catch (e) { m_secondqA = this.ScoreAway2P }
                    try {
                        if (e) (int.Parse(this.ScoreHome3P) < 10)
                        m_thirdqH = "&nbsp;&nbsp;" + this.ScoreHome3P;
                    } catch (e) { m_thirdqH = this.ScoreHome3P }
                    try {
                        if (e) (int.Parse(this.ScoreAway2P) < 10)
                        m_thirdqA = "&nbsp;&nbsp;" + this.ScoreAway2P;
                    } catch (e) { m_thirdqA = this.ScoreAway2P }
                    try {
                        if (int.Parse(this.ScoreHome4P) < 10)
                            m_fourthqH = "&nbsp;&nbsp;" + this.ScoreHome4P;
                    } catch (e) { m_fourthqH = this.ScoreHome4P }
                    try {
                        if (int.Parse(this.ScoreAway2P) < 10)
                            m_fourthqA = "&nbsp;&nbsp;" + this.ScoreAway2P;
                    } catch (e) { m_fourthqA = this.ScoreAway2P }
                    try {
                        if (int.Parse(this.ScoreHome5P) < 10)
                            m_fifthqH = "&nbsp;&nbsp;" + this.ScoreHome5P;
                    } catch (e) { m_fifthqH = this.ScoreHome5P }
                    try {
                        if (int.Parse(this.ScoreAway5P) < 10)
                            m_fifthqA = "&nbsp;&nbsp;" + this.ScoreAway5P;
                    } catch (e) { m_fifthqA = this.ScoreAway5P }
                    if (this.Pobednik == 1) {
                        teamHome = "<b>" + teamHome + "</b>";
                    } else if (this.Pobednik == 2) {
                        teamAway = "<b>" + teamAway + "</b>";
                    }

                    if (this.EventStarted) {
                        html = html + "<tr class='" + this.cssClassMinute + "'>" +
                                        "<td title='My Matches' align='center' valign='middle' id='star_" + this.EventId + "' onclick='myMatches(" + this.EventId +
                                        ");' class='star_selected'><img src='media/images/start_blue.png' alt='' ></td>" +
                                        "<td class='mtv'>";
                        if (this.Tv == 1) {
                            html = html + "<a onclick='refreshPageTV(\"liveOnTv\", \"" + this.EventId + "\", \"basketball\");' href='#'>" +
                                "<img alt='tv' src='media/images/tvicon.gif'/>" +
                              "</a>";
                        }
                        html = html + "</td><td class='" + this.cssClassSignal + "' style='border-right: medium none; width: 20px; text-align: right;'>";
                        if (this.Live) {
                            html = html + "<!-- ImageReady Slices (Untitled-1) -->" +
                                                    "<img src='media/images/second.gif' width='8' height='8' alt='' class='blkdot'>" +
                                                    "<!-- End ImageReady Slices -->";
                        }
                        html = html + "</td><td class='" + this.cssClassSignal + "'>" + this.Comment + "</td>" +
                                        "<td>" +
                                            "<table class='innertable'>" +
                                                "<tr>" +
                                                    "<td class='team2'>" + teamHome + "</td>" +
                                                    "<td class='team3'>" + m_firstqH + "&nbsp;&nbsp;" + m_secondqH + "&nbsp;&nbsp;" +
                                                        m_thirdqH + "&nbsp;&nbsp;" + m_fourthqH + "&nbsp;&nbsp;" + m_fifthqH + "</td>" +
                                                    "<td class='score1'>&nbsp;&nbsp;" + this.ScoreHome + "&nbsp;&nbsp;</td>" + 
                                                "</tr>" +
                                                "<tr class='last'>" +
                                                    "<td class='team2'>" + teamAway + "</td>" +
                                                    "<td class='team3'>" + m_firstqA + "&nbsp;&nbsp;" + m_secondqA + "&nbsp;&nbsp;" +
                                                        m_thirdqA + "&nbsp;&nbsp;" + m_fourthqA + "&nbsp;&nbsp;" + m_fifthqA + "</td>" +
                                                    "<td class='score1'>&nbsp;&nbsp;" + this.ScoreAway + "&nbsp;&nbsp;</td>" + 
                                                "</tr>" +
                                            "</table>" +
                                        "</td>" +
                                      "</tr>";
                    } else {
                        html = html + "<tr class='" + this.cssClassMinute + "'>" +
                                        "<td title='My Matches' align='center' valign='middle' id='star_" + this.EventId + "' onclick='myMatches(" + this.EventId +
                                        ");' class='star_selected'><img src='media/images/start_blue.png' alt='' ></td>" +
                                        "<td class='mtv'>";
                        if (this.Tv == 1) {
                            html = html + "<a onclick='refreshPageTV(\"liveOnTv\", \"" + this.EventId + "\", \"basketball\");' href='#'>" +
                                "<img alt='tv' src='media/images/tvicon.gif'/>" +
                              "</a>";
                        }
                        html = html + "</td><td class='" + this.cssClassSignal + "' style='border-right: medium none; width: 20px; text-align: right;'></td>" +
                                        "<td class='" + this.cssClassSignal + "'>" + this.Comment + "</td>" +
                                        "<td>" +
                                            "<table class='innertable'>" + 
                                                "<tr>" +
                                                    "<td class='team2'>" + teamHome + "</td>" +
                                                    "<td class='team3'>" + m_firstqH + "&nbsp;&nbsp;" + m_secondqH + "&nbsp;&nbsp;" +
                                                        m_thirdqH + "&nbsp;&nbsp;" + m_fourthqH + "&nbsp;&nbsp;" + m_fifthqH + "</td>" +
                                                    "<td class='score1' rowspan='2'>" + this.ScoreHome + "</td>" + 
                                                "</tr>" +
                                                "<tr class='last'>" +
                                                    "<td class='team2'>" + teamAway + "</td>" +
                                                    "<td class='team3'>" + m_firstqA + "&nbsp;&nbsp;" + m_secondqA + "&nbsp;&nbsp;" +
                                                        m_thirdqA + "&nbsp;&nbsp;" + m_fourthqA + "&nbsp;&nbsp;" + m_fifthqA + "</td>" +
                                                "</tr>" +
                                            "</table>" +
                                        "</td>" +
                                      "</tr>";
                    }
                }
            });
            html = html + "</table></div><div class='bottomMargin'/></div></div>";
            $("#accordians").append(html);
        }
    });
    $("#accordians").append("<br /><div style='text-align:right'><a href='http://www.sport.cc' target='_blank'>" +
                                "<img height='44px' src='media/images/goal.cc-logo-add.png'>" +
                                "</a></div><br />");
    if (arrayMyMatchesBasketball.length == 0) {
        $("#accordians").empty();
        $("#accordians").append("<span style='font-size:14px'>No My Matches Selected</span>");
    }
}

function checkMyMatches() {
    if (sport == "football") {
        $.each(arrayMyMatchesSoccer, function() {
            if ($("#star_" + this).hasClass("star")) {
                jQuery($("#star_" + this)).attr('class', "star_selected");
                $("#star_" + this).html("<img src='media/images/start_blue.png' alt='blue_star'>");
            } else {
                jQuery($("#star_" + this)).attr('class', "star");
                $("#star_" + this).html("<img src='media/images/start_gray.png' alt='grey_star'>");
            }
        });
    } else if (sport == "basketball") {
        $.each(arrayMyMatchesBasketball, function() {
            if ($("#star_" + this).hasClass("star")) {
                jQuery($("#star_" + this)).attr('class', "star_selected");
                $("#star_" + this).html("<img src='media/images/start_blue.png' alt='blue_star'>");
            } else {
                jQuery($("#star_" + this)).attr('class', "star");
                $("#star_" + this).html("<img src='media/images/start_gray.png' alt='grey_star'>");
            }
        });
    } else if (sport == "nfl") {
        $.each(arrayMyMatchesNFL, function() {
            if ($("#star_" + this).hasClass("star")) {
                jQuery($("#star_" + this)).attr('class', "star_selected");
                $("#star_" + this).html("<img src='media/images/start_blue.png' alt='blue_star'>");
            } else {
                jQuery($("#star_" + this)).attr('class', "star");
                $("#star_" + this).html("<img src='media/images/start_gray.png' alt='grey_star'>");
            }
        });
    } else if (sport == "hockey") {
        $.each(arrayMyMatchesHockey, function() {
            if ($("#star_" + this).hasClass("star")) {
                jQuery($("#star_" + this)).attr('class', "star_selected");
                $("#star_" + this).html("<img src='media/images/start_blue.png' alt='blue_star'>");
            } else {
                jQuery($("#star_" + this)).attr('class', "star");
                $("#star_" + this).html("<img src='media/images/start_gray.png' alt='grey_star'>");
            }
        });
    } else if (sport == "handball") {
        $.each(arrayMyMatchesHandball, function() {
            if ($("#star_" + this).hasClass("star")) {
                jQuery($("#star_" + this)).attr('class', "star_selected");
                $("#star_" + this).html("<img src='media/images/start_blue.png' alt='blue_star'>");
            } else {
                jQuery($("#star_" + this)).attr('class', "star");
                $("#star_" + this).html("<img src='media/images/start_gray.png' alt='grey_star'>");
            }
        });
    } else if (sport == "volleyball") {
        $.each(arrayMyMatchesVolleyball, function() {
            if ($("#star_" + this).hasClass("star")) {
                jQuery($("#star_" + this)).attr('class', "star_selected");
                $("#star_" + this).html("<img src='media/images/start_blue.png' alt='blue_star'>");
            } else {
                jQuery($("#star_" + this)).attr('class', "star");
                $("#star_" + this).html("<img src='media/images/start_gray.png' alt='grey_star'>");
            }
        });
    } else if (sport == "tennis") {
        $.each(arrayMyMatchesTennis, function() {
            if ($("#star_" + this).hasClass("star")) {
                jQuery($("#star_" + this)).attr('class', "star_selected");
                $("#star_" + this).html("<img src='media/images/start_blue.png' alt='blue_star'>");
            } else {
                jQuery($("#star_" + this)).attr('class', "star");
                $("#star_" + this).html("<img src='media/images/start_gray.png' alt='grey_star'>");
            }
        });
    }
}