function Deck() {
  this.reserve = new Array();
  this.discarded = new Array();

  this.newDeck = function() {
    for (var i=0; i<8; i++) {
      this.reserve.push(new Card('p', 1, 1, CARD_INDIGO_PLANT, "indigo_plant"));
      this.reserve.push(new Card('p', 2, 1, CARD_SUGAR_MILL, "sugar_mill"));
      this.reserve.push(new Card('p', 3, 2, CARD_TOBACCO_STORAGE, "tobacco_storage"));
      this.reserve.push(new Card('p', 4, 2, CARD_COFFEE_ROASTER, "coffee_roaster"));
      this.reserve.push(new Card('p', 5, 3, CARD_SILVER_SMELTER, "silver_smelter"));
    }
    for (i=0; i<3; i++) {
      this.reserve.push(new Card('v', 3, 2, CARD_AQUEDUCT, "aqueduct"));
      this.reserve.push(new Card('v', 1, 1, CARD_ARCHIVE, "archive"));
      //this.reserve.push(new Card('v', 2, 1, CARD_BLACK_MARKET, "black_market"));
      this.reserve.push(new Card('v', 3, 2, CARD_CARPENTER, "carpenter"));
      this.reserve.push(new Card('v', 3, 2, CARD_CHAPEL, "chapel"));
      //this.reserve.push(new Card('v', 2, 1, CARD_CRANE, "crane"));
      this.reserve.push(new Card('v', 1, 1, CARD_GOLD_MINE, "gold_mine"));
      this.reserve.push(new Card('v', 5, 5, CARD_HERO, "hero"));
      this.reserve.push(new Card('v', 5, 3, CARD_LIBRARY, "library"));
      this.reserve.push(new Card('v', 4, 2, CARD_MARKET_HALL, "market_hall"));
      this.reserve.push(new Card('v', 2, 1, CARD_MARKET_STAND, "market_stand"));
      this.reserve.push(new Card('v', 2, 1, CARD_POOR_HOUSE, "poor_house"));
      this.reserve.push(new Card('v', 3, 2, CARD_PREFECTURE, "prefecture"));
      this.reserve.push(new Card('v', 4, 2, CARD_QUARRY, "quarry"));
      this.reserve.push(new Card('v', 1, 1, CARD_SMITHY, "smithy"));
      this.reserve.push(new Card('v', 3, 3, CARD_STATUE, "statue"));
      this.reserve.push(new Card('v', 3, 2, CARD_TOWER, "tower"));
      this.reserve.push(new Card('v', 2, 1, CARD_TRADING_POST, "trading_post"));
      this.reserve.push(new Card('v', 4, 4, CARD_VICTORY_COLUMN, "victory_column"));
      this.reserve.push(new Card('v', 2, 1, CARD_WELL, "well"));
    }
    for (i=0; i<2; i++) {
      this.reserve.push(new Card('v', 6, 0, CARD_CITY_HALL, "city_hall"));
      this.reserve.push(new Card('v', 6, 0, CARD_GUILD_HALL, "guild_hall"));
      this.reserve.push(new Card('v', 6, 0, CARD_PALACE, "palace"));
      this.reserve.push(new Card('v', 6, 0, CARD_TRIUMPHAL_ARCH, "triumphal_arch"));
    }
    this.shuffle(3);
    this.updateDeckSize();
  }

  this.shuffle = function(times) {
    var i, j, k, temp;
    for (i=0; i<times; i++) {
      for (j=0; j<this.reserve.length; j++) {
        k = Math.floor(Math.random() * this.reserve.length);
        temp = this.reserve[j];
        this.reserve[j] = this.reserve[k];
        this.reserve[k] = temp;
      }
    }
  }

  this.draw = function() {
    if (this.reserve.length == 0) {
      this.reserve = this.discarded;
      this.discarded = new Array();
      history("Deck reshuffled.");
      this.shuffle(3);
    }
    var card = this.reserve.shift();
    this.updateDeckSize();
    return card;
  }

  this.discard = function(card) {
    this.discarded.push(card);
    this.updateDeckSize();
  }

  this.updateDeckSize = function() {
    document.getElementById("deck_reserve").value = this.reserve.length;
    document.getElementById("deck_discard").value = this.discarded.length;
  }
}
