
_G[gTable] = {};

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


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


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

end

-- Called after a level is loaded
_G[gTable].OnLevelLoad = function(pObj)

   if pObj.mState == 1 then
		if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
	      _G[aTmpVal].StartNoise(pObj);
      end
   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)

	local aState = 0 + pObj.mState;
   if aState == 0 then
		if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj.mState = aState + 1;
	      _G[aTmpVal].StartNoise(pObj);
      end
	elseif aState == 1 then
		if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj.mState = aState - 1;
         local aNum	= pObj.mCsData:GetInt("SoundIndex");
	      pObj.mGobj.mSoundPlayer:StopLoopingSfx(aNum);
      end
	end

end


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

   if pName == "" then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
      else
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      end
		return true;
	else
      pObj.mPdata:UnMouseObject();
		cm.ObjDoesNotWork(pObj);
		return true;
   end

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 to start machine sound
_G[gTable].StartNoise =  function(pObj)
	local aSfx = pObj.mCsData:GetString("MachineSound");
	local aNum = pObj.mGobj.mSoundPlayer:PlayLoopingSfx(aSfx);
	pObj.mCsData:SetVal("SoundIndex", aNum);
end

