_G[gTable] = {};

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


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
	pObj.mCsData:SetVal("Downsizing", false);
	pObj.mCsData:SetVal("AppearStart", 1);
	pObj.mCsData:SetVal("AppearEnd",   2);
end

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

end


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

   -- Prevent aState from being a reference to pObj.mState
   local aState = 0 + pObj.mState;

	if aState == 0 then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj.mState = aState + 1;
   		pObj.mAlpha:AncSetupChange(0.0, 255.0, PhMaterializeTimeLong,
            IcAutoNumChgr.IC_ANCM_LINEAR);

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

      end

	elseif aState == 1 then
		if not pObj.mAlpha:AncIsChanging() then
         pObj.mState = aState + 1;
         pObj.mDrawer= "DrawSimpleZoomable";
         pObj:SetActive(0, 0, true);
         pObj:SetActive(1, 0, true);
		end

   elseif aState == 2 then
      if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag") then

         pObj.mState   = pObj.mState + 1;
			pObj.mCurCell = pObj.mCurCell + 1;

			local aNum = pObj:GetFirstActive(0);
			pObj:SetActive(0, aNum, false);
			pObj:SetActive(1, aNum, false);
			pObj:SetActive(0, aNum + 1, true);
			pObj:SetActive(1, aNum + 1, true);
         pObj:StopParticleEmission(false);
         _G[aTmpVal].GetBigger(pObj, PhGetBigTime);

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

      end
	end

	cm.ZoomOutUpdate(pObj);

end



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

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

	if pName == "" then

		-- Spawn Puzzle
      if aState == 2 then
			cm.LaunchPuzzle(pObj);
         return true;
      end

		if pObj.mCurImg == 0 then
			_G[aTmpVal].GetBigger(pObj, PhGetBigTime);
			return true;

		else

			if aState == 3 then

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

				pObj.mState   = aState + 1;
				pObj.mCurCell = pObj.mCurCell + 1;
				cm.DisableObject(pObj);

				_G[aTmpVal].NormalSize(pObj);
				pObj.mCb:Disappear();
				IcPlaySample("PH_SFX_OBJ_PICKUP");
				return true;
			end
		end
		return true;

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

-- Called on draw
_G[gTable].OnDraw =  cm.DrawZoomable;


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


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




