--// Local auxiliary functions - - - - - - - - - - - - - - - - - - - - - - - -

--// 1.	rename:					" -> '
--// 2. comment root composition with texture "createComposition018_back = function()"
--// 3. add auxiliaru routines for managing animations: 
--		auxPlayAncientTeleportPortalAnimation(), 
--		auxGetAncientTeleportPortalAnimation(), 
--		auxCreateAncientTeleportPortalAnimations()
--// 4. replace CreateCompositionElement() with auxCreateCompositionElement() for some corrections ...

------------------------------------------------------------------------------
--
function auxPlayAncientTeleportPortalAnimation(compositionName)
    local composition = auxGetAncientTeleportPortalAnimation(compositionName);
    composition:SetVisible(true);
    composition:Play();
end

------------------------------------------------------------------------------
--
function auxHideAncientTeleportPortalAnimation(compositionName)
    local composition = auxGetAncientTeleportPortalAnimation(compositionName);
    composition:Stop();
    composition:SetVisible(false);
end

------------------------------------------------------------------------------
--
function auxGetAncientTeleportPortalAnimation(compositionName)
    assert(ancientTeleportPortalCompositions ~= nil and compositionName ~= nil);
	assert(ancientTeleportPortalCompositions[compositionName] ~= nil and 'Composition with specified name not exists !');

    return ancientTeleportPortalCompositions[compositionName];
end

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

function CreateComposition(parent, compositionRoutine, name, x,y,z, hotSpotX,hotSpotY, duration, isCyclic)
    assert(parent ~= nil and compositionRoutine ~= nil and name ~= nil and z ~= nil and duration ~= nil);

    local element = compositionRoutine();
    element:SetName(name);
    element:SetHotSpot(hotSpotX,hotSpotY);
    element:SetXY(x, y);
    assert(z - parent:GetZ());
    element:SetZ(z - parent:GetZ());
    element:AttachTo(parent);

    element:SetScaleX(1);
    element:SetScaleY(1);
    element:SetAngle(0);
    element:SetOpacity(1);
    
    if (isCyclic == true) then
        element:SetDuration(duration);
        element:SetCyclic(true);
    end
	element:Stop();
    element:SetVisible(false);
    element:AssignClippingRect(parent);
	element:SetConsiderParentOpacity(true);

	return element;
end

-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function auxCreateCompositionElement(resourceName)
	local element = CreateCompositionElement(resourceName);
--    local texName = string.sub(resourceName, string.len('data/Texture/Scenes/18_AncientTeleportPortal/Animations/Env') + 1);

    --// set up blend mode for sprites with additive blending
--	if (texName == '.jpg') then
--		element:GetTexture():SetColor('0xBBFFFFFF');
--        element:GetTexture():SetBlendMode(0);
--    end
	return element;
end

------------------------------------------------------------------------------
--
function auxCreateAncientTeleportPortalAnimations(parent)
    assert(parent ~= nil);
    ancientTeleportPortalCompositions = {};
    local CreateAndRegisterComposition = function (parent, compositionRoutine, name, x,y,z, hotSpotX,hotSpotY, duration, isCyclic)
        local element = CreateComposition(parent, compositionRoutine, name, x,y,z, hotSpotX,hotSpotY, duration, isCyclic);
        ancientTeleportPortalCompositions[element:GetName()] = element;
    end
    
    CreateAndRegisterComposition(parent, createCompositionportal,  'PortalAnimation', -24,-76,102.0, 512,384, 0.291667, true);
end

------------------------------------------------------------------------------
--
--createComposition018_back = function()
--
--    local composition018_back = CreateCompositionSequence();
--    local compositionDuration = 0.29166666666667;
--    composition018_back:SetDuration(compositionDuration);
--
--
--    -- Layer Layer_1
--    local element = createCompositionportal();
--    element:SetName("Layer_1");
--    composition018_back:AddElement(element);
--    element:SetHotSpot(512, 384);
--    element:SetParentElement(composition018_back);
--    element:SetXY(487, 308);
--
--    element:SetScaleX(1);
--    element:SetScaleY(1);
--    element:SetAngle(0);
--    element:SetOpacity(1);
--
--    effector = element:GetEffector();
--    effector:SetLifeTime(0, 0.29166666666667);
--
--
--    effector:Play();
--
--
--    -- Layer Layer_2
--    local element = CreateCompositionElement("data/Texture/Scenes/18_AncientTeleport/Animations/Portal/018_back.jpg");
--    element:SetName("Layer_2");
--    composition018_back:AddElement(element);
--    element:SetHotSpot(512, 384);
--    element:SetParentElement(composition018_back);
--    element:SetXY(512, 384);
--
--    element:SetScaleX(1);
--    element:SetScaleY(1);
--    element:SetAngle(0);
--    element:SetOpacity(1);
--
--    effector = element:GetEffector();
--    effector:SetLifeTime(0, 4.66666666666667);
--
--
--    effector:Play();
--
--    composition018_back:Play();
--
--    return composition018_back;
--end

createCompositionportal = function()

    local compositionportal = CreateCompositionSequence();
    local compositionDuration = 0.29166666666667;
    compositionportal:SetDuration(compositionDuration);


    -- Layer Layer_1
    local element = CreateCompositionElement("data/Texture/Scenes/18_AncientTeleport/Animations/Portal/018_portal_00.jpg");
    element:SetName("Layer_1");
    compositionportal:AddElement(element);
    element:SetHotSpot(250, 250);
    element:SetParentElement(compositionportal);
    element:SetAngle(0);
    element:SetOpacity(1);

    effector = element:GetEffector();
    effector:SetLifeTime(0, 0.04166666666667);

    effector:SetKey(_TRACK_X, 0, 512);
    effector:SetKey(_TRACK_Y, 0, 384);
    effector:SetKey(_TRACK_X, 0.04166666666667, 512);
    effector:SetKey(_TRACK_Y, 0.04166666666667, 384);
    effector:SetKey(_TRACK_SX, 0, 1);
    effector:SetKey(_TRACK_SY, 0, 1);
    effector:SetKey(_TRACK_SX, 0.04166666666667, 1);
    effector:SetKey(_TRACK_SY, 0.04166666666667, 1);

    effector:Play();


    -- Layer Layer_2
    local element = CreateCompositionElement("data/Texture/Scenes/18_AncientTeleport/Animations/Portal/018_portal_01.jpg");
    element:SetName("Layer_2");
    compositionportal:AddElement(element);
    element:SetHotSpot(250, 250);
    element:SetParentElement(compositionportal);
    element:SetAngle(0);
    element:SetOpacity(1);

    effector = element:GetEffector();
    effector:SetLifeTime(0.04166666666667, 0.08333333333333);

    effector:SetKey(_TRACK_X, 0.04166666666667, 512);
    effector:SetKey(_TRACK_Y, 0.04166666666667, 384);
    effector:SetKey(_TRACK_X, 0.08333333333333, 512);
    effector:SetKey(_TRACK_Y, 0.08333333333333, 384);
    effector:SetKey(_TRACK_SX, 0.04166666666667, 1);
    effector:SetKey(_TRACK_SY, 0.04166666666667, 1);
    effector:SetKey(_TRACK_SX, 0.08333333333333, 1);
    effector:SetKey(_TRACK_SY, 0.08333333333333, 1);

    effector:Play();


    -- Layer Layer_3
    local element = CreateCompositionElement("data/Texture/Scenes/18_AncientTeleport/Animations/Portal/018_portal_02.jpg");
    element:SetName("Layer_3");
    compositionportal:AddElement(element);
    element:SetHotSpot(250, 250);
    element:SetParentElement(compositionportal);
    element:SetAngle(0);
    element:SetOpacity(1);

    effector = element:GetEffector();
    effector:SetLifeTime(0.08333333333333, 0.125);

    effector:SetKey(_TRACK_X, 0.08333333333333, 512);
    effector:SetKey(_TRACK_Y, 0.08333333333333, 384);
    effector:SetKey(_TRACK_X, 0.125, 512);
    effector:SetKey(_TRACK_Y, 0.125, 384);
    effector:SetKey(_TRACK_SX, 0.08333333333333, 1);
    effector:SetKey(_TRACK_SY, 0.08333333333333, 1);
    effector:SetKey(_TRACK_SX, 0.125, 1);
    effector:SetKey(_TRACK_SY, 0.125, 1);

    effector:Play();


    -- Layer Layer_4
    local element = CreateCompositionElement("data/Texture/Scenes/18_AncientTeleport/Animations/Portal/018_portal_03.jpg");
    element:SetName("Layer_4");
    compositionportal:AddElement(element);
    element:SetHotSpot(250, 250);
    element:SetParentElement(compositionportal);
    element:SetAngle(0);
    element:SetOpacity(1);

    effector = element:GetEffector();
    effector:SetLifeTime(0.125, 0.16666666666667);

    effector:SetKey(_TRACK_X, 0.125, 512);
    effector:SetKey(_TRACK_Y, 0.125, 384);
    effector:SetKey(_TRACK_X, 0.16666666666667, 512);
    effector:SetKey(_TRACK_Y, 0.16666666666667, 384);
    effector:SetKey(_TRACK_SX, 0.125, 1);
    effector:SetKey(_TRACK_SY, 0.125, 1);
    effector:SetKey(_TRACK_SX, 0.16666666666667, 1);
    effector:SetKey(_TRACK_SY, 0.16666666666667, 1);

    effector:Play();


    -- Layer Layer_5
    local element = CreateCompositionElement("data/Texture/Scenes/18_AncientTeleport/Animations/Portal/018_portal_04.jpg");
    element:SetName("Layer_5");
    compositionportal:AddElement(element);
    element:SetHotSpot(250, 250);
    element:SetParentElement(compositionportal);
    element:SetAngle(0);
    element:SetOpacity(1);

    effector = element:GetEffector();
    effector:SetLifeTime(0.16666666666667, 0.20833333333333);

    effector:SetKey(_TRACK_X, 0.16666666666667, 512);
    effector:SetKey(_TRACK_Y, 0.16666666666667, 384);
    effector:SetKey(_TRACK_X, 0.20833333333333, 512);
    effector:SetKey(_TRACK_Y, 0.20833333333333, 384);
    effector:SetKey(_TRACK_SX, 0.16666666666667, 1);
    effector:SetKey(_TRACK_SY, 0.16666666666667, 1);
    effector:SetKey(_TRACK_SX, 0.20833333333333, 1);
    effector:SetKey(_TRACK_SY, 0.20833333333333, 1);

    effector:Play();


    -- Layer Layer_6
    local element = CreateCompositionElement("data/Texture/Scenes/18_AncientTeleport/Animations/Portal/018_portal_05.jpg");
    element:SetName("Layer_6");
    compositionportal:AddElement(element);
    element:SetHotSpot(250, 250);
    element:SetParentElement(compositionportal);
    element:SetAngle(0);
    element:SetOpacity(1);

    effector = element:GetEffector();
    effector:SetLifeTime(0.20833333333333, 0.25);

    effector:SetKey(_TRACK_X, 0.20833333333333, 512);
    effector:SetKey(_TRACK_Y, 0.20833333333333, 384);
    effector:SetKey(_TRACK_X, 0.25, 512);
    effector:SetKey(_TRACK_Y, 0.25, 384);
    effector:SetKey(_TRACK_SX, 0.20833333333333, 1);
    effector:SetKey(_TRACK_SY, 0.20833333333333, 1);
    effector:SetKey(_TRACK_SX, 0.25, 1);
    effector:SetKey(_TRACK_SY, 0.25, 1);

    effector:Play();


    -- Layer Layer_7
    local element = CreateCompositionElement("data/Texture/Scenes/18_AncientTeleport/Animations/Portal/018_portal_06.jpg");
    element:SetName("Layer_7");
    compositionportal:AddElement(element);
    element:SetHotSpot(250, 250);
    element:SetParentElement(compositionportal);
    element:SetAngle(0);
    element:SetOpacity(1);

    effector = element:GetEffector();
    effector:SetLifeTime(0.25, 0.29166666666667);

    effector:SetKey(_TRACK_X, 0.25, 512);
    effector:SetKey(_TRACK_Y, 0.25, 384);
    effector:SetKey(_TRACK_X, 0.29166666666667, 512);
    effector:SetKey(_TRACK_Y, 0.29166666666667, 384);
    effector:SetKey(_TRACK_SX, 0.25, 1);
    effector:SetKey(_TRACK_SY, 0.25, 1);
    effector:SetKey(_TRACK_SX, 0.29166666666667, 1);
    effector:SetKey(_TRACK_SY, 0.29166666666667, 1);

    effector:Play();

    compositionportal:Play();

    return compositionportal;
end
