require "StandardMouseButton"
require "CDialog"

		
-- reserve constructor
CreditsDialog = 
{
	name = "CreditsDialog"
}

function CreditsDialog:Create(  )
	
	-- The main screen container
	local instance = root.dialogs[ self.name ];
	-- default to something sensible
	instance:SetPosition( 139, 99 )
	CDialog:Attach( instance )
	
	-- Get the main menu dialog instance, to align this dialog with
	local menuDialog = root.dialogs[ "GameMenuDialog" ];
	if (menuDialog ~= nil) then
		if( menuDialog:IsCentered() ) then
			instance:SetPosition( 139, 99 )
		else
			instance:SetPosition( 208, 99 )
		end
	else
		log( 'Could not find the game menu dialog' );
	end
	
	local mask = instance.mask
	local scroller = instance.credits
	
	-- The ok button
	local okButton = instance.button
	
	-- masking values
	instance.masklayer = 4
	instance.maskedin = 65537
	instance.maskclear = 131073
	
	-- make sure we get updates
	instance:SetOnUpdateEnabled( true );
	
	-- The main init function
	function instance:Init( )
		StandardMouseButton( okButton );
		
		scroller:SetY( mask:GetY( ) + mask:GetHeight( ) )
		
		--test = 01000001;
		
		--log( tostring( test ) )
		mask:SetMaterial( instance.masklayer )
		scroller:SetMaterial( instance.maskedin ) 
		
		okButton:Init( )
		okButton:SetMaterial( instance.maskclear )
	end

	-- Handle keyboard input for the dialog
	function instance:CharDown( i_Key )
		log( "On Key Down" .. i_Key )
	end
	
	function instance:OnUpdate( )
		if( instance.inGame ~= nil and instance.inGame == true ) then
			instance:SetPosition( 208, 99 )	
		end
		
		scroller:SetY( scroller:GetY( ) - .25 )
		if( scroller:GetY( ) + scroller:GetHeight( ) < mask:GetY( ) ) then
			scroller:SetY( mask:GetY( ) + mask:GetHeight( ) )
		end
		
		self:OnUpdateDialog()
	end
	
	-- The ok button
	function okButton:Init( )
		function okButton:Pressed( )
			instance:CloseAndSave( )
		end
		self:AddAsGlobalHandler()
		function okButton:OnKeyDown( i_Key )
			-- press ok on pressing return button
			if ( i_Key == 13 ) then
				okButton:Pressed()
			end
		end
	end
end

CreditsDialog:Create( )
