function UpgradeWindow() {
  
  this.isOpen = false;
  
  this.buttonsX = [86, 266, 446, 86, 266, 446];
  this.buttonsY = [172, 172, 172, 312, 312, 312];
  
  this.prices = [];
  
  
  this.init = function() {
    for(var i = 0; i < 6; i++) {
      this.prices[i] = new Text();
      this.prices[i].setFont("Black");
    }
  };
  
  
  this.open = function() {
    this.isOpen = true;
    mouse.registerUpArea("closeUpgradeWindow", 296, 382, 100, 34, function() {
      panel.closeUpgradeWindow();
    });
    mouse.registerUpArea("upgradeSail", 96, 182, 140, 38, function() {
      panel.upgradeWindow.upgrade(0);
    });
    mouse.registerUpArea("upgradeHull", 276, 182, 140, 38, function() {
      panel.upgradeWindow.upgrade(1);
    });
    mouse.registerUpArea("upgradeSpies", 456, 182, 140, 38, function() {
      panel.upgradeWindow.upgrade(2);
    });
    mouse.registerUpArea("upgradeCannon", 96, 322, 140, 38, function() {
      panel.upgradeWindow.upgrade(3);
    });
    mouse.registerUpArea("upgradeCannonballs", 276, 322, 140, 38, function() {
      panel.upgradeWindow.upgrade(4);
    });
    mouse.registerUpArea("upgradeSailors", 456, 322, 140, 38, function() {
      panel.upgradeWindow.upgrade(5);
    });
  };
  
  
  this.upgrade = function(what) {
    if(this.isUpgradePossible(what)) {
      if(what == 0) {
        ship.gold -= Ship.sailsCost[ship.ownSails + 1];
        ship.ownSails++;
      } else if(what == 1) {
        ship.gold -= Ship.hullsCost[ship.ownHulls + 1];
        ship.ownHulls++;
      } else if(what == 2) {
        ship.gold -= Ship.spiesCost[ship.ownSpies + 1];
        ship.ownSpies++;
      } else if(what == 3) {
        ship.gold -= Ship.cannonCost[ship.ownCannon + 1];
        ship.ownCannon++;
      } else if(what == 4) {
        ship.gold -= Ship.cannonballsCost[ship.ownCannonballs + 1];
        ship.ownCannonballs++;
      } else if(what == 5) {
        ship.gold -= Ship.sailorsCost[ship.ownSailors + 1];
        ship.ownSailors++;
      }
      ship.setUpdates();
    }
  };
  

  this.isUpgradePossible = function(what) {
    if(what == 0) {
      return ship.ownSails != 5 && ship.gold >= Ship.sailsCost[ship.ownSails + 1];
    } else if(what == 1) {
      return ship.ownHulls != 5 && ship.gold >= Ship.hullsCost[ship.ownHulls + 1];
    } else if(what == 2) {
      return ship.ownSpies != 5 && ship.gold >= Ship.spiesCost[ship.ownSpies + 1];
    } else if(what == 3) {
      return ship.ownCannon != 5 && ship.gold >= Ship.cannonCost[ship.ownCannon + 1];
    } else if(what == 4) {
      return ship.ownCannonballs != 5 && ship.gold >= Ship.cannonballsCost[ship.ownCannonballs + 1];
    } else if(what == 5) {
      return ship.ownSailors != 5 && ship.gold >= Ship.sailorsCost[ship.ownSailors + 1];
    }
  };
  
  
  this.costOfUpgrade = function(what) {
    if(what == 0) {
      if(ship.ownSails == 5) {
        return -1;
      }
      return Ship.sailsCost[ship.ownSails + 1];
    } else if(what == 1) {
      if(ship.ownHulls == 5) {
        return -1;
      }
      return Ship.hullsCost[ship.ownHulls + 1];
    } else if(what == 2) {
      if(ship.ownSpies == 5) {
        return -1;
      }
      return Ship.spiesCost[ship.ownSpies + 1];
    } else if(what == 3) {
      if(ship.ownCannon == 5) {
        return -1;
      }
      return Ship.cannonCost[ship.ownCannon + 1];
    } else if(what == 4) {
      if(ship.ownCannonballs == 5) {
        return -1;
      }
      return Ship.cannonballsCost[ship.ownCannonballs + 1];
    } else if(what == 5) {
      if(ship.ownSailors == 5) {
        return -1;
      }
      return Ship.sailorsCost[ship.ownSailors + 1];
    }
  };
  
  
  this.close = function() {
    this.isOpen = false;
    mouse.deleteUpArea("closeUpgradeWindow");
    mouse.deleteUpArea("upgradeSail");
    mouse.deleteUpArea("upgradeHull");
    mouse.deleteUpArea("upgradeSpies");
    mouse.deleteUpArea("upgradeCannon");
    mouse.deleteUpArea("upgradeCannonballs");
    mouse.deleteUpArea("upgradeSailors");
  };
  
  
  this.draw = function() {
    if(this.isOpen) {
      img.draw("upgradeWindow", 0, 0);
      
      for(var i = 0; i < 6; i++) {
        var offset = 0;
        if(this.isUpgradePossible(i)) {
          if(mouse.isOver(this.buttonsX[i] + 10, this.buttonsY[i] + 10, 140, 38)) {
            if(mouse.down) {
              offset = 1;
              img.drawSprite("upgradeButtons", this.buttonsX[i], this.buttonsY[i], 160, 60, 2, i);
            } else {
              img.drawSprite("upgradeButtons", this.buttonsX[i], this.buttonsY[i], 160, 60, 1, i);
            }
          } else {
            img.drawSprite("upgradeButtons", this.buttonsX[i], this.buttonsY[i], 160, 60, 0, i);
          }
        } else {
          img.drawSprite("upgradeButtons", this.buttonsX[i], this.buttonsY[i], 160, 60, 3, i);
        }
        
        var price = this.costOfUpgrade(i);
        if(price != -1) {
          img.draw("coin", this.buttonsX[i] + 100, this.buttonsY[i] + 12 + offset);
          
          this.prices[i].setPosition(this.buttonsX[i] + 121, this.buttonsY[i] + 13 + offset);
          this.prices[i].text(price.toString());
          this.prices[i].draw();
        }
      }
      
      if(mouse.isOver(296, 382, 100, 34)) {
        if(mouse.down) {
          img.drawSprite("closeButton", 286, 372, 120, 54, 2, 0);
        } else {
          img.drawSprite("closeButton", 286, 372, 120, 54, 1, 0);
        }
      } else {
        img.drawSprite("closeButton", 286, 372, 120, 54, 0, 0);
      }
      
    }
  };
  
}