--------------------------------------------------------------------------------
--  File     : Tavern_ext_Well
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
function OnFirstEntry()
	stars:Show("ACORN", "4")
	stars:Show("BUCKET", "4")
	stars:Show("M_HANDLE", "4")
	stars:Show("M_SCULL_H_JAW", "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_SCULL_H_JAW" then				--дно колодца
		comment:Add("CMT_Gem_Player")
		return true

	elseif itemName == "BUCKET" then				--ведро
		comment:Add("CMT_Bucket_Player")
		return true	

	elseif itemName == "M_HANDLE" then				--ручка колодца
		item:Hide()
		room:GetItem("HANDLE"):Hide()
		room:GetItem("M_SCULL_H_JAW"):Hide()
		room:GetItem("ROLLER_ROPE"):Hide()
				
		local videoHandle = room:GetItem("WELL_HANDLE")
		videoHandle:Show()
		videoHandle:PlayAnimation()
		room:AddCtrlAnimation("WELL_HANDLE", -1)
		
		local videoRoller = room:GetItem("WELL_ROLLER")
		videoRoller:Show()
		videoRoller:PlayAnimation()
		room:AddCtrlAnimation("WELL_ROLLER", -1)
		
		room.sfx:Play ("well_animation")			--звук "спускаемаем ведро, поднимаем ведро"
		return true	
	
	elseif itemName == "ACORN" then					--ингридиент
		cstore:SetKey("Close_up_Well", true)
		if cstore:GetKey("Hollow_HO") >= 2 then
			journal:CompleteTask("p2_acorn")
		end
		pick_panel:AddItems(itemName, true)
		room:MarkCompleted()
		return
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)						 				
	local itemName = item:GetName()

	if (itemName == "BUCKET") or 					--веревка на ведро 
	   (itemName == "ROLLER") then					
		if cursor:IsItemNameEqual("ROPE") then		
			cursor:RemoveItem()
			room:GetItem("BUCKET"):Hide()
			room:GetItem("ROLLER"):Hide()
			room:GetItem("ROLLER_ROPE"):Show()
			room:GetItem("M_HANDLE"):Show()
			cstore:SetKey("Bucket_Roller", true)
			room.sfx:Play ("tie_rope")				--звук "привязываем веревку к колодцу"
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate(elapsedTime)
	if not(room:GetItem("HANDLE"):IsVisible()) then					
		local acorn = room:GetItem("ACORN")
		
		if not(acorn:IsVisible()) and not(cstore:IsKeyExists("Close_up_Well")) then
			local videoRoller = room:GetItem("WELL_ROLLER")
			local frame = room:GetVideoFrame(videoRoller:GetName())
		
			if frame >= 216 then
				acorn:Show()
			end
		end		
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации поворачивания ручки
function OnAnimationComplete_WELL_HANDLE(forced)
	
	if forced == true then
		room:VideoSeekToEnd(room:GetItem("WELL_HANDLE"))
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации поднятия ведра
function OnAnimationComplete_WELL_ROLLER(forced)

	if forced == true then
		room:VideoSeekToEnd(room:GetItem("WELL_ROLLER"))
		room:GetItem("ACORN"):Show()
	end
end