-- should have a set of horizontal sliders with inertia, and max / min with bounceback
HorizontalSlider = class(DragArea, function(o, x, y, w, h)
	DragArea.init(o, x, y, w, h)
	o.bgSprite = nil
	-- contains a camera that scrolls over the objects contained within
	o.sliderCamera = Camera(x, y, w, h)
	o.sliderCamera.scroll = Vec2(0, 0)
	o.objs = Group()
	o.fingerCount = 0
	-- going to store a temporary size for now, should recalc this based on the objects contained within eventually
	o.viewingRect = Rectangle(x, y, w, h)
	o.hasInertia = true
	o:setLayer(0)
end)
function HorizontalSlider:draw()
	-- draw a default background so that we can see what is sliding
	if self.bgSprite then
		local py, ph = self.pos.y, self.h
		self.pos.y = self.pos.y+self.h-self.bgSprite:getHeight()
		self.h = self.bgSprite:getHeight()
		self.sprite = self.bgSprite
		self:drawStretched()
		self.pos.y,self.h = py, ph
	end
	-- this is an object that can be looped out in the backgound
	if self.backgroundSlide then
		self.backgroundSlide.w = math.floor(self.backgroundSlide.w)
		-- figure out the first position
		local fpx = math.floor(self.sliderCamera.scroll.x)%self.backgroundSlide.w
		-- self.backgroundSlide:setCamera(self.sliderCamera)
		local px = -self.backgroundSlide.w-fpx
		for i=-1,math.ceil(self.w/self.backgroundSlide.w) do
			self.backgroundSlide.pos.x = px
			self.backgroundSlide.pos.y = self.pos.y
			self.backgroundSlide:drawStretched()
			px = px + self.backgroundSlide.w
		end
	end
	-- draw all of the sliding objects
	self.objs:draw()
end

function HorizontalSlider:update()
	-- update all of the internal objects
	self.objs:update()
	
	if self.camera_vel then
		cam_vel_t = Vec2(self.camera_vel.x, self.camera_vel.y)
		cam_vel_t:mult(bludG.elapsed)
		self.sliderCamera.scroll = Vec2(self.sliderCamera.scroll.x+cam_vel_t.x, self.sliderCamera.scroll.y)
		self.camera_vel:mult(0.9)
		if self.camera_vel:distance(Vec2(0,0)) < 1 then
			self.camera_vel = nil
		end
	end
	-- check to see if there are no fingers down, and we are beyond the limits of the scrolling. If so, then bounce back the camera
	if self.fingerCount <= 0 and not self.cameraTween then
		-- this is assuming that everything is 0'ed
		if self.sliderCamera.scroll.x < self.viewingRect.pos.x then
			self.cameraTween = Tweener:addTween(self.sliderCamera.scroll, {x=self.viewingRect.pos.x}, {onComplete=function()
				self.cameraTween = nil
			end})
			self.camera_vel = nil
		elseif self.sliderCamera.scroll.x + self.w > self.viewingRect.w then
			self.cameraTween = Tweener:addTween(self.sliderCamera.scroll, {x=self.viewingRect.w-self.w}, {onComplete=function()
				self.cameraTween = nil
			end})
			self.camera_vel = nil
		end
	end
end
function HorizontalSlider:addObject(obj)
	local o = self.objs:add(obj)
	o:setCamera(self.sliderCamera)
	return o
end
function HorizontalSlider:touchDown(x, y, id)
	if DragArea.touchDown(self, x,y,id) ~= false then
		-- change the x, y to account for the camera
		nx, ny = x-self.sliderCamera.pos.x+self.sliderCamera.scroll.x, y-self.sliderCamera.pos.y+self.sliderCamera.scroll.y
		return self.objs:touchDown(nx, ny, id)
	end
	return false
end
function HorizontalSlider:onStart(x, y, id)
	if id == 0 then
		self.fingerCount = self.fingerCount + 1
		self.startTouch = Vec2(x, y)
		self.touchStartTime = bludG.last_t
		if self.cameraTween then 
			Tweener:removeTween(self.cameraTween)
			self.cameraTween = nil
		end
	end
	return true
end
function HorizontalSlider:onMove(x, y, id)
	local p = Vec2(x, y)
	p:sub(self.lastPos[id])
	-- because we are just a horizontal slider, limit the slide to one axis
	p.y = 0
	if id == 0 then
		self.sliderCamera.scroll:sub(p)
		-- check to see if the camera is past the size of the included objects
	end
end
function HorizontalSlider:touchUp(x, y, id)
	local rval = DragArea.touchUp(self, x,y,id)
	if rval ~= false then
		nx, ny = x-self.sliderCamera.pos.x+self.sliderCamera.scroll.x, y-self.sliderCamera.pos.y+self.sliderCamera.scroll.y
		self.objs:touchUp(nx, ny, id)
		return not self:doesPointTouch(Vec2(x, y))
	end
	return false
end
-- handle the inertia and bounceback
function HorizontalSlider:onPress(x, y, id)
	if id == 0 then
		self.fingerCount = self.fingerCount - 1
		-- calculate the camera velocity and falloff
		if self.hasInertia then
		    time_delta = bludG.last_t - self.touchStartTime
		    if time_delta ~= 0 then
		      self.camera_vel = Vec2((self.startTouch.x-x)/time_delta, (self.startTouch.y-y)/time_delta)
		    end
		end
		if math.abs(self.startTouch.x-x) > 80 then
			-- cancel the touch on the subbutton
			return false
		end
	end
	return true
end