_G[gTable] = {};

local aTmpVal = gTable;
local aMachineActive = false;
cm.SetupTable(_G[aTmpVal]);


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

-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)

   aMachineActive = pObj.mCsData:GetBool("MachineActive");
   if aMachineActive then
      _G[aTmpVal].SetupParticleSystem(pObj);
   end

end

-- Called after data being loaded from disk
_G[gTable].OnLevelLoad = function(pObj)

   aMachineActive = pObj.mCsData:GetBool("MachineActive");
   if aMachineActive and (not pObj:ParticleEmitterOn()) then
      _G[aTmpVal].SetupParticleSystem(pObj);
   end

end

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

   local aState = 0 + pObj.mState;
   if not aMachineActive then

      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1")
       and cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
         aMachineActive = true;
         pObj.mCsData:SetVal("MachineActive", aMachineActive);
         _G[aTmpVal].SetupParticleSystem(pObj);
         cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
      end

   else

      if aState == 1
       and cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag3") then
         pObj:StartFadeRects(PhFadeAnotherTimeLong);
      	pObj.mState   = aState + 1;
      	pObj.mCurCell = pObj.mCurCell + 1;

			local aNum = pObj:GetFirstActive(0);
			pObj:SetActive(0, aNum,     false);
			pObj:SetActive(0, aNum + 1, true);
      end

   end

end


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

   pObj.mDiscovered = true;
   local aState = 0 + pObj.mState;

	if pName == "" then

      if pPolyNum == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      else
         if aState == 2 then
            pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         	pObj.mState   = aState        + 1;
         	pObj.mCurCell = pObj.mCurCell + 1;
         	pObj:SetActive(0, pPolyNum, false);
         	cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         	cm.SetGlobalValIndirect(pObj, "SetFlag3", true);
         	IcPlaySample("PH_SFX_BIGBUTTONPRESSED");
      	end
      end

		return true;

	elseif pName == pObj.mCsData:GetString("KeyObject1") then

      pObj:StartFadeRects(PhFadeAnotherTimeMedium);
		pObj.mState     = aState        + 1;
		pObj.mCurCell   = pObj.mCurCell + 1;
		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
      cm.SetGlobalValIndirect(pObj, "SetFlag2", true);
		return true;

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

	return false;

end


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

	local aImg  = IcImageWrap(pObj.mSmallImage);
	assert(aImg);
	local aRpos = pObj.mSmPos;
	local aRect = aImg:GetCelRect(pObj.mCurCell);

	if (aState == 3 or aState == 1) then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
	end
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end


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

	pObj.mDiscovered = true;
   local aStr = pObj.mCsData:GetString("ParticleSystemName");
   pObj:SetupParticleSystem(aStr);
   pObj:StartParticleEmission(2);

end

