_G[gTable] = {};

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

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


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


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

	-- See if the activation condition is met
   if not pObj.mCsData:GetBool("IsActivated") then

      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj.mCsData:SetVal("IsActivated", true);
      end

	elseif pObj.mState == 0 then

      if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag") then
         pObj.mState = pObj.mState + 1;
         cm.DisableObject(pObj);

			if pObj.mCsData:IfExistsString("ObjSfxId") then
			   local aStr = pObj.mCsData:GetString("ObjSfxId");
			   IcPlaySample(aStr);
			end

        	local aImg  = IcImageWrap(pObj.mSmallImage);
        	if aImg:GetNumCells() > pObj.mCurCell + 1 then
				pObj:StartFadeRects(PhFadeAnotherTimeShort);
				pObj.mCurCell	= pObj.mCurCell + 1;
        	end

      end
   end

end


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

	pObj.mDiscovered = true;
   if not pObj.mCsData:GetBool("IsActivated") then
      if pName == "" then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         return true;
      else
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         return false;
      end
   end

	if pName == "" then
		if pObj.mState == 0 then
         cm.LaunchPuzzle(pObj);
		   return true;
		end
		return false;
	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



-- Zooms Out
_G[gTable].NormalSize = cm.ZoomOut;


-- Zooms In
_G[gTable].GetBigger = cm.ZoomIn;


