_G[gTable] = {};

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


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
	pObj.mCsData:SetVal("AppearStart", 1);
	pObj.mCsData:SetVal("AppearEnd",   2);
	pObj.mCsData:SetVal("DisappearEnd",4);
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);
	local aState = pObj.mState;

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

   -- Check if the puzzle is solved
   elseif aState == 1 then

      if not pObj.mAlpha:AncIsChanging() then
         pObj.mState    = aState + 1;
         pObj.mDrawer	="DrawFadeMulti";
  			pObj:SetActive(0, 0, true);
         cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
      end

	elseif aState == 2 then

      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
  	      pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         pObj.mState     = aState + 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);
      end

   end


end


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

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

	if pName == "" then
	
      if aState < 3 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime");
         return true;

		elseif aState == 3 then

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

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
         pObj.mState     = aState + 1;
   		pObj.mCurCell   = pObj.mCurCell + 1;
   		cm.DisableObject(pObj);
   		IcPlaySample("PH_SFX_OBJ_PICKUP");
			return true;
		end

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

	if pObj.mCsData:GetBool("IsFadingIn") then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
   end

	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);


end



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


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


