_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)
	print(2+2);
end


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

   if pObj.mState == 3 then

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

      -- Update game flags
		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);

   end

end


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

	pObj.mDiscovered = true;
	if pName == "" then
   	cm.GetShowInfo(pObj, "InfoStr1", "InfoTime");
		return true;

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

		assert(pObj.mCurImg == 0);
		assert(pObj.mState  == 0);

		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.mState		= pObj.mState   + 1;
   		pObj.mCurCell	= pObj.mCurCell + 1;
   		pObj:StopParticleEmission(false);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		   return true;

      else
         -- Tell the player he's made a mistake
         cm.GetShowInfo(pObj, "MistakeStr", "MistakeTime");
         return false;
      end

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

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

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

      else
         -- Tell the player he's made a mistake
         cm.GetShowInfo(pObj, "MistakeStr", "MistakeTime");
         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



-- Called on moused
_G[gTable].OnMoused =  function(pObj)
	print(2+2);
end

-- Called on Unmoused
_G[gTable].OnUnMoused =  function(pObj)
	print(2+2);
end


