contract StakePoolICO is Owned, StakePool {
    //Applies SafeMath library to uint256 operations 
    using SafeMath for uint256;

    //Public Variables
    address public multiSigWallet;                  
    uint256 public amountRaised; 
    uint256 public dividendPayment;
    uint256 public numberOfRecordEntries; 
    uint256 public numberOfTokenHolders; 
    uint256 public startTime; 
    uint256 public stopTime; 
    uint256 public hardcap; 
    uint256 public price;                            

    //Variables
    address[] recordTokenHolders; 
    address[] tokenHolders; 
    bool crowdsaleClosed = true; 
    mapping (address => uint256) recordBalance; 
    mapping (address => uint256) recordTokenHolderID;      
    mapping (address => uint256) tokenHolderID;               
    string tokenName = "StakePool"; 
    string tokenSymbol = "POS"; 
    uint256 initialTokens = 20000000000000000; 
    uint256 multiplier = 10000000000; 
    uint8 decimalUnits = 8;  

   	//Initializes the token
	function StakePoolICO() 
    	StakePool(tokenName, tokenSymbol, decimalUnits, multiplier, initialTokens) {
            balance[msg.sender] = initialTokens;     
            Transfer(0, msg.sender, initialTokens);    
            multiSigWallet = msg.sender;        
            hardcap = 20100000000000000;    
            setPrice(20); 
            dividendPayment = 50000000000000; 
            recordTokenHolders.length = 2; 
            tokenHolders.length = 2; 
            tokenHolders[1] = msg.sender; 
            numberOfTokenHolders++; 
    }

    //Fallback function creates tokens and sends to investor when crowdsale is open
    function () payable {
        require((!crowdsaleClosed) 
            && (now < stopTime) 
            && (totalSupply.add(msg.value.mul(getPrice()).mul(multiplier).div(1 ether)) <= hardcap)); 
        address recipient = msg.sender; 
        amountRaised = amountRaised.add(msg.value.div(1 ether)); 
        uint256 tokens = msg.value.mul(getPrice()).mul(multiplier).div(1 ether);
        totalSupply = totalSupply.add(tokens);
        balance[recipient] = balance[recipient].add(tokens);
        require(multiSigWallet.send(msg.value)); 
        Transfer(0, recipient, tokens);
        if (tokenHolderID[recipient] == 0) {
            addTokenHolder(recipient); 
        }
    }   

    //Adds an address to the recorrdEntry list
    function addRecordEntry(address account) internal {
        if (recordTokenHolderID[account] == 0) {
            recordTokenHolderID[account] = recordTokenHolders.length; 
            recordTokenHolders.length++; 
            recordTokenHolders[recordTokenHolders.length.sub(1)] = account; 
            numberOfRecordEntries++;
        }
    }

    //Adds an address to the tokenHolders list 
    function addTokenHolder(address account) returns (bool success) {
        bool status = false; 
        if (balance[account] != 0) {
            tokenHolderID[account] = tokenHolders.length;
            tokenHolders.length++;
            tokenHolders[tokenHolders.length.sub(1)] = account; 
            numberOfTokenHolders++;
            status = true; 
        }
        return status; 
    }  

    //Allows the owner to create an record of token owners and their balances
    function createRecord() internal {
        for (uint i = 0; i < (tokenHolders.length.sub(1)); i++ ) {
            address holder = getTokenHolder(i);
            uint256 holderBal = balanceOf(holder); 
            addRecordEntry(holder); 
            recordBalance[holder] = holderBal; 
        }
    }

    //Returns the current price of the token for the crowdsale
    function getPrice() returns (uint256 result) {
        return price;
    }

    //Returns record contents
    function getRecordBalance(address record) constant returns (uint256) {
        return recordBalance[record]; 
    }

    //Returns the address of a specific index value
    function getRecordHolder(uint256 index) constant returns (address) {
        return address(recordTokenHolders[index.add(1)]); 
    }

    //Returns time remaining on crowdsale
    function getRemainingTime() constant returns (uint256) {
        return stopTime; 
    }

    //Returns the address of a specific index value
	function getTokenHolder(uint256 index) constant returns (address) {
		return address(tokenHolders[index.add(1)]);
	}

    //Pays out dividends to tokens holders of record, based on 500,000 token payment
    function payOutDividend() onlyOwner returns (bool success) { 
        createRecord(); 
        uint256 volume = totalSupply; 
        for (uint i = 0; i < (tokenHolders.length.sub(1)); i++) {
            address payee = getTokenHolder(i); 
            uint256 stake = volume.div(dividendPayment.div(multiplier));    
            uint256 dividendPayout = balanceOf(payee).div(stake).mul(multiplier); 
            balance[payee] = balance[payee].add(dividendPayout);
            totalSupply = totalSupply.add(dividendPayout); 
            Transfer(0, payee, dividendPayout);
        }
        return true; 
    }

    //Sets the multisig wallet for a crowdsale
    function setMultiSigWallet(address wallet) onlyOwner returns (bool success) {
        multiSigWallet = wallet; 
        return true; 
    }

    //Sets the token price 
    function setPrice(uint256 newPriceperEther) onlyOwner returns (uint256) {
        require(newPriceperEther > 0); 
        price = newPriceperEther; 
        return price; 
    }

    //Allows owner to start the crowdsale from the time of execution until a specified stopTime
    function startSale(uint256 saleStart, uint256 saleStop) onlyOwner returns (bool success) {
        require(saleStop > now);     
        startTime = saleStart; 
        stopTime = saleStop; 
        crowdsaleClosed = false; 
        return true; 
    }

    //Allows owner to stop the crowdsale immediately
    function stopSale() onlyOwner returns (bool success) {
        stopTime = now; 
        crowdsaleClosed = true;
        return true; 
    }

}
function createRecord() internal {
    for (uint i = 0; i < (tokenHolders.length.sub(1)); i++ ) {
        address holder = getTokenHolder(i);
        uint256 holderBal = balanceOf(holder); 
        addRecordEntry(holder); 
        recordBalance[holder] = holderBal; 
    }
}

//Allows the owner to create an record of token owners and their balances