_G[gTable] = {};

local aTmpVal  = gTable;
local aCount   = 0;
cm.SetupTable(_G[aTmpVal]);


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
	aCount = 0;
end


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

-- Called after a level is loaded
_G[gTable].OnLevelLoad = function(pObj)
   if not pObj:ParticleEmitterOn() then
		if pObj.mCurCell > 0 then
		   _G[aTmpVal].StartParticleSystem(pObj, 1, pObj.mCurCell);
		end
	end
end


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

   if cm.GetGlobalBoolIndirect(pObj, "SetFlag1") then
      pObj:DisablePolys();
   end

end


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

	pObj.mDiscovered = true;
	if pName == "" then

      if pObj.mCurCell == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      else
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
      end
      return true;

	elseif pName == pObj.mCsData:GetString("KeyObject1") then
	
      local aNextCell = 0 + pActorObj.mCurCell;
      if aNextCell == 0 then
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
   		return false;
      end

      local aFills = cm.GetGlobalIntIndirect(pObj, "PoolFillCount");
      if pObj.mCurCell > 0 then
         aFills = aFills - 10^pObj.mCurCell;
      end

      aFills = aFills + 10^aNextCell;
      cm.SetGlobalValIndirect(pObj, "PoolFillCount", aFills);
      if aFills == 11110 then
         cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
      end

      pObj:StartFadeRects(PhFadeAnotherTimeShort);
      pObj.mCurCell = aNextCell;
		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		_G[aTmpVal].StartParticleSystem(pObj, 1, aNextCell);
		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


-- Starts particle system
_G[gTable].StartParticleSystem =  function(pObj, pPolyNum, pIndex)

	local aColorVec =
	{
	   { Start = Color(58, 12, 20, 20), Mid = Color(219, 40,  71,  180), End = Color(41, 5, 9,  20) },
	   { Start = Color(23, 12, 58, 20), Mid = Color(62,  126, 198, 180), End = Color(12, 5, 41, 20) },
	   { Start = Color(60, 61, 10, 20), Mid = Color(230, 220, 30,  80), End = Color(92, 90, 16, 20) },
	   { Start = Color(10, 62,  9, 20), Mid = Color(44,  216, 65,  104), End = Color(18, 92, 16, 20)}
	}

	local aTable    = aColorVec[pIndex];
   local aPsysName = pObj.mCsData:GetString("ParticleSystemName");
   pObj:SetupParticleSystem(aPsysName);
   pObj:SetEmitterColors(aTable.Start, aTable.Mid, aTable.End);
   pObj:StartParticleEmission(pPolyNum);

end

