--------------------------------------------------------------------------------
--  File     : Chapel_int_Casket
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
function OnSerialize(system)
	runes = system:Serialize(runes)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	cstore:SetKey("Close_up_ChapelBox", 0)
	
	local videoCasket = room:GetItem("CHAPEL_CASKET_OPEN")
	videoCasket:PlayAnimation()
	videoCasket:SetAnimPause (true)
	
	runes = 0
	
	stars:Show("KEYHOLE1", "4")
	stars:Show("COPPER_RING", "4")
	stars:Show("RUNE_AIR", "4")
	stars:Show("RUNE_EARTH", "4")
	stars:Show("RUNE_FIRE", "4")
	stars:Show("RUNE_WATER", "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "KEYHOLE1" then					--замочная скважина
		comment:Add("CMT_Key_Player")
		return true
	
	elseif 	itemName == "COPPER_RING" then			--медное кольцо
		pick_panel:AddItems(itemName, false)
		return true
		
	elseif 	itemName == "RUNE_AIR" then				--руна воздуха
		TakeRune(itemName)
		return true
		
	elseif 	itemName == "RUNE_EARTH" then			--руна земли
		TakeRune(itemName)
		return true
		
	elseif 	itemName == "RUNE_FIRE" then			--руна огня
		TakeRune(itemName)
		return true
		
	elseif 	itemName == "RUNE_WATER" then			--руна воды
		TakeRune(itemName)
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()

	if itemName == "KEYHOLE1" then					--ключ от шкатулки на шкатулку  
		if cursor:IsItemNameEqual("CASKET_KEY") then			
			cursor:RemoveItem()
			item:Hide()
						
			local video_unlock = room:GetItem("CHAPEL_CASKET_UNLOCK")
			video_unlock:Show()
			video_unlock:PlayAnimation()
			room:AddCtrlAnimation("CHAPEL_CASKET_UNLOCK", -1)
						
			room.sfx:Play ("open_box")	--звук "открывается шкатулка ключом"
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)
	local video_unlock = room:GetItem("CHAPEL_CASKET_UNLOCK")
	
	if video_unlock:IsVisible() then
		local local_frame = room:GetVideoFrame(video_unlock:GetName())
		
		if local_frame >= 43 then
			room:GetItem("RUNE_AIR"):Show()
			room:GetItem("RUNE_EARTH"):Show()
			room:GetItem("RUNE_FIRE"):Show()
			room:GetItem("RUNE_WATER"):Show()
			
			room:GetItem("CHAPEL_CASKET_OPEN"):SetAnimPause (false)
			room:AddCtrlAnimation("CHAPEL_CASKET_OPEN", -1)
		end
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации поворачивания ключа
function OnAnimationComplete_CHAPEL_CASKET_UNLOCK(forced)
	room:GetItem("CHAPEL_CASKET_UNLOCK"):Hide()
	room:GetItem("KEYHOLE2"):Show()
	
	if forced == true then
		room:GetItem("RUNE_AIR"):Show()
		room:GetItem("RUNE_EARTH"):Show()
		room:GetItem("RUNE_FIRE"):Show()
		room:GetItem("RUNE_WATER"):Show()
			
		room:GetItem("CHAPEL_CASKET_OPEN"):SetAnimPause (false)
		room:VideoSeekToEnd(room:GetItem("CHAPEL_CASKET_OPEN"))
		cstore:SetKey("Close_up_ChapelBox", 1)
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации открывния шкатулки
function OnAnimationComplete_CHAPEL_CASKET_OPEN(forced)
	cstore:SetKey("Close_up_ChapelBox", 1)
	
	if forced == true then
		room:VideoSeekToEnd(room:GetItem("CHAPEL_CASKET_OPEN"))
	end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function TakeRune(name)
	runes = runes + 1
	
	if runes < 4 then
		pick_panel:AddItems(name, false)
	else
		pick_panel:AddItems(name, true)
		cstore:SetKey("Close_up_ChapelBox", 2)
		room:MarkCompleted()
	end
end
