_G[gTable] = {};

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


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

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

   if pObj.mState == 0 then
      _G[aTmpVal].SetupParticleSystem(pObj);
   end

end

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

   if pObj.mState == 0 then
      if not pObj:ParticleEmitterOn() then
         _G[aTmpVal].SetupParticleSystem(pObj);
		end
   end

end

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

   local aState = 0 + pObj.mState;
   if aState == 3 then

      if not pObj.mAlpha:AncIsChanging() then

			local aImg = IcImageWrap(pObj.mSmallImage);
         if pObj.mCurCell < aImg:GetNumCells() - 2 then
         	pObj:StartFadeRects(PhFadeAnotherTimeLong);
         	pObj.mCurCell = pObj.mCurCell + 1;
         	pObj:StopParticleEmission(false);
			else
			   pObj.mState = aState + 1;
			   local aNum = pObj:GetFirstActive(0);
           	pObj:SetActive(0, aNum, false);
			   pObj:SetActive(0, aNum + 1, true);
			end

		end

	end

end


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

   local aState = 0 + pObj.mState;

	if pName == "" then
	
      if pPolyNum == 1 or pPolyNum == 2 then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");

      elseif pPolyNum == 3 then

         if aState == 2 then
            pObj:StartFadeRects(PhFadeAnotherTimeLong);
         	pObj.mState   = aState        + 1;
         	pObj.mCurCell = pObj.mCurCell + 1;
			   pObj:StopParticleEmission(false);
			   IcPlaySample("PH_SFX_KNOBTURNEDON");

         else
            cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
            IcPlaySample("PH_SFX_DOES_NOT_WORK");
         end

      elseif pPolyNum == 4 then

         if not cm.SpawnObject(pObj, 0, pObj.mSmPos, "SpawnObject") then
            return true;
         end

         pObj:StartFadeRects(PhFadeAnotherTimeReallyShort);
      	pObj.mState   = aState        + 1;
      	pObj.mCurCell = pObj.mCurCell + 1;
      	cm.DisableObject(pObj);
      	IcPlaySample("PH_SFX_OBJ_PICKUP");

      end
		return true;

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

      if aState == 0 then
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState     = aState        + 1;
   		pObj.mCurCell   = pObj.mCurCell + 1;
			pObj:SetActive(0, pPolyNum,     false);
			pObj:SetActive(0, pPolyNum + 1, true);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		return true;
      end
      return _G[aTmpVal].MaybeLater(pObj);

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

      if aState == 1 then
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState     = aState        + 1;
   		pObj.mCurCell   = pObj.mCurCell + 1;
			pObj:SetActive(0, pPolyNum,     false);
			pObj:SetActive(0, pPolyNum + 1, true);

         pObj.mDiscovered = false;
         pObj:StopParticleEmission(false);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		return true;
      end
      return _G[aTmpVal].MaybeLater(pObj);

	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(0);

end

-- Show some info
_G[gTable].MaybeLater =  function(pObj)
   pObj.mPdata:UnMouseObject();
   cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
   IcPlaySample("PH_SFX_DOES_NOT_WORK");
   return true;
end
