function show_statistics()
	local t = "lose_statistics_window"
	if g_GameVars.WinLevel then t = "win_statistics_window" end

	local stat = CLuaWindow(g_Loader:GetTemplate(t), g_Lua)
	stat:SetModal(true)
	stat:SetLayer(g_Game:GetLayer()+2)
	g_State:AddWidget(stat)
	stat:MoveTo((g_State:GetRect().mWidth - stat:GetRect().mWidth)/2, 
		(g_State:GetRect().mHeight - stat:GetRect().mHeight)/2)

	if not g_GameVars.WinLevel then
		if g_Player:GetLives()>0 then
			local w = ConvertToCLabelWidget(stat:GetWidget("lives_left"))
			if w then w:SetText(string.format(w:GetText(), g_Player:GetLives())) end
		end
	end

	CreateDarkenEffect(g_State, g_State:GetRect(), g_Game:GetLayer()+1, 
		CColor(0, 0, 0, 0), CColor(0, 0, 0, get_number("level", "dark_alpha")), 
		get_number("level", "dark_time"), -1, "game_darken_effect")
end

-------------------------------------------------------------------------------------------------

function statistics_on_load_template(window)
	g_GameVars.statistics = {}
	g_GameVars.statistics.time = 0
	g_GameVars.statistics.next_time = 0
end

-------------------------------------------------------------------------------------------------

local function text_width(text, font)
	local f = CFont(font)
	return f:GetFont():StringWidth(text)
end

local function text_height(font)
	local f = CFont(font)
	return f:GetFont():GetHeight()
end

local function show_text_effect(w)
	local from = CFloatVector(0, 0)
	from.x = w:GetRect().mX - text_width(w:GetText(), "font_number")/2
	from.y = w:GetRect().mY + text_height("font_number")/1.5
	FlashEffect(w:GetParent(), from)
end

-------------------------------------------------------------------------------------------------

function statistics_update(window, dt)
	local step_time = 500
	g_GameVars.statistics.time = g_GameVars.statistics.time + dt

	if (not g_GameVars.statistics.global) and g_GameVars.statistics.time > g_GameVars.statistics.next_time then
		local w = ConvertToCLabelWidget(window:GetWidget("global_score"))
		if w then
			w:SetText(g_Player:GetGlobalScore().."")
		end

		play_sound("sound_statistics_value")

		g_GameVars.statistics.global = true
		g_GameVars.statistics.next_time = g_GameVars.statistics.next_time + step_time


	elseif (not g_GameVars.statistics.satisfaction) and g_GameVars.statistics.time > g_GameVars.statistics.next_time then

		if not g_GameVars.statistics.eye then
			local sat = (g_GameVars.middle_sat) / 10.0
			if sat - math.floor(sat) >= 0.5 then sat = math.floor(sat) + 1
			else sat = math.floor(sat) end
			log("middle sat="..g_GameVars.middle_sat)
			log("sat = "..sat)
			g_GameVars.statistics.eye = 1
			g_GameVars.statistics.eye_sat = sat

			if g_GameVars.statistics.eye_sat<=0 then 
				g_GameVars.statistics.satisfaction = true
				return
			end
		end

		local w = ConvertToCImageWidget(window:GetWidget("eye"..g_GameVars.statistics.eye))
		if w then
			if g_GameVars.statistics.eye_sat>=2 then
				w:SetImage("image_game_gui_reeye")
			elseif g_GameVars.statistics.eye_sat>=1 then
				w:SetImage("image_game_gui_reeye_half")
			end
			w:GetImage():RandomizeTime()
			play_sound("sound_statistics_reeye")
		end
		g_GameVars.statistics.eye_sat = g_GameVars.statistics.eye_sat - 2
		g_GameVars.statistics.eye = g_GameVars.statistics.eye + 1

		if g_GameVars.statistics.eye>5 or  g_GameVars.statistics.eye_sat<=0 then 
			g_GameVars.statistics.satisfaction = true
			g_GameVars.statistics.next_time = g_GameVars.statistics.next_time + step_time
		else
			g_GameVars.statistics.next_time = g_GameVars.statistics.next_time + 100
		end

	elseif (not g_GameVars.statistics.goal) and g_GameVars.statistics.time > g_GameVars.statistics.next_time then
		local w = ConvertToCLabelWidget(window:GetWidget("goal_score"))
		if w then
			w:SetText(g_GameVars.goal_score..'')
			
			show_text_effect(w)
		end
		play_sound("sound_statistics_value")
		g_GameVars.statistics.goal = true
		g_GameVars.statistics.next_time = g_GameVars.statistics.next_time + step_time

	elseif (not g_GameVars.statistics.score) and g_GameVars.statistics.time > g_GameVars.statistics.next_time then
		local w = ConvertToCLabelWidget(window:GetWidget("score"))
		if w then
			w:SetText(g_GameVars.level_score.."")
			
			show_text_effect(w)			
		end
		play_sound("sound_statistics_value")
		g_GameVars.statistics.score = true
		g_GameVars.statistics.next_time = g_GameVars.statistics.next_time + step_time

	elseif (not g_GameVars.statistics.profit) and g_GameVars.statistics.time > g_GameVars.statistics.next_time then
		local w = ConvertToCLabelWidget(window:GetWidget("profit"))
		if w then
			w:SetText((g_GameVars.level_score - g_GameVars.goal_score).."")
			
			show_text_effect(w)			
		end
		play_sound("sound_statistics_value")
		g_GameVars.statistics.profit = true
		g_GameVars.statistics.next_time = g_GameVars.statistics.next_time + step_time

	elseif (not g_GameVars.statistics.gems) and g_GameVars.statistics.time >g_GameVars.statistics.next_time then
		g_GameVars.statistics.gems = true
		
		if not g_GameVars.WinLevel then
			return
		end
		g_GameVars.statistics.next_time = g_GameVars.statistics.next_time + step_time
		play_sound("sound_statistics_value")

		local profit = g_GameVars.level_score - g_GameVars.goal_score

		local coins = 0
		if profit >= g_GameVars.goal_score then 
			coins = 15
		else 			
			-- формула начисления драгоценных камней
			coins = profit / g_GameVars.goal_score * 10 + g_GameVars.middle_sat / 20

			if coins - math.floor(coins) >= 0.5 then coins = math.floor(coins) + 1
			else coins = math.floor(coins) end
			
			if coins<=5 then
				coins = coins + 1
			end
		end
		add_coins(coins)
		log('gained '..coins..' gems')

		local w = ConvertToCImageWidget(g_Game:GetWidget("show_gem"))
		if w then
			w:GetImage():PlayAnimationTimes(get_number("gem_view", "times"))
		end

		local w = ConvertToCLabelWidget(window:GetWidget("gems"))
		if w then
			w:SetText(coins.."")
			w:Hide(false)
			
			show_text_effect(w)			
		end
	elseif (not g_GameVars.statistics.goal_failed) and g_GameVars.statistics.time > g_GameVars.statistics.next_time then
		g_GameVars.statistics.goal_failed = true
		if g_GameVars.WinLevel then
			return
		end
		g_GameVars.statistics.next_time = g_GameVars.statistics.next_time + step_time
		play_sound("sound_statistics_value")

		local w = ConvertToCLabelWidget(window:GetWidget("goal_failed"))
		if w then
			w:Hide(false)
		end

	elseif (not g_GameVars.statistics.lives_left) and g_GameVars.statistics.time > g_GameVars.statistics.next_time then
		g_GameVars.statistics.lives_left = true
		if g_GameVars.WinLevel then
			return
		end
		g_GameVars.statistics.next_time = g_GameVars.statistics.next_time + step_time
		play_sound("sound_statistics_value")
		
		local w = nil
		if g_Player:GetLives()<=0 then
			w = ConvertToCLabelWidget(window:GetWidget("text_retry"))
		else
			w = ConvertToCLabelWidget(window:GetWidget("lives_left"))
		end
		if w then
			w:Hide(false)
		end

	elseif (not g_GameVars.statistics.record) and g_GameVars.statistics.time > g_GameVars.statistics.next_time then
		g_GameVars.statistics.record = true
		local w = ConvertToCLabelWidget(window:GetWidget("record"))
		if w then
			if records_manager():AddRecord(profile_manager():GetCurrentProfile():GetName(), g_Player:GetGlobalScore(), 
		    		g_GameVars.location.." - "..g_GameVars.level, g_Player:GetOverallCoins(), g_Player:GetOverallSatisfaction() / (g_Player:GetLevel()-1)) 
			then
				w:Disable(false)
				w:Hide(false)
				play_sound("sound_statistics_record")
				g_GameVars.statistics.next_time = g_GameVars.statistics.next_time + step_time

			end
		end
	elseif (not g_GameVars.statistics.gems_counted) and g_GameVars.statistics.time > g_GameVars.statistics.next_time then

		if not g_GameVars.WinLevel or g_Player:GetLevel()<100 then
			g_GameVars.statistics.gems_counted = true
			return
		end
		
		if not g_GameVars.statistics.gems_counted_init then
			g_GameVars.statistics.gems_counted_init = true

			local time = get_number("gem_count", "gem_time")
			if time*g_Player:GetCoins() > get_number("gem_count", "max_time") then
				time = get_number("gem_count", "max_time")/g_Player:GetCoins()
			end
			g_GameVars.statistics.gems_count = {}
			g_GameVars.statistics.gems_count.time = time
			g_GameVars.statistics.gems_count.gems = g_Player:GetCoins()
			g_GameVars.statistics.gems_count.all_gems = g_Player:GetCoins()
			g_GameVars.statistics.gems_count.start_score = g_Player:GetGlobalScore()
			g_GameVars.statistics.gems_count.sound_delay = 0
			
			log("Counting gems:")
			log("1 gem time: "..time)
			log("gems: "..g_GameVars.statistics.gems_count.all_gems)
			log("start score: "..g_GameVars.statistics.gems_count.start_score)
		else
			g_GameVars.statistics.gems_count.gems = 
				g_GameVars.statistics.gems_count.gems - dt/g_GameVars.statistics.gems_count.time

			if g_GameVars.statistics.gems_count.gems<=0 then 
				g_GameVars.statistics.gems_count.gems = 0 

				g_GameVars.statistics.gems_counted = true
				g_GameVars.statistics.next_time = g_GameVars.statistics.time
			end

			local gems = math.floor(g_GameVars.statistics.gems_count.gems)

			g_GameVars.statistics.gems_count.sound_delay = g_GameVars.statistics.gems_count.sound_delay - dt
			if g_GameVars.statistics.gems_count.sound_delay<=0 then
				play_sound("sound_gem_count")
				g_GameVars.statistics.gems_count.sound_delay = get_number("gem_count", "gem_time") + 
					g_GameVars.statistics.gems_count.sound_delay
			end

			g_Player:SetCoins(gems)
			add_coins(0)

			local gems_counted = g_GameVars.statistics.gems_count.all_gems - gems

			local score = g_GameVars.statistics.gems_count.start_score
			if gems_counted>=1 then 
				local boost = get_number("gem_count", "boost_score")
				local gem_score = get_number("gem_count", "base_score")
				local count = gems_counted
				while count > 0 do
					score = score + gem_score
					gem_score = gem_score + boost
					count = count - 1
				end
				ConvertToCImageWidget(g_Game:GetWidget('show_gem')):GetImage():PlayAnimationTimes(1)
			end
			g_Player:SetGlobalScore(score)
			
			add_score(0)
			local w = ConvertToCLabelWidget(window:GetWidget("global_score"))
			if w then
				w:SetText(g_Player:GetGlobalScore().."")
			end
		end

	elseif (not g_GameVars.statistics.buttons) and g_GameVars.statistics.time > g_GameVars.statistics.next_time then
		g_GameVars.statistics.buttons = true
		g_GameVars.statistics.next_time = g_GameVars.statistics.next_time + step_time
		play_sound("sound_statistics_value")

		event_on_show_statistics(window)

		if not g_GameVars.WinLevel then
			local w = window:GetWidget("try_again")
			if w then 
				w:Hide(false)
				w:Disable(false)
			end

			if g_Player:GetLives()<=0 then
				local w = window:GetWidget("menu")
				if w then 
					w:Hide(false)
					w:Disable(false)
				end
			else
				local w = window:GetWidget("shop")
				if w then 
					w:Hide(false)
					w:Disable(false)
	
					if not g_GameVars.events.shop_info then 
						CreateButtonFlashEffect(w, "image_game_gui_button") 
					else
						CreateButtonLightenEffect(w, "image_game_gui_button", get_number("statistics", "shop_light"))
					end
				end		
			end
		else
			if g_Player:GetLevel()>100 then
				-- выигрыш игры!
				local w = window:GetWidget("victory")
				if w then 
					w:Hide(false)
					w:Disable(false)
				end	
			else
				local w = window:GetWidget("next_level")
				if w then 
					w:Hide(false)
					w:Disable(false)
				end	
				local w = window:GetWidget("shop")
				if w then 
					w:Hide(false)
					w:Disable(false)

					if not g_GameVars.events.shop_info then 
						CreateButtonFlashEffect(w, "image_game_gui_button") 
					else
						CreateButtonLightenEffect(w, "image_game_gui_button", get_number("statistics", "shop_light"))
					end
				end	
				local w = window:GetWidget("replay")
				if w then 
					w:Hide(false)
					w:Disable(false)
				end	

			end
		end
	end
end

-------------------------------------------------------------------------------------------------

function statistics_generic_handler(window, action, widget)
	standard_generic_handler(window, action, widget)
	
	if action=="pressed" then
		if widget:GetId()=="next_level" then
			g_Player:AddAmulet(-1)
			window:Close()
			init_level(g_Player:GetLevel())

		elseif widget:GetId()=="try_again" then
			if g_Player:GetLives()<=0 then 
				-- преходим на начало локации
				-- загружаем состояние игрока на начало локации
				g_GameVars.location_loses = g_GameVars.location_loses + 1
				log("location loses = "..g_GameVars.location_loses)
				g_GameVars.easy_lvl1 = false
				g_GameVars.easy_lvl2 = false
				if g_GameVars.location_loses==1 or g_GameVars.location_loses>2 then
					g_GameVars.easy_lvl2 = true
				end
				if g_GameVars.location_loses >= 2 then
					g_GameVars.easy_lvl3 = true
				end

				--g_Player:Load("saves/save.loc")
				load_player("loc")
				if g_LifeScene:GetMeltCount()~=0 then g_GameVars.force_change = true end
				g_GameVars.melt_count = 0
			end
			g_Player:AddAmulet(-1)
			window:Close()
			init_level(g_Player:GetLevel())

		elseif widget:GetId()=="victory" then
			g_State:SetNeedSaving(false)
			profile_manager():GetCurrentProfile():ClearSave()
			start_mainmenu_state()

		elseif widget:GetId()=="shop" then
			window:Close()
			local shop = CLuaWindow(g_Loader:GetTemplate("shop_window"), g_Lua)
			shop:SetModal(true)
			g_State:AddWidget(shop)

		elseif widget:GetId()=="menu" then
			start_mainmenu_state()

		elseif widget:GetId()=="record" then
			local w = CLuaWindow(g_Loader:GetTemplate("records_window"), g_Lua)
			g_State:AddWidget(w)

		elseif widget:GetId()=="replay" then
			local dlg = create_dialog(window, "replay_level_dialog")
			dlg:SetGenericActionHandler("replay_level_dialog_generic_handler")
		end
	end
end

-------------------------------------------------------------------------------------------------

function replay_level_dialog_generic_handler(window, action, widget)
	standard_generic_handler(window, action, widget)
	
	if action=="pressed" then
		if widget:GetId()=="left" then
			ConvertToCLuaWindow(window:GetParent()):Close()

			load_player("lev")
			g_Player:AddAmulet(-1)
			g_GameVars.force_change = true
			init_level(g_Player:GetLevel())
			
			
		else
			window:Close()
		end
		
	end
end

-------------------------------------------------------------------------------------------------

local function update_amulets_and_gifts(shop)

	local red_color = CColor(255, 0, 0, 255)

	local gems = ConvertToCLabelWidget(shop:GetWidget('gems'))
	if gems then
		gems:SetText('x'..g_Player:GetCoins())
	end

	local gemstext = ConvertToCLabelWidget(shop:GetWidget("gemstext"))
	if gemstext and gems then
		local font = CFont("font_button")
		local width = gems:GetRect().mX + font:GetFont():StringWidth(gems:GetText()) -
			(gemstext:GetRect().mX - font:GetFont():StringWidth(gemstext:GetText()) )
		local deltax = (shop:GetRect().mWidth - width)/2 -
			(gemstext:GetRect().mX - font:GetFont():StringWidth(gemstext:GetText()) )

		gemstext:MoveTo(gemstext:GetRect().mX + deltax, gemstext:GetRect().mY)
		gems:MoveTo(gems:GetRect().mX + deltax, gems:GetRect().mY)
		local image = shop:GetWidget("gemsimage")
		image:MoveTo(image:GetRect().mX + deltax, image:GetRect().mY)
		
		local gem_widget = ConvertToCImageWidget(shop:GetWidget('gemsimage'))
		if gem_widget then
			local coords = gem_widget:GetAbsPos()
			coords.x = coords.x + gem_widget:GetRect().mWidth/2
			coords.y = coords.y + gem_widget:GetRect().mHeight/2
			
			local effect_widget = ConvertToCParticleWidget(shop:GetWidget('gem_qwidget'))
			if effect_widget then
				effect_widget:MoveTo(coords.x, coords.y)
			end
		end
			
	end

	local num = 16
	while num>0 do
		id = ''..num
		local amulet = ConvertToCShopButton(shop:GetWidget(id))
		if amulet~=nil then 
			local cost = CAmulet(num-1):GetCoinsValue()
			if cost > g_Player:GetCoins() and not (g_Player:IsBoughtAmulet(num-1)) then 
				local gem_cost = ConvertToCLabelWidget(amulet:GetWidget('gem cost'))
				if gem_cost then
					gem_cost:SetColor(red_color)
				end
				local gem_text = ConvertToCLabelWidget(amulet:GetWidget('gems'))
				if gem_text then gem_text:SetColor(red_color) end
				amulet:DisableButton(true) 
			end
		end
		num = num - 1
	end
	local gift = ConvertToCButtonWidget(shop:GetWidget('lotus'))
	if gift then
		if get_number('gifts', 'lotus_cost') > g_Player:GetCoins() then
			local gem_cost = ConvertToCLabelWidget(gift:GetWidget('cost'))
			if gem_cost then
				gem_cost:SetColor(red_color)
			end
			local gem_text = ConvertToCLabelWidget(gift:GetWidget('gem_text'))
			if gem_text then gem_text:SetColor(red_color) end
			gift:DisableButton(true)
		end
		local count = ConvertToCLabelWidget(gift:GetWidget('count'))
		if count then count:SetText('x'..g_Player:GetGiftCount(CPlayer.gift_lotus)) end
	end
	local gift = ConvertToCButtonWidget(shop:GetWidget('food'))
	if gift then
		if get_number('gifts', 'food_cost') > g_Player:GetCoins() then
			local gem_cost = ConvertToCLabelWidget(gift:GetWidget('cost'))
			if gem_cost then gem_cost:SetColor(red_color) end
			local gem_text = ConvertToCLabelWidget(gift:GetWidget('gem_text'))
			if gem_text then gem_text:SetColor(red_color) end
			gift:DisableButton(true)
		end
		local count = ConvertToCLabelWidget(gift:GetWidget('count'))
		if count then count:SetText('x'..g_Player:GetGiftCount(CPlayer.gift_food)) end
	end
	local gift = ConvertToCButtonWidget(shop:GetWidget('aroma'))
	if gift then
		if get_number('gifts', 'aroma_cost') > g_Player:GetCoins() then
			local gem_cost = ConvertToCLabelWidget(gift:GetWidget('cost'))
			if gem_cost then
				gem_cost:SetColor(red_color)
			end
			local gem_text = ConvertToCLabelWidget(gift:GetWidget('gem_text'))
			if gem_text then gem_text:SetColor(red_color) end
			gift:DisableButton(true)
		end
		local count = ConvertToCLabelWidget(gift:GetWidget('count'))
		if count then count:SetText('x'..g_Player:GetGiftCount(CPlayer.gift_aroma)) end
	end
end

-------------------------------------------------------------------------------------------------
local shop_shutter = false

function shop_on_load_template(shop)
	g_Player:AddAmulet(-1)
	local num = 16
	while num>0 do
		id = ''..num

		local amulet = ConvertToCShopButton(shop:GetWidget(id))
		if amulet~=nil then
			local a = CAmulet(num - 1)
			amulet:SetFrontImage(a:GetImage())

			local name = ConvertToCLabelWidget(amulet:GetWidget('name'))
			if name~=nil then 
				name:SetText(a:GetAmuletName())
			end

			local cost = ConvertToCLabelWidget(amulet:GetWidget('cost'))
			if cost~=nil then 
				cost:SetText(a:GetPrice()..'')
			end

			local gem_cost = ConvertToCLabelWidget(amulet:GetWidget('gem cost'))
			if gem_cost~=nil then 
				if g_Player:IsBoughtAmulet(num-1) then
					gem_cost:SetText("")
					local static_gem = ConvertToCLabelWidget(amulet:GetWidget("gems"))
					if static_gem then static_gem:SetText(string_table():GetString(167)) end
					amulet:Buy(true)
				else
					gem_cost:SetText(a:GetCoinsValue()..'')
				end
			end

		end
		num = num - 1
	end

	local gem_widget = ConvertToCImageWidget(shop:GetWidget('gemsimage'))
	if gem_widget then
		ShopGemStarsEffect(shop, CFloatVector(0, 0))
	end

	local gems = ConvertToCLabelWidget(shop:GetWidget('gems'))
	if gems then
		gems:SetText(string_table():GetString(16)..g_Player:GetCoins())
	end

	local gift = ConvertToCButtonWidget(shop:GetWidget('lotus'))
	if gift then
		local cost = ConvertToCLabelWidget(gift:GetWidget('cost'))
		if cost then cost:SetText(get_string('gifts', 'lotus_cost')) end
		local effect = ConvertToCLabelWidget(gift:GetWidget('effect'))
		if effect then effect:SetText(get_string('gifts', 'lotus')..'%') end
		local count = ConvertToCLabelWidget(gift:GetWidget('count'))
		if count then count:SetText('x'..g_Player:GetGiftCount(CPlayer.gift_lotus)) end
	end
	local gift = ConvertToCButtonWidget(shop:GetWidget('food'))
	if gift then
		local cost = ConvertToCLabelWidget(gift:GetWidget('cost'))
		if cost then cost:SetText(get_string('gifts', 'food_cost')) end
		local effect = ConvertToCLabelWidget(gift:GetWidget('effect'))
		if effect then effect:SetText(get_string('gifts', 'food')..'%') end
		local count = ConvertToCLabelWidget(gift:GetWidget('count'))
		if count then count:SetText('x'..g_Player:GetGiftCount(CPlayer.gift_food)) end
	end
	local gift = ConvertToCButtonWidget(shop:GetWidget('aroma'))
	if gift then
		local cost = ConvertToCLabelWidget(gift:GetWidget('cost'))
		if cost then cost:SetText(get_string('gifts', 'aroma_cost')) end
		local effect = ConvertToCLabelWidget(gift:GetWidget('effect'))
		if effect then effect:SetText(get_string('gifts', 'aroma')..'%') end
		local count = ConvertToCLabelWidget(gift:GetWidget('count'))
		if count then count:SetText('x'..g_Player:GetGiftCount(CPlayer.gift_aroma)) end
	end

	update_amulets_and_gifts(shop)

	shop_shutter = false
end

-------------------------------------------------------------------------------------------------

function shop_window_update(shop)
	if not shop_shutter then
		shop_shutter = true
		event_on_shop_window_open(shop)
	end
end

-------------------------------------------------------------------------------------------------

function shop_on_continue_pressed(window, action, widget)

	play_sound("sound_gui_click")
	window:Close()
	init_level(g_Player:GetLevel())
end

-------------------------------------------------------------------------------------------------

function shop_on_hint_pressed(window, action, widget)
	play_sound("sound_gui_click")
	
	local win = CLuaWindow(g_Loader:GetTemplate("shop_hint_window"), g_Lua)
	window:AddWidget(win)
end

-------------------------------------------------------------------------------------------------

local function buy_amulet(widget)
	local id = tonumber(widget:GetId()) - 1
	local amulet  = CAmulet(id)
	if (amulet:GetCoinsValue() > g_Player:GetCoins()) or 
		(g_Player:IsBoughtAmulet(id)) then return end
	add_coins(-amulet:GetCoinsValue())
	g_Player:AddAmulet(id)
	local gem_cost = ConvertToCLabelWidget(widget:GetWidget('gem cost'))
	if gem_cost then gem_cost:SetText("") end
	local static_gem = ConvertToCLabelWidget(widget:GetWidget("gems"))
	if static_gem then static_gem:SetText(string_table():GetString(167)) end
	widget:Buy(true)

	play_sound("sound_amulet_buy")
	event_on_amulet_buy(widget)
end

-------------------------------------------------------------------------------------------------

function shop_on_lotus_pressed(shop, action, lotus)
	if action=='over' then
		play_sound("sound_shop_over")
	end
	if action=='disabled' then
		play_sound("sound_gui_disabled")
	end
	if action~='pressed' then return end

	g_Player:SetGiftCount(CPlayer.gift_lotus, g_Player:GetGiftCount(CPlayer.gift_lotus) + 1)
	add_coins(-get_number('gifts', 'lotus_cost'))

	play_sound("sound_gift_buy")

	update_amulets_and_gifts(shop)
	event_on_gift_buy(lotus)
end

-------------------------------------------------------------------------------------------------

function shop_on_food_pressed(shop, action, food)
	if action=='over' then
		play_sound("sound_shop_over")
	end
	if action=='disabled' then
		play_sound("sound_gui_disabled")
	end
	if action~='pressed' then return end

	g_Player:SetGiftCount(CPlayer.gift_food, g_Player:GetGiftCount(CPlayer.gift_food) + 1)
	add_coins(-get_number('gifts', 'food_cost'))

	play_sound("sound_gift_buy")

	update_amulets_and_gifts(shop)
	event_on_gift_buy(food)
end

-------------------------------------------------------------------------------------------------

function shop_on_aroma_pressed(shop, action, aroma)
	if action=='over' then
		play_sound("sound_shop_over")
	end
	if action=='disabled' then
		play_sound("sound_gui_disabled")
	end
	if action~='pressed' then return end

	g_Player:SetGiftCount(CPlayer.gift_aroma, g_Player:GetGiftCount(CPlayer.gift_aroma) + 1)
	add_coins(-get_number('gifts', 'aroma_cost'))

	play_sound("sound_gift_buy")

	update_amulets_and_gifts(shop)
	event_on_gift_buy(aroma)
end

-------------------------------------------------------------------------------------------------

function shop_generic_handler(shop, action, widget)
	if action=='over' and widget:GetId()~="continue" and widget:GetId()~="hint" then
		play_sound("sound_shop_over")
	end
	if action=='disabled' then
		play_sound("sound_gui_disabled")
	end
	if action~='pressed' then return end
	
	buy_amulet(ConvertToCShopButton(widget))
	update_amulets_and_gifts(shop)
end

-------------------------------------------------------------------------------------------------

function shophint_generic_handler(window, action, widget)
	standard_generic_handler(window, action, widget)
	
	if action=="pressed" and widget:GetId()=="close" then
		window:Close()
	end
end