-------------------------------------------------------------------------------
class 'Node'
-------------------------------------------------------------------------------

function Node:__init(parent, name, order)
	assert(parent and name)
	self.main_entity = nil

	self:createMainEntity(parent, name, order)
	assert(self.main_entity)

	local mt = {}
	mt.__index = function(t, name)
		local entity = self.main_entity:getEntityNamed(name)
		if not entity then
			WriteError('No entity named "'..name..'"')
			assert(false)
		end
		t[name] = entity
		local function on_modify()
			t[name] = nil
		end
		entity.onDestroy:add(on_modify)
		entity.onRename:add(on_modify)
		return entity
	end
	self.entities = {}
	setmetatable(self.entities, mt)
end
-------------------------------------------------------------------------------

function Node:destroy()
	self.main_entity:destroy()
	self.main_entity = nil
end
-------------------------------------------------------------------------------

function Node:createMainEntity(parent, name, order)
	assert(self.main_entity == nil)

	self.main_entity = CreateSpriteEntity('')
	self.main_entity:setName(name)
	self.main_entity:attachTo(parent)
	self.main_entity:setOrder(order or 0)
	self.main_entity.onDestroy:add( self.onEntityDestroy )
	
	--[[ Prevent last update of node and its childs after destroy. In current version (ERSE_lib rev. 633) this is done by engine. Uncomment if this behaviour will be changed.
	self.main_entity.onDestroy:add( function()
			self.main_entity:setActive(false)
		end)]]
end
-------------------------------------------------------------------------------

function Node:onEntityDestroy()
	if self.timer then
		self.timer:clear()
	end
end
-------------------------------------------------------------------------------

function Node:getMainEntity()
	return self.main_entity
end
-------------------------------------------------------------------------------

function Node:hasEntityNamed(name)
	local entity = self.main_entity:getEntityNamed(name)
	if entity then
		return true
	else
		return false
	end
end
-------------------------------------------------------------------------------

function Node:getEntityNamed(name)
	return self.entities[name]
end
-------------------------------------------------------------------------------

function Node:entityVisible(name, is_visible)
	if type(name) == 'table' then
		for _, v in pairs(name) do
			self:entityVisible(v, is_visible)
		end
	else
		self:getEntityNamed(name):setVisible(is_visible)
	end
end
-------------------------------------------------------------------------------

function Node:entityOpacity(name, opacity, time)
	time = time or 0
	
	if type(name)=='table' then
		for _, v in pairs(name) do
			self:entityOpacity(v, opacity, time)
		end
	else
		if time == 0 then
			self:getEntityNamed(name):setOpacity(opacity)
		else
			self:getEntityNamed(name):ChangeOpacity( self:getEntityNamed(name):getOpacity(), opacity, time)
		end
	end
end
-------------------------------------------------------------------------------

function Node:entityFadeShow(...)
	local objects = {...}
	for _, name in pairs(objects) do
		local obj = self:getEntityNamed(name)
		obj:show()
		obj:ChangeOpacity(0, 1, CHANGE_OBJECT_OPACITY)
	end
end
-------------------------------------------------------------------------------

function Node:entityFadeHide(...)
	local objects = {...}
	for _, name in pairs(objects) do
		local obj = self:getEntityNamed(name)
		obj:ChangeOpacity(1, 0, CHANGE_OBJECT_OPACITY)
		obj:setRespondable(false)
	end
end
-------------------------------------------------------------------------------

function Node:entityShow(...)
	local objects = {...}
	for _, name in pairs(objects) do
		local obj = self:getEntityNamed(name)
		obj:show()
		obj:setOpacity(1)
	end
end
-------------------------------------------------------------------------------

function Node:entityHide(...)
	local objects = {...}
	for _, name in pairs(objects) do
		self:getEntityNamed(name):hide()
	end
end
-------------------------------------------------------------------------------

function Node:hide()
	self.main_entity:hide()
end
-------------------------------------------------------------------------------

function Node:show()
	self.main_entity:show()
end
-------------------------------------------------------------------------------

function Node:getMaxOrder()
	local childs = self.main_entity:getChilds()
	local max_order = 0
	for i = 0, childs:size() - 1 do
		local child_order = childs:get(i):getOrder()
		if child_order > max_order then
			max_order = child_order
		end
	end
	return max_order
end
-------------------------------------------------------------------------------

function Node:__finalize()
end
-------------------------------------------------------------------------------



--Temp------------------------------------------------------------------------

function Node:moveIn(move_time)
	self.main_entity:show()
end
-------------------------------------------------------------------------------

function Node:moveOut(move_time)
	self.main_entity:hide()
end
-------------------------------------------------------------------------------