_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)

	if pObj.mState == 5 then
	   local aPsysName = pObj.mCsData:GetString("ParticleSystemName");
	   pObj:SetupParticleSystem(aPsysName);
	   pObj:StartParticleEmission(1);
	end

end


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

   if pObj.mState == 4 then

      -- Update object state
      pObj.mState       = pObj.mState + 1;
   	local aNum = pObj:GetFirstActive(0);
   	pObj:SetActive(0, aNum, false);

      -- Update game flags
		local aFlagName = pObj.mCsData:GetString("SetFlag1");
		pObj.mPdata.mCsData:SetVal(aFlagName, true);

	   local aPsysName = pObj.mCsData:GetString("ParticleSystemName");
	   pObj:SetupParticleSystem(aPsysName);
	   pObj:StartParticleEmission(1);
	   pObj.mDiscovered = true;
	   
	   IcPlaySample("PH_SFX_GOOD_JOB");

	end

end


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


	pObj.mDiscovered = true;

	if pName == "" then

      cm.ShowInfo(pObj, "A rune built into the ground. Looks magical.", 200);
		return true;

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

      pObj:StartFadeRects(PhFadeAnotherTimeShort);
		pObj.mState		= pObj.mState + 1;
		pObj.mCurCell	= pObj.mCurCell + 1;
		pObj:StopParticleEmission(false);
		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		return true;

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

		assert(pObj.mCurImg == 0);
		if pObj.mState == 1 then

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState		= pObj.mState + 1;
   		pObj.mCurCell	= pObj.mCurCell + 1;
   		pObj:StopParticleEmission(false);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		   return true;

      else
         cm.ShowInfo(pObj, "Perhaps another piece needs to be fitted first.");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return false;
      end

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

		assert(pObj.mCurImg == 0);
		if pObj.mState == 2 then

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState		= pObj.mState + 1;
   		pObj.mCurCell	= pObj.mCurCell + 1;
   		pObj:StopParticleEmission(false);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		   return true;

      else
         cm.ShowInfo(pObj, "Perhaps another piece needs to be fitted first.");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return false;
      end

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

		assert(pObj.mCurImg == 0);
		if pObj.mState == 3 then

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState		= pObj.mState + 1;
   		pObj.mCurCell	= pObj.mCurCell + 1;
   		pObj:StopParticleEmission(false);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		   return true;

      else
         cm.ShowInfo(pObj, "Perhaps another piece needs to be fitted first.");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return false;
      end

	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




