﻿function live_click()
{
    ShowMenu('divLive');
    $get('lblTitel').innerHTML = "utrechtschaak live";
}

function GetLiveBijdragen(MaxID, Bottom)
{
    DisplayYes('imgMeerLiveLoading');
    DisplayNone('lblMeerLive');

    if (Bottom)
        UsfService.GetLiveBijdragen(MaxID, GotLiveBijdragenBottom);
    else
        UsfService.GetLiveBijdragen(MaxID, GotLiveBijdragenTop);
}

function GotLiveBijdragenBottom(lijst)
{
    GotLiveBijdragen(lijst, true);
}

function GotLiveBijdragenTop(lijst)
{
    GotLiveBijdragen(lijst, false);
}

function GotLiveBijdragen(lijst, Bottom)
{
    if (Bottom)
    {
        for (var i = 0; i < lijst.length; i++)
        {
            Add2Live(lijst[i], Bottom, false);
        }
    }
    else
    {
        for (i = lijst.length - 1; i >= 0; i--)
        {
            Add2Live(lijst[i], Bottom, false);
        }
    }

    DisplayNone('imgMeerLiveLoading');

    if (lijst.length > 0)
        DisplayYes('lblMeerLive');

    DisplayYes('divLiveBijdragen');

    if (!HeartbeatInitialized)
    {
        HeartbeatInitialized = true;
        //OK
        CheckUpdates(true);
    }
}

var LiveLastID = 0;

function Add2Live(Bijdrage, Bottom, Update)
{
    if (!Bijdrage.Registered)
        Bijdrage.Gebruiker = "<i>" + Bijdrage.Gebruiker + "</i>";

    var trCssClass = "LiveBijdrage";

    if (Update)
        trCssClass = "LiveBijdrageNew";

    Bijdrage.Datum = FormatDatum(Bijdrage.Datum);

    var onclickRow = 'onclick="HaveReadLive(' + Bijdrage.BijdrageID + ')"';
    var onclickTitle = 'onclick="GetDiscussieInhoud(' + Bijdrage.DiscussieID + ')"';

    var opacity = 'style="filter:Alpha(opacity=100);"';

    if (!Update || document.all)
        opacity = '';

    var html = '<table ' + opacity + 'id="tblLive_' + Bijdrage.BijdrageID + '" cellspacing="0" class="tblLive" ><tr ' +
        onclickTitle + '><td class="tdLiveDiscussieTitel"><b>' + 
        Bijdrage.DiscussieTitel + '</b><br />' +
        Bijdrage.Gebruiker + '<br />' + 
        Bijdrage.Datum.substring(14) + '</td></tr>' +
        '<tr><td class="tdLiveBijdrageText">' + Bijdrage.BijdrageText + '</td></tr></table>';

    if (Bottom)
        $get('divLiveBijdragen').innerHTML += html;
    else
        $get('divLiveBijdragen').innerHTML = html + $get('divLiveBijdragen').innerHTML;

    if (CurrentBijdrageID < Bijdrage.BijdrageID)
        CurrentBijdrageID = Bijdrage.BijdrageID;

    if (LiveLastID == 0 || LiveLastID > Bijdrage.BijdrageID)
    {
        LiveLastID = Bijdrage.BijdrageID;
    }

    if (Update && !document.all)
        FadeIn('tblLive_' + Bijdrage.BijdrageID, 0);
}


function MeerLive()
{
    GetLiveBijdragen(LiveLastID - 1, true);
}

function HaveReadLive(id)
{
    var Row = $get('trLive_' + id);

    if (Row == null)
        return;

    if (Row.className == "LiveBijdrageNew")
    {
        Row.className = "LiveBijdrage";
        HaveReadLive(id - 1);
    }
}
