game =  {
  // block_ids is an array.
  show_blocks: function(block_ids)
  {
    $('.game-block').each(function(){
      to_show = false;
      for(idx in block_ids) {
        block = block_ids[idx] + "_block";
        if (this.id == block){
          to_show = true
          break;
        }
      };

      if (! to_show){
        if ($(this).is(':visible'))
        {
           $(this).slideUp();
        }
      }
    });

    for(idx in block_ids) {
      block = '#' +block_ids[idx] + "_block";
      if ($(block).is(':hidden')){
        $(block).slideDown();
      }
    };

  },

  tick: function(i){
    this.move_ticker();
    this.health_ticker();
  },

  stop_ticking: function(){
    $(document).stopTime();
  },

  start_ticking: function(){
    $(document).everyTime(1000, function(){game.tick()}, 1000);
  },

  move_ticker: function(){
      countdown = parseInt($('#move_countdown').attr('seconds'));
      move_max = parseInt($('#move_max').text());
      moves = parseInt($('#moves').text());
      move_interval = parseInt($('#move_max').attr('interval'));
      min_sec = "0:00";

      if (moves < move_max)
      {
        if (countdown > 0)
        {
          countdown -= 1;
        }
        else
        {
          countdown = move_interval
          moves += 1;
          $('#moves').text(moves);
        }
       
        if (moves <  move_max)
        {
          min_sec = parseInt(countdown / 60) + ":" ;
          sec = parseInt(countdown % 60);
          if (sec < 10) { sec = "0" + sec}
          min_sec += sec
        }
        $('#move_countdown').attr('seconds', countdown);
      }
      $('#move_countdown').text(min_sec);
  },

  health_ticker: function(){
      countdown = parseInt($('#health_countdown').attr('seconds'));
      health_max = parseInt($('#health_max').text());
      health = parseInt($('#health').text());
      health_interval = parseInt($('#health_max').attr('interval'));
      min_sec = "0:00";

      if (health < health_max)
      {
        if (countdown > 0)
        {
          countdown -= 1;
        }
        else
        {
          countdown = health_interval
          health += 1;
          $('#health').text(health);
        }
       
        if (health <  health_max)
        {
          min_sec = parseInt(countdown / 60) + ":" ;
          sec = parseInt(countdown % 60);
          if (sec < 10) { sec = "0" + sec}
          min_sec += sec
        }
        $('#health_countdown').attr('seconds', countdown);
      }
      $('#health_countdown').text(min_sec);
  },



  show_main_blocks: function(){
    this.show_blocks(['location', 'event', 'action']);
  },

  init_game: function()
  {
    $('#system_working').ajaxStart(function(){
        $(this).show();
        document.body.style.cursor = 'wait';
    });

    $('#system_working').ajaxStop(function(){
        $(this).hide();
        document.body.style.cursor = '';
    });

    $.post("/game/move", function(data){
          eval(data);
    });
  },

  system_error: function(display){
    document.getElementById('system_error_cover').style.display = display;
  }

}


// to bypass firebug errors that accidentally make it to production...


if (! console)
{
  var console;
  console = {
    debug: function(){}
  }
}
