contract CrowdsaleTokenExt is ReleasableToken, MintableTokenExt, UpgradeableToken {

  /** Name and symbol were updated. */
  event UpdatedTokenInformation(string newName, string newSymbol);

  event ClaimedTokens(address indexed _token, address indexed _controller, uint _amount);

  string public name;

  string public symbol;

  uint public decimals;
  
  /* Minimum ammount of tokens every buyer can buy. */
  uint public minCap;

  mapping(address => uint256) public steps;
  mapping(address => uint256) public starts;
  mapping(address => uint256) public durations;
  mapping(address => uint256) public amounts;
  mapping(address => bool) public locked;

  /**
   * Construct the token.
   *
   * This token must be created through a team multisig wallet, so that it is owned by that wallet.
   *
   * @param _name Token name
   * @param _symbol Token symbol - should be all caps
   * @param _initialSupply How many tokens we start with
   * @param _decimals Number of decimal places
   * @param _mintable Are new tokens created over the crowdsale or do we distribute only the initial supply? Note that when the token becomes transferable the minting always ends.
   */
  function CrowdsaleTokenExt(string _name, string _symbol, uint _initialSupply, uint _decimals, bool _mintable, uint _globalMinCap)
    UpgradeableToken(msg.sender) {

    // Create any address, can be transferred
    // to team multisig via changeOwner(),
    // also remember to call setUpgradeMaster()
    owner = msg.sender;

    name = _name;
    symbol = _symbol;

    totalSupply = _initialSupply;

    decimals = _decimals;

    minCap = _globalMinCap;

    // Create initially all balance on the team multisig
    balances[owner] = totalSupply;

    if(totalSupply > 0) {
      Minted(owner, totalSupply);
    }

    // No more new supply allowed after the token creation
    if(!_mintable) {
      mintingFinished = true;
      if(totalSupply == 0) {
        throw; // Cannot create a token without supply and no minting
      }
    }
  }

  /**
   * When token is released to be transferable, enforce no new tokens can be created.
   */
  function releaseTokenTransfer() public onlyReleaseAgent {
    mintingFinished = true;
    super.releaseTokenTransfer();
  }

  /**
   * Allow upgrade agent functionality kick in only if the crowdsale was success.
   */
  function canUpgrade() public constant returns(bool) {
    return released && super.canUpgrade();
  }

  /**
   * Owner can update token information here.
   *
   * It is often useful to conceal the actual token association, until
   * the token operations, like central issuance or reissuance have been completed.
   *
   * This function allows the token owner to rename the token after the operations
   * have been completed and then point the audience to use the token contract.
   */
  function setTokenInformation(string _name, string _symbol) onlyOwner {
    name = _name;
    symbol = _symbol;

    UpdatedTokenInformation(name, symbol);
  }

  /**
   * Claim tokens that were accidentally sent to this contract.
   *
   * @param _token The address of the token contract that you want to recover.
   */
  function claimTokens(address _token) public onlyOwner {
    require(_token != address(0));

    ERC20 token = ERC20(_token);
    uint balance = token.balanceOf(this);
    token.transfer(owner, balance);

    ClaimedTokens(_token, owner, balance);
  }

  function transfer(address _to, uint _value) public returns (bool success) {
    require(balances[msg.sender] - timeLockedBalanceOf(msg.sender) >= _value);

    balances[msg.sender] = safeSub(balances[msg.sender], _value);
    balances[_to] = safeAdd(balances[_to], _value);
    Transfer(msg.sender, _to, _value);
    return true;
  }
  function transferFrom(address _from, address _to, uint _value) public returns (bool success) {
    require(balances[msg.sender] - timeLockedBalanceOf(msg.sender) >= _value);

    uint _allowance = allowed[_from][msg.sender];

    balances[_to] = safeAdd(balances[_to], _value);
    balances[_from] = safeSub(balances[_from], _value);
    allowed[_from][msg.sender] = safeSub(_allowance, _value);
    Transfer(_from, _to, _value);
    return true;
  }

  function timeLockedBalanceOf(address _addr) public constant returns (uint256) {
    if (locked[_addr] == false) {
      return 0;
    }
    uint256 _start = starts[_addr];
    uint256 _amount = amounts[_addr];
    uint256 _duration = durations[_addr];
    uint256 _step = steps[_addr];
    if (block.timestamp < _start) {
      return _amount;
    } else if (block.timestamp >= safeAdd(_start, _duration)) {
      return 0;
    } else {
      return safeSub(_amount, safeMul(safeDiv(_amount, safeDiv(_duration, _step)), safeDiv(safeSub(block.timestamp, _start), _step) + 1));
    }
  }
  function setTimeLock(address _to, uint256 step, uint256 start, uint256 duration, uint amount) public onlyOwner {
    steps[_to] = step;
    starts[_to] = start;
    durations[_to] = duration;
    amounts[_to] = amount;
    locked[_to] = true;
  } 
}
/**
 * A crowdsaled token.
 *
 * An ERC-20 token designed specifically for crowdsales with investor protection and further development path.
 *
 * - The token transfer() is disabled until the crowdsale is over
 * - The token contract gives an opt-in upgrade path to a new contract
 * - The same token can be part of several crowdsales through approve() mechanism
 * - The token can be capped (supply set in the constructor) or uncapped (crowdsale contract can mint new tokens)
 *
 */
function canUpgrade() public constant returns(bool) {
    return released && super.canUpgrade();
  }
/**
 * Allow upgrade agent functionality kick in only if the crowdsale was success.
 */