﻿
var matches = {};
var lsto = 0;
var lsmto = 0;
var mchanged = { play: { "Goal": false, "StartFinish": false }, unhighlight: new Array() };
var updateNo = 0;
function getAllLivescore() {
    //  $("#livescore").css({ opacity: 0.2 });
   // $("#livescore").hide();
    $.ajax({
        type: "POST",
        url: all_ls,
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            matches = msg;
            //lsto = setTimeout("updateMatchDetails(true)", 0);
            updateMatchDetails(true);
        },
        error: function(msg, textStatus, errorThrown) {
            //  alert(textStatus + " " + errorThrown);
            getAllLivescore();
        }
    });
}
 function updateLivescore() {
    clearTimeout(lsto);
    clearTimeout(lsmto);


    if (updateNo > -1)
    //if (updateNo > 25) 
    {
        $.ajax({
            type: "POST",
            url: all_ls,
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                //matches = msg;
                //lsto = setTimeout("updateMatchDetails(true)", 0);
                setUpdatedMatches(msg);
                updateNo = 0;
            },
            error: function(msg, textStatus, errorThrown) {
                //  alert(textStatus + " " + errorThrown);
                updateLivescore();
            }
        });

        return;
    }
    /* 
    else{
        $.ajax({
        type: "POST",
        url: u_ls,
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
        setUpdatedMatches(msg);
        },
        error: function(msg, textStatus, errorThrown) {
        //  alert(textStatus + " " + errorThrown);
        lsto = setTimeout("updateLivescore()", 1000);
        }
        });
    }
   */
}

function setUpdatedMatches(msg) {
    if (msg != 0) {
        for (var j = 0; j < matches.length; j++) {
            for (var i = 0; i < msg.length; i++) {
                if (matches[j].ID_livescore == msg[i].ID_livescore) {
                    msg[i].changed = true;

                    if (msg[i].FinalStatus != matches[j].FinalStatus)
                        msg[i].ChangedStatus = MatchChanged.FinalStatus;
                    else
                        if (msg[i].LiveStatus != matches[j].LiveStatus)
                        msg[i].ChangedStatus = MatchChanged.LiveStatus;
                    else
                        if (msg[i].Cards.length != matches[j].Cards.length)
                        msg[i].ChangedStatus = MatchChanged.Card;
                    else {
                        var osh = 0;
                        var osa = 0;
                        var nsh = 0;
                        var nsa = 0;
                        for (var ios = 0; ios < msg[i].Scores.length; ios++)
                            if (msg[i].Scores[ios].Status == MatchScoreStatus.Current) {
                            nsh = msg[i].Scores[ios].HomeTeamScore;
                            nsa = msg[i].Scores[ios].AwayTeamScore;
                        }

                        for (var ios = 0; ios < matches[j].Scores.length; ios++)
                            if (matches[j].Scores[ios].Status == MatchScoreStatus.Current) {
                            osh = matches[j].Scores[ios].HomeTeamScore;
                            osa = matches[j].Scores[ios].AwayTeamScore;
                        }

                        if (osh != nsh || osa != nsa)
                            msg[i].ChangedStatus = MatchChanged.Score;
                    }
                    matches[j] = msg[i];
                }
            }
        };
    }
    updateMatchDetails(false);
    updateNo++;
}

function updateMatchDetails(all) {
    var now = new Date();
    var status = "";
    var score = "-";
    var now_dt = new Date();
    now_dt = now_dt.setDate(now_dt.getDate() + 1);
    var first_start = now_dt / 1000;
    var first_start_tmp;
    clearTimeout(lsmto);
    var has_live = false;
    mchanged.play.Goal= false;
    mchanged.play.StartFinish = false;
    mchanged.unhighlight = new Array();
    
    var hj=0;
    for (var i = 0; i < matches.length; i++) {
        //LiveStatus
        if (matches[i].FinalStatus == MatchFinalStatus.Live) {
            has_live = true;
        }
        if (matches[i].changed || all) {
            status = "";
            score = "-";

            RemCssClass(getMatchRowElm(matches[i].ID_livescore),
                    MatchLSTableDetail.MatchTdLiveCssClass);

            if (all) {

                for (var ic = 0; ic < matches[i].Cards.length; ic++) {

                    if (matches[i].Cards[ic].isForHomeTeam) {
                        if (matches[i].Cards[ic].CardType == MatchCardType.Red)
                            AddHtml(getMatchDetailCellElm(matches[i].ID_livescore,
                                 MatchLSTableDetail.MatchTdHomeTeamCssClass), redCardHtml);
                    }
                    else {

                        if (matches[i].Cards[ic].CardType == MatchCardType.Red)
                            AddHtml(getMatchDetailCellElm(matches[i].ID_livescore,
                                MatchLSTableDetail.MatchTdAwayTeamCssClass), redCardHtml);
                    }
                }


            }

            if (matches[i].changed) {
                matches[i].highlight = true;

                if (matches[i].ChangedStatus == MatchChanged.FinalStatus) {
                    highlight(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass));

                    var tmc = new Object();
                    
                    tmc.ID_livescore=matches[i].ID_livescore;
                    tmc.Detail = MatchLSTableDetail.MatchTdStatusCssClass;
                    mchanged.unhighlight.push(tmc);
                  //  setTimeout("unhighlight('" + matches[i].ID_livescore + "','" +
                    //                                MatchLSTableDetail.MatchTdStatusCssClass + "')", TimeoutValues.Unhighlight);
                    mchanged.play.StartFinish = true;
                }

                if (matches[i].ChangedStatus == MatchChanged.LiveStatus) {
                    highlight(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass));

                    var tmc = new Object();

                    tmc.ID_livescore = matches[i].ID_livescore;
                    tmc.Detail = MatchLSTableDetail.MatchTdStatusCssClass;
                    mchanged.unhighlight.push(tmc);
                    
                   // setTimeout("unhighlight('" + matches[i].ID_livescore + "','" +
                     //                               MatchLSTableDetail.MatchTdStatusCssClass + "')", TimeoutValues.Unhighlight);
                    mchanged.play.StartFinish = true;
                }

                if (matches[i].ChangedStatus == MatchChanged.Score) {
                    highlight(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdCurrentScoreCssClass));

                    mchanged.play.Goal = true;

                    var tmc = new Object();

                    tmc.ID_livescore = matches[i].ID_livescore;
                    tmc.Detail = MatchLSTableDetail.MatchTdCurrentScoreCssClass;
                    mchanged.unhighlight.push(tmc);
                  
                    
                    //setTimeout("unhighlight('" + matches[i].ID_livescore + "','" +
                      //   MatchLSTableDetail.MatchTdCurrentScoreCssClass + "')", TimeoutValues.Unhighlight);


                }

                if (matches[i].ChangedStatus == MatchChanged.Card) {
                }
                matches[i].changed = false;
            }
            else {
                matches[i].highlight = false;
            }

           

            if (matches[i].FinalStatus == MatchFinalStatus.NotStarted) {
                first_start_tmp = jsDate2utime(MatchDate2JSUtcDate(matches[i].Start));
                if (first_start > first_start_tmp) {
                    first_start = first_start_tmp;
                  //  alert(first_start);
                    //alert(matches[i].Start);
                    // alert(MatchDate2JSUtcDate(matches[i].Start));
                    //alert(jsDate2utime(MatchDate2JSUtcDate(matches[i].Start)));
                }
            }

            if (matches[i].FinalStatus == MatchFinalStatus.Finished) {
                status = "Finished";
                styleMatchFinished(matches[i]);

                if (matches[i].changed) {
                    highlight(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass));
                    var tmc = new Object();

                    tmc.ID_livescore = matches[i].ID_livescore;
                    tmc.Detail = MatchLSTableDetail.MatchTdStatusCssClass;
                    mchanged.unhighlight.push(tmc);
                }
            }

            if (matches[i].FinalStatus == MatchFinalStatus.Interrupted) {
                status = "Interrupted";
            }

            if (matches[i].FinalStatus == MatchFinalStatus.Suspended) {
                status = "Suspended";
            }

            if (matches[i].FinalStatus == MatchFinalStatus.Postponed) {
                status = "Postponed";
            }

            if (matches[i].FinalStatus == MatchFinalStatus.Cancelled) {
                status = "Cancelled";
            }

            if (matches[i].FinalStatus == MatchFinalStatus.Abandoned) {
                status = "Abandoned";
            }

            if (matches[i].FinalStatus == MatchFinalStatus.Delayed) {
                status = "Delayed";
            }

            if (matches[i].FinalStatus == MatchFinalStatus.AfterET) {
                status = "After ET";
                styleMatchFinished(matches[i]);

                if (matches[i].changed) {
                    highlight(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass));

                    var tmc = new Object();
                    tmc.ID_livescore = matches[i].ID_livescore;
                    tmc.Detail = MatchLSTableDetail.MatchTdStatusCssClass;
                    mchanged.unhighlight.push(tmc);
                }
            }

            if (matches[i].FinalStatus == MatchFinalStatus.AfterPen) {
                status = "After Pen.";
                styleMatchFinished(matches[i]);

                if (matches[i].changed) {
                    highlight(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass));

                    var tmc = new Object();
                    tmc.ID_livescore = matches[i].ID_livescore;
                    tmc.Detail = MatchLSTableDetail.MatchTdStatusCssClass;
                    mchanged.unhighlight.push(tmc);
                }
                
            }

            if (!all) {
				score="-";
                for (var is = 0; is < matches[i].Scores.length; is++) {
                    //alert(matches[i].Scores[is].Status);
                    if (matches[i].Scores[is].Status == MatchScoreStatus.Current
                    || matches[i].Scores[is].Status == MatchScoreStatus.Finished)
                        score = matches[i].Scores[is].HomeTeamScore + " - " + matches[i].Scores[is].AwayTeamScore;
                }
                SetHtml(getMatchDetailCellElm(matches[i].ID_livescore,
                    MatchLSTableDetail.MatchTdCurrentScoreCssClass), score);
            }

            SetHtml(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass), status);
        }

    } //end for


    if (mchanged.play.Goal) {setTimeout("playSound(1)", 2000); }
    if(mchanged.play.StartFinish){ playSound(SoundType.StartFinish);}
    
    setTimeout(unhighlightMatches,TimeoutValues.Unhighlight);
    
   
    
    clearTimeout(lsto);
   //alert(has_live);
    if (has_live) {
        lsto = setTimeout("updateLivescore()", TimeoutValues.Update);
        //lsmto = setTimeout("updateMatchLiveStatus()", 0);
       updateMatchLiveStatus();
    }
    else 
    {
        first_start_tmp = Math.floor(jsDate2utime(new Date()));
        first_start_tmp = first_start - first_start_tmp - 120;
        first_start_tmp = Math.round(first_start_tmp);
		
		if (first_start_tmp < 0) {
            first_start_tmp = 20;
            lsto = setTimeout("updateLivescore()", first_start_tmp * 1000);
        }
        else
            lsto = setTimeout("getAllLivescore()", first_start_tmp * 1000);
    }
    if (all) {
       // $("div.livescore.loading").hide();
       // $("#livescore").show();
    }
    
}

function playGoal() {
    playSound(SoundType.Goal); 
}

function unhighlightMatches()
{
     for(var j=0;j<mchanged.unhighlight.length;j++) {
       //  alert(mchanged.unhighlight[j].ID_livescore);
       unhighlight(  mchanged.unhighlight[j].ID_livescore   ,   
                       mchanged.unhighlight[j].Detail );
    }
    mchanged.unhighlight = new Array();
    
}

function updateMatchLiveStatus() {
    var now = new Date();
    var status = "";
    for (var i = 0; i < matches.length; i++) {

        if (matches[i].FinalStatus == MatchFinalStatus.Live) {
            status = "";
            if (matches[i].LiveStatus == MatchLiveStatus.Half1) {
                // alert( matches[i].Start);
                //alert(MatchDate2JSUtcDate(matches[i].LastPeriodStartedAt));
                matches[i].Min = Math.round((jsDate2utime(now) -
                    jsDate2utime(MatchDate2JSUtcDate(matches[i].LastPeriodStartedAt))) / 60);
                status = matches[i].Min;
                if (matches[i].Min <= 0) { status = 1; matches[i].Min = 1 }
                if (matches[i].Min > 45) status = "45+";

                AddCssClass(getMatchRowElm(matches[i].ID_livescore),
                    MatchLSTableDetail.MatchTdLiveCssClass);
                    
                SetHtml(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass), status);
                AddHtml(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass), minuteSignHtml);

            }

            if (matches[i].LiveStatus == MatchLiveStatus.Half2) {
                //  alert(matches[i].LastPeriodStartedAt);
                // alert(matches[i].Start);
                // alert(MatchDate2JSUtcDate(matches[i].LastPeriodStartedAt));
                matches[i].Min = Math.round((jsDate2utime(now) -
                    jsDate2utime(MatchDate2JSUtcDate(matches[i].LastPeriodStartedAt))) / 60);
                matches[i].Min += 45;
                status = matches[i].Min;
                if (matches[i].Min <= 45) { status = 46; matches[i].Min = 46; }
                if (matches[i].Min > 90) status = "90+";

                AddCssClass(getMatchRowElm(matches[i].ID_livescore),
                    MatchLSTableDetail.MatchTdLiveCssClass);
                SetHtml(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass), status);
                AddHtml(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass), minuteSignHtml);
            }

            if (matches[i].LiveStatus == MatchLiveStatus.HalfTime) {
                status = "Half Time";

                AddCssClass(getMatchRowElm(matches[i].ID_livescore),
                    MatchLSTableDetail.MatchTdLiveCssClass);
                SetHtml(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass), status);
            }

            if (matches[i].LiveStatus == MatchLiveStatus.ExtraTime) {
                status = "Extra Time";

                AddCssClass(getMatchRowElm(matches[i].ID_livescore),
                    MatchLSTableDetail.MatchTdLiveCssClass);
                    
                SetHtml(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass), status);
                    
            }

            if (matches[i].LiveStatus == MatchLiveStatus.Penalties) {
                status = "Penalties";
                
                AddCssClass(getMatchRowElm(matches[i].ID_livescore),
                    MatchLSTableDetail.MatchTdLiveCssClass);
                SetHtml(getMatchDetailCellElm(matches[i].ID_livescore, MatchLSTableDetail.MatchTdStatusCssClass), status);
            }
            
        }
    }
    
    try{
        clearTimeout(lsmto);
    } catch (e) {
    }
    //lsmto = setTimeout("updateMatchLiveStatus()", TimeoutValues.MatchStatusUpdate);
}


