contract ERC223Token is ERC223,SafeMath ,Ownable {

  mapping(address => uint) balances;
  
  string public name;
  string public symbol;
  uint256 public decimals;
  uint256 public totalSupply;
  
  address public crowdsaleAgent;
  address[] public addrCotracts;
  bool public released = false;  
  mapping(address => bool) public privilegedAddr;
  bool public privilege = false;
  
  /**
   * @dev The function can be called only by crowdsale agent.
   */
  modifier onlyCrowdsaleAgent() {
    assert(msg.sender == crowdsaleAgent);
    _;
  }
  
  /**
   * @dev Limit token transfer until the crowdsale is over.
   */
  modifier canTransfer() {
    if(msg.sender != address(this)){
      if(!released){
        if(!privilege){
          revert();
        }else if(!privilegedAddr[msg.sender]){
          revert();
        }
      }
    }
    _;
  } 
  
  // Function to access name of token .
  function name() public view returns (string _name) {
      return name;
  }
  // Function to access symbol of token .
  function symbol() public view returns (string _symbol) {
      return symbol;
  }
  // Function to access decimals of token .
  function decimals() public view returns (uint256 _decimals) {
      return decimals;
  }
  // Function to access total supply of tokens .
  function totalSupply() public view returns (uint256 _totalSupply) {
      return totalSupply;
  }
  
  
  // Function that is called when a user or another contract wants to transfer funds .
  function transfer(address _to, uint _value, bytes _data) public canTransfer returns (bool success) {
    if(isContract(_to)) {
        return transferToContract(_to, _value, _data);
    }
    else {
        return transferToAddress(_to, _value, _data);
    }
  }
  
  // Standard function transfer similar to ERC20 transfer with no _data .
  // Added due to backwards compatibility reasons .
  function transfer(address _to, uint _value) public canTransfer returns (bool success) {
    //standard function transfer similar to ERC20 transfer with no _data
    //added due to backwards compatibility reasons
    bytes memory empty;
    if(isContract(_to)) {
        return transferToContract(_to, _value, empty);
    }
    else {
        return transferToAddress(_to, _value, empty);
    }
  }

  //assemble the given address bytecode. If bytecode exists then the _addr is a contract.
  function isContract(address _addr) private view returns (bool is_contract) {
      uint length;
      assembly {
            //retrieve the size of the code on target address, this needs assembly
            length := extcodesize(_addr)
      }
      return (length>0);
    }

  //function that is called when transaction target is an address
  function transferToAddress(address _to, uint _value, bytes _data) private returns (bool success) {
    if (balanceOf(msg.sender) < _value) revert();
    balances[msg.sender] = safeSub(balanceOf(msg.sender), _value);
    balances[_to] = safeAdd(balanceOf(_to), _value);
    emit Transfer(msg.sender, _to, _value, _data);
    return true;
  }
  
  //function that is called when transaction target is a contract
  function transferToContract(address _to, uint _value, bytes _data) private returns (bool success) {
    if (balanceOf(msg.sender) < _value) revert();
    balances[msg.sender] = safeSub(balanceOf(msg.sender), _value);
    bool flag = false;
    for(uint i = 0; i < addrCotracts.length; i++) {
      if(_to == addrCotracts[i]) flag = true;
    }
    if(flag){
      balances[this] = safeAdd(balanceOf(this), _value);
    }else{
      balances[_to] = safeAdd(balanceOf(_to), _value);
    }
    ContractReceiver receiver = ContractReceiver(_to);
    if(receiver.tokenFallback(msg.sender, _value, _data)){
      emit Transfer(msg.sender, _to, _value, _data);
      return true;
    }else{
      revert();
    }
    if(flag){
      emit Transfer(msg.sender, this, _value, _data);
    }else{
      emit Transfer(msg.sender, _to, _value, _data);
    }
    return true;
}

  function balanceOf(address _owner) public view returns (uint balance) {
    return balances[_owner];
  }
  
  /** 
   * @dev Create new tokens and allocate them to an address. Only callably by a crowdsale agent
   * @param _to dest address
   * @param _value tokens amount
   * @return mint result
   */ 
  function mint(address _to, uint _value, bytes _data) public onlyCrowdsaleAgent returns (bool success) {
    totalSupply = safeAdd(totalSupply, _value);
    balances[_to] = safeAdd(balances[_to], _value);
    emit Transfer(0, _to, _value, _data);
    return true;
  }

  /**
   * @dev Set the crowdsale Agent
   * @param _crowdsaleAgent crowdsale contract address
   */
  function setCrowdsaleAgent(address _crowdsaleAgent) public onlyOwner {
    crowdsaleAgent = _crowdsaleAgent;
  }
  
  /**
   * @dev One way function to release the tokens to the wild. Can be called only from the release agent that is the final ICO contract. 
   */
  function releaseTokenTransfer() public onlyCrowdsaleAgent {
    released = true;
  }
  
  function releasePrivilege() public onlyCrowdsaleAgent {
    privilege = true;
  }
  
  function setAddrForPrivilege(address _owner) public onlyCrowdsaleAgent {
    privilegedAddr[_owner] = true;
  }
  
  function getAddrForPrivilege(address _owner) public view returns (bool success){
    return privilegedAddr[_owner];
  }

}
function isContract(address _addr) private view returns (bool is_contract) {
      uint length;
      assembly {
            //retrieve the size of the code on target address, this needs assembly
            length := extcodesize(_addr)
      }
      return (length>0);
    }
//assemble the given address bytecode. If bytecode exists then the _addr is a contract.