contract Cascading is Ownable {
    using SafeMath for uint256;

    struct Cascade {
        address cascade;
        uint16 percentage;
    }

    uint public totalCascadingPercentage;
    Cascade[] public cascades;    

    /// @dev Adds an address and associated percentage for transfer.
    /// @param newAddress The new address
    function addCascade(address newAddress, uint newPercentage) public onlyOwner {
        cascades.push(Cascade(newAddress, uint16(newPercentage)));
        totalCascadingPercentage += newPercentage;
    }

    /// @dev Deletes an address and associated percentage at the given index.
    /// @param index The index of the cascade to be deleted.
    function deleteCascade(uint index) public onlyOwner {
        require(index < cascades.length);
        
        totalCascadingPercentage -= cascades[index].percentage;

        cascades[index] = cascades[cascades.length - 1];
        delete cascades[cascades.length - 1];
        cascades.length--;
    }

    /// @dev Transfers the cascade values to the assigned addresses
    /// @param totalJackpot the total jackpot amount
    function transferCascades(uint totalJackpot) internal {
        for (uint i = 0; i < cascades.length; i++) {
            uint cascadeTotal = getCascadeTotal(cascades[i].percentage, totalJackpot);

            // Should be safe from re-entry given gas limit of 2300.
            cascades[i].cascade.transfer(cascadeTotal);
        }
    }

    /// @dev Gets the cascade total for the given percentage
    /// @param percentage the percentage of the total pot as a uint
    /// @param totalJackpot the total jackpot amount
    /// @return the total amount the percentage represents
    function getCascadeTotal(uint percentage, uint totalJackpot) internal pure returns(uint) {
        return totalJackpot.mul(percentage).div(100);        
    }
   
    /// A utility method to calculate the total after cascades have been applied.
    /// @param totalJackpot the total jackpot amount
    /// @return the total amount after the cascades have been applied
    function getTotalAfterCascades(uint totalJackpot) internal constant returns (uint) {
        uint cascadeTotal = getCascadeTotal(totalCascadingPercentage, totalJackpot);
        return totalJackpot.sub(cascadeTotal);
    }
}
function getCascadeTotal(uint percentage, uint totalJackpot) internal pure returns(uint) {
    return totalJackpot.mul(percentage).div(100);        
}

/// @dev Gets the cascade total for the given percentage
/// @param percentage the percentage of the total pot as a uint
/// @param totalJackpot the total jackpot amount
/// @return the total amount the percentage represents