--------------------------------------------------------------------------------
--  File     : Hut_2nd_floor_Cage
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
LOC_2FLOOR				= "Hut_2nd_floor"

I_DOOR_CLOSED			= "DOOR_CLOSED"	--закрытая дверь клетки
I_DOOR_OPENED			= "DOOR_OPENED"	--открытая дверь клетки
I_MIRROR				= "MIRROR"		--зеркало
I_WIRE					= "WIRE"		--проволока
I_THREAD				= "THREAD"		--нитки
I_THREAD_CUT			= "THREAD_CUT"	--перерезанные нитки
I_KNIFE					= "KNIFE_Q"		--нож

V_KNIFE					= "CAGE_KNIFE"	--анимация перерезания ножом ниток
--------------------------------------------------------------------------------
time_thread = 0.5
--------------------------------------------------------------------------------
function OnFirstEntry()
	cstore:SetKey("Close_up_Cage", 0)
	
	room:GetItem(I_MIRROR):Disable()
	room:GetItem(I_THREAD):Disable()
	
	stars:Show(I_MIRROR, "4")
	stars:Show(I_WIRE, "4")
end

--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	chapter:ExitCloseup()
	return true
end

--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == I_WIRE  then					--проволока
		room:GetItem(I_DOOR_CLOSED):SetCursorForNormal("hand")
		cstore:SetKey("Close_up_Cage", 1)
		pick_panel:AddItems(itemName, false)
		room.sfx:Play ("take_wire")				--звук "скручиваем проволоку"
		return true
	elseif itemName == I_DOOR_CLOSED  then		--дверь клетки
		if room:GetItem(I_WIRE):IsVisible() then
			comment:Add("CMT_Door_Player")
		else
			item:Hide()
			room:GetItem(I_DOOR_OPENED):Show()
			room:GetItem(I_THREAD):Enable()
			cstore:SetKey("Close_up_Cage", 2)
			room.sfx:Play ("open_cage")			--звук "открываем дверцу"
			return true
		end
	elseif itemName == I_THREAD  then			--нитки, которыми привязано зеркало
		if item:IsEnabled() then
			comment:Add("CMT_Thread_Player")
			return true	
		end
	elseif itemName == I_MIRROR  then			--зеркало
		if item:IsEnabled() then
			room:GetItem(I_THREAD_CUT):Hide()
			cstore:SetKey("Close_up_Cage", 3)
			pick_panel:AddItems(itemName, true)
			room:MarkCompleted()
		else
			comment:Add("CMT_Mirror_Player")
		end
		return true	
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)				
	local itemName = item:GetName()

	if cursor:IsItemNameEqual(I_KNIFE) then				--нож на нитки
		if (itemName == I_THREAD) and (item:IsEnabled()) then
			inventory.alt:AddItemFromCursor()
			
			local videoKnife = room:GetItem(V_KNIFE)
			videoKnife:Show()
			videoKnife:PlayAnimation()
			room:AddCtrlAnimation(V_KNIFE, -1)
			room.sfx:Play ("cut_threads")				--звук "разрезаем ножом нитки на зеркале"
			return true
		end
	end
end	
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)

	local videoKnife = room:GetItem(V_KNIFE)
	local thread_cut = room:GetItem(I_THREAD_CUT)
	local thread = room:GetItem(I_THREAD)
	
	if videoKnife:IsVisible() then
		if videoKnife:IsAnimPlaying () then
			if not(thread_cut:IsVisible()) then
				local frame = room:GetVideoFrame(videoKnife:GetName())
			
				if frame >= 20 then
					thread_cut:Show()
					local color = thread_cut:GetColor()
					local color1 = thread_cut:GetColor()
					color1:SetNormalAlpha(0)
					thread_cut:SetColor(color1)
					thread_cut.effects:PlayColor(color, time_thread)
		
					local color2 = thread:GetColor()
					color2:SetNormalAlpha(0)
					thread.effects:PlayColor(color2, time_thread)
				end
			end
		end
	end
	
	if not(thread.effects:IsPlayingColor ()) 
	   and (thread_cut:IsVisible()) 
	   and (thread:IsVisible()) then
		thread:Hide()
		room:GetItem(I_MIRROR):Enable()
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации разрезания ниток
function OnAnimationComplete_CAGE_KNIFE(forced)
	room:GetItem(V_KNIFE):Hide()
	inventory:AddInventoryItem(inventory.alt:GetItem(I_KNIFE))
	inventory.alt:EraseOneItemFromCell(I_KNIFE)
	
	if forced == true then
		local thread_cut = room:GetItem(I_THREAD_CUT)
		if thread_cut:IsVisible() then
			thread_cut.effects:StopColor()
			local color = thread_cut:GetColor()
			color:SetNormalAlpha(1)
			thread_cut:SetColor(color)
		else
			thread_cut:Show()
		end
	end
end