$(document).ready(function() {
  
  var Spawnlist = {
    state:          0,
    current_row:    1,
    snake_heads:    3,
    last_active:    0,
    running:        null,
    row:            null,
    board_width:    7,
    going_right:    1,
    
    init: function () {
      $(document).keydown(function(e) {
        if(e.which == 32) {
          Spawnlist.spacebar();
        }
        if (e.which == 13) {
          Spawnlist.start_game();
        }
      });
      
      $(document).bind('mousedown', function() {
        Spawnlist.spacebar();
      });
            
    },
    
    init_row: function() {
      this.row = new Array("", "", "", "", "", "","");
      this.going_right = 1;
    },
    
    spacebar: function() {
      if (this.state == 0) {
        this.start_game();
      } else if (this.state == 1) {
        this.drop_it();
      }

    },    
    
    start_game: function() {
      clearTimeout(this.running);
      
      this.state       = 1;
      this.current_row = 1;
      this.snake_heads = 3;
      
      $("td").removeClass('active');

      this.init_row();
      this.run_row();
    },
    
    run_row: function() {
    
      if ((this.current_row == 4 && this.snake_heads == 3) || this.current_row ==  8 && this.snake_heads == 2) {
        this.snake_heads--;
      }
      
      var self = this;
      var scope_bridge = function() {
        self.cycle();
      }
      
      var speed = 275 - this.current_row * 25;
      
      this.running = setTimeout(scope_bridge, speed);
    },
    
    cycle: function() {
            
      var active_count = 0;
      var last_active  = 0;
      for (var i = 0; i < this.row.length; i++) {
        if (this.row[i] == 1) {
          active_count++;
          last_active = i;
        }
      }
      
      if (active_count == 0) {
        this.row[0] = 1;
      }
      
      if (this.going_right == 1) {
        if (active_count < this.snake_heads && active_count > 0) {
          this.row[last_active + 1] = 1;
        }
      
        if (active_count == this.snake_heads && last_active < this.board_width - 1) {
          this.row[last_active + 1] = 1;
          this.row[(last_active + 1) - this.snake_heads] = 0;
        }
      
        if (active_count == this.snake_heads && last_active == this.board_width -1) {
          this.row[last_active - this.snake_heads] = 1;
          this.row[this.board_width - 1] = 0;
          this.going_right = 0;
        }
      } else {
        first_position = (last_active + 1) - this.snake_heads;
        
        if (first_position > 0) {
          this.row[first_position - 1] = 1;
          this.row[last_active] = 0;
        }
      
        if (first_position == 0) {
          this.row[0] = 0;
          this.row[last_active + 1] = 1;
          this.going_right = 1;
        }
      }
      
      this.render_row();
      this.run_row();
    },
    
    render_row: function()
    {
      for ( i = 0; i < this.row.length; i++) {
        if (this.row[i] == 1) {
          $(".level" + this.current_row + " td:eq(" + i + ")").addClass('active');
        } else {
          $(".level" + this.current_row + " td:eq(" + i + ")").removeClass('active');
        }
      }
    },
    
    drop_it: function() {
      clearTimeout(this.running);
            
      var original_snake_heads = this.snake_heads;
      var on_the_board = 0;
      if (this.current_row != 1) {        
        for(var i = 0; i < this.row.length; i++) {
          if (this.row[i] == 1) {
              on_the_board++;
            if (!$(".level" + (this.current_row - 1) + " td:eq(" + i + ")").hasClass('active')) {
              this.row[i] = 0;
              this.render_row();
              this.snake_heads--;
            }
          }
        }
        this.snake_heads = this.snake_heads - (original_snake_heads - on_the_board);
      }
      
      if (this.snake_heads > 0) {
        this.current_row++;

        this.init_row();
        this.run_row();
      }
    },
        
  }
  Spawnlist.init();
  
});

