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



-- Button "class" which describes standard mouse interaction
function StandardMouseButton( buttonInstance )

	isDisabled = false
	buttonInstance.isSoundEnabled = true
	
	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
	
	function buttonInstance:Disable()
		--log("buttonInstance:Disable()")
		self.isDisabled = true
		self:SetFrame(mouseDown)
	end
	
	function buttonInstance:Enable()
		--log("buttonInstance:Enable()")
		self.isDisabled = false
		self:SetFrame(mouseRest)
	end	

	function buttonInstance:DisableSound()
		self.isSoundEnabled = false
	end
	function buttonInstance:EnableSound()
		self.isSoundEnabled = true
	end


		
	-- note that this may be overridden safely .. it just won't look quite so good on extents
	function buttonInstance:OnUpdate()
		if(self.isDisabled == true) then 
			return
		end
			
	
		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
	end
	
	function buttonInstance:OnMouseEnter( )
		if(self.isDisabled == true) then 
			return
		end
		
	--	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( )
		if(self.isDisabled == true) then 
			return
		end
		
		--log("OnMouseLeave")
		self.m_bCheckEngineExtents = false
		
		if ( self:GetFrame() == mouseOver ) then
			self:showMouseLeave()
		end
	end
	
	function buttonInstance:OnLeftMouseUpOutside()
		if(self.isDisabled == true) then 
			return
		end
		
		--log("OnLeftMouseUpOutside")
		self:showMouseLeave()
		buttonInstance.m_bMouseDownHere = false
	end
	
	function buttonInstance:OnLeftMouseUp( )
		if(self.isDisabled == true) then 
			return
		end
		
		if ( buttonInstance.m_bMouseDownHere == false ) then
			return
		end

		if ( self._extents ~= nil 
				and self:isMouseWithinExtents( self._extents ) == false ) then
			self:showMouseLeave()
			buttonInstance.m_bMouseDownHere = false
			return
		end
		
		self:SetFrame( mouseOver )
		

		if(self:GetVisible() == true and self.isSoundEnabled == true) then
			log("PlaySound Click 2")
			PlaySound( "Click" )
		end
		self:Pressed()
		
		buttonInstance.m_bMouseDownHere = false
		
		-- the glint should not react to mouse events
		if ( buttonInstance._buttonGlint ~= nil ) then
			buttonInstance._buttonGlint:SetInputEnabled( false )
		end
	end
	
	function buttonInstance:OnLeftMouseDown( )
		if(self.isDisabled == true) then 
			return
		end
		
		if ( self._extents ~= nil 
				and self:isMouseWithinExtents( self._extents ) == false ) then
			return
		end

		self.m_bMouseDownHere = true
		self:SetFrame( mouseDown )
		
		-- the glint should not react to mouse events
		if ( buttonInstance._buttonGlint ~= nil ) then
			buttonInstance._buttonGlint:SetInputEnabled( false )
		end
	end 

	function buttonInstance:showMouseLeave()
		if(self.isDisabled == true) then 
			return
		end
		
		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()
		if(self.isDisabled == true) then 
			return
		end
		
		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
