ltstamp=0;
ltout = 0;
tsList = null;

setInterval('ltstamp+=10',10000);
function getContent(p1, p2, p3,reload) {
    cnt = $("#content_section");
    if(reload)
        cnt.html('<img src="images/loading.gif" width="25" height="25"/> Loading...<br><br>'+cnt.html());
    else
        cnt.html('<img src="images/loading.gif" width="25" height="25"/> Loading...');
    league_id = p1;
    bet_type = p2;
    period = p3;
    $.post("t_simple_loader.php", {
        param1 : p1,
        param2 : p2,
        param3 : p3
    }, function(k) {
        cnt.html(k.rs);
        ltstamp=k.ts;
        tsList = k.tsList;
        setColors();
    },'json');
    setTimeout("getNewData()",10000);
}
function setColors(){
    $.each(tsList,function(k,v){
        var val = ltstamp-parseInt(v);
        if ( (val >= 300) && (val < 600)){
            $('#'+k).css('background','lime');
        }
        if((val >= 120) && (val < 300)){
            $('#'+k).css('background','yellow');
        }
        if (val < 120){
            $('#'+k).css('background','#900');
        }
        if(val >= 600){
            $('#'+k).css('background','none');            
        }
    });
    if(ltout != 0)
        clearTimeout(ltout);
    tout = setTimeout("setColors()",10000);
}
function getNewData(){
    ctime = ltstamp;
    cnt = $("#content_section");
    $.post("prueba.php", {
        lid : league_id,
        btp : bet_type,
        pr  : period
    }, function(k) {
        $.each(k,function(){
            var ar = this.ar;
            var hr = this.hr;
            var ac = this.ac;
            var hc = this.hc;
            var dt = this.dt;
            var d = this.d;
            if(!tsList['acl_'+ar]){
                getContent(league_id, bet_type, period,true);
            }
            if($('#acl_'+ar).html() != null && $('#acl_'+ar).html().replace(/\s/,'&nbsp;') != this.acl){
                tsList['acl_'+ar] = ctime;
                $('#acl_'+ar).html(this.acl);
            }
            if($('#hcl_'+hr).html() != null && $('#hcl_'+hr).html().replace(/\s/,'&nbsp;') != this.hcl){
                tsList['hcl_'+hr] = ctime;
                $('#hcl_'+hr).html(this.hcl);
            }
            i=0;
            if( $("#achc_"+ar)  == 'n/a' && (ac != 0 || hc != 0)){
            }else if($("#achc_"+ar)  != 'n/a'){
                if($.trim($("#ac_"+ar).html()) != (''+ac+'%') || $.trim($("#hc_"+hr).html()) != (''+hc+'%')){
                    $("#ac_"+ar).html(ac+'%');
                    $("#hc_"+hr).html(hc+'%');
                }
            }
            $.each(this.books,function(key,value){
                var b = $('#book_'+key+'_'+ar).html();
                if(b == 'n/a' ){
                    if(value['away_percentage'] != 0 || value['home_percentage'] != 0){
                        tsList['book_'+key+'_'+ar] = ctime;
                        var a = '<a href="#" onclick="openInfo(\''+key+'\',\''+d+'\','+ar+')"><span id="a_book_'+key+'_'+ar+'">'+value['away_percentage']+'%</span><span id="h_book_'+key+'_'+hr+'">' +value['home_percentage']+ '%</span>';
                        $('#book_'+key+'_'+ar).html(a);
                    }
                }else{
                    if($('#a_book_'+key+'_'+ar).html() != (value['away_percentage']+'%') || $('#h_book_'+key+'_'+hr).html() != (value['home_percentage']+'%')){
                        tsList['book_'+key+'_'+ar] = ctime;
                        $('#a_book_'+key+'_'+ar).html(value['away_percentage']+'%');
                        $('#h_book_'+key+'_'+hr).html(value['home_percentage']+'%');
                    }
                }
            });
            setColors();
        });
    },'json');
    setTimeout("getNewData()",30000);
}

function openInfo(bid,d,r){
     var link =  'movement.php?league_id='+league_id+'&bet_type='+bet_type+'&period='+period+'&book_id='+bid+'&event_date='+d+'&rot_num='+r;
     show_popup(link);
}
