﻿var arrayMyMatchesSoccer = new Array();
var jsonMyMatchesSoccer;

function myMatches(idIN) {
    if ($("#chk_" + idIN)[0].checked) { 
         arrayMyMatchesSoccer.push(idIN);
    } else { 
        arrayMyMatchesSoccer = jQuery.grep(arrayMyMatchesSoccer, function(value) {                
            return value != idIN;
        });
        if (typeOfPage == "mymatches") {
            showMyMatchesSoccer(null, false);
        }
    }
}

function showMyMatchesSoccer(data, newFeed) {
    if (newFeed) {
        jsonMyMatchesSoccer = json_parse(data);
    }
    $("#main_table").empty();
    jQuery.each(jsonMyMatchesSoccer[0].contest, function() {
        var postoji = false;
        $.each(this.Event, function() {
            if (jQuery.inArray(this.EventId, arrayMyMatchesSoccer) > -1) {
                postoji = true;
            }
        });
        if (postoji) {
            var sport = this.SportId;
            var sport_name = this.SportName;
            var subConId = this.SubContestId;
            var rowClass = "";
            if (sport == 1) {
                var subID = this.SubContestId;
                var divSubc = "";
                if (typeOfPage == "forDay") {
                    divSubc = "<tr><td class='lc' colspan='6'>" + this.ContestName + "</td></tr>";
                } else {
                    divSubc = "<tr><td class='lc' colspan='7'>" + this.ContestName + "</td></tr>";
                }
                $("#main_table").append(divSubc);
                jQuery.each(this.Event, function() {
                    if (jQuery.inArray(this.EventId, arrayMyMatchesSoccer) > -1) {
                        var htmlEvents = "";
                        var h = this.HomeScore;
                        var a = this.AwayScore;
                        var minute  = ""; 
                        var infoExist = false;
                        try
                        {
                            minute = parseInt(this.Minute) + "'";
                        }catch(e){
                            minute = this.Minute;
                        }
                        
                        var rezultat = h + " - " + a;
                        if (h == "" && a == "") {
                            rezultat = " - ";
                            if(this.Info == 1){
                                infoExist = true;
                            }
                        } else {
                            if(h == 0 && a == 0)
                            {
                                if(this.Info == 1){
                                    infoExist = true;
                                }
                            }
                            else
                            {
                                infoExist = true;
                            }
                        }
                        if (this.Pair == "true") {
                            if(infoExist){
                                rowClass = "class ='lmbg1_click'";
                            } else{
                                rowClass = "class = 'lmbg1'";
                            }
                        } else {
                            if(infoExist){
                                rowClass = "class='lmbg2_click'";
                            } else{
                                rowClass = "class='lmbg2'";
                            }
                        } 
                        htmlEvents = htmlEvents + "<tr " + rowClass + ">";
                        if(typeOfPage != "forDay"){
                        htmlEvents = htmlEvents +   
                                    "<td class='chekboxtd'>" +
                                        "<input id='chk_" + this.EventId + "' type='checkbox' checked='checked' value='' name='"
                                        + subConId + "' onclick='myMatches(" + this.EventId + ");'/>" +
                                    "</td>";
                        }
                        if(infoExist){
                            htmlEvents = htmlEvents +
                                        "<td class='lm1' onclick='javascript:openInfo(" + this.EventId + ");'>" + this.StartTime + "</td>" + 
                                        "<td class='lm2' onclick='javascript:openInfo(" + this.EventId + ");'><div id='info" 
                                            + this.EventId + "' class='info5' style='display: none;'/>" + this.Home + "</td>";
                            htmlEvents = htmlEvents +            
                                        "<td class='lm3' onclick='javascript:openInfo(" + this.EventId + ");'>" + rezultat + "</td>";
                            htmlEvents = htmlEvents +             
                                        "<td class='lm4' onclick='javascript:openInfo(" + this.EventId + ");'>" + this.Away + "</td>";
                        } else {
                            htmlEvents = htmlEvents +
                                        "<td class='lm1' >" + this.StartTime + "</td>" + 
                                        "<td class='lm2'><div id='info" + this.EventId + "' class='info5' style='display: none;'/>" + this.Home + "</td>";
                            htmlEvents = htmlEvents +            
                                        "<td class='lm3'>" + rezultat + "</td>";
                            htmlEvents = htmlEvents +             
                                        "<td class='lm4'>" + this.Away + "</td>";
                        }
                        if (this.EventStarted) {
                            if (this.Live) {
                                htmlEvents = htmlEvents +
                                                       "<td class='lm5'><div class='status_live'>" + minute +
                                                        "</div></td>";
                            } else {
                                htmlEvents = htmlEvents +
                                    "<td class='lm5'>" + this.Comment +
                                    "</td>";
                            }
                        } else {
                            if (leaguesort == 1) {
                                htmlEvents = htmlEvents +
                                    "<td class='lm5'>" +
                                        "<a target='_blank' href='http://adserver.itsfogo.com/click.aspx?zoneid=86549'>" +
                                            "<img class='btn_pariez' title='Pariez avant le début du match' src='slike/pixel.gif'/>" +
                                        "</a>";
                                    "</td>";
                            } else {
                                htmlEvents = htmlEvents +
                                        "<td class='lm5'>" + this.Comment +
                                        "</td>";
                            }                            
                        }
                        $("#main_table").append(htmlEvents);
                    }
                });
            }
        }
    });
    if (arrayMyMatchesSoccer.length == 0) {
        $("#main_table").empty();
        $("#main_table").append("<span style='font-size:14px'>No My Matches Selected</span>");
    }
}

function checkMyMatches() {
    $.each(arrayMyMatchesSoccer, function() {
        if ($("#chk_" + this).length > 0){
            if ($("#chk_" + this).is(':checked')) {
            } else {
                $("#chk_" + this).attr('checked', true);
            }
        }
    });
}