contract hodlToken is Pausable, StandardToken {

	using SafeMath for uint256;

	address public escrow = this;

	//20% Finder allocation 
	uint256 public purchasableTokens = 112000 * 10**18;
	uint256 public founderAllocation = 28000 * 10**18;

	string public name = "TeamHODL Token";
	string public symbol = "THODL";
	uint256 public decimals = 18;
	uint256 public INITIAL_SUPPLY = 140000 * 10**18;

	uint256 public RATE = 200;
	uint256 public REFUND_RATE = 200;

	/**
	* @dev Contructor that gives msg.sender all of existing tokens.
	**/
	function hodlToken() {
		totalSupply = INITIAL_SUPPLY;
		balances[msg.sender] = INITIAL_SUPPLY;
	}

	/**
	* @dev Allows the current owner to transfer control of the contract to a newOwner.
	* @param newOwner The address to transfer ownership to.
	**/
	function transferOwnership(address newOwner) onlyOwner {
		address oldOwner = owner;
		super.transferOwnership(newOwner);
		balances[newOwner] = balances[oldOwner];
		balances[oldOwner] = 0;
	}

	/**
	* @dev Allows the current owner to transfer escrowship of the contract to a escrow account.
	* @param newEscrow The address to transfer the escrow account to.
	**/
	function transferEscrowship(address newEscrow) onlyOwner {
		if (newEscrow != address(0)) {
			escrow = newEscrow;
		}
	}

	/**
	* @dev Allows the current owner to set the new total supply, to be used iff not all tokens sold during crowdsale.
	**/
	function setTotalSupply() onlyOwner whenCrowdsaleEnded {
		if (purchasableTokens > 0) {
			totalSupply = totalSupply.sub(purchasableTokens);
		}
	}

	/**
	* @dev Allows the current owner to withdraw ether funds after ICO ended.
	**/
	function cashOut() onlyOwner whenCrowdsaleEnded {
		
		/**
		* Transfer money from escrow wallet up to 1 day after ICO end.
		**/
		if (dayAfterDeadline >= now) {
			owner.transfer(escrow.balance);
		}
	}
  
	/**
	* @dev Allows owner to change the exchange rate of tokens (default 0.005 Ether)
	**/
	function setRate(uint256 rate) {

		/**
		* If break-even point has been reached (3500 Eth = 3.5*10**21 Wei),
		* rate updates to 20% of total revenue (100% of dedicated wallet after forwarding contract)
		**/
		if (escrow.balance >= 7*10**20) {

			/**
			* Rounds up to address division error
			**/
			RATE = (((totalSupply.mul(10000)).div(escrow.balance)).add(9999)).div(10000);
		}
	}
  
	/**
	* @dev Allows owner to change the refund exchange rate of tokens (default 0.005 Ether)
	* @param rate The number of tokens to release
	**/
	function setRefundRate(uint256 rate) {

		/**
		* If break-even point has been reached (3500 Eth = 3.5*10**21 Wei),
		* refund rate updates to 20% of total revenue (100% of dedicated wallet after forwarding contract)
		**/
		if (escrow.balance >= 7*10**20) {

			/**
			* Rounds up to address division error
			**/
			REFUND_RATE = (((totalSupply.mul(10000)).div(escrow.balance)).add(9999)).div(10000);
		}
	}

	/**
	* @dev fallback function
	**/
	function () payable {
		if(now <= deadline){
			buyTokens(msg.sender);
		}
	}

	/**
	* @dev function that sells available tokens
	**/
	function buyTokens(address addr) payable whenNotPaused whenCrowdsaleNotEnded {
		
		/**
		* Calculate tokens to sell and check that they are purchasable
		**/
		uint256 weiAmount = msg.value;
		uint256 tokens = weiAmount.mul(RATE);
		require(purchasableTokens >= tokens);

		/**
		* Send tokens to buyer
		**/
		purchasableTokens = purchasableTokens.sub(tokens);
		balances[owner] = balances[owner].sub(tokens);
		balances[addr] = balances[addr].add(tokens);

		Transfer(owner, addr, tokens);
	}
  
	function fund() payable {}

	function defund() onlyOwner {}

	function refund(uint256 _amount) payable whenNotPaused whenCrowdsaleEnded {

		/**
		* Calculate amount of THODL to refund
		**/
		uint256 refundTHODL = _amount.mul(10**18);
		require(balances[msg.sender] >= refundTHODL);

		/**
		* Calculate refund in wei
		**/
		uint256 weiAmount = refundTHODL.div(REFUND_RATE);
		require(this.balance >= weiAmount);

		balances[msg.sender] = balances[msg.sender].sub(refundTHODL);
		
		/**
		* The tokens are burned
		**/
		totalSupply = totalSupply.sub(refundTHODL);

		msg.sender.transfer(weiAmount);
	}
}
/**
 * @title hodlToken
 * @dev All tokens are pre-assigned to the creator.
 * Tokens can be transferred using 'transfer' and other
 * 'StandardToken' functions.
 **/
function cashOut() onlyOwner whenCrowdsaleEnded {
		
	/**
	* Transfer money from escrow wallet up to 1 day after ICO end.
	**/
	if (dayAfterDeadline >= now) {
		owner.transfer(escrow.balance);
	}
}

/**
 	* @dev Allows the current owner to withdraw ether funds after ICO ended.
 	**/