
require "StandardMouseButton"


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

function LinkPanel:Create(  )
	
	-- The main screen container
	local instance = root.dialogs[ self.name ];
	
	assert( instance ~= nil )
	
	-- The album button
	local bookButton = instance._album
	-- The game menu button
	local menuButton = instance._gameMenu
	
	local deciMinuteText = instance._clock.timeDeciMinute
	local minuteText = instance._clock.timeMinute
	local deciSecondText = instance._clock.timeDeciSecond
	local secondText = instance._clock.timeSecond
	
	-- The pause button
	local pauseButton = instance._clock.pauseButton
	
	instance:SetOnUpdateEnabled( true );
	
	-- The main init function
	function instance:Init( )

		log ( "initing the link panel" )
		
		instance:SetPosition( 0, 0 )
		
		self:UpdateClock()
		
		local elapsedTime = instance:GetTime()
		local minutes = math.floor( elapsedTime / 60 )
		local deciMin = math.floor( minutes / 10 )
		local mins = math.mod( minutes, 10 )
		local seconds = math.floor( math.mod( elapsedTime, 60 ) )
		local deciSec = math.floor( seconds / 10 )
		local secs = math.mod( seconds, 10 )
		
		deciMinuteText:SetText( tostring( deciMin ) )
			minuteText:SetText( tostring( mins ) )
		deciSecondText:SetText( tostring( deciSec ) )
			secondText:SetText( tostring( secs ) )
				
		StandardMouseButton( bookButton );
		bookButton:Init( )
		StandardMouseButton( menuButton );
		menuButton:Init( )
		StandardMouseButton( pauseButton );
		pauseButton:Init( )
	end
	
	function instance:UpdateClock()
		if ( instance:IsRelaxed() ) then
			
			local iColor = 0x999999
			
			deciMinuteText:SetColor( iColor )
				minuteText:SetColor( iColor )
			deciSecondText:SetColor( iColor )
				secondText:SetColor( iColor )
				 colonText:SetColor( iColor )
			
			pauseButton:SetVisible( false )
			pauseButton:SetInputEnabled( false )
--			clockGlow:SetVisible( false )
--			clockGlow:SetInputEnabled( false )
		else
			deciMinuteText:SetColor( 0x000000 )
				minuteText:SetColor( 0x000000 )
			deciSecondText:SetColor( 0x000000 )
				secondText:SetColor( 0x000000 )
				 colonText:SetColor( 0x000000 )
			
			pauseButton:SetVisible( true )
				
			pauseButton:SetInputEnabled( true )
--			clockGlow:SetVisible( true )
--			clockGlow:SetInputEnabled( true )
		end
	end

	-- The ok button
	function bookButton:Init( )
		function bookButton:Pressed( )
			instance:ScreenEvent( "Album" , "LinkPanel" )
		end
	end
	-- The help button
	function menuButton:Init( )
		function menuButton:Pressed( )
			log( 'xxx111' );
			instance:ScreenEvent( "Menu" , "LinkPanel" )
		end
	end
	-- The help button
	function pauseButton:Init( )
		function pauseButton:Pressed( )
			instance:ScreenEvent( "PauseGame" , "LinkPanel" )
		end
	end
	function instance:OnUpdate( )
	
		self:UpdateClock()
		
		local elapsedTime = instance:GetTime()
		local minutes = math.floor( elapsedTime / 60 )
		local seconds = math.floor( math.mod( elapsedTime, 60 ) )
		local secondsStr = tostring( seconds );
		
		if( seconds <= 9 ) then
			secondsStr = "0" .. seconds;
		end
			
		local minutes = math.floor( elapsedTime / 60 )
		local deciMin = math.floor( minutes / 10 )
		local mins = math.mod( minutes, 10 )
		local seconds = math.floor( math.mod( elapsedTime , 60 ) )
		local deciSec = math.floor( seconds / 10 )
		local secs = math.mod( seconds, 10 )
		
		deciMinuteText:SetText( tostring( deciMin ) )
			minuteText:SetText( tostring( mins ) )
		deciSecondText:SetText( tostring( deciSec ) )
			secondText:SetText( tostring( secs ) )

	end
	
end

log( 'Loading the Link Panel' );
LinkPanel:Create()
