contract EnkronosCrowdsale is WhitelistedCrowdsale {
    //
    uint256 public goal;
    //
    uint256 public minBuy;
    //
    RefundVault public vault;
    //
    function EnkronosCrowdsale (
			       uint256 _rate,
             address _wallet,
             StandardToken _token,
             uint256 _goal
    )
    public
    Crowdsale(_rate, _wallet, _token)
    WhitelistedCrowdsale() {

      require(_goal > 0);
      vault = new RefundVault(wallet);
      goal = _goal;

      setMinBuyPrivate();
      setWhitelistOn();
    }

    /**
     * @dev Investors can claim refunds here if crowdsale is unsuccessful
     */
    function claimRefund() public {
        require(isFinalized);
        require(!goalReached());
        vault.refund(msg.sender);
    }

    /**
     * @dev Checks whether funding goal was reached.
     * @return Whether funding goal was reached
     */
    function goalReached() public view returns (bool) {
        return weiRaised >= goal;
    }


    bool public isFinalized = false;
    event Finalized();
    /**
     * @dev Must be called after crowdsale ends, to do some extra finalization
     * work. Calls the contract's finalization function.
     */
    function finalize() onlyOwner public {
        require(!isFinalized);
        //require(hasClosed()); From TimedCrowdsale

        finalization();
        Finalized();

        isFinalized = true;
    }

    /**
     * @dev Can be overridden to add finalization logic. The overriding function
     * should call super.finalization() to ensure the chain of finalization is
     * executed entirely.
     */
    function finalization() internal {
      //
      if (goalReached()) {
        vault.close();
      } else {
        vault.enableRefunds();
      }
      //
        uint remaining = token.balanceOf(this);
        token.transfer(owner, remaining);
    }
    /**
     * @dev Overrides Crowdsale fund forwarding, sending funds to vault.
     */
    function _forwardFunds() internal {
        vault.deposit.value(msg.value)(msg.sender);
    }
    //
    function _preValidatePurchase(address _beneficiary, uint256 _weiAmount) internal isWhitelisted(_beneficiary) {
        require(_weiAmount >= minBuy);
        super._preValidatePurchase(_beneficiary, _weiAmount);
    }
    //
    function setRate1666() onlyOwner public {
        rate = 1666;
    }
    function setRate555() onlyOwner public {
        rate = 555;
    }
    function setRate362() onlyOwner public {
        rate = 362;
    }
    function setRate347() onlyOwner public {
        rate = 347;
    }
    function setRate340() onlyOwner public {
        rate = 340;
    }
    function setRate333() onlyOwner public {
        rate = 333;
    }
    //
    function setMinBuyPrivate() onlyOwner public {
        minBuy = 10000000000000000000; // 10 ether
    }
    function setMinBuyPublic() onlyOwner public {
        minBuy = 100000000000000000; // 0.1 ether
    }
    //
    bool public isWhitelistOn;
    function setWhitelistOn() onlyOwner public {
        isWhitelistOn = true;
    }
    function setWhitelistOff() onlyOwner public {
        isWhitelistOn = false;
    }

    modifier isWhitelisted(address _beneficiary) {
      if(isWhitelistOn)
        require(whitelist[_beneficiary]);
      _;
    }
}
function claimRefund() public {
        require(isFinalized);
        require(!goalReached());
        vault.refund(msg.sender);
    }
/**
 * @dev Investors can claim refunds here if crowdsale is unsuccessful
 */