_G[gTable] = {};

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

-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
   cm.SetGlobalValIndirect(pObj, "CurrentFace", 0);
end


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

end


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

   local aState = 0 + pObj.mState;
   if aState == 0 then
      if cm.GetGlobalBoolIndirect(pObj, "DeActivatorFlag1") then
         pObj.mState = aState + 1;
         cm.DisableObject(pObj);
      end
   end

end


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

   if cm.GetGlobalBoolIndirect(pObj, "OnePuzzleSolved") then
      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      
      if pName ~= "" then
         pObj.mPdata:UnMouseObject();
      end
      IcPlaySample("PH_SFX_DOES_NOT_WORK");
      return true;
   end

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

	if pName == "" then
      pObj:StopParticleEmission(false);
      local aIndex    = cm.GetGlobalIntIndirect(pObj, "CurrentFace");
      local aMaxIndex = pObj.mCsData:GetInt("MaxFaces");
      local aNewIndex = (aIndex + 1)%aMaxIndex;

      cm.SetGlobalValIndirect(pObj, "CurrentFace", aNewIndex);
      cm.SetGlobalValIndirect(pObj, "FaceChange", true);
      IcPlaySample("PH_SFX_BIGBUTTONPRESSED");
      return true;

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

   return false;
end



-- Called when mouse enters the object
_G[gTable].OnMouseEnter = function(pObj)

   pObj.mCurCell = 1;

end


-- Called when mouse leaves object
_G[gTable].OnMouseLeave = function(pObj)

   pObj.mCurCell = 0;

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


