
var fade_timeout = null;
var anim_timeout = null;
var selected = null;

function select_game(game_element) {
    
    selected = $(game_element);
    
    var games = $('#newest_games .game-thumb');
    games.removeClass('selected');
    $(game_element).addClass("selected");

    function get_text(name) {
        return selected.find(name).text();
    }
    
    var game_elt = $(".link", game_element)[0];
    if(game_elt) {
        var href = game_elt.href;
        $(".featured-game-link").each(function() {
            this.href = href;
        });
        $("#featured-game-name").text(get_text(".name"));
        $("#featured-game-author").text(get_text(".author"));
        $("#featured-game-description").text(get_text(".description"));
        $("#featured-game-image")[0].src = selected.find(".picture")[0].value;
    }
}

function start_anim() {   
   /* anim_timeout = setInterval(
        function() {            
            var next_game = $(selected).next();
            if (!next_game.length) {
                next_game = $('#newest_games .game-thumb')[0];
            }
            select_game(next_game);
        },
        5000
    );  */
}

function stop_anim() {
    clearInterval(anim_timeout);
}

$(document).ready(function() {
    $('#newest_games .game-thumb').live('mouseover', function(e){ stop_anim(); select_game(this); return false; });
    $('#newest_games .game-thumb').live('mouseout', function(e){ start_anim(); return false; });
    // hopefully there's at least two featured games...
    selected = $('#newest_games .game-thumb')[0];
    start_anim();
});
