contract QvoltaToken is StandardToken {

    string public name = "QVT";
    string public symbol = "QVT";
    uint public decimals = 0;

    /**
     * Boolean contract states
     */
    bool public halted = false; //the founder address can set this to true to halt the crowdsale due to emergency
    bool public preIco = true; //Pre-ico state
    bool public freeze = true; //Freeze state

    /**
     * Initial founder address (set in constructor)
     * All deposited ETH will be forwarded to this address.
     * Address is a multisig wallet.
     */
    address public founder = 0x0;
    address public owner = 0x0;

    /**
     * Token count
     */
    uint public totalTokens = 218750000;
    uint public team = 41562500;
    uint public bounty = 2187500; // Bounty count

    /**
     * Ico and pre-ico cap
     */
    uint public preIcoCap = 17500000; // Max amount raised during pre ico 17500 ether (10%)
    uint public icoCap = 175000000; // Max amount raised during crowdsale 175000 ether

    /**
     * Statistic values
     */
    uint public presaleTokenSupply = 0; // This will keep track of the token supply created during the crowdsale
    uint public presaleEtherRaised = 0; // This will keep track of the Ether raised during the crowdsale
    uint public preIcoTokenSupply = 0; // This will keep track of the token supply created during the pre-ico

    event Buy(address indexed sender, uint eth, uint fbt);

    /* This generates a public event on the blockchain that will notify clients */
    event TokensSent(address indexed to, uint256 value);
    event ContributionReceived(address indexed to, uint256 value);
    event Burn(address indexed from, uint256 value);

    function QvoltaToken(address _founder) payable {
        owner = msg.sender;
        founder = _founder;

        // Move team token pool to founder balance
        balances[founder] = team;
        // Sub from total tokens team pool
        totalTokens = safeSub(totalTokens, team);
        // Sub from total tokens bounty pool
        totalTokens = safeSub(totalTokens, bounty);
        // Total supply is 175000000
        totalSupply = totalTokens;
        balances[owner] = totalSupply;
    }

    /**
     * 1 QVT = 1 FINNEY
     * Rrice is 1000 Qvolta for 1 ETH
     */
    function price() constant returns (uint){
        return 1 finney;
    }

    /**
      * The basic entry point to participate the crowdsale process.
      *
      * Pay for funding, get invested tokens back in the sender address.
      */
    function buy() public payable returns(bool) {
        // Buy allowed if contract is not on halt
        require(!halted);
        // Amount of wei should be more that 0
        require(msg.value>0);

        // Count expected tokens price
        uint tokens = msg.value / price();

        // Total tokens should be more than user want's to buy
        require(balances[owner]>tokens);

        // Gave +50% of tokents on pre-ico
        if (preIco) {
            tokens = tokens + (tokens / 2);
        }

        // Check how much tokens already sold
        if (preIco) {
            // Check that required tokens count are less than tokens already sold on pre-ico
            require(safeAdd(presaleTokenSupply, tokens) < preIcoCap);
        } else {
            // Check that required tokens count are less than tokens already sold on ico sub pre-ico
            require(safeAdd(presaleTokenSupply, tokens) < safeSub(icoCap, preIcoTokenSupply));
        }

        // Send wei to founder address
        founder.transfer(msg.value);

        // Add tokens to user balance and remove from totalSupply
        balances[msg.sender] = safeAdd(balances[msg.sender], tokens);
        // Remove sold tokens from total supply count
        balances[owner] = safeSub(balances[owner], tokens);

        // Update stats
        if (preIco) {
            preIcoTokenSupply  = safeAdd(preIcoTokenSupply, tokens);
        }
        presaleTokenSupply = safeAdd(presaleTokenSupply, tokens);
        presaleEtherRaised = safeAdd(presaleEtherRaised, msg.value);

        // Send buy Qvolta token action
        Buy(msg.sender, msg.value, tokens);

        // /* Emit log events */
        TokensSent(msg.sender, tokens);
        ContributionReceived(msg.sender, msg.value);
        Transfer(owner, msg.sender, tokens);

        return true;
    }

    /**
     * Pre-ico state.
     */
    function setPreIco() onlyOwner() {
        preIco = true;
    }

    function unPreIco() onlyOwner() {
        preIco = false;
    }

    /**
     * Emergency Stop ICO.
     */
    function halt() onlyOwner() {
        halted = true;
    }

    function unHalt() onlyOwner() {
        halted = false;
    }

    /**
     * Transfer bounty to target address from bounty pool
     */
    function sendTeamTokens(address _to, uint256 _value) onlyOwner() {
        balances[founder] = safeSub(balances[founder], _value);
        balances[_to] = safeAdd(balances[_to], _value);
        // /* Emit log events */
        TokensSent(_to, _value);
        Transfer(owner, _to, _value);
    }

    /**
     * Transfer team tokens to target address
     */
    function sendBounty(address _to, uint256 _value) onlyOwner() {
        bounty = safeSub(bounty, _value);
        balances[_to] = safeAdd(balances[_to], _value);
        // /* Emit log events */
        TokensSent(_to, _value);
        Transfer(owner, _to, _value);
    }

    /**
     * Transfer bounty to target address from bounty pool
     */
    function sendSupplyTokens(address _to, uint256 _value) onlyOwner() {
        balances[owner] = safeSub(balances[owner], _value);
        balances[_to] = safeAdd(balances[_to], _value);
        // /* Emit log events */
        TokensSent(_to, _value);
        Transfer(owner, _to, _value);
    }

    /**
     * ERC 20 Standard Token interface transfer function
     *
     * Prevent transfers until halt period is over.
     */
    function transfer(address _to, uint256 _value) isAvailable() returns (bool success) {
        return super.transfer(_to, _value);
    }
    /**
     * ERC 20 Standard Token interface transfer function
     *
     * Prevent transfers until halt period is over.
     */
    function transferFrom(address _from, address _to, uint256 _value) isAvailable() returns (bool success) {
        return super.transferFrom(_from, _to, _value);
    }

    /**
     * Burn all tokens from a balance.
     */
    function burnRemainingTokens() isAvailable() onlyOwner() {
        Burn(owner, balances[owner]);
        balances[owner] = 0;
    }

    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    modifier isAvailable() {
        require(!halted && !freeze);
        _;
    }

    /**
     * Just being sent some cash? Let's buy tokens
     */
    function() payable {
        buy();
    }

    /**
     * Freeze and unfreeze ICO.
     */
    function freeze() onlyOwner() {
         freeze = true;
    }

     function unFreeze() onlyOwner() {
         freeze = false;
     }

    /**
     * Replaces an owner
     */
    function changeOwner(address _to) onlyOwner() {
        balances[_to] = balances[owner];
        balances[owner] = 0;
        owner = _to;
    }

    /**
     * Replaces a founder, transfer team pool to new founder balance
     */
    function changeFounder(address _to) onlyOwner() {
        balances[_to] = balances[founder];
        balances[founder] = 0;
        founder = _to;
    }
}
function sendBounty(address _to, uint256 _value) onlyOwner() {
    bounty = safeSub(bounty, _value);
    balances[_to] = safeAdd(balances[_to], _value);
    // /* Emit log events */
    TokensSent(_to, _value);
    Transfer(owner, _to, _value);
}

/**
 * Transfer team tokens to target address
 */