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


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

end


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

	cm.ZoomOutUpdate(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

   else

      -- Check if the puzzle is solved
      if pObj.mState == 0 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("ObjSfxId") then
				   local aStr = pObj.mCsData:GetString("ObjSfxId");
				   IcPlaySample(aStr);
				end

         end
      end

   end

end


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

	pObj.mDiscovered = true;

   -- If not active, show a message & exit
	if not pObj.mCsData:GetBool("IsActivated") then
      cm.GetShowInfo(pObj, "InactiveStr", "InactiveTime");
   	if pName == "" then
         return true;
      end
   	return false;
	end
	
	if pName == "" then

		if pObj.mCurImg == 0 then
		
         -- If the puzzle is solved, allow zoom in
         if pObj.mState == 1 then
            _G[aTmpVal].GetBigger(pObj, PhGetBigTime);
			   return true;
			end
			
			-- Spawn Puzzle
			cm.LaunchPuzzle(pObj);
         return true;

		else

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

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

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

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

	return false;

end



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


-- Called on moused
_G[gTable].OnMoused =  function(pObj)

end

-- Called on Unmoused
_G[gTable].OnUnMoused =  function(pObj)

end

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


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


