ScrollBar = Frame:new()
function ScrollBar:init()	
	self.scrollX = 0
	self.w = 0
	local scrollBaseLeft = Image:new()
	scrollBaseLeft.name = "scrollBaseLeft"
	scrollBaseMiddle:setImage("SCROLLBAR_BASE_LEFT")
	self:addChild(scrollBaseLeft)
	local scrollBaseMiddle = Image:new()
	scrollBaseMiddle.name = "scrollBaseMiddle"
	scrollBaseMiddle:setImage("SCROLLBAR_BASE_MIDDLE")
	self:addChild(scrollBaseMiddle)
	local scrollBaseRight = Image:new()
	scrollBaseRight.name = "scrollBaseRight"
	scrollBaseRight:setImage("SCROLLBAR_BASE_RIGHT")
	self:addChild(scrollBaseRight)	
	local marker = Image:new()
	marker.name = "marker"
	marker:setImage("SCROLLBAR_CHALLENGEMENU")
	self:addChild(marker)	
end
function ScrollBar:setWidth(width)
	self.w = width
	local scrollBaseMiddle = self:getChild("scrollBaseMiddle")	
	scrollBaseMiddle:setSize(self.w, nil)
	local scrollBaseBottom = self:getChild("scrollBaseRight)
	scrollBaseBottom.y = self.w
end
function ScrollBar:setAlpha(alpha)
	self:getChild("scrollBaseLeft").alpha = alpha
	self:getChild("scrollBaseMiddle").alpha = alpha
	self:getChild("scrollBaseRight").alpha = alpha
	self:getChild("marker").alpha = alpha	
	if alpha == nil then
		self.visible = false
	else
		self.visible = true
	end
end
function ScrollBar:draw(x,y)
	if self.visible then
		Frame.draw(self,x,y)
	end
end
function ScrollBar:setScrollX(relativeScrollX)
	self.scrollX = self.w * relativeScrollX
	local marker = self:getChild("marker")
	marker.x = self.scrollX
end

function ScrollBar:update(dt, time)

	local relativeScroll = self:getRelativeScrollX()
	local marker = self:getChild("marker")
	local w,h = _G.res.getSpriteBounds(marker.image)
	local left = self:getChild("scrollBaseLeft")
	

	if(relativeScroll < 0 ) then		
		marker.scaleY = 1 - _G.math.min(_G.math.abs(relativeScroll), 0.5)	
		relativeScroll = 0
		marker.y = - (h - (h * marker.scaleY)) * 0.5
		
	elseif (relativeScroll > 1) then
		marker.scaleY = 1 - _G.math.min(_G.math.abs(1 - relativeScroll), 0.5)	
		relativeScroll = 1		
		marker.y = scrollBar.h + (h - (h * marker.scaleY)) * 0.5
	else
		marker.scaleY = 1
		scrollBar:setScrollY(relativeScroll)	
	end	
	-- Cap marker movement & scale
	local ww,hh = _G.res.getSpriteBounds(left.image)
	if marker.y - hh < marker.h * 0.5 then 
		marker.y = -hh +  marker.h * 0.5 - (h - (h * marker.scaleY)) * 0.5 
	end	
	if marker.y > hh + self.h - marker.h * 0.5 then 
		marker.y = hh + self.h - marker.h * 0.5 + (h - (h * marker.scaleY)) * 0.5 
	end
	Frame.update(self,dt,time)	
end	