_G[gTable] = {};

local aTmpVal  = gTable;
local aCount   = 0;

-- Particle system colors
local aCols    = {};
aCols[0] = { Color(58, 12, 20, 20), Color(219, 40,  71,  90), Color(41, 5, 9,  20) }
aCols[1] = { Color(23, 12, 58, 20), Color(62,  126, 198, 90), Color(12, 5, 41, 20) }
aCols[2] = { Color(60, 61, 10, 20), Color(230, 220, 30,  40),  Color(92, 90, 16, 20) }
aCols[3] = { Color(10, 62,  9, 20), Color(44,  216, 65,  52), Color(18, 92, 16, 20) }
cm.SetupTable(_G[aTmpVal]);


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
	aCount = 0;
   pObj.mDiscovered = true;
   _G[aTmpVal].SetupParticleSystem(pObj, pObj.mCurCell);
end


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
	aCount = 0;
   _G[aTmpVal].SetupParticleSystem(pObj, pObj.mCurCell);
end

-- Called after a level is loaded
_G[gTable].OnLevelLoad = function(pObj)
   pObj.mGobj.mSoundPlayer:PlayLoopingSfx("PH_SFX_MAGICCOLUMN");
   if not pObj:ParticleEmitterOn() then
      _G[aTmpVal].SetupParticleSystem(pObj, pObj.mCurCell);
	end
end


-- Called after a level is exited
_G[gTable].OnLevelExit = function(pObj)
   pObj.mGobj.mSoundPlayer:FadeoutLoopingSfx(40);
end

-- Called on a update
_G[gTable].OnUpdate = function(pObj)

   aCount = aCount + 1;
   if aCount == 200 then
     	aCount = 0;
      local aImg = IcImageWrap(pObj.mSmallImage);
      pObj:StartFadeRects(PhFadeAnotherTimeShort);
      pObj.mCurCell  = (pObj.mCurCell + 1)%aImg:GetNumCells();

      -- Change particle system
      local aCell= pObj.mCurCell;
      pObj:SetEmitterColors(aCols[aCell][1], aCols[aCell][2], aCols[aCell][3]);
   end


end


-- Called on a succesful click
_G[gTable].OnClick =  function(pObj, pName, pActorObj, pPolyNum, pX, pY)

	pObj.mDiscovered = true;
	if pName == "" then
      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      return true;

	elseif pName == pObj.mCsData:GetString("KeyObject1") then
		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		return true;

	else
		cm.ObjDoesNotWork(pObj);
		return false;
	end

	return false;
end



-- Called on draw
_G[gTable].OnDraw =  function(pObj, pG)

	local aRpos = pObj.mSmPos;
	local aImg  = IcImageWrap(pObj.mSmallImage);
	assert(aImg);
	local aRect = aImg:GetCelRect(pObj.mCurCell);
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end



-- Called on draw
_G[gTable].SetupParticleSystem =  function(pObj, pIndex)

   local aStr  = "PH_PSYS_S03_L02_POOL_00";
   local aPos1 = IcVector2d(0, 0)    + pObj.mSmPos;
   local aPos2 = IcVector2d(0, -360) + pObj.mSmPos;
   pObj.mSmPos = aPos2;
   pObj:SetupParticleSystem(aStr);
   pObj:SetEmitterColors(aCols[pIndex][1], aCols[pIndex][2], aCols[pIndex][3]);
   pObj:StartParticleEmission(1);
   pObj.mSmPos = aPos1;
end
