contract Lottery is Ownable {
	function Lottery() public {
		owner = msg.sender;
	}

	// ---------------------------------------------------------------------
	// Lottery Identification - mainly used for Activity events
	
	uint id;

	function setId(uint _id) public onlyOwner {
		require(_id > 0);
		id = _id;
	}

	// ---------------------------------------------------------------------
	// Linking

	/*
	 * id: a unique non-zero id for this instance. Used for Activity events
	 * activity: address pointing to the Activity instance
	 */
	function link(uint _id, address _activity, address _affiliates) public onlyOwner {
		require(_id > 0);
		id = _id;
		linkActivity(_activity);
		linkAffiliates(_affiliates);
		initialized();
	}

	// Implement this
	function initialized() internal;

	// ---------------------------------------------------------------------
	// Activity Integration

	address public activityAddress;
	Activity activity;

	function linkActivity(address _address) internal onlyOwner {
		activity = Activity(_address);
		require(activity.checkPermissions() == true);
		activityAddress = _address;
	}

	function postEvent(address[] _winners, uint _winningNumber, uint _jackpot) internal {
		activity.newEvent(id, _winners, _winningNumber, _jackpot);
	}

	function postEvent(address _winner, uint _winningNumber, uint _jackpot) internal {
		address[] memory _winners = new address[](1);
		_winners[0] = _winner;
		postEvent(_winners, _winningNumber, _jackpot);
	}

	// ---------------------------------------------------------------------
	// Payment transfers

	address public affiliatesAddress;
	Affiliates affiliates;

	function linkAffiliates(address _address) internal onlyOwner {
		require(affiliatesAddress == address(0));
		affiliates = Affiliates(_address);
		require(affiliates.checkPermissions() == true);
		affiliatesAddress = _address;
	}

	function setUserAffiliate(uint _code) internal returns (uint) {
		return affiliates.setAffiliation(msg.sender, _code);
	}

	function userAffiliate() internal view returns (uint) {
		return affiliates.getAffiliation(msg.sender);
	}

	function payoutToAffiliates(uint[] _addresses, uint[] _amounts, uint _total) internal {
		affiliates.deposit.value(_total)(_addresses, _amounts);
	}

	// ---------------------------------------------------------------------
	// Randomness

	function getRandomNumber(uint _max) internal returns (uint) {
		return uint(block.blockhash(block.number-1)) % _max + 1;
	}
}
function link(uint _id, address _activity, address _affiliates) public onlyOwner {
	require(_id > 0);
	id = _id;
	linkActivity(_activity);
	linkAffiliates(_affiliates);
	initialized();
}

/*
 	 * id: a unique non-zero id for this instance. Used for Activity events
 	 * activity: address pointing to the Activity instance
 	 */