require "StandardMouseButton"
require "ItemMouseButton"
require "PartMouseButton"
require "StickMouseButton"
require "SlidersCell"
require "StateMachine"
require "CIrpMan"

-- reserve constructor
if ( ISpyPanel_esc == nil ) then
	ISpyPanel_esc = 
	{
		name					= "ISpyPanel_esc",

		State_Playing			= 0,
	}
end

ISpyPanel_esc.s_fInitWaitTime				= 0.1
ISpyPanel_esc.s_fTransitionInDur			= 1.0	 -- in secs
ISpyPanel_esc.s_iBluebirdMeterCount			= 5
ISpyPanel_esc.s_fBluebirdMeterDurMod		= 2

-- color (dup)
--ISpyPanel_esc.s_fBirdHintTrailDecayRate		= 0.005
--ISpyPanel_esc.s_fBirdHintTrailStartAlpha	= 0.19

-- grayscale
ISpyPanel_esc.s_fBirdHintTrailDecayRate		= 0.025
ISpyPanel_esc.s_fBirdHintTrailStartAlpha	= 0.5

-- light part of bird
--ISpyPanel_esc.s_iBirdHintTrailColor			= 0xFFFFFF
ISpyPanel_esc.s_fBirdFlapSpeed				= 0.1

-- dark part of bird 
ISpyPanel_esc.s_iBirdHintTrailColor			= 0xFFBE00

function ISpyPanel_esc:Create(  )
	
	-- The main screen container
	local instance = root.dialogs[ self.name ];
	CIrpMan:Attach( instance )
	StateMachine:Create( ISpyPanel_esc.State_Playing, instance )
	
	--local wasX = 0
	--local wasY = 0
	--local underMouse
	
	-- The ok button
	local bookButton = instance.bookButton
	local panicButton = instance.panicButton
	local menuButton = instance.menubutton
	local listMask = instance.listMask
	--//local itemCount = instance.list.itemCountText
--	local cupid = instance.top.cupid
	--local clockHand = cupid.hand
	--local clockGlow = cupid.glow
	local hintbutton = instance.clues.hintbutton
	local pauseButton = instance.top.pauseButton
	local lastsecond = -1;
	local clockframe = 0;
	
			
	--VV add arrows
--	local leftArrow = instance.leftArrow
--	local rightArrow = instance.rightArrow

	
	local hintHandles = {}
	
	instance.internalList = {}
	
	instance.cellsList = {}
	
	instance.cellsList[0] = instance.list.cell_1;
	instance.cellsList[1] = instance.list.cell_2;
	instance.cellsList[2] = instance.list.cell_3;
	instance.cellsList[3] = instance.list.cell_4;
	instance.cellsList[4] = instance.list.cell_5;
	instance.cellsList[5] = instance.list.cell_6;
	
	instance.slidersCellsList = {}
	instance.lastSlidersCellNo = -1
	
	instance.firstSwap = nil
	instance.secondSwap = nil
	instance.swapDir = -1
	
	
	
	instance.flashTime = 0;
	
	-- make sure we get updates
	instance:SetOnUpdateEnabled( true );
	
	if ( IsRetail() == false ) then
		instance:AddAsGlobalHandler()
		function instance:OnCharDown( i_iChar )
			if ( i_iChar == 190 ) then		-- '.'
				log( "RELOAD LUA!" )
				console( "lua.dofile ISpyPanel_esc.lua" )
			elseif ( i_iChar == 188 ) then	-- ','
				log( "RELOAD ART!" )
				console( "library.reloadpackage input" )
				console( "library.reloadpackage ui" )
			end
		end
	end	
	
	
	
	function instance:UpdateClock()
		local iColor = 0x0

		if ( self.Relaxed ) then
			
			iColor = 0x999999
			
			pauseButton:SetOpacity( 0.3 )
			pauseButton:SetInputEnabled( false )
--			clockGlow:SetVisible( false )
--			clockGlow:SetInputEnabled( false )
		else
			pauseButton:SetOpacity( 1.0 )
			pauseButton:SetInputEnabled( true )
--			clockGlow:SetVisible( true )
--			clockGlow:SetInputEnabled( true )
		end
		
		self.m_oMinutesText:SetColor( iColor )
		self.m_oColonText:SetColor( iColor )
		self.m_oSecondsText:SetColor( iColor )
	end
	
	-- The main init function
	function instance:Init( )
		
		ISpyPanel_esc.G = self

		self.m_oMinutesText = self.top.minutes
		self.m_oColonText = self.top.colon
		self.m_oSecondsText = self.top.seconds
		self.m_oBluebirds = self.top.bluebirdCounter
--		self.m_oCupid = self.top.cupid
		self.m_fArrowFadeInOn = 0
		self.m_iLastBluebirdCount = 0
		self.m_iTargetBluebirdCount = 0
		self.m_oBluebirdMeterIrp = nil
		self.m_oBonusArrowIrp = nil
		self.m_oHintCounterText = instance.clues.hintbutton.hintcounter
		self.m_bLockHintCounter = false
		self.m_oHintButton = instance.clues.hintbutton

		self.m_oHintCounterText.m_fOrigX = self.m_oHintCounterText:GetX()
		self.m_oHintCounterText.m_fOrigY = self.m_oHintCounterText:GetY()
		self.m_oHintCounterText.m_fOrigWidth = self.m_oHintCounterText:GetWidth()
		self.m_oHintCounterText.m_fOrigHeight = self.m_oHintCounterText:GetHeight()

		instance:SetPosition( 0, 0 )
		
		self:UpdateClock()
		
		local minutes = math.floor( instance.Time / 60 )
		local seconds = math.floor( math.mod( instance.Time , 60 ) )
		local secondsStr = tostring( seconds );
		
		if( seconds <= 9 ) then
			secondsStr = "0"..seconds;
		end
		
		self.m_oMinutesText:SetText( tostring( minutes ) )
		self.m_oSecondsText:SetText( secondsStr )
		
		--//itemCount:SetText( "" .. instance.totalItems )
		
		instance:updateUI( )
		
		StandardMouseButton( bookButton );
		bookButton:Init( )
		StandardMouseButton( panicButton );
		panicButton:Init( )	
		StandardMouseButton( menuButton );
		menuButton:Init( )
		StandardMouseButton( hintbutton );
		hintbutton:Init( )
		StandardMouseButton( pauseButton );
		pauseButton:Init( )
		pauseButton:DisableSound()
		
		--VV arrows
--		StandardMouseButton( leftArrow )
--		leftArrow:Init( )
--		StandardMouseButton( rightArrow )
--		rightArrow:Init( )

		
		
		ItemMouseButton( instance.cellsList[0] );
		ItemMouseButton( instance.cellsList[1] );
		ItemMouseButton( instance.cellsList[2] );
		ItemMouseButton( instance.cellsList[3] );
		ItemMouseButton( instance.cellsList[4] );
		ItemMouseButton( instance.cellsList[5] );
		
		InitCL(instance.cellsList[0], 0);
		InitCL(instance.cellsList[1], 1);
		InitCL(instance.cellsList[2], 2);
		InitCL(instance.cellsList[3], 3);
		InitCL(instance.cellsList[4], 4);
		InitCL(instance.cellsList[5], 5);
		

		
		self.m_oHintCounterText:SetFrame( tonumber( instance.hintcount ) )

		self.listMask:SetVisible( false )

		-- set list masking
		self.listMaskTop:SetVisible( false )
		
		self:SetBluebirdCount( self.Bluebirds, true )
	end
	
	-- add an item to the find list
	function instance:AddItem( i_Item )
		
		local newItem = {}
		newItem.string = i_Item
		newItem.found = false
		newItem.super = false
		
		table.insert( self.internalList, newItem )
	end
	
	-- add an item to the find list
	function instance:AddSuperItem( i_Item )
		
		local newItem = {}
		newItem.string = i_Item
		newItem.found = false
		newItem.super = true
		
		table.insert( self.internalList, newItem )
	end
	
	function instance:SetItemFound( i )
		if ( self.internalList[i] ~= nil ) then
			self.internalList[i].string = self:GetLastItemName()
			self.internalList[i].status = true
		end
	end
	
	function instance:SetItemHidden( i )
		if ( self.internalList[i] ~= nil ) then
			self.internalList[i].string = self:GetLastItemName()
			self.internalList[i].status = false
		end
	end
	
	function instance:SetFlashTime( flash )
		self.flashTime = flash
	end
	
	function instance:ClearItemList()
		self.internalList = {}
	end

	-- Handle keyboard input for the dialog
	function instance:OnCharDown( i_Key )
		--[[
		if ( IsDebug() ) then
			if ( i_Key == "R"  ) then
				self:Reload()
			end
		end
		--]]
	end
	
	function instance:Reload()
		log( "RELOAD!" )
		console( "lua.dofile ISpyPanel_esc.lua" )
		ISpyPanel_esc:Create( )
		self:HintHint()
	end
	
	function instance:OnHint()

--		self.m_oCupid.cupid.arrow.m_fOpacity = 0
--		self.m_oCupid.cupid.arrow:SetOpacity( 0 )
		self.m_fArrowFadeInOn = self:GetTimeInState() + 1.75
	end
		
	function instance:GetSplinePathAndShow( i_locX, i_locY, i_itemWidth )
		local aRet = self:GetSplinePath( i_locX, i_locY, i_itemWidth )

		self:CanvasDrawLine( 0, 300, 800, 300, 0xffffff00 )
		self:CanvasDrawLine( 450, 0, 450, 600, 0xffffff00 )

	
		for i = 1, #aRet, 1 do
			self:CanvasDrawCircle( aRet[ i ][ 1 ], aRet[ i ][ 2 ], 20, 0xffffff00, 32 )
			self:CanvasDrawTextLine( aRet[ i ][ 1 ], aRet[ i ][ 2 ], 20, 0xffffff00, tostring( i - 1 ) )
			if ( i > 1 ) then
				if ( i == 2 or i == #aRet ) then
					self:CanvasDrawLine( aRet[ i - 1 ][ 1 ], aRet[ i - 1 ][ 2 ], aRet[ i ][ 1 ], aRet[ i ][ 2 ], 0xffff0000 )
				else
					self:CanvasDrawLine( aRet[ i - 1 ][ 1 ] - 1, aRet[ i - 1 ][ 2 ] - 1, aRet[ i ][ 1 ] - 1, aRet[ i ][ 2 ] - 1, 0xffff0000 )
					self:CanvasDrawLine( aRet[ i - 1 ][ 1 ], aRet[ i - 1 ][ 2 ], aRet[ i ][ 1 ], aRet[ i ][ 2 ], 0xffff0000 )
					self:CanvasDrawLine( aRet[ i - 1 ][ 1 ] + 1, aRet[ i - 1 ][ 2 ] + 1, aRet[ i ][ 1 ] + 1, aRet[ i ][ 2 ] + 1, 0xffff0000 )
				end
			end
		end
	
		return aRet
	end

	function instance:GetSplinePath( i_locX, i_locY, i_itemWidth )
		
		local fHalfWidth = 450.0;
		local fHalfHeight = 300.0;
		
		local aRet = {}
		
		-- first and last points are spline control points that are not traversed
		-- (see http://www.mvps.org/directx/articles/catmull/ )
		
		local fStartX = 90
		local fStartY = 65

		local fItemSpacing = i_itemWidth / 2 + 30
		
		if ( i_locX < fHalfWidth and i_locY < fHalfHeight ) then
		
			-- UPPERLEFT
		
			-- adjust location to be slightly away from object
			local fAdjLocX = i_locX + fItemSpacing
			local fDeltaX = fAdjLocX - fStartX
			local fDeltaY = i_locY - fStartY
			local fDist = math.sqrt( fDeltaX * fDeltaX + fDeltaY * fDeltaY )

			local fMult = fDist * 0.002

			table.insert( aRet,	{	500,					60,									0.0 } )
			table.insert( aRet,	{	fStartX,				fStartY,							0.0 } )
			table.insert( aRet,	{	fStartX + 315 * fMult,	fStartY + 440 * fMult,				1.0 } )
			table.insert( aRet,	{	fStartX + 565 * fMult,	fStartY + 340 * fMult,				1.2 } )
			table.insert( aRet,	{	fStartX + 615 * fMult,	fStartY + 190 * fMult,				1.2 } )
			table.insert( aRet,	{	fStartX + 515 * fMult,	fStartY + 80 * fMult,				1.2 } )
			table.insert( aRet,	{	fAdjLocX,				i_locY,								1.3 } )
			table.insert( aRet,	{	fStartX + 315 * fMult,	fStartY + 440 * fMult,				0.0 } )
		
		elseif ( i_locX >= fHalfWidth and i_locY < fHalfHeight ) then
			
			-- UPPERRIGHT
			
			local fAdjLocX = i_locX - fItemSpacing

			table.insert( aRet,	{	500,					60,									0.0 } )
			table.insert( aRet,	{	fStartX,				fStartY,							0.0 } )
			table.insert( aRet,	{	250,					350,								0.9 } )
			table.insert( aRet,	{	550,					500,								1.0 } )
			table.insert( aRet,	{	700,					350,								1.2 } )
			table.insert( aRet,	{	fAdjLocX,				i_locY,								1.0 } )
			table.insert( aRet,	{	fAdjLocX,				i_locY,								0.0 } )

		elseif ( i_locX < fHalfWidth and i_locY >= fHalfHeight ) then

			-- LOWERLEFT
			
			local fAdjLocX = i_locX + fItemSpacing

			table.insert( aRet,	{	500,					60,									0.0 } )
			table.insert( aRet,	{	fStartX,				fStartY,							0.0 } )
			table.insert( aRet,	{	300,					550,								1.0 } )
			table.insert( aRet,	{	650,					400,								1.2 } )
			table.insert( aRet,	{	525,					250,								1.2 } )
			table.insert( aRet,	{	fAdjLocX,				i_locY,								1.2 } )
			table.insert( aRet,	{	fAdjLocX,				i_locY,								0.0 } )

		elseif ( i_locX >= fHalfWidth and i_locY >= fHalfHeight ) then

			-- LOWERRIGHT 
			
			local fAdjLocX = i_locX - fItemSpacing

			table.insert( aRet,	{	500,					60,									0.0 } )
			table.insert( aRet,	{	fStartX,				fStartY,							0.0 } )
			table.insert( aRet,	{	600,					200,								1.0 } )
			table.insert( aRet,	{	400,					400,								1.3 } )
			table.insert( aRet,	{	fAdjLocX,				i_locY,								1.0 } )
			table.insert( aRet,	{	fAdjLocX,				i_locY,								0.0 } )

		end

		for i = 3, #aRet - 1, 1 do
			local fDeltaX = aRet[ i ][ 1 ] - aRet[ i - 1 ][ 1 ]
			local fDeltaY = aRet[ i ][ 2 ] - aRet[ i - 1 ][ 2 ]
			
			local fDist = math.sqrt( fDeltaX * fDeltaX + fDeltaY * fDeltaY )
			aRet[ i ][ 3 ] = aRet[ i ][ 3 ] * fDist * 0.0015
		end
		
		return aRet
	end

	function instance:SetBluebirdCount( i_iCount, i_bAllowReset )
		
		log( "HEREHERE" .. i_iCount )
		local iCount = math.mod( i_iCount, ISpyPanel_esc.s_iBluebirdMeterCount )
		if ( i_iCount > 0 and iCount == 0 and i_bAllowReset ~= true ) then
			iCount = ISpyPanel_esc.s_iBluebirdMeterCount
		end
		
		local oCounter = self.m_oBluebirds
		oCounter:SetFrame( i_iCount )
	end
	
	-- The Album button
	function bookButton:Init( )
		function bookButton:Pressed( )
			instance:ScreenEvent( "Albumn" , "ISpyPanel_esc" )
		end
	end
	
	function panicButton:Init( )
		function panicButton:Pressed( )
			instance:ScreenEvent( "Panic" , "ISpyPanel_esc" )
		end
	end
	
	function instance:DisablePanic()
		log("DisablePanic")
		panicButton:Disable()
	end
	
	
	--VV arrows
--	function leftArrow:Init( )
--		function leftArrow:Pressed( )
--			instance:ScreenEvent( "LeftArrow" , "ISpyPanel_esc" )
--		end
--	end
	
--	function rightArrow:Init( )
--		function rightArrow:Pressed( )
--			instance:ScreenEvent( "RightArrow" , "ISpyPanel_esc" )
--		end
--	end


	-- The help button
	function hintbutton:Init( )
		function hintbutton:Pressed( )
			instance:ScreenEvent( "hint" , "ISpyPanel_esc" )
		end
	end
		
	-- The help button
	function menuButton:Init( )
		function menuButton:Pressed( )
			--[[
			if ( IsDebug() ) then
				instance:Reload()
			end
			--]]
			
			instance:ScreenEvent( "GameMenu" , "ISpyPanel_esc" )
		end
	end

	-- The help button
	function pauseButton:Init( )
		function pauseButton:Pressed( )
			instance:ScreenEvent( "PauseGame", "ISpyPanel_esc" )
		end
		--function clockGlow:OnLeftMouseUp( )
		--	instance:ScreenEvent( "PauseGame", "ISpyPanel_esc" )
		--end
	end
	
	
	--local lastPutted
	--local was = 0
	local lastFrame = 0
	local lastItemCellNo = -1
	
	
	
	function instance:GetUsableItem( cellNo, itemNo )
		log("GetUsableItem " .. instance.cellsList[cellNo]:GetName() .. " " .. cellNo .. " " .. itemNo)
		instance.cellsList[cellNo].items:SetFrame(itemNo);
	end
	function instance:PutUsableItem( cellNo )
		instance.cellsList[cellNo]:Disable()
		---instance.cellsList[cellNo].items:SetFrame(0);
		--lastPutted:SetFrame(1)
		lastItemCellNo = -1
	end
	function instance:StoneCell( cellNo )
		instance.cellsList[cellNo]:Stone()
		---instance.cellsList[cellNo].items:SetFrame(0);
		--lastPutted:SetFrame(1)
		--lastItemCellNo = -1
	end
	
	
	function instance:GetAppearing( appNo )
		log("instance:GetAppearing")
		instance.appearings:SetFrame(appNo)
			
		if(appNo == 1) then
			PartMouseButton(instance.appearings.papperMovie.use_afisha_1_part)
			PartMouseButton(instance.appearings.papperMovie.use_afisha_2_part)
			PartMouseButton(instance.appearings.papperMovie.use_afisha_3_part)
			--PartMouseButton(instance.appearings.papperMovie.use_afisha_4_part)
			--PartMouseButton(instance.appearings.papperMovie.use_afisha_5_part)
			
			InitAL(instance.appearings.papperMovie.use_afisha_1_part)
			InitAL(instance.appearings.papperMovie.use_afisha_2_part)
			InitAL(instance.appearings.papperMovie.use_afisha_3_part)
			--InitAL(instance.appearings.papperMovie.use_afisha_4_part)
			--InitAL(instance.appearings.papperMovie.use_afisha_5_part)
			InitCross(instance.appearings.papperMovie)
		elseif(appNo == 2) then		
			PartMouseButton(instance.appearings.keyboardMovie.use_repairkit)			
			InitUL(instance.appearings.keyboardMovie.use_repairkit)	
			
			PartMouseButton(instance.appearings.keyboardMovie.use_button)		
			InitUL(instance.appearings.keyboardMovie.use_button)
			InitCross(instance.appearings.keyboardMovie)
		
		-- VV apartment
		elseif(appNo == 3) then		
			PartMouseButton(instance.appearings.switchMovie.click_switch)			
			InitClick(instance.appearings.switchMovie.click_switch)	
			InitCross(instance.appearings.switchMovie)
		elseif(appNo == 4) then		
			PartMouseButton(instance.appearings.socketMovie.use_bulb)			
			InitUL(instance.appearings.socketMovie.use_bulb)	
			InitCross(instance.appearings.socketMovie)
		elseif(appNo == 5) then		--ACTIVATING_TYPE_SHOEBOX
			PartMouseButton(instance.appearings.shoeboxMovie.click_shoebox)			
			InitClick(instance.appearings.shoeboxMovie.click_shoebox)	
			InitCross(instance.appearings.shoeboxMovie)
		elseif(appNo == 6) then		--ACTIVATING_TYPE_LARGEBOX
			PartMouseButton(instance.appearings.largeboxMovie.use_cutter)
			InitUL(instance.appearings.largeboxMovie.use_cutter)	
			InitCross(instance.appearings.largeboxMovie)
		elseif(appNo == 7) then		--ACTIVATING_TYPE_SAFE
			PartMouseButton(instance.appearings.safeMovie.click_Safe)			
			InitClick(instance.appearings.safeMovie.click_Safe)	
			PartMouseButton(instance.appearings.safeMovie.safe_keyboard.click_number_1)			
			InitClick(instance.appearings.safeMovie.safe_keyboard.click_number_1)	
			PartMouseButton(instance.appearings.safeMovie.safe_keyboard.click_number_2)			
			InitClick(instance.appearings.safeMovie.safe_keyboard.click_number_2)	
			PartMouseButton(instance.appearings.safeMovie.safe_keyboard.click_number_3)			
			InitClick(instance.appearings.safeMovie.safe_keyboard.click_number_3)	
			InitCross(instance.appearings.safeMovie)
		elseif(appNo == 8) then		--ACTIVATING_TYPE_LOCKET
			PartMouseButton(instance.appearings.locketMovie.use_Key_under_rug)
			InitUL(instance.appearings.locketMovie.use_Key_under_rug)	
			InitCross(instance.appearings.locketMovie)	
		elseif(appNo == 9) then		--ACTIVATING_TYPE_LETTER
			PartMouseButton(instance.appearings.letterMovie.use_Letter_1_part)
			PartMouseButton(instance.appearings.letterMovie.use_Letter_2_part)
			PartMouseButton(instance.appearings.letterMovie.use_Letter_3_part)
			
			InitAL(instance.appearings.letterMovie.use_Letter_1_part)
			InitAL(instance.appearings.letterMovie.use_Letter_2_part)
			InitAL(instance.appearings.letterMovie.use_Letter_3_part)
			InitCross(instance.appearings.letterMovie)
		elseif(appNo == 10) then		--ACTIVATING_TYPE_SHEAK_BOOK
			PartMouseButton(instance.appearings.sheakBookMovie.click_sheak_book)
			InitClick(instance.appearings.sheakBookMovie.click_sheak_book)
			InitCross(instance.appearings.sheakBookMovie)
		elseif(appNo == 11) then		--ACTIVATING_TYPE_PRINTER
			PartMouseButton(instance.appearings.printerMovie.use_Ink_bottle)
			InitUL(instance.appearings.printerMovie.use_Ink_bottle)
			InitCross(instance.appearings.printerMovie)
		elseif(appNo == 12) then		--ACTIVATING_TYPE_PALM
			InitCross(instance.appearings.palmMovie)
		elseif(appNo == 13) then		--ACTIVATING_TYPE_LIGHTER
			InitCross(instance.appearings.lighterMovie)
		elseif(appNo == 14) then		--ACTIVATING_TYPE_CAROUSEL
			PartMouseButton(instance.appearings.carouselMovie.use_candle_thin)
			PartMouseButton(instance.appearings.carouselMovie.use_candle_thick)
			
			InitUL(instance.appearings.carouselMovie.use_candle_thin)
			InitUL(instance.appearings.carouselMovie.use_candle_thick)
			InitCross(instance.appearings.carouselMovie)	
		elseif(appNo == 15) then		--ACTIVATING_TYPE_BOWL
			PartMouseButton(instance.appearings.bowlMovie.use_Apple_red)
			PartMouseButton(instance.appearings.bowlMovie.use_Apple_green)
			PartMouseButton(instance.appearings.bowlMovie.use_Apple_yellow)
			
			InitUL(instance.appearings.bowlMovie.use_Apple_red)
			InitUL(instance.appearings.bowlMovie.use_Apple_green)
			InitUL(instance.appearings.bowlMovie.use_Apple_yellow)
			InitCross(instance.appearings.bowlMovie)
		elseif(appNo == 16) then		--ACTIVATING_TYPE_PHOTO_FRAME
			PartMouseButton(instance.appearings.photoFrameMovie.use_Photo)
			InitUL(instance.appearings.photoFrameMovie.use_Photo)
			InitCross(instance.appearings.photoFrameMovie)
		elseif(appNo == 17) then		--ACTIVATING_TYPE_WALL_OUTLET
			PartMouseButton(instance.appearings.wallOutletMovie.click_Wall_outlet_plugger)
			InitClick(instance.appearings.wallOutletMovie.click_Wall_outlet_plugger)
			InitCross(instance.appearings.wallOutletMovie)
		elseif(appNo == 18) then		--ACTIVATING_TYPE_CONFETTI_HEAP
			PartMouseButton(instance.appearings.confettiHeapMovie.use_Vacuum_cleaner)
			InitUL(instance.appearings.confettiHeapMovie.use_Vacuum_cleaner)
			InitCross(instance.appearings.confettiHeapMovie)
		elseif(appNo == 19) then		--ACTIVATING_TYPE_VACUUM_CLEANER
			InitCross(instance.appearings.vacuumCleanerMovie)
		elseif(appNo == 20) then		--ACTIVATING_TYPE_MED_CABINET
			PartMouseButton(instance.appearings.medCabinetMovie.use_Hairpin)
			InitUL(instance.appearings.medCabinetMovie.use_Hairpin)
			InitCross(instance.appearings.medCabinetMovie)
		elseif(appNo == 21) then		--ACTIVATING_TYPE_DISPLACED_TILE
			PartMouseButton(instance.appearings.displacedTileMovie.use_Crowbar)
			InitUL(instance.appearings.displacedTileMovie.use_Crowbar)
			InitCross(instance.appearings.displacedTileMovie)
		elseif(appNo == 22) then		--ACTIVATING_TYPE_NAIL_POLISH_POOL
			PartMouseButton(instance.appearings.nailPolishPoolMovie.use_Cotton_ball_wetted)
			InitUL(instance.appearings.nailPolishPoolMovie.use_Cotton_ball_wetted)
			InitCross(instance.appearings.nailPolishPoolMovie)
		elseif(appNo == 23) then		--ACTIVATING_TYPE_TRASHCAN
			PartMouseButton(instance.appearings.trashcanMovie.use_Cotton_ball_dirty)
			InitUL(instance.appearings.trashcanMovie.use_Cotton_ball_dirty)
			InitCross(instance.appearings.trashcanMovie)
		elseif(appNo == 24) then		--ACTIVATING_TYPE_MAGAZINE
			PartMouseButton(instance.appearings.magazineMovie.use_Hair_dryer)
			InitUL(instance.appearings.magazineMovie.use_Hair_dryer)
			InitCross(instance.appearings.magazineMovie)
		elseif(appNo == 25) then		--ACTIVATING_TYPE_BATHROOM_LIGHT
			PartMouseButton(instance.appearings.bathroomLightMovie.use_Tweezers)
			InitUL(instance.appearings.bathroomLightMovie.use_Tweezers)
			InitCross(instance.appearings.bathroomLightMovie)
		elseif(appNo == 26) then		--ACTIVATING_TYPE_BOX
			PartMouseButton(instance.appearings.boxMovie.use_Stone)
			InitUL(instance.appearings.boxMovie.use_Stone)
			PartMouseButton(instance.appearings.boxMovie.click_Fire_Button)
			InitClick(instance.appearings.boxMovie.click_Fire_Button)
			InitCross(instance.appearings.boxMovie)
		elseif(appNo == 27) then		--ACTIVATING_TYPE_BUCKET
			PartMouseButton(instance.appearings.bucketMovie.use_Bowl)
			InitUL(instance.appearings.bucketMovie.use_Bowl)
			PartMouseButton(instance.appearings.bucketMovie.take_Stone)
			InitTake(instance.appearings.bucketMovie.take_Stone)
			InitCross(instance.appearings.bucketMovie)
		elseif(appNo == 28) then		--ACTIVATING_TYPE_BASIN
			PartMouseButton(instance.appearings.basinMovie.use_Tap_valve)
			InitUL(instance.appearings.basinMovie.use_Tap_valve)
			PartMouseButton(instance.appearings.basinMovie.take_Bowl)
			InitTake(instance.appearings.basinMovie.take_Bowl)
			InitCross(instance.appearings.basinMovie)
		elseif(appNo == 29) then		--ACTIVATING_TYPE_SLIDERS
			
			local children = instance.appearings.slidersMovie.sliders_field:GetChildren() 
			for i in pairs( children ) do
				--instance.slidersCellsList[i] = children[ i ]
				SlidersCell(children[ i ], self)
				InitSliderCell(children[ i  ], i)
				--children[ i ].cell_images:SetFrame(i - 1) --SetFrame(16 - i) --SetFrame(i - 1)
			end
			InitCross(instance.appearings.slidersMovie)
		elseif(appNo == 30) then		--ACTIVATING_TYPE_FLOWERS_BOWL
			PartMouseButton(instance.appearings.flowersBowlMovie.use_Carafe)
			InitUL(instance.appearings.flowersBowlMovie.use_Carafe)
			InitCross(instance.appearings.flowersBowlMovie)
		elseif(appNo == 31) then		--ACTIVATING_TYPE_CABINET
			InitCross(instance.appearings.cabinetMovie)
		elseif(appNo == 32) then		--ACTIVATING_TYPE_BOTTLE
			PartMouseButton(instance.appearings.oilBottleMovie.use_Corkscrew)
			InitUL(instance.appearings.oilBottleMovie.use_Corkscrew)
			PartMouseButton(instance.appearings.oilBottleMovie.take_Oil)
			InitTake(instance.appearings.oilBottleMovie.take_Oil)
			InitCross(instance.appearings.oilBottleMovie)
		elseif(appNo == 33) then		--ACTIVATING_TYPE_WIRE
			PartMouseButton(instance.appearings.wireMovie.use_Wire_cutter)
			InitUL(instance.appearings.wireMovie.use_Wire_cutter)
			PartMouseButton(instance.appearings.wireMovie.take_Piece_of_Wire)
			InitTake(instance.appearings.wireMovie.take_Piece_of_Wire)
			InitCross(instance.appearings.wireMovie)
		elseif(appNo == 34) then		--ACTIVATING_TYPE_DOOR
			PartMouseButton(instance.appearings.doorMovie.use_Oil)
			InitUL(instance.appearings.doorMovie.use_Oil)
			PartMouseButton(instance.appearings.doorMovie.use_Piece_of_Wire)
			InitUL(instance.appearings.doorMovie.use_Piece_of_Wire)
			InitCross(instance.appearings.doorMovie)
		elseif(appNo == 35) then		--ACTIVATING_TYPE_ENTANGLED_FILM
			PartMouseButton(instance.appearings.entangledFilmMovie.use_Reel)
			InitUL(instance.appearings.entangledFilmMovie.use_Reel)
			InitCross(instance.appearings.entangledFilmMovie)
		elseif(appNo == 36) then		--ACTIVATING_TYPE_NOTEBOOK
			InitCross(instance.appearings.notebookMovie)
		elseif(appNo == 37) then		--ACTIVATING_TYPE_PROJECTOR
			PartMouseButton(instance.appearings.projectorMovie.use_Knob)
			InitUL(instance.appearings.projectorMovie.use_Knob)
			PartMouseButton(instance.appearings.projectorMovie.use_Reel_with_film)
			InitUL(instance.appearings.projectorMovie.use_Reel_with_film)
			InitCross(instance.appearings.projectorMovie)
		elseif(appNo == 38) then		--ACTIVATING_TYPE_AQUARIUM
			PartMouseButton(instance.appearings.aquariumMovie.use_Food_for_fish)
			InitUL(instance.appearings.aquariumMovie.use_Food_for_fish)
			InitCross(instance.appearings.aquariumMovie)
		elseif(appNo == 39) then		--ACTIVATING_TYPE_GUMBALL_MACHINE
			PartMouseButton(instance.appearings.gumballMachineMovie.use_Coin)
			InitUL(instance.appearings.gumballMachineMovie.use_Coin)
			InitCross(instance.appearings.gumballMachineMovie)
		elseif(appNo == 40) then		--ACTIVATING_TYPE_OLD_WATCH
			PartMouseButton(instance.appearings.oldWatchMovie.use_Gem)
			InitUL(instance.appearings.oldWatchMovie.use_Gem)
			InitCross(instance.appearings.oldWatchMovie)
		elseif(appNo == 41) then		--ACTIVATING_TYPE_UNSHARPENED_PENCIL
			PartMouseButton(instance.appearings.unsharpenedPencilMovie.use_Sharpener)
			InitUL(instance.appearings.unsharpenedPencilMovie.use_Sharpener)
			InitCross(instance.appearings.unsharpenedPencilMovie)
		elseif(appNo == 42) then		--ACTIVATING_TYPE_NOTEPAD_IN_DESK
			PartMouseButton(instance.appearings.notepadInDeskMovie.click_Notepad_in_desk)
			InitClick(instance.appearings.notepadInDeskMovie.click_Notepad_in_desk)
			InitCross(instance.appearings.notepadInDeskMovie)
		elseif(appNo == 43) then		--ACTIVATING_TYPE_PHONE_IN_DESK
			InitCross(instance.appearings.phoneInDeskMovie)
		elseif(appNo == 44) then		--ACTIVATING_TYPE_FAX_IN_DESK
			InitCross(instance.appearings.faxInDeskMovie)
		elseif(appNo == 45) then		--ACTIVATING_TYPE_DRAWER
			PartMouseButton(instance.appearings.drawerMovie.use_Screwdriver)
			InitUL(instance.appearings.drawerMovie.use_Screwdriver)
			PartMouseButton(instance.appearings.drawerMovie.click_Drawer)
			InitClick(instance.appearings.drawerMovie.click_Drawer)
			PartMouseButton(instance.appearings.drawerMovie.take_Piece2i)
			InitTake(instance.appearings.drawerMovie.take_Piece2i)
			InitCross(instance.appearings.drawerMovie)
		elseif(appNo == 46) then		--ACTIVATING_TYPE_OVEN
			PartMouseButton(instance.appearings.ovenMovie.take_Cake)
			InitTake(instance.appearings.ovenMovie.take_Cake)
			InitCross(instance.appearings.ovenMovie)
		elseif(appNo == 47) then		--ACTIVATING_TYPE_CAKE_STAND
			PartMouseButton(instance.appearings.cakeStandMovie.use_Cake)
			InitUL(instance.appearings.cakeStandMovie.use_Cake)
			PartMouseButton(instance.appearings.cakeStandMovie.use_Knife)
			InitUL(instance.appearings.cakeStandMovie.use_Knife)
			PartMouseButton(instance.appearings.cakeStandMovie.take_Piece3i)
			InitTake(instance.appearings.cakeStandMovie.take_Piece3i)
			InitCross(instance.appearings.cakeStandMovie)
		elseif(appNo == 48) then		--ACTIVATING_TYPE_ITEMS_LIST
			PartMouseButton(instance.appearings.itemslistMovie.use_Piece1i)
			InitUL(instance.appearings.itemslistMovie.use_Piece1i)
			PartMouseButton(instance.appearings.itemslistMovie.use_Piece2i)
			InitUL(instance.appearings.itemslistMovie.use_Piece2i)
			PartMouseButton(instance.appearings.itemslistMovie.use_Piece3i)
			InitUL(instance.appearings.itemslistMovie.use_Piece3i)
			PartMouseButton(instance.appearings.itemslistMovie.take_Item_list)
			InitTake(instance.appearings.itemslistMovie.take_Item_list)
			InitCross(instance.appearings.itemslistMovie)
		elseif(appNo == 49) then		--ACTIVATING_TYPE_PIGGY_BANK
			PartMouseButton(instance.appearings.piggyBankMovie.use_Hammer)
			InitUL(instance.appearings.piggyBankMovie.use_Hammer)
			InitCross(instance.appearings.piggyBankMovie)
		elseif(appNo == 50) then		--ACTIVATING_TYPE_JUKEBOX
			PartMouseButton(instance.appearings.jukeboxMovie.use_Cloth_in_desk)
			InitUL(instance.appearings.jukeboxMovie.use_Cloth_in_desk)
			InitCross(instance.appearings.jukeboxMovie)
		elseif(appNo == 51) then		--ACTIVATING_TYPE_FORTUNE_MACHINE
			PartMouseButton(instance.appearings.fortuneMachineMovie.use_Coin_for_jukebox)
			InitUL(instance.appearings.fortuneMachineMovie.use_Coin_for_jukebox)
			InitCross(instance.appearings.fortuneMachineMovie)
		elseif(appNo == 52) then		--ACTIVATING_TYPE_NEWSPAPPER
			InitCross(instance.appearings.newspaperMovie)
		elseif(appNo == 53) then		--ACTIVATING_TYPE_HEART_SHAPED_MUG
			--PartMouseButton(instance.appearings.heartShapedMugMovie.click_Heart_shaped_mug)
			--InitClick(instance.appearings.heartShapedMugMovie.click_Heart_shaped_mug)
			InitCross(instance.appearings.heartShapedMugMovie)
		elseif(appNo == 54) then		--ACTIVATING_TYPE_BRICK
			PartMouseButton(instance.appearings.brickMovie.use_Chisel)
			InitUL(instance.appearings.brickMovie.use_Chisel)
			InitCross(instance.appearings.brickMovie)
		elseif(appNo == 55) then		--ACTIVATING_TYPE_SINK
			PartMouseButton(instance.appearings.sinkMovie.use_Tongs)
			InitUL(instance.appearings.sinkMovie.use_Tongs)
			PartMouseButton(instance.appearings.sinkMovie.use_Peels)
			InitUL(instance.appearings.sinkMovie.use_Peels)
			PartMouseButton(instance.appearings.sinkMovie.take_Key)
			InitTake(instance.appearings.sinkMovie.take_Key)
			PartMouseButton(instance.appearings.sinkMovie.click_Switch)
			InitClick(instance.appearings.sinkMovie.click_Switch)
			InitCross(instance.appearings.sinkMovie)
		elseif(appNo == 56) then		--ACTIVATING_TYPE_DRAWER1
			PartMouseButton(instance.appearings.drawer1Movie.use_Key)
			InitUL(instance.appearings.drawer1Movie.use_Key)
			PartMouseButton(instance.appearings.drawer1Movie.take_Item_list)
			InitTake(instance.appearings.drawer1Movie.take_Item_list)
			InitCross(instance.appearings.drawer1Movie)			
		elseif(appNo == 57) then		--ACTIVATING_TYPE_DRAWER2
			PartMouseButton(instance.appearings.drawer2Movie.take_Tongs)
			InitTake(instance.appearings.drawer2Movie.take_Tongs)
			InitCross(instance.appearings.drawer2Movie)
		end
	end
	
	function InitCross(popup)
		if ( popup._cross ~= nil ) then
			StandardMouseButton( popup._cross )
			function popup._cross:Pressed( )
				instance:ScreenEvent( "Cross" , "ISpyPanel_esc" )
			end	
		end	
	end
	
	function instance:ActivateItemCutter()
		log("ActivateItemCutter")
		PartMouseButton(instance.appearings.shoeboxMovie.take_cutter)			
		InitTake(instance.appearings.shoeboxMovie.take_cutter)
	end
	
	function instance:DeactivateItemCutter()
		log("DeactivateItemCutter")
		instance.appearings.shoeboxMovie.take_cutter:SetFrame(2)			
	end
	
	function instance:ActivateItemBulb()
		log("ActivateItemBulb")
		PartMouseButton(instance.appearings.largeboxMovie.take_bulb)			
		InitTake(instance.appearings.largeboxMovie.take_bulb)
	end
	
	function instance:DeactivateItemBulb()
		log("DeactivateItemBulb")
		instance.appearings.largeboxMovie.take_bulb:SetFrame(2)			
	end
	
	--function instance:ActivateItemKeyUnderRug()
	--	log("ActivateItemKeyUnderRug")
	--	PartMouseButton(instance.appearings.rugMovie.take_Key_under_rug)			
	--	InitTake(instance.appearings.rugMovie.take_Key_under_rug)
	--end
	--function instance:DeactivateItemKeyUnderRug()
	--	log("DeactivateItemKeyUnderRug")
	--	instance.appearings.rugMovie.take_Key_under_rug:SetFrame(2)			
	--end
	
	function instance:ActivateCodeForSafe()
		PartMouseButton(instance.appearings.locketMovie.click_Safe_code)			
		InitClick(instance.appearings.locketMovie.click_Safe_code)
	end
	
	
	function instance:ActivateItemListInSafe()
		PartMouseButton(instance.appearings.safeMovie.take_Item_list)			
		InitTake(instance.appearings.safeMovie.take_Item_list)
	end
	--unused
	--function instance:DeactivateItemListInLocket()
	--	log("DeactivateItemListInLocket")
	--	instance.appearings.locketMovie.take_Item_list:SetFrame(2)			
	--end
	
	function instance:ActivateItemListInPrinter()
		log("ActivateItemListInPrinter")
		PartMouseButton(instance.appearings.printerMovie.take_Item_list)			
		InitTake(instance.appearings.printerMovie.take_Item_list)
	end
	
	function instance:DeactivateItemListInPrinter()
		log("DeactivateItemListInPrinter")
		instance.appearings.printerMovie.take_Item_list:SetFrame(2)			
	end
	
	
	
	function instance:ActivateEnterKeyButton()
		local keypadObj = instance.appearings.keyboardMovie.keypad
		
		StandardMouseButton(keypadObj.key_3)	--key_arrow
		InitKeyButton(keypadObj.key_3)			--key_arrow
		keypadObj.key_3:SetFrame(0)				--key_arrow
		keypadObj.key_3:DisableSound()
	end
	
	function instance:DeactivateEnterKeyButton()
		local keypadObj = instance.appearings.keyboardMovie.keypad
		keypadObj.key_3:SetFrame(3)				--key_arrow
	end
	
	function instance:FixEnterKeyButton()
		local keypadObj = instance.appearings.keyboardMovie.keypad	
		keypadObj.key_3:SetFrame(0)				--key_arrow
	end
	
	
	
	function instance:InitKeyButtons()
		local keypadObj = instance.appearings.keyboardMovie.keypad
		
		StandardMouseButton(keypadObj.key_0)
		StandardMouseButton(keypadObj.key_1)
		StandardMouseButton(keypadObj.key_2)
		--StandardMouseButton(keypadObj.key_3)
		StandardMouseButton(keypadObj.key_4)
		StandardMouseButton(keypadObj.key_5)
		StandardMouseButton(keypadObj.key_6)
		StandardMouseButton(keypadObj.key_7)
		StandardMouseButton(keypadObj.key_8)
		StandardMouseButton(keypadObj.key_9)
		StandardMouseButton(keypadObj.key_star)
		StandardMouseButton(keypadObj.key_arrow)
		
		InitKeyButton(keypadObj.key_0)
		InitKeyButton(keypadObj.key_1)
		InitKeyButton(keypadObj.key_2)
		--InitKeyButton(keypadObj.key_3)
		InitKeyButton(keypadObj.key_4)
		InitKeyButton(keypadObj.key_5)
		InitKeyButton(keypadObj.key_6)
		InitKeyButton(keypadObj.key_7)
		InitKeyButton(keypadObj.key_8)
		InitKeyButton(keypadObj.key_9)
		InitKeyButton(keypadObj.key_star)
		InitKeyButton(keypadObj.key_arrow)
		
		keypadObj.key_0:DisableSound()
		keypadObj.key_1:DisableSound()
		keypadObj.key_2:DisableSound()
		--keypadObj.key_3:DisableSound()
		keypadObj.key_4:DisableSound()
		keypadObj.key_5:DisableSound()
		keypadObj.key_6:DisableSound()
		keypadObj.key_7:DisableSound()
		keypadObj.key_8:DisableSound()
		keypadObj.key_9:DisableSound()
		keypadObj.key_star:DisableSound()
		keypadObj.key_arrow:DisableSound()
	end
	
	function instance:InputNumber(pos, no)
		local screenObj = instance.appearings.keyboardMovie.screen
		local obj
		if (pos == 0) then
			obj = screenObj.numbers_1
		elseif (pos == 1) then
			obj = screenObj.numbers_2
		elseif (pos == 2) then
			obj = screenObj.numbers_3
		elseif (pos == 3) then
			obj = screenObj.numbers_4
		end
		
		obj:SetFrame(no)	
	end
	
	function instance:ResetPassword()
		local screenObj = instance.appearings.keyboardMovie.screen
		screenObj.numbers_1:SetFrame(0)
		screenObj.numbers_2:SetFrame(0)
		screenObj.numbers_3:SetFrame(0)
		screenObj.numbers_4:SetFrame(0)
	end
	
	--phone
	function instance:InitPhoneButtons()
		local keypadObj = instance.appearings.phoneInDeskMovie.keypad
		
		StandardMouseButton(keypadObj.key_0)
		StandardMouseButton(keypadObj.key_1)
		StandardMouseButton(keypadObj.key_2)
		StandardMouseButton(keypadObj.key_3)
		StandardMouseButton(keypadObj.key_4)
		StandardMouseButton(keypadObj.key_5)
		StandardMouseButton(keypadObj.key_6)
		StandardMouseButton(keypadObj.key_7)
		StandardMouseButton(keypadObj.key_8)
		StandardMouseButton(keypadObj.key_9)
		StandardMouseButton(keypadObj.key_star)
		StandardMouseButton(keypadObj.key_arrow)
		
		InitKeyButton(keypadObj.key_0)
		InitKeyButton(keypadObj.key_1)
		InitKeyButton(keypadObj.key_2)
		InitKeyButton(keypadObj.key_3)
		InitKeyButton(keypadObj.key_4)
		InitKeyButton(keypadObj.key_5)
		InitKeyButton(keypadObj.key_6)
		InitKeyButton(keypadObj.key_7)
		InitKeyButton(keypadObj.key_8)
		InitKeyButton(keypadObj.key_9)
		InitKeyButton(keypadObj.key_star)
		InitKeyButton(keypadObj.key_arrow)

		keypadObj.key_0:DisableSound()
		keypadObj.key_1:DisableSound()
		keypadObj.key_2:DisableSound()
		keypadObj.key_3:DisableSound()
		keypadObj.key_4:DisableSound()
		keypadObj.key_5:DisableSound()
		keypadObj.key_6:DisableSound()
		keypadObj.key_7:DisableSound()
		keypadObj.key_8:DisableSound()
		keypadObj.key_9:DisableSound()
		keypadObj.key_star:DisableSound()
		keypadObj.key_arrow:DisableSound()
	end
	
	function instance:InputPhoneNumber(pos, no)
		local screenObj = instance.appearings.phoneInDeskMovie.screen
		local obj
		if (pos == 0) then
			obj = screenObj.numbers_1
		elseif (pos == 1) then
			obj = screenObj.numbers_2
		elseif (pos == 2) then
			obj = screenObj.numbers_3
		elseif (pos == 3) then
			obj = screenObj.numbers_4
		elseif (pos == 4) then
			obj = screenObj.numbers_5
		--elseif (pos == 5) then
		--	obj = screenObj.numbers_6
		--elseif (pos == 6) then
		--	obj = screenObj.numbers_7
		end
		
		obj:SetFrame(no)	
	end
	
	function instance:ResetPhoneNumber()
		local screenObj = instance.appearings.phoneInDeskMovie.screen
		screenObj.numbers_1:SetFrame(0)
		screenObj.numbers_2:SetFrame(0)
		screenObj.numbers_3:SetFrame(0)
		screenObj.numbers_4:SetFrame(0)
		screenObj.numbers_5:SetFrame(0)
		--screenObj.numbers_6:SetFrame(0)
		--screenObj.numbers_7:SetFrame(0)
	end
	
	
	function InitKeyButton( obj )
		function obj:Pressed( )
			instance:ScreenEvent( "Key_" .. obj:GetName() , "ISpyPanel_esc" )
		end
	end
	
	--palm
	function instance:InitPalmScreenButtons()
		local palmKeypadObj = instance.appearings.palmMovie.palm_screen.palm_keypad
		
		StandardMouseButton(palmKeypadObj.key_A)
		StandardMouseButton(palmKeypadObj.key_B)
		StandardMouseButton(palmKeypadObj.key_C)
		StandardMouseButton(palmKeypadObj.key_D)
		StandardMouseButton(palmKeypadObj.key_E)
		StandardMouseButton(palmKeypadObj.key_F)
		StandardMouseButton(palmKeypadObj.key_G)
		StandardMouseButton(palmKeypadObj.key_H)
		StandardMouseButton(palmKeypadObj.key_I)
		StandardMouseButton(palmKeypadObj.key_J)
		StandardMouseButton(palmKeypadObj.key_K)
		StandardMouseButton(palmKeypadObj.key_L)
		StandardMouseButton(palmKeypadObj.key_M)
		StandardMouseButton(palmKeypadObj.key_N)
		StandardMouseButton(palmKeypadObj.key_O)
		StandardMouseButton(palmKeypadObj.key_P)
		StandardMouseButton(palmKeypadObj.key_Q)
		StandardMouseButton(palmKeypadObj.key_R)
		StandardMouseButton(palmKeypadObj.key_S)
		StandardMouseButton(palmKeypadObj.key_T)
		StandardMouseButton(palmKeypadObj.key_U)
		StandardMouseButton(palmKeypadObj.key_V)
		StandardMouseButton(palmKeypadObj.key_W)
		StandardMouseButton(palmKeypadObj.key_X)
		StandardMouseButton(palmKeypadObj.key_Y)
		StandardMouseButton(palmKeypadObj.key_Z)
		StandardMouseButton(palmKeypadObj.key_clear)
		StandardMouseButton(palmKeypadObj.key_enter)

		
		InitKeyButton(palmKeypadObj.key_A)
		InitKeyButton(palmKeypadObj.key_B)
		InitKeyButton(palmKeypadObj.key_C)
		InitKeyButton(palmKeypadObj.key_D)
		InitKeyButton(palmKeypadObj.key_E)
		InitKeyButton(palmKeypadObj.key_F)
		InitKeyButton(palmKeypadObj.key_G)
		InitKeyButton(palmKeypadObj.key_H)
		InitKeyButton(palmKeypadObj.key_I)
		InitKeyButton(palmKeypadObj.key_J)
		InitKeyButton(palmKeypadObj.key_K)
		InitKeyButton(palmKeypadObj.key_L)
		InitKeyButton(palmKeypadObj.key_M)
		InitKeyButton(palmKeypadObj.key_N)
		InitKeyButton(palmKeypadObj.key_O)
		InitKeyButton(palmKeypadObj.key_P)
		InitKeyButton(palmKeypadObj.key_Q)
		InitKeyButton(palmKeypadObj.key_R)
		InitKeyButton(palmKeypadObj.key_S)
		InitKeyButton(palmKeypadObj.key_T)
		InitKeyButton(palmKeypadObj.key_U)
		InitKeyButton(palmKeypadObj.key_V)
		InitKeyButton(palmKeypadObj.key_W)
		InitKeyButton(palmKeypadObj.key_X)
		InitKeyButton(palmKeypadObj.key_Y)
		InitKeyButton(palmKeypadObj.key_Z)
		InitKeyButton(palmKeypadObj.key_clear)
		InitKeyButton(palmKeypadObj.key_enter)
		
		palmKeypadObj.key_A:DisableSound()
		palmKeypadObj.key_B:DisableSound()
		palmKeypadObj.key_C:DisableSound()
		palmKeypadObj.key_D:DisableSound()
		palmKeypadObj.key_E:DisableSound()
		palmKeypadObj.key_F:DisableSound()
		palmKeypadObj.key_G:DisableSound()
		palmKeypadObj.key_H:DisableSound()
		palmKeypadObj.key_I:DisableSound()
		palmKeypadObj.key_J:DisableSound()
		palmKeypadObj.key_K:DisableSound()
		palmKeypadObj.key_L:DisableSound()
		palmKeypadObj.key_M:DisableSound()
		palmKeypadObj.key_N:DisableSound()
		palmKeypadObj.key_O:DisableSound()
		palmKeypadObj.key_P:DisableSound()
		palmKeypadObj.key_Q:DisableSound()
		palmKeypadObj.key_R:DisableSound()
		palmKeypadObj.key_S:DisableSound()
		palmKeypadObj.key_T:DisableSound()
		palmKeypadObj.key_U:DisableSound()
		palmKeypadObj.key_V:DisableSound()
		palmKeypadObj.key_W:DisableSound()
		palmKeypadObj.key_X:DisableSound()
		palmKeypadObj.key_Y:DisableSound()
		palmKeypadObj.key_Z:DisableSound()
		palmKeypadObj.key_clear:DisableSound()
		palmKeypadObj.key_enter:DisableSound()
		
		
	end
	
	function instance:InitPalmPrintButton()
		local palmPrintButton = instance.appearings.palmMovie.palm_screen.key_print
		
		StandardMouseButton(palmPrintButton)
		InitKeyButton(palmPrintButton)
	end
	function instance:ResetPalmPassword(pos)
		log("ResetPalmPassword " .. pos)
		local screenObj = instance.appearings.palmMovie.palm_screen.palm_password_field
		--[[screenObj.palm_character_1:SetFrame(0)
		screenObj.palm_character_2:SetFrame(0)
		screenObj.palm_character_3:SetFrame(0)
		screenObj.palm_character_4:SetFrame(0)
		screenObj.palm_character_5:SetFrame(0)]]--
		
		local obj
		if (pos == 0) then
			obj = screenObj.palm_character_1
		elseif (pos == 1) then
			obj = screenObj.palm_character_2
		elseif (pos == 2) then
			obj = screenObj.palm_character_3
		elseif (pos == 3) then
			obj = screenObj.palm_character_4
		elseif (pos == 4) then
			obj = screenObj.palm_character_5
		end
		
		obj:SetFrame(0)	

	end
	function instance:InputLetter(pos)
		local screenObj = instance.appearings.palmMovie.palm_screen.palm_password_field
		local obj
		if (pos == 0) then
			obj = screenObj.palm_character_1
		elseif (pos == 1) then
			obj = screenObj.palm_character_2
		elseif (pos == 2) then
			obj = screenObj.palm_character_3
		elseif (pos == 3) then
			obj = screenObj.palm_character_4
		elseif (pos == 4) then
			obj = screenObj.palm_character_5
		end
		
		obj:SetFrame(1)	
	end
	
	--lighter
	function instance:InitLighterFiller()
		PartMouseButton(instance.appearings.lighterMovie.use_fluid_can)
		InitUL(instance.appearings.lighterMovie.use_fluid_can)
	end
	function instance:InitLighterTaker()
		PartMouseButton(instance.appearings.lighterMovie.take_lighter)			
		InitTake(instance.appearings.lighterMovie.take_lighter)
	end
	
	--carousel
	function instance:ActivateItemListInCarousel()
		PartMouseButton(instance.appearings.carouselMovie.take_Item_list)			
		InitTake(instance.appearings.carouselMovie.take_Item_list)
	end
	function instance:DeactivateItemListInCarousel()
		instance.appearings.carouselMovie.take_Item_list:SetFrame(2)			
	end	
	function instance:InitCarouselBurner()
		PartMouseButton(instance.appearings.carouselMovie.use_lighter)
		InitUL(instance.appearings.carouselMovie.use_lighter)
	end
	
	--bowl
	function instance:ActivatePhoto()
		PartMouseButton(instance.appearings.bowlMovie.take_Photo)			
		InitTake(instance.appearings.bowlMovie.take_Photo)
	end
	function instance:DeactivatePhoto()
		instance.appearings.bowlMovie.take_Photo:SetFrame(2)			
	end
	function instance:InitPhoter()
		PartMouseButton(instance.appearings.bowlMovie.use_Polaroid)
		InitUL(instance.appearings.bowlMovie.use_Polaroid)
	end

	--photo frame
	function instance:ActivateItemListPhotoFrame()
		PartMouseButton(instance.appearings.photoFrameMovie.take_Item_list)			
		InitTake(instance.appearings.photoFrameMovie.take_Item_list)
	end
	function instance:DeactivateItemListPhotoFrame()
		instance.appearings.photoFrameMovie.take_Item_list:SetFrame(2)			
	end
	
	--med cabinet
	function instance:InitWetter()
		PartMouseButton(instance.appearings.medCabinetMovie.use_Cotton_ball)
		InitUL(instance.appearings.medCabinetMovie.use_Cotton_ball)
	end
	function instance:ActivateWettedCottonBall()
		PartMouseButton(instance.appearings.medCabinetMovie.take_Cotton_ball_wetted)			
		InitTake(instance.appearings.medCabinetMovie.take_Cotton_ball_wetted)
		instance.appearings.medCabinetMovie.take_Cotton_ball_wetted:SetFrame(0)
	end
	function instance:DeactivateWettedCottonBall()			
		instance.appearings.medCabinetMovie.take_Cotton_ball_wetted:SetFrame(2)
	end
	
	--displaced tile
	function instance:ActivateHairpin()
		PartMouseButton(instance.appearings.displacedTileMovie.take_Hairpin)			
		InitTake(instance.appearings.displacedTileMovie.take_Hairpin)
	end
	function instance:DeactivateHairpin()			
		instance.appearings.displacedTileMovie.take_Hairpin:SetFrame(2)
	end
	
	--nail polish pool
	function instance:ActivateDirtyCottonBall()
		PartMouseButton(instance.appearings.nailPolishPoolMovie.take_Cotton_ball_dirty)			
		InitTake(instance.appearings.nailPolishPoolMovie.take_Cotton_ball_dirty)
	end
	function instance:DeactivateDirtyCottonBall()			
		instance.appearings.nailPolishPoolMovie.take_Cotton_ball_dirty:SetFrame(2)
	end
	
	--trashcan
	function instance:ActivateItemListInTrashcan()
		PartMouseButton(instance.appearings.trashcanMovie.take_Item_list)			
		InitTake(instance.appearings.trashcanMovie.take_Item_list)
	end
	function instance:DeactivateItemListInTrashcan()			
		instance.appearings.trashcanMovie.take_Item_list:SetFrame(2)
	end
	
	--vacuun cleaner
	function instance:ActivateKeyInVC()
		PartMouseButton(instance.appearings.vacuumCleanerMovie.take_Key_in_VC)			
		InitTake(instance.appearings.vacuumCleanerMovie.take_Key_in_VC)
	end
	function instance:DeactivateKeyInVC()	
		DeInitTake(instance.appearings.vacuumCleanerMovie.take_Key_in_VC)		
		instance.appearings.vacuumCleanerMovie.take_Key_in_VC:SetFrame(2)
	end
	function instance:ActivateButtonVC()
		PartMouseButton(instance.appearings.vacuumCleanerMovie.take_Vacuum_cleaner)			
		InitTake(instance.appearings.vacuumCleanerMovie.take_Vacuum_cleaner)
	end
	function instance:DeactivateButtonVC()			
		DeInitTake(instance.appearings.vacuumCleanerMovie.take_Vacuum_cleaner)
		instance.appearings.vacuumCleanerMovie.take_Vacuum_cleaner:SetFrame(2)
	end
	
	--confetti heap
	function instance:InitBoxInConfettiHeap()
		PartMouseButton(instance.appearings.confettiHeapMovie.use_Key_in_VC)
		InitUL(instance.appearings.confettiHeapMovie.use_Key_in_VC)
	end
	function instance:ActivateItemListInConfettiHeap()
		PartMouseButton(instance.appearings.confettiHeapMovie.take_Item_list)			
		InitTake(instance.appearings.confettiHeapMovie.take_Item_list)
	end
	function instance:DeactivateItemListInConfettiHeap()			
		instance.appearings.confettiHeapMovie.take_Item_list:SetFrame(2)
	end
	
	--magazine
	function instance:InitMagazineOpener()
		PartMouseButton(instance.appearings.magazineMovie.click_Magazine)
		InitClick(instance.appearings.magazineMovie.click_Magazine)
	end
	
	--bathroom light
	function instance:InitBathroomLightItemListTaker()
		PartMouseButton(instance.appearings.bathroomLightMovie.take_Item_list)
		InitTake(instance.appearings.bathroomLightMovie.take_Item_list)
	end
	
	--flowers bowl
	function instance:ActivateKeyInFlowersBowl()
		instance.appearings.flowersBowlMovie.take_Key_for_marked_box:SetFrame(0)
		PartMouseButton(instance.appearings.flowersBowlMovie.take_Key_for_marked_box)			
		InitTake(instance.appearings.flowersBowlMovie.take_Key_for_marked_box)
	end
	function instance:DeactivateKeyInFlowersBowl()			
		instance.appearings.flowersBowlMovie.take_Key_for_marked_box:SetFrame(2)
	end
	
	--cabinet 
	function instance:ActivateItemListInMarkedBox()
		PartMouseButton(instance.appearings.cabinetMovie.center.take_Item_list)			
		InitTake(instance.appearings.cabinetMovie.center.take_Item_list)
	end
	function instance:DeactivateItemListInMarkedBox()			
		instance.appearings.cabinetMovie.center.take_Item_list:SetFrame(2)
	end
	
	function instance:ActivateCenterPart()
		PartMouseButton(instance.appearings.cabinetMovie.center.use_Cabinet_1_part)
		InitUL(instance.appearings.cabinetMovie.center.use_Cabinet_1_part)
	end
	function instance:ActivateBottomPart()
		PartMouseButton(instance.appearings.cabinetMovie.bottom.use_Cabinet_2_part)
		InitUL(instance.appearings.cabinetMovie.bottom.use_Cabinet_2_part)
	end
	function instance:ActivateCenterNail()
		PartMouseButton(instance.appearings.cabinetMovie.center.use_Nail_left)
		InitUL(instance.appearings.cabinetMovie.center.use_Nail_left)
	end
	function instance:ActivateBottomNail()
		PartMouseButton(instance.appearings.cabinetMovie.bottom.use_Nail_right)
		InitUL(instance.appearings.cabinetMovie.bottom.use_Nail_right)
	end
	function instance:ActivateCenterAndLeftAndRight()
		PartMouseButton(instance.appearings.cabinetMovie.use_Key_for_marked_box)
		InitUL(instance.appearings.cabinetMovie.use_Key_for_marked_box)
		
		PartMouseButton(instance.appearings.cabinetMovie.click_bottom)
		InitClick(instance.appearings.cabinetMovie.click_bottom)
		
		--PartMouseButton(instance.appearings.cabinetMovie.click_Cabinet_right)
		--InitClick(instance.appearings.cabinetMovie.click_Cabinet_right)
	end
	
	--entangled film
	function instance:ActivateReelWithFilm()
		PartMouseButton(instance.appearings.entangledFilmMovie.take_Reel_with_film)			
		InitTake(instance.appearings.entangledFilmMovie.take_Reel_with_film)
	end
	--notebook
	function instance:ActivateWritingInNotepad()
		PartMouseButton(instance.appearings.notebookMovie.use_Pen)
		InitUL(instance.appearings.notebookMovie.use_Pen)
	end
	function instance:ActivateItemListInNotebook()
		PartMouseButton(instance.appearings.notebookMovie.take_Item_list)			
		InitTake(instance.appearings.notebookMovie.take_Item_list)
	end
	--projector
	function instance:DeactivateProjectorReel()			
		instance.appearings.projectorMovie.use_Reel_with_film:SetFrame(1)
	end
	function instance:DeactivateProjectorKnob()			
		instance.appearings.projectorMovie.use_Knob:SetFrame(1)
	end
	function instance:ActivateProjector()			
		PartMouseButton(instance.appearings.projectorMovie.click_Projector)
		InitClick(instance.appearings.projectorMovie.click_Projector)
	end
	--notepad
	function instance:ActivateUsingSharpenedPencil()
		PartMouseButton(instance.appearings.notepadInDeskMovie.use_Sharpened_pencil)
		InitUL(instance.appearings.notepadInDeskMovie.use_Sharpened_pencil)
	end
	
	--aquarium
	function instance:ActivateCoin()
		PartMouseButton(instance.appearings.aquariumMovie.take_Coin)			
		InitTake(instance.appearings.aquariumMovie.take_Coin)
	end
	--gumball machine
	function instance:ActivateGem()
		PartMouseButton(instance.appearings.gumballMachineMovie.take_Gem)			
		InitTake(instance.appearings.gumballMachineMovie.take_Gem)
	end
	--old watch
	function instance:ActivateItemListInOldWatch()
		PartMouseButton(instance.appearings.oldWatchMovie.take_Item_list)			
		InitTake(instance.appearings.oldWatchMovie.take_Item_list)
	end
	function instance:ActivateOldWatchOpener()
		PartMouseButton(instance.appearings.oldWatchMovie.click_Old_watch)			
		InitClick(instance.appearings.oldWatchMovie.click_Old_watch)
	end
	--unsharpened pencil
	function instance:ActivateSharpenedPencil()
		PartMouseButton(instance.appearings.unsharpenedPencilMovie.take_Sharpened_pencil)			
		InitTake(instance.appearings.unsharpenedPencilMovie.take_Sharpened_pencil)
	end
	--fax
	function instance:ActivateItemListInFax()
		PartMouseButton(instance.appearings.faxInDeskMovie.take_Item_list)			
		InitTake(instance.appearings.faxInDeskMovie.take_Item_list)
	end
	function instance:ActivateFaxButton()
		PartMouseButton(instance.appearings.faxInDeskMovie.click_Fax_button)			
		InitClick(instance.appearings.faxInDeskMovie.click_Fax_button)
	end
	--piggy bank
	function instance:ActivateCoinInPiggyBank()
		PartMouseButton(instance.appearings.piggyBankMovie.take_Coin_for_jukebox)			
		InitTake(instance.appearings.piggyBankMovie.take_Coin_for_jukebox)
	end
	--jukebox
	function instance:ActivateItemListInJukebox()
		PartMouseButton(instance.appearings.jukeboxMovie.take_Item_list)			
		InitTake(instance.appearings.jukeboxMovie.take_Item_list)
	end
	function instance:ActivateJukeboxButtons()
		local keypadObj = instance.appearings.jukeboxMovie.buttons
		
		StickMouseButton(keypadObj.key_1)
		StickMouseButton(keypadObj.key_2)
		StickMouseButton(keypadObj.key_3)
		StickMouseButton(keypadObj.key_4)
		StickMouseButton(keypadObj.key_5)
		StickMouseButton(keypadObj.key_6)
		StickMouseButton(keypadObj.key_7)
		StickMouseButton(keypadObj.key_8)
		StickMouseButton(keypadObj.key_9)
		StickMouseButton(keypadObj.key_10)
		StickMouseButton(keypadObj.key_11)
		StickMouseButton(keypadObj.key_12)
		StickMouseButton(keypadObj.key_13)
		StickMouseButton(keypadObj.key_14)
		StickMouseButton(keypadObj.key_15)
		

		InitKeyButton(keypadObj.key_1)
		InitKeyButton(keypadObj.key_2)
		InitKeyButton(keypadObj.key_3)
		InitKeyButton(keypadObj.key_4)
		InitKeyButton(keypadObj.key_5)
		InitKeyButton(keypadObj.key_6)
		InitKeyButton(keypadObj.key_7)
		InitKeyButton(keypadObj.key_8)
		InitKeyButton(keypadObj.key_9)
		InitKeyButton(keypadObj.key_10)
		InitKeyButton(keypadObj.key_11)
		InitKeyButton(keypadObj.key_12)
		InitKeyButton(keypadObj.key_13)
		InitKeyButton(keypadObj.key_14)
		InitKeyButton(keypadObj.key_15)

	end
	
	function instance:UnstickJukeboxButton(pos)
		local screenObj = instance.appearings.jukeboxMovie.buttons
		local obj
		if (pos == 1) then
			obj = screenObj.key_1
		elseif (pos == 2) then
			obj = screenObj.key_2
		elseif (pos == 3) then
			obj = screenObj.key_3
		elseif (pos == 4) then
			obj = screenObj.key_4
		elseif (pos == 5) then
			obj = screenObj.key_5
		elseif (pos == 6) then
			obj = screenObj.key_6
		elseif (pos == 7) then
			obj = screenObj.key_7
		elseif (pos == 8) then
			obj = screenObj.key_8
		elseif (pos == 9) then
			obj = screenObj.key_9
		elseif (pos == 10) then
			obj = screenObj.key_10
		elseif (pos == 11) then
			obj = screenObj.key_11
		elseif (pos == 12) then
			obj = screenObj.key_12
		elseif (pos == 13) then
			obj = screenObj.key_13
		elseif (pos == 14) then
			obj = screenObj.key_14
		elseif (pos == 15) then
			obj = screenObj.key_15
		end
		
		obj:UnStick( )	
	end
	
	--heartshaped mug
	function instance:ActivateChisel()
		PartMouseButton(instance.appearings.heartShapedMugMovie.take_Chisel)			
		InitTake(instance.appearings.heartShapedMugMovie.take_Chisel)
	end
	--brick
	function instance:ActivateItemListInBrick()
		PartMouseButton(instance.appearings.brickMovie.take_Item_list)			
		InitTake(instance.appearings.brickMovie.take_Item_list)
	end
	function instance:ActivateBrickRemover()
		PartMouseButton(instance.appearings.brickMovie.use_Suction_cup)
		InitUL(instance.appearings.brickMovie.use_Suction_cup)
	end
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	

	
	-- VV click items
	function InitClick( obj )
		--log("InitClick " .. obj:GetName())
		function obj:Pressed( )		
			--log("InitClick Pressed " .. obj:GetName())
			instance:ScreenEvent( "Click_" .. obj:GetName(), "ISpyPanel_esc" )
		end
		function obj:UnPressed( )
			--log("InitClick UnPressed " .. obj:GetName())
			--instance:ScreenEvent( "Click_" .. obj:GetName(), "ISpyPanel_esc" )
		end
	end
	
	function DeInitClick( obj )
		function obj:Pressed( )		

		end
		function obj:UnPressed( )

		end
	end
	
	function InitTake( obj )
		--log("InitTake " .. obj:GetName())
		function obj:Pressed( )		
			--log("InitTake Pressed " .. obj:GetName())
			instance:ScreenEvent( "Take_" .. obj:GetName(), "ISpyPanel_esc" )
		end
		function obj:UnPressed( )
			--log("InitTake UnPressed " .. obj:GetName())
			--instance:ScreenEvent( "Take_" .. obj:GetName(), "ISpyPanel_esc" )
		end
	end
	
	function DeInitTake( obj )
		function obj:Pressed( )		

		end
		function obj:UnPressed( )

		end
	end
	
	function InitUL( obj )
		function obj:Pressed( )		
			--log("InitUL Pressed")
		end
		function obj:UnPressed( )
			--log("InitUL UnPressed")
			instance:ScreenEvent( "ItemUL_" .. obj:GetName(), "ISpyPanel_esc" )
		end
	end
	
	function InitAL( obj )
		function obj:Pressed( )		
			--log("InitAL Pressed")
		end
		function obj:UnPressed( )
			--log("InitAL UnPressed")
			--lastPutted = obj
			instance:ScreenEvent( "ItemAL_" .. obj:GetName(), "ISpyPanel_esc" )
		end
	end
	
	
	
	
	function InitCL( obj, no )
		function obj:Pressed( )		
			if(lastItemCellNo == -1) then
				lastFrame = obj.items:GetFrame()
				lastItemCellNo = no
				--log("InitCL Pressed " .. lastItemCellNo .. " " .. lastFrame)
				obj.items:SetFrame(0);
				instance:ScreenEvent( "ItemCL_" .. no, "ISpyPanel_esc" )
			end
		end
		function obj:UnPressed( )		
			if(lastItemCellNo == no) then		
				obj.items:SetFrame(lastFrame);
				--log("InitCL UnPressed " .. no .. " " .. lastFrame)
				lastFrame = 0
				lastItemCellNo = -1
				instance:ScreenEvent( "UnpressedItemCL_" .. no, "ISpyPanel_esc" )
			end
		end
	end
	
	function instance:CloseSlidersPopup()
		if(instance.swapDir ~= -1) then
		
			log("CloseSlidersPopup")
		
			local firstSwapX = instance.firstSwap.prevX
			local firstSwapY = instance.firstSwap.prevY
			
			local secondSwapX = instance.secondSwap.prevX
			local secondSwapY = instance.secondSwap.prevY
			
			instance.firstSwap:SetX(secondSwapX)
			instance.firstSwap:SetY(secondSwapY)
			instance.firstSwap:Stand()
			
			instance.secondSwap:SetX(firstSwapX)
			instance.secondSwap:SetY(firstSwapY)
			instance.secondSwap:Stand()
			
			instance:ScreenEvent( "Swap_" .. instance.firstSwap:GetName() .. "_" .. instance.secondSwap:GetName(), "ISpyPanel_esc" )
				
			instance.firstSwap = nil
			instance.secondSwap = nil
				
			instance.swapDir = -1
		
		end
	end
	
	
	function instance:StandSlidersCell(no)
		local children = instance.appearings.slidersMovie.sliders_field:GetChildren()
		 
		for i in pairs( children ) do
			if(children[i]:GetName() == ("cell_"..no)) then
				children[no]:Stand()
			end
		end
	end
	
	
	
	function InitSliderCell( obj, no )
		obj:Stand()

		function obj:Pressed( )
			if(instance.lastSlidersCellNo == -1 and instance.swapDir == -1) then 
				instance.lastSlidersCellNo = no
			end
		end
		
		function obj:UnPressed( )	
			if(instance.lastSlidersCellNo == no) then		
				instance.lastSlidersCellNo = -1
			end
		end
		
		function obj:Swap( dir )
			--log("obj:Swap( dir ) dir == " .. instance.swapDir)
			if(instance.swapDir ~= -1) then
				return
			end
			local mPrevX = obj.prevX
			local mPrevY = obj.prevY
			
			--log("mPrevX == " .. mPrevX .. " mPrevY == " .. mPrevY)
			
			local swapObj = nil
			
			local children = instance.appearings.slidersMovie.sliders_field:GetChildren() 
			for i in pairs( children ) do
				if(dir == 0) then
					if(children[ i ]:GetX() == mPrevX + 90 and children[ i ]:GetY() == mPrevY) then
						swapObj = children[ i ]
						--obj:SetY(mPrevY)
					end
				elseif (dir == 1) then
					if(children[ i ]:GetX() == mPrevX - 90 and children[ i ]:GetY() == mPrevY) then
						swapObj = children[ i ]
						--obj:SetY(mPrevY)
					end
				elseif (dir == 2) then
					if(children[ i ]:GetY() == mPrevY + 90 and children[ i ]:GetX() == mPrevX) then
						swapObj = children[ i ]
						--obj:SetX(mPrevX)
					end
				elseif (dir == 3) then
					if(children[ i ]:GetY() == mPrevY - 90 and children[ i ]:GetX() == mPrevX) then
						swapObj = children[ i ]
						--obj:SetX(mPrevX)
					end
				end
			end
			
			if(swapObj == nil) then
				log("!!!!!!!!!!!!!!!!!!!!!!!!! swapObj == nil !!!!!!!!!!!!!!!!!!!!!!!!!")
				return
			end
			
			instance.firstSwap = obj
			instance.secondSwap = swapObj
			instance.swapDir = dir
			
			
			
			log("swap " .. instance.firstSwap:GetName() .. "  " .. instance.secondSwap:GetName())					
		end
	end
	
	
	
	function instance:Swaping( )
		if(instance.swapDir ~= -1) then
					
			local firstSwapX = instance.firstSwap.prevX
			local firstSwapY = instance.firstSwap.prevY
			
			local secondSwapX = instance.secondSwap.prevX
			local secondSwapY = instance.secondSwap.prevY
			
			local firstEnd = false
			local secondEnd = false
			
			local step = 6
	
			if(instance.swapDir == 0) then
				instance:BalanceY(step)
				if(instance.firstSwap:GetX() < secondSwapX) then
					instance.firstSwap:SetX(instance.firstSwap:GetX() + step)
				else
					instance.firstSwap:SetX(secondSwapX)
					firstEnd = true
				end
				if(instance.secondSwap:GetX() > firstSwapX) then
					instance.secondSwap:SetX(instance.secondSwap:GetX() - step)
				else
					instance.secondSwap:SetX(firstSwapX)
					secondEnd = true
				end
			elseif (instance.swapDir == 1) then
				instance:BalanceY(step)
				if(instance.firstSwap:GetX() > secondSwapX) then
					instance.firstSwap:SetX(instance.firstSwap:GetX() - step)
				else
					instance.firstSwap:SetX(secondSwapX)
					firstEnd = true
				end
				if(instance.secondSwap:GetX() < firstSwapX) then
					instance.secondSwap:SetX(instance.secondSwap:GetX() + step)
				else
					instance.secondSwap:SetX(firstSwapX)
					secondEnd = true
				end
			elseif (instance.swapDir == 2) then
				instance:BalanceX(step)
				if(instance.firstSwap:GetY() < secondSwapY) then
					instance.firstSwap:SetY(instance.firstSwap:GetY() + step)
				else
					instance.firstSwap:SetY(secondSwapY)
					firstEnd = true
				end
				if(instance.secondSwap:GetY() > firstSwapY) then
					instance.secondSwap:SetY(instance.secondSwap:GetY() - step)
				else
					instance.secondSwap:SetY(firstSwapY)
					secondEnd = true
				end
			elseif (instance.swapDir == 3) then
				instance:BalanceX(step)
				if(instance.firstSwap:GetY() > secondSwapY) then
					instance.firstSwap:SetY(instance.firstSwap:GetY() - step)
				else
					instance.firstSwap:SetY(secondSwapY)
					firstEnd = true
				end
				if(instance.secondSwap:GetY() < firstSwapY) then
					instance.secondSwap:SetY(instance.secondSwap:GetY() + step)
				else
					instance.secondSwap:SetY(firstSwapY)
					secondEnd = true
				end
			end
			
			if(firstEnd == true and secondEnd == true) then
				instance.firstSwap:Stand()
				instance.secondSwap:Stand()
				
				instance:ScreenEvent( "Swap_" .. instance.firstSwap:GetName() .. "_" .. instance.secondSwap:GetName(), "ISpyPanel_esc" )
				
				instance.firstSwap = nil
				instance.secondSwap = nil
				
				instance.swapDir = -1
				
				log("STOP SWAPING")
			end
		end
	end
	
	function instance:BalanceY(step)
		local firstSwapY = instance.firstSwap.prevY
		if(instance.firstSwap:GetY() ~= firstSwapY) then
			if(instance.firstSwap:GetY() > firstSwapY + 2*step) then
				instance.firstSwap:SetY(instance.firstSwap:GetY() - step)
			elseif(instance.firstSwap:GetY() < firstSwapY - 2*step) then
				instance.firstSwap:SetY(instance.firstSwap:GetY() + step)
			else 
				instance.firstSwap:SetY(firstSwapY)
			end
		end
	end
	function instance:BalanceX(step)
		local firstSwapX = instance.firstSwap.prevX
		if(instance.firstSwap:GetX() ~= firstSwapX) then
			if(instance.firstSwap:GetX() > firstSwapX + 2*step) then
				instance.firstSwap:SetX(instance.firstSwap:GetX() - step)
			elseif(instance.firstSwap:GetX() < firstSwapX - 2*step) then
				instance.firstSwap:SetX(instance.firstSwap:GetX() + step)
			else 
				instance.firstSwap:SetX(firstSwapX)
			end
		end
	end
	
	function instance:OnUpdate( )
		self:RunStateMachine()
	end	
	
	function instance:OnRunState( i_eState )
		-- dont update the UI if the board it reflects has been lost
		if ( self.Lost == 1 ) then
			return
		end
	
		local fTimeElapsed = self:GetTimeInState()
		
		self:UpdateIrps( 1 / 60 )
		
--		local oCupidArrow = self.m_oCupid.cupid.arrow
--		if ( fTimeElapsed > self.m_fArrowFadeInOn 
--				and oCupidArrow ~= nil
--				and oCupidArrow.m_fOpacity ~= nil 
--				and oCupidArrow:GetVisible() ) then
--			oCupidArrow.m_fOpacity = math.min( 1, oCupidArrow.m_fOpacity + 0.02 )
--			oCupidArrow:SetOpacity( oCupidArrow.m_fOpacity )
--		end
	
		if ( i_eState == ISpyPanel_esc.State_Playing ) then

			self:Swaping()
			
			self:UpdateClock()

			local minutes = math.floor( instance.Time / 60 )
			local seconds = math.floor( math.mod( instance.Time , 60 ) )
			
			if( minutes < 0 or seconds < 0 ) then
				minutes = 0
				seconds = 0	
			end
			
			self.m_oMinutesText:SetText( tostring( minutes ) )
			
			local secondsStr = tostring( seconds );
			
			if( seconds <= 9 ) then
				secondsStr = "0"..seconds;
			end
			
			if( lastsecond ~= seconds ) then
				lastsecond = seconds
				clockframe = clockframe + 1
				
				if( clockframe >= 60 ) then
					clockframe = 1;
				end
				--clockHand:SetFrame( clockframe )
			end

			self.m_oSecondsText:SetText( secondsStr )
			
			if ( self.Relaxed == false ) then
				if (instance.flashTime > 0) then
					if (math.mod(instance.flashTime, 20) <= 10)  then
						self.m_oMinutesText:SetColor( 0xff0000 )
						self.m_oSecondsText:SetColor( 0xff0000 )
					else
						self.m_oMinutesText:SetColor( 0x0A323C )
						self.m_oSecondsText:SetColor( 0x0A323C )
					end
					instance.flashTime = self.flashTime - 1
				else
					self.m_oMinutesText:SetColor( 0x0A323C )
					self.m_oSecondsText:SetColor( 0x0A323C )
				end
			end
			
			instance:updateUI( )
		
		end

	end
	
	
	function instance:updateUI( )
		for i in pairs( instance.internalList ) do
			local item = instance.internalList[i]
			
			local listItem = instance.list[ "findList"..(i-1) ]
			local iColor = 0xffffff
			if ( listItem ~= nil ) then
				if( item.status == true ) then
					listItem:SetFrame( 1 )
					iColor = 0xffffff
				else
					listItem:SetFrame( 0 )
					if ( item.super == false ) then
						iColor = 0xffffff
					else
						iColor = 0x0000ff
					end
				end
				
				listItem.text:SetColor( iColor )
				listItem.text:SetText( item.string )
				
			end
		end
		
--		if( instance.hintcount <= 0 ) then
--			if( cupid.cupid.arrow:GetVisible( ) == true ) then
--				cupid.cupid.arrow:SetVisible( false );		
--			end
--		else
--			if( cupid.cupid.arrow:GetVisible( ) == false ) then
--				cupid.cupid.arrow:SetVisible( true );	
--			end
--		end
	
		if ( self.m_bLockHintCounter == false ) then
			self.m_oHintCounterText:SetFrame( tonumber( instance.hintcount ) )
		end
		
		--//itemCount:SetText( "" .. instance.totalItems )
		
	end
	
	function instance:HintHint( )	
		if( instance.hintcount > 0 
				and self.m_oHintButton._buttonGlint ~= nil ) then
			-- a slower version of the glint (so that the user sees it!)
			local oIrp = self:CreateIrp( 0, self.m_oHintButton._buttonGlint:GetNumFrames() - 1, 2, CIrpFuncs.mLinear, 0.75 )
			oIrp.m_oHookContext = self.m_oHintButton

			oIrp.mOnChange = function( i_oIrp, i_oContext )
				if ( i_oContext._buttonGlint ~= nil ) then
					i_oContext._buttonGlint:SetFrame( math.floor( i_oIrp:GetVal() ) )
				end
			end

			oIrp.mOnComplete = function( i_oIrp, i_oContext )
				if ( i_oContext._buttonGlint ~= nil ) then
					i_oContext._buttonGlint:SetFrame( 0 )
				end
			end
			
--			local hintie = cupid.cupid.arrow
			hintie:CreateParticleEffect( "hinthint", "", hintie:GetWidth()/2, hintie:GetHeight()/2 )
		end
	end
end

if ( g_bReloading == nil or g_bReloading == false ) then
	ISpyPanel_esc:Create( )
end
