--------------------------------------------------------------------------------
--  File     : Witch's_house_int_Witch_book_MG
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
time_rotate_key = 0.5
time_fade_book = 0.5
--------------------------------------------------------------------------------
function OnSerialize(system)
	book_opening = system:Serialize(book_opening)
	
	EndOffBookOpening()
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	book_opening = 0

	stars:Show("M_KEY", "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()

	if itemName == "BACKGROUND" then				--книга
		if room:GetItem("M_KEY"):IsVisible() then 
			comment:Add("CMT_Book_Player")
			return true
		end
	
	elseif itemName == "M_KEY" then					--место для ключа
		comment:Add("CMT_Key_Player")
		return true
	end	
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)							
	local itemName = item:GetName()

	if itemName == "M_KEY" then						--ключ на книгу 
		if cursor:IsItemNameEqual("WITCH_BOOK_KEY") then
			cursor:RemoveItem()
			item:Hide()
			local key = room:GetItem("KEY")
			key:Show()
			key.effects:PlayRotate(90, time_rotate_key)
			book_opening = 1
			
			cstore:SetKey("Hollow_HO", 2)
			room.sfx:Play("install_key")	--звук "вставляем ключик в книгу"
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)
	
	local key = room:GetItem("KEY")
	
	if (book_opening == 1) and not(key.effects:IsPlayingRotate()) then
		HideFade(key, time_fade_book)
		ShowFade(room:GetItem("BOOK_OPENED"), time_fade_book)
		ShowFade(room:GetItem("RECIPE_TITLE"), time_fade_book)
		ShowFade(room:GetItem("RECIPE"), time_fade_book)
		book_opening = 2	
		room.sfx:Play("open_book")		--звук "открываем книгу"
	end
	
	if (book_opening == 2) and not(key.effects:IsPlayingColor()) then
		key:Hide()
		book_opening = 3	
		journal:Add("potion2")
		if cstore:IsKeyExists("Close_up_Well") then
			journal:CompleteTask("p2_acorn")
		end
	end
	
end
--------------------------------------------------------------------------------
function OnExit()
	EndOffBookOpening()
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Cheat_WitchHouseIntBook(value)				--выполнить все в локации  !!!
	room:GetItem("M_KEY"):Hide()
	room:GetItem("KEY"):Hide()
	room:GetItem("BOOK_OPENED"):Show()
	room:GetItem("RECIPE_TITLE"):Show()
	room:GetItem("RECIPE"):Show()
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
--------------------------------------------------------------------------------
function EndOffBookOpening()
	if (book_opening == 1) or (book_opening == 2) then
		journal:Add("potion2")
		if cstore:IsKeyExists("Close_up_Well") then
			journal:CompleteTask("p2_acorn")
		end
		
		local key = room:GetItem("KEY")
		local book = room:GetItem("BOOK_OPENED")
		local recipe_title = room:GetItem("RECIPE_TITLE")
		local recipe = room:GetItem("RECIPE")
		
		if book_opening == 1 then
			key.effects:StopRotate()
			book:Show()
			recipe_title:Show()
			recipe:Show()
		else
			book.effects:StopColor()
			local color = book:GetColor()
			color:SetNormalAlpha(1)
			book:SetColor(color)
			
			recipe_title.effects:StopColor()
			local color1 = recipe_title:GetColor()
			color1:SetNormalAlpha(1)
			recipe_title:SetColor(color1)
			
			recipe.effects:StopColor()
			local color2 = recipe_title:GetColor()
			color2:SetNormalAlpha(1)
			recipe:SetColor(color2)
		end
		key:Hide()
		
		book_opening = 3
	end
end