_G[gTable] = {};

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


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
   local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag");
	pObj.mPdata.mCsData:SetVal(aPuzzleFlag, 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);
   if pObj.mState == 0 then

      if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag") then
         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         pObj.mState   = pObj.mState   + 1;
			pObj.mCurCell = pObj.mCurCell + 1;

			local aNum = pObj:GetFirstActive(0);
			pObj:SetActive(0, aNum, false);
			pObj:SetActive(0, aNum + 1, true);
         pObj:StopParticleEmission(false);
         IcPlaySample("PH_SFX_GOOD_JOB");
      end
   end

end


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

	pObj.mDiscovered = true;
	if pName == "" then

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

		   assert(pObj.mCurImg == 0);
		   cm.LaunchPuzzle(pObj);
		   return true;

		elseif pObj.mState == 1 then

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
			pObj.mState		  = pObj.mState   + 1;
			pObj.mCurCell	  = pObj.mCurCell + 1;
			pObj:SetActive(0, pPolyNum,     false);
			pObj:SetActive(0, pPolyNum + 1, true);
			pObj:StopParticleEmission(false);
			IcPlaySample("PH_SFX_CUPBOARDOPENS");
			return true;

		elseif pObj.mState == 2 then

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

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
			pObj.mState		  = pObj.mState   + 1;
			pObj.mCurCell	  = pObj.mCurCell + 1;
			cm.DisableObject(pObj);
			IcPlaySample("PH_SFX_OBJ_PICKUP");
			return true;
		end
		assert(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;


