
-- standard button states ### should eventually be labels in the button movie clip
mouseRest = 0
mouseOver = 1
mouseDown = 2  --#### dont have this frame yet

pressX = -1
pressY = -1

byX = false
byY = false



-- Button "class" which describes standard mouse interaction
function SlidersCell( buttonInstance, parent )

	buttonInstance.prevX = -1
	buttonInstance.prevY = -1

	--Stand( )
	
	buttonInstance:SetOnUpdateEnabled( true )
	
	-- the glint should not react to mouse events
	if ( buttonInstance._buttonGlint ~= nil ) then
		buttonInstance._buttonGlint:SetInputEnabled( false )
	end
	
	function buttonInstance:isMouseWithinExtents( i_oExtentsMovie )
		if ( i_oExtentsMovie == nil
				or i_oExtentsMovie._upLeft == nil 
				or i_oExtentsMovie._downRight == nil ) then
			-- just bail
			return false
		end
		
		local fMouseX = i_oExtentsMovie:GlobalToLocalX( GetMouseX() )
		local fMouseY = i_oExtentsMovie:GlobalToLocalY( GetMouseY() )

		--log( tostring( i_oExtentsMovie._upLeft:GetX() ) .. "," .. tostring( i_oExtentsMovie._upLeft:GetY() ) .. "<->" .. 
		--	 tostring( i_oExtentsMovie._downRight:GetX() ) .. "," .. tostring( i_oExtentsMovie._downRight:GetY() ) )
		--log( tostring( fMouseX ) .. "," .. tostring( fMouseY ) )

		return fMouseX >= i_oExtentsMovie._upLeft:GetX()
				and fMouseX <= i_oExtentsMovie._downRight:GetX()
				and fMouseY >= i_oExtentsMovie._upLeft:GetY()
				and fMouseY <= i_oExtentsMovie._downRight:GetY()
	end
	
	-- note that this may be overridden safely .. it just won't look quite so good on extents
	function buttonInstance:OnUpdate()
		if ( self.m_bCheckEngineExtents ) then
		
			if ( self:GetFrame() == mouseRest
					and self:isMouseWithinExtents( self._extents ) ) then

				self:showMouseEnter()
					
			elseif ( self:GetFrame() == mouseOver 
					and self:isMouseWithinExtents( self._extents ) == false ) then
				
				self:showMouseLeave()
			end
		end
		
		if ( self:GetFrame() == mouseDown and pressX ~= -1 and pressY ~= -1) then
				local nowX = self:GlobalToLocalX( GetMouseX() )
				local nowY = self:GlobalToLocalY( GetMouseY() )
				
				local WIDHT = 90
				local OFFSET = 20
				
				--log(self:GetName() .. " " .. (self:GetX()) .. " " .. (self:GetY()))
				
				--log((nowX - pressX) .. " " .. (nowY - pressY))
				
				
				
				local curX = self:GetX()
				local curY = self:GetY()
				
				self:SetX(self:GetX() + (nowX - pressX))
				self:SetY(self:GetY() + (nowY - pressY))
				
				dx = self:GetX() - buttonInstance.prevX
				dy = self:GetY() - buttonInstance.prevY
				
				
				
				--log(dx .. " " .. dy)
				if(dx >= WIDHT or dx <= -WIDHT) then
					self:SetX(curX)
				end
				if(dy >= WIDHT or dy <= -WIDHT) then
					self:SetY(curY)
				end
				
				if(self:GetX() < 0) then
					self:SetX(0)
				end
				if(self:GetX() > 3 * WIDHT) then
					self:SetX(3 * WIDHT)
				end
				if(self:GetY() < 0) then
					self:SetY(0)
				end
				if(self:GetY() > 3 * WIDHT) then
					self:SetY(3 * WIDHT)
				end
				
				

				
				
				
				--log("%x=" .. (self:GetX() % WIDHT) .. " %y=" .. (self:GetY() % WIDHT) .. " x=" .. self:GetX() .. " px=" .. buttonInstance.prevX)
				
					if(self:GetX() >= buttonInstance.prevX) then
						if(self:GetX() % WIDHT > OFFSET) then
							log("self:Swap(0)")
							self:Swap(0)
							self:OnLeftMouseUp( )
							return
						end
					else
						if(self:GetX() % WIDHT < WIDHT - OFFSET) then
							log("self:Swap(1)")
							self:Swap(1)
							self:OnLeftMouseUp( )
							return
						end
					end
					
					

				
					if(self:GetY() >= buttonInstance.prevY) then
						if(self:GetY() % WIDHT > OFFSET) then
							log("self:Swap(2)")
							self:Swap(2)
							self:OnLeftMouseUp( )
							return
						end
					else
						if(self:GetY() % WIDHT < WIDHT - OFFSET) then
							log("self:Swap(3)")
							self:Swap(3)
							self:OnLeftMouseUp( )
							return
						end
					end
					
				

				--log(self:GetName() .. " " .. (self:GetX()) .. " " .. (self:GetY()))
		end
	end
	
	function buttonInstance:Stand( )
		buttonInstance.prevX = self:GetX()
		buttonInstance.prevY = self:GetY()
		log("Stand " .. self:GetName() .. " x=" .. self:GetX())
	end
	
	function buttonInstance:OnMouseEnter( )
	--	PlaySound( "Roll" )
		--log("enter")
		
		-- double check extents (if available)
		if ( self._extents ~= nil ) then
			self.m_bCheckEngineExtents = true
		else
			self:showMouseEnter()
		end
	end
		
	
	function buttonInstance:OnMouseLeave( )
		--log("OnMouseLeave")
		self.m_bCheckEngineExtents = false
		
		if ( self:GetFrame() == mouseOver ) then
			self:showMouseLeave()
		end
	end
	
	function buttonInstance:OnLeftMouseUpOutside()
		log("OnLeftMouseUpOutside")
		self:showMouseLeave()
		buttonInstance.m_bMouseDownHere = false
		
		pressX = -1
		pressY = -1
		
		if(parent.swapDir == -1) then
			self:SetX(buttonInstance.prevX)
			self:SetY(buttonInstance.prevY)
		end
		
		self:UnPressed()
	end
	
	function buttonInstance:OnLeftMouseUp( )
		
		if ( buttonInstance.m_bMouseDownHere == false ) then
			return
		end
		--log("2")
		if ( self._extents ~= nil 
				and self:isMouseWithinExtents( self._extents ) == false ) then
			self:showMouseLeave()
			buttonInstance.m_bMouseDownHere = false
			return
		end
		--log("3")
		log("OnLeftMouseUp")
		
		self:SetFrame( mouseOver )
		--PlaySound( "Click" )
		self:UnPressed()
		
		buttonInstance.m_bMouseDownHere = false
		pressX = -1
		pressY = -1
		
		--[[local WIDHT = 90
		local OFFSET = 30
		if(self:GetX() >= buttonInstance.prevX) then
			if(self:GetX() % WIDHT > OFFSET) then
				log("self:Swap(0)")
				self:Swap(0)
				--self:OnLeftMouseUp( )
				return
			end
		else
			if(self:GetX() % WIDHT < WIDHT - OFFSET) then
				log("self:Swap(1)")
				self:Swap(1)
				--self:OnLeftMouseUp( )
				return
			end
		end
					
					

				
		if(self:GetY() >= buttonInstance.prevY) then
			if(self:GetY() % WIDHT > OFFSET) then
				log("self:Swap(2)")
				self:Swap(2)
				--self:OnLeftMouseUp( )
				return
			end
		else
			if(self:GetY() % WIDHT < WIDHT - OFFSET) then
				log("self:Swap(3)")
				self:Swap(3)
				--self:OnLeftMouseUp( )
				return
			end
		end--]]
		

		if(parent.swapDir == -1) then
			--log("!!!!!!!!!!!! " .. " parent.swapDir == " .. parent.swapDir)
			self:SetX(buttonInstance.prevX)
			self:SetY(buttonInstance.prevY)
		end
		
		
		
		-- the glint should not react to mouse events
		if ( buttonInstance._buttonGlint ~= nil ) then
			buttonInstance._buttonGlint:SetInputEnabled( false )
		end
	end
	
	function buttonInstance:OnLeftMouseDown( )
		--log(parent.lastSlidersCellNo .. " ")
		--log(parent.lastSlidersCellNo .. " parent.swapDir == " .. parent.swapDir)
		if(parent.lastSlidersCellNo ~= -1 or parent.swapDir ~= -1) then 
			return
		end
		
		if ( self._extents ~= nil 
				and self:isMouseWithinExtents( self._extents ) == false ) then
			return
		end
		
		log("OnLeftMouseDown( )")

		self.m_bMouseDownHere = true
		self:SetFrame( mouseDown )
		self:Pressed()
		
		
		pressX = self:GlobalToLocalX( GetMouseX() )
		pressY = self:GlobalToLocalY( GetMouseY() )
		self:MoveToFront()
		
		--log(self:GetName() .. " " ..  pressX .. " " .. pressY)
		
		-- the glint should not react to mouse events
		if ( buttonInstance._buttonGlint ~= nil ) then
			buttonInstance._buttonGlint:SetInputEnabled( false )
		end
	end 

	function buttonInstance:showMouseLeave()
		self:SetFrame( mouseRest )
		
		-- the glint should not react to mouse events
		if ( buttonInstance._buttonGlint ~= nil ) then
			buttonInstance._buttonGlint:SetInputEnabled( false )
		end
	end
	
	function buttonInstance:showMouseEnter()
		self:SetFrame( mouseOver )
	
		-- the glint should not react to mouse events
		if ( buttonInstance._buttonGlint ~= nil ) then
			buttonInstance._buttonGlint:SetInputEnabled( false )
		end
	end

	buttonInstance.m_bMouseDownHere = false
	
	if ( buttonInstance._extents ~= nil ) then
		buttonInstance.m_bCheckEngineExtents = buttonInstance:isMouseWithinExtents( buttonInstance._extents )
	end

	
end
