var PREVIEW = {
    current     : -1,
    distance    : 526,
    animation   : false,
    interval    : null,
    refresh     : 4000,
    display_ad  : 3000,
    movetoright : true,
    showAd      : false,
    lang        : document.location.href.toString().match(/\/fr\//) ? 'fr' : 'de',

    init : function () {

        // no games => no fun
        if (preview_games.length == 0) {
            return;
        }

        // tag dedicated for the application
        var application = document.getElementById("preview_application");
        application.className = "preview_application";

        // black canvas
        var canvas = document.createElement("div");
        canvas.className = "preview_canvas";
        canvas.id = "preview_canvas";
        canvas.style.left = "0px";
        canvas.style.width = (PREVIEW.distance*preview_games.length+230)+"px";
        application.appendChild(canvas);

        // silver cover
        var cover = document.createElement("div");
        cover.className = "preview_cover";
        cover.id = "preview_cover";
        application.appendChild(cover);
    
        var div;

        // left arrow
        div = document.createElement("div");
        div.className = "preview_arrow_left";
        div.onmouseover = function() { this.style.backgroundPosition= "0px 1px"; }
        div.onmouseout = function() { this.style.backgroundPosition= "0px 50px"; }
        div.onclick = function () { PREVIEW.back(); }
        cover.appendChild(div);

        // right arrow
        div = document.createElement("div");
        div.className = "preview_arrow_right";
        div.onmouseover = function() { this.style.backgroundPosition= "0px 1px"; }
        div.onmouseout = function() { this.style.backgroundPosition= "0px 50px"; }
        div.onclick = function () { PREVIEW.next(); }
        cover.appendChild(div);

        // clickable area
        div = document.createElement("div");
        div.className = "preview_clickable";
        div.onclick = function () { PREVIEW.details(); }
        cover.appendChild(div);

        // season
        div = document.createElement("div");
        div.className = "preview_season";
        div.id = "preview_season";
        div.innerHTML = "SAISON "+(preview_games[0].season-1)+"/"+preview_games[0].season;
        cover.appendChild(div);

        // begin
        div = document.createElement("div");
        div.className = "preview_begin";
        div.id = "preview_begin";
        cover.appendChild(div);

        // ad
        div = document.createElement("div");
        div.className = "preview_ad";
        div.id = "preview_ad";

        if (this.showAd) {
            cover.appendChild(div);
            window.setTimeout(function () {PREVIEW.removeAd();}, this.display_ad);
        }

        // show first match
        this.interval = window.setInterval(function () {PREVIEW.auto();}, this.refresh);
    },

    // diashow
    auto : function () {
        if (this.movetoright) {
            PREVIEW.next();
        } else {
            PREVIEW.back();
        }
    },

    // clicked on right arrow
    next : function () {
        this.movetoright = true;
        if (this.animation) {
            return;
        }
        this.animation = true;

        // get a container for the game to be displayed
        var game = this.get_game();
        game.style.left = "750px";

        // get game to be displayed
        PREVIEW.current++;
        if (PREVIEW.current >= preview_games.length) {
            PREVIEW.current = 0;
        }

        // attach game info
        this.attach_game(game);

        // move game into visible area
        document.getElementById("preview_game").interval = window.setInterval(function () {
            var tag = document.getElementById("preview_game");
            var tag2 = document.getElementById("preview_last_game");
            tag.step++;
            var left = Math.round(Math.sin(tag.step/20)*526);
            if ( left == 526) {
                window.clearInterval(tag.interval);
                if (tag2 != null) {
                    if (tag2.childNodes.length > 0) {
                        tag2.removeChild(tag2.firstChild);
                    }
                    tag2.parentNode.removeChild(tag2);
                }
                tag.id = "preview_last_game";
                PREVIEW.animation = false;
            }
            tag.style.left = 768-left + "px";
            if (tag2 != null) {
                tag2.style.left = 222-left + "px";
            }
        }, 50);
    },

    // clicked on left arrow
    back : function () {
        this.movetoright = false;
        if (this.animation) {
            return;
        }
        this.animation = true;
    
        var game = this.get_game();
        game.style.left = "-303px";

        PREVIEW.current--;
        if (PREVIEW.current < 0) {
            PREVIEW.current = preview_games.length-1;
        }
 
        this.attach_game(game);

        document.getElementById("preview_game").interval = window.setInterval(function () {
            var tag = document.getElementById("preview_game");
            var tag2 = document.getElementById("preview_last_game");
            tag.step++;
            var left = Math.round(Math.sin(tag.step/20)*526);
            if (left == 526) {

                window.clearInterval(tag.interval);
                if (tag2 != null) {
                    if (tag2.childNodes.length > 0) {
                        tag2.removeChild(tag2.firstChild);
                    }
                    tag2.parentNode.removeChild(tag2);
                }
                tag.id = "preview_last_game";
                PREVIEW.animation = false;
            }
            tag.style.left = -280+left + "px";
            if (tag2 != null) {
                tag2.style.left = 222+left + "px";
            }
        }, 50);
    },

    get_game : function () {

        var game = document.getElementById("preview_game");
        if (game == null) {
            game = document.createElement("div");
            game.id = "preview_game";
            game.className = "preview_game";
        } else {
            while (game.childNodes.length > 0) {
                game.removeNode(game.firstNode);
            }
        }
        game.step = 0;
        return game;
    },

    attach_game : function (game)  {

        // home team
        div = document.createElement("div");
        div.className = "preview_home_team";
        div.style.left = "0px";
        div.id = "preview_home_team";
        div.style.backgroundImage = "url(/preview/pics/clubs/logo_"+preview_games[PREVIEW.current].hometeamid+".gif)";
        div.innerHTML = preview_names[preview_games[PREVIEW.current].hometeamid];
        game.appendChild(div);

        // "vs"
        div = document.createElement("div");
        div.className = "preview_vs";
        div.style.left = "240px";
        div.innerHTML = "vs.";
        game.appendChild(div);

        // away team
        div = document.createElement("div");
        div.className = "preview_away_team";
        div.style.left = "270px";
        div.id = "preview_away_team";
        div.style.backgroundImage = "url(/preview/pics/clubs/logo_"+preview_games[PREVIEW.current].awayteamid+".gif)";
        div.innerHTML = preview_names[preview_games[PREVIEW.current].awayteamid];
        game.appendChild(div);
    
        document.getElementById("preview_canvas").appendChild(game);

        // kick off
        document.getElementById("preview_begin").innerHTML = preview_games[PREVIEW.current].begin[this.lang];
    },

    removeAd : function () {
        var ad = document.getElementById("preview_ad");
        if (ad == null) {
            return;
        }
        ad.step = 0;
        ad.last = 2;
        ad.interval = window.setInterval(function () {
            var ad = document.getElementById("preview_ad");
            ad.step++;
            var opa = 1-Math.sin(ad.step/10);

            if (opa >= 0) {
                ad.style.opacity = opa;
            }
            if (ad.last < opa) {
                window.clearInterval(ad.interval);
                ad.parentNode.removeChild(ad);
                return;
            }
            ad.last = opa;
        }, 100);
    },

    details : function () {
// /NL/spiele/nla/de/details_playoffs.php?gameid=20091105078001
// /SIH/fanzone/de/totomat.php?gameid=20126315030001
        
        var url = "/NL/spiele/";
        switch(preview_games[this.current].leagueid) {
            case 1:
                url += "nla/";
                break;
            case 8: //Leagueid 8 = Laenderspiele
                url = "../fanzone/de/totomat.php";
                break;
            default:
                return;
        }
        
        //wenn wir die Leagueid 8 haben, muss der Pfad nicht weiter manipuliert werden.
        if(preview_games[this.current].leagueid != 8){
            url += "de/";
            switch(preview_games[this.current].qualificationid) {
                case 1:
                    url += "details_qualification.php";
                    break;
                case 2:
                case 3:
                case 4:
                    url += "details_playoffs.php/";
                    break;
                default:
                    return;
            }
        }
        url += "?gameid="+preview_games[this.current].gameid;
        document.location.href = url;
    }
}

