
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

function ENT:Initialize()

	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:SetUseType( ONOFF_USE )
	self.Entity:SetNetworkedInt(0,0)
	
	self:SetOn( false )
	
end


function ENT:Use( activator, caller )

	if ( !activator:IsPlayer() ) then return end

	if ( self:IsOn() ) then 
	
		self:Toggle( false )
		
	return end

	self:Toggle( true )
	self.Entity:NextThink( CurTime() )
	self.LastUser = activator
	
end

function ENT:Think()

	// If the player looks away while holding down use it will stay on
	// Lets fix that..
	if ( self:IsOn() ) then 
	
		if ( !self.LastUser || 
			 !self.LastUser:IsValid() || 
			 !self.LastUser:KeyDown( IN_USE ) ) then
		
			Msg("Forcing Off\n")
		
			self:Toggle( false )
			self.LastUser = nil
		end	
		
		self.Entity:NextThink( CurTime() )
	
	end
	
	
	
end

function ENT:Toggle( bool )

	local plyindex 	= self:GetPlayerIndex()
	local key 		= self:GetKey()
	
	if (bool) then
	
		numpad.Activate( NULL, _, {key}, plyindex )
		self:SetOn( true )
		
	else
	
		numpad.Deactivate( NULL, _, {key}, plyindex )
		self:SetOn( false )
		
	end
	
end

