var _matches;
var _lastMatches = Array();
var _finishedMatches = Array();

Element.addMethods('SPAN', {
    updateScore : function(element, score)
    {
        element = $(element);
        if(score != element.innerHTML)
        {
            element.update(score);
            element.setStyle({
                fontWeight: 'bold',
                color: '#c00'                
            });
        }
        else
        {
            element.update(score);
            element.setStyle({
                fontWeight: 'normal',
                color: '#000'                
            });
        }
        
        return element;
    }
});

function updateLiveScore()
{
    new Ajax.Request ('/' + _lang + '/livescore/update/check/format/json/mdate/' + _mdate,
        {
            method : 'post',
            onSuccess : function (req) 
            {
            
            _matches = eval('(' + req.responseText + ')');
            updateLiveScoreElements();
            },
            onLoading : function () {
            }
        }
    );
}

function updateLiveScoreElements()
{
    if(_matches[0]['mod_date'].length > 0)
        _mdate = _matches[0]['mod_date'];
        
        
    for(var i = 0;i < _matches.length;i++)
    {
        if(_finishedMatches[_matches[i]['mid']] > 0)
        {
            continue;
        }
        
        if(!$('match_' + _matches[i]['mid']) && _matches[i]['sid'] == 0)
        {
            var newMatch = matchHTML;
            
            // set match id
            newMatch = newMatch.replace(/<matchid>/g, _matches[i]['mid']);
            
            // set players
            newMatch = newMatch.replace(/<home>/g, _matches[i]['home']);
            newMatch = newMatch.replace(/<home_country>/g, _matches[i]['home_country']);
            newMatch = newMatch.replace(/<away>/g, _matches[i]['away']);
            newMatch = newMatch.replace(/<away_country>/g, _matches[i]['away_country']);
            
            // set startdate
            newMatch = newMatch.replace(/<startdate>/g, _matches[i]['time']);
            newMatch = newMatch.replace(/<status>/g, _matches[i]['status']);
            
            var returnContent = '';
            
            if(!$('liga_' + _matches[i]['lid']))
            {
                returnContent = leagueHTML;
                
                returnContent = returnContent.replace(/<leagueid>/g, _matches[i]['lid']);
                returnContent = returnContent.replace(/<league_name>/g, _matches[i]['leaguename']);
                if(_matches[i]['surface'])
                    returnContent = returnContent.replace(/<surface>/g, _matches[i]['surface']);
                returnContent = returnContent.replace(/<match>/g, newMatch);
                
                Element.insert('leftColumm', returnContent);
            }
            
            Element.insert(('liga_' + _matches[i]['lid']), newMatch);
        }
        
        for(var j = 1;j <= 5;j++)
        {
            $('hset_' + j + '_' + _matches[i]['mid']).down().updateScore(_matches[i]['gem_h' + j]);
            $('bset_' + j + '_' + _matches[i]['mid']).down().updateScore(_matches[i]['gem_b' + j]);
        }
        
        
        $('status_' + _matches[i]['mid']).update(_matches[i]['status']);
        
        if(_matches[i]['winner'] != 0)
        {   
            if(_matches[i]['winner'] == 1)
            {
                var player = $('match_' + _matches[i]['mid']).down('div.players', 1);
            }
            else if(_matches[i]['winner'] == 2)
            {
                var player = $('match_' + _matches[i]['mid']).down('div.players', 2);
            }
            
            if(player)
            {
                player.insert('<img src="/gfx/check.gif" alt="winner" />');
                player.addClassName('won');
            }
            
            _finishedMatches[_matches[i]['mid']] = _matches[i]['mid'];
        }
    }

    _lastMatches[_matches[i]['mid']] = _matches[i];
}

Event.observe(window, 'load', function() {
   setInterval(updateLiveScore, 10000);
});
