--------------------------------------------------------------------------------
--  File     : Wizard's_tower_lab_Table
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
time_map = 0.5
--------------------------------------------------------------------------------
function OnSerialize(system)
	hide_map = system:Serialize(hide_map)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	hide_map = 0 
	
	stars:Show("M_CACHE", "1")
	stars:Show("M_MAP", "4")
	stars:Show("LOCKET", "4")
	stars:Show("LOCKET_OPEN", "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if 	itemName == "M_CACHE" then
		chapter:JumpRoom("Wizard's_tower_lab_Cache_HO")
		return true
	end

	if 	itemName == "M_MAP" then						--карта
		comment:Add("CMT_Map_Player")
		return true
	end
	
	if 	itemName == "LOCKET" then						--медальон закрытый
		item:Hide()
		room:GetItem("LOCKET_OPEN"):Show()
		comment:Add("CMT_Locket1_Player")
		return true
	end
	
	if 	itemName == "LOCKET_OPEN" then					--медальон открытый
		comment:Add("CMT_Locket2_Player")
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)
	local itemName = item:GetName()
	
	if cursor:IsItemNameEqual("KNIFE_Q") then		--нож на карту
		if itemName == "M_MAP" then
			inventory.alt:AddItemFromCursor()
			item:Hide()
			cstore:SetKey("scheme_knife", true)
			room.sfx:Play ("cutting_map")	--звук "разрезаем карту"
			
			local knife_map = room:GetItem("KNIFE_MAP")
			knife_map:Show()
			knife_map:PlayAnimation()
			room:AddCtrlAnimation("KNIFE_MAP", -1)
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)

	local map = room:GetItem("MAP")
	
	if not(map.effects:IsPlayingColor()) and (hide_map == 1) then
		hide_map = 2
		map:Hide()
		room:GetItem("MAP_LIGHT"):Hide()
	end
		
	
	local knife_map = room:GetItem("KNIFE_MAP")
	local cut1 = room:GetItem("CUT1")
	local cut2 = room:GetItem("CUT2")
	local cut3 = room:GetItem("CUT3")
	local cut4 = room:GetItem("CUT4")
	
	if knife_map:IsVisible() then
		if knife_map:IsAnimPlaying () then
			local frame = room:GetVideoFrame(knife_map:GetName())
			
			--первый разрез
			if not(cut1:IsVisible()) then
				if frame >= 25 then
					ShowFade(cut1, time_map)
				end
			end
			
			--второй разрез
			if not(cut2:IsVisible()) then
				if frame >= 90 then
					ShowFade(cut2, time_map)
				end
			end
			
			--третий разрез
			if not(cut3:IsVisible()) then
				if frame >= 155 then
					ShowFade(cut3, time_map)
				end
			end
			
			--четвертый разрез
			if not(cut4:IsVisible()) then
				if frame >= 230 then
					ShowFade(cut4, time_map)
				end
			end
		end
	end
	
end
--------------------------------------------------------------------------------
-- Окончание анимации разрезания карты
function OnAnimationComplete_KNIFE_MAP(forced)
	room:GetItem("KNIFE_MAP"):Hide()
	room:GetItem("M_CACHE"):Show()
	
	if inventory.alt:IsItemExisting ("KNIFE_Q") then
		inventory.alt:EraseOneItemFromCell("KNIFE_Q")
	end
	
	local map = room:GetItem("MAP")
	local map_light = room:GetItem("MAP_LIGHT")
	
	if forced == true then
		map:Hide()
		map_light:Hide()
		AlphaItem(room:GetItem("CUT1"))
		AlphaItem(room:GetItem("CUT2"))
		AlphaItem(room:GetItem("CUT3"))
		AlphaItem(room:GetItem("CUT4"))
		
	else
		HideFade(map, time_map)
		HideFade(map_light, time_map)
		hide_map = 1
	end
end
--------------------------------------------------------------------------------
-----------------Проявление по фэйду--------------------------------------------
function ShowFade(item, time_effect)
	item:Show()
	local color = item:GetColor()
	local color1 = item:GetColor()
	color:SetNormalAlpha(1)
	color1:SetNormalAlpha(0)
	item:SetColor(color1)
	item.effects:PlayColor(color, time_effect)
end
-----------------Исчезновение по фэйду--------------------------------------------
function HideFade(item, time_effect)
	local color = item:GetColor()
	color:SetNormalAlpha(0)
	item.effects:PlayColor(color, time_effect)
end
-----------------Установить альфа = 1---------------------------------------------
function AlphaItem(item)
	item:Show()
	local color = item:GetColor()
	color:SetNormalAlpha(1)
	item:SetColor(color)
end