--	Animate.Rainbow = require(script:WaitForChild("Rainbow"))

	Animate.Rotate = require(script:WaitForChild("Rotate"))
	Animate.Animations = {
		--[[ Tool = {
			Idle = animation_track;
			Action = {
				animation_track;
				animation_track;
				animation_track;	
			}
		}]]--
	}
	
	Animate.Last = {}
	
	Animate.Humanoid = nil; -- Player requires this module and sets this.
	
	function Animate.GetHumanoid()
		if not Animate.Humanoid then
			repeat wait() until Animate.Humanoid and Animate.Humanoid:IsDescendantOf(workspace)
		end
		return Animate.Humanoid
	end
	
	
	function Animate.GetNextNumber(category,state)
		local category_list = Animate.Animations[category]
		local state_list = category_list and category_list[state] or nil
		if state_list then
			local state_last_number = Animate.Last[category][state] or 1
			local next_number = (state_last_number + 1 <= #state_list) and state_last_number+1 or 1
			return next_number
		end
		return 1
	end
	
	-- stops the animation from the current category of the given state.
	-- if no state is given, stop all
	function Animate.Stop(category,state)
		if category then
			if state then
				local track = Animate.Animations[category][state][Animate.Last[category][state]]
				if track then
					track:Stop()
				end
			else
				if Animate.Animations[category]then
					for state,tracks in pairs(Animate.Animations[category]) do
						for _,track in pairs(tracks) do
							if track then
								track:Stop()
							end
						end
					end
				end
			end
		end
	end

	function Animate.Play(category,state,number,at_speed)
		if category and state then
			local category_list = Animate.Animations[category]
			if category_list then
				local state_list = category_list[state]
				if state_list then
					local state_last_number = Animate.Last[state]
					number = number or Animate.GetNextNumber(category,state)
					Animate.Last[category][state] = number
					state_list[number]:Play()
					state_list[number]:AdjustSpeed(at_speed or 1.0)
				end
			end
		end
	end
	
	-- creates a new animation track, and saves it to Animate.Animations[category][state]
	function Animate.Load(category,state,id,replace_slot)
		if category and state and id then
			local humanoid = Animate.GetHumanoid()
			local animation = Instance.new("Animation")
			animation.AnimationId = id
			local track = humanoid:LoadAnimation(animation)
			Animate.Animations[category] = Animate.Animations[category] or {}
			Animate.Animations[category][state] = Animate.Animations[category][state] or {}
			if not replace_slot then
				table.insert(Animate.Animations[category][state], track)
			else
				for i=1,#Animate.Animations[category][state] do -- loop thru and replace the animation track with the new one (called from reload)
					local t = Animate.Animations[category][state][i]
					if t.Animation.AnimationId == id then
						Animate.Animations[category][state][i] = track; 
					end
				end
			end
			Animate.Last[category] = {
				[state] = 1;
			}
			return track
		end
	end
	
	-- removes an id from the state list.
	-- if no state supplied, all states are unloaded (set to {})
	-- if no id supplied, sets the state list to {};
	function Animate.Unload(category,state,id)
		local category_list = Animate.Animations[category]
		if not state and category_list then
			for state,list in pairs(category_list) do
				category_list[state] = {}
			end
		else
			local state_list = category_list and category_list[state] or nil
			if state_list then
				if id then
					for i=1,#state_list do
						if state_list[i] == id then
							table.remove(state_list,i)
						end
					end
				else
					category_list[state] = {};
				end
			end
		end
	end
	
	function Animate.GetTrack(category, state, id)
		if Animate.Animations[category] and Animate.Animations[category][state] then
			for _, track in pairs(Animate.Animations[category][state]) do
				if track.Animation.AnimationId == id then
					return track
				end
			end
		end
	end
	
	-- reloads all the current anims
	function Animate.Reload()
		for category, states in pairs(Animate.Animations) do
			for state, tracks in pairs(states) do
				for track_num, track in pairs(tracks) do
					Animate.Load(category, state, track.Animation.AnimationId, true)
				end
			end
		end
	end


return Animate