var TOTO = {
    leagueid : 1,
    interval : 30000,

    init : function () {
        this.refresh();
    },
    
    initParam : function(myleaguid) {
        this.leagueid = myleaguid;
        this.refresh();
    },

    select : function (leagueid) {
        this.leagueid = leagueid;
        this.load();
    },

    refresh : function () {
        this.load();
//        window.setInterval("toto_load()", Math.round(TOTO.interval*(0.9+Math.random() * 0.2)));
        window.setInterval("toto_load()", 30000);
    },

    load : function (response) {

        if (response == null) {

            switch(this.leagueid) {
                case 1:
                    document.getElementById("toto_tabs_bg").style.background="url(/images/reiter_top_hi_li.gif)";
                    document.getElementById("toto_tab_nla").style.color="#D00221";
                    document.getElementById("toto_tab_nlb").style.color="#313131";
                    break;
                case 2:
                    document.getElementById("toto_tabs_bg").style.background="url(/images/reiter_top_hi_re.gif)";
                    document.getElementById("toto_tab_nla").style.color="#313131";
                    document.getElementById("toto_tab_nlb").style.color="#D00221";
                    break;
                case 8:
                    document.getElementById("toto_tabs_bg").style.background="url(/images/reiter_top_hi_1.gif)";
                    document.getElementById("toto_tab_8").style.color="#D00221";
                    document.getElementById("toto_tab_6").style.color="#313131";
                    document.getElementById("toto_tab_7").style.color="#313131";
                    document.getElementById("toto_tab_47").style.color="#313131";
                    break;
                case 6:
                    document.getElementById("toto_tabs_bg").style.background="url(/images/reiter_top_hi_2.gif)";
                    document.getElementById("toto_tab_8").style.color="#313131";
                    document.getElementById("toto_tab_6").style.color="#D00221";
                    document.getElementById("toto_tab_7").style.color="#313131";
                    document.getElementById("toto_tab_47").style.color="#313131";
                    break;
                case 7:
                    document.getElementById("toto_tabs_bg").style.background="url(/images/reiter_top_hi_3.gif)";
                    document.getElementById("toto_tab_8").style.color="#313131";
                    document.getElementById("toto_tab_6").style.color="#313131";
                    document.getElementById("toto_tab_7").style.color="#D00221";
                    document.getElementById("toto_tab_47").style.color="#313131";
                    break;
                case 47:
                    document.getElementById("toto_tabs_bg").style.background="url(/images/reiter_top_hi_4.gif)";
                    document.getElementById("toto_tab_8").style.color="#313131";
                    document.getElementById("toto_tab_6").style.color="#313131";
                    document.getElementById("toto_tab_7").style.color="#313131";
                    document.getElementById("toto_tab_47").style.color="#D00221";
                    break;
            }

            ajax_send("/totomat.php?leagueid="+this.leagueid);
            return;
        }

        if (response.next.length > 0) {
            document.getElementById("toto_title_next").style.display = "block";
        } else {
            document.getElementById("toto_title_next").style.display = "none";
        }

        this.last(response);
        this.next(response);
    },

    last : function (response) {
        var div, div2, parent, current;
       current = response["last"];
       
       language = document.location.href.match(/\/(de|fr)\//)[1];

       parent = document.getElementById("toto_last");
       while (parent.childNodes.length > 0) {
           parent.removeChild(parent.firstChild);
       }
       
       if (current.length == 0) {
           return;
       }
    
       div = document.createElement("div");
       div.innerHTML = current[0].begin.replace(/^(\d\d\d\d)-(\d\d)-(\d\d).*$/, "$3.$2.$1 ");
       div.className = "toto_date";
       parent.appendChild(div);

       for (var index in current) {
            
           navdata = current[index].nav;              

           div = document.createElement("div");
           div.innerHTML = '<a style="text-decoration: none;" href="'+navdata.dir+language+'/'+navdata.file+'?gameid='+current[index].gameid+'">'+response.teams[current[index].hometeamid]+"-"+response.teams[current[index].awayteamid]+'</a>';
           div.className = "toto_teams";
           parent.appendChild(div);

           div = document.createElement("div");
           div.innerHTML = current[index].homescore+":"+current[index].awayscore;
           div.className = "toto_score";
           if ( current[index].status >= 1 && current[index].status <= 8 ) {
             div.className += " toto_details_highlighted";
           }
           parent.appendChild(div);

           div = document.createElement("div");
           div.className = "toto_details";
           parent.appendChild(div);

           detail = [
             [current[index].status == 7 ? "V" : "", false],
             [current[index].status == 8 ? "P" : "", false],
             [current[index].status==10 || (current[index].status==12 && current[index].decisiontype==2) ? "V" : "", false],
             [current[index].status==11 || (current[index].status==12 && current[index].decisiontype==4) ? "P" : "", false],
             ["(", false],
             [current[index].onethirdhomescore+":"+current[index].onethirdawayscore, (current[index].status == 1) ? true : false],
             ["|", false],
             [current[index].twothirdshomescore+":"+current[index].twothirdsawayscore, (current[index].status == 3) ? true : false],
             ["|", false],
             [current[index].threethirdshomescore+":"+current[index].threethirdsawayscore, (current[index].status == 5) ? true : false],
             [")", false]
           ];
           for (index in detail) {
               div2 = document.createElement("div");
               div2.innerHTML = detail[index][0];
               div2.className = "toto_details_"+(detail[index][1] ? "highlighted" : "normal");
               div.appendChild(div2);
           }

           div = document.createElement("div");
           div.style.clear = "both";
           parent.appendChild(div);
       }

       div = document.createElement("div");
       div.style.height = "20px";
       parent.appendChild(div);
    },

    next : function (response) {
        var div, div2, parent, current;
        current = response["next"];
        
        language = document.location.href.match(/\/(de|fr)\//)[1];

        parent = document.getElementById("toto_next");
        while (parent.childNodes.length > 0) {
            parent.removeChild(parent.firstChild);
        }

        if (current.length == 0) {
            return;
        }
 
        div = document.createElement("div");
        div.innerHTML = current[0].begin.replace(/^(\d\d\d\d)-(\d\d)-(\d\d).*$/, "$3.$2.$1");
        div.className = "toto_date";
        parent.appendChild(div);
 
        for (var index in current) {
            
            navdata = current[index].nav;              

            div = document.createElement("div");
            div.innerHTML = '<a style="text-decoration: none;" href="'+navdata.dir+language+'/'+navdata.file+'?gameid='+current[index].gameid+'">'+response.teams[current[index].hometeamid]+"-"+response.teams[current[index].awayteamid]+'</a>';
            div.className = "toto_teams";
            parent.appendChild(div);
 
            div = document.createElement("div");
            div.innerHTML = current[index].begin.replace(/^.*T(.*):\d\d$/, "$1");
            div.className = "toto_kickoff";
            parent.appendChild(div);
 
            div = document.createElement("div");
            div.style.clear = "both";
            parent.appendChild(div);
        }
    }
}


function toto_load(response) {
    TOTO.load(response);
}

function toto_refresh() {
    TOTO.refresh();
}


