_G[gTable] = {};

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


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
	pObj.mCsData:SetVal("Downsizing", false);
	pObj.mCsData:SetVal("Visible", false);
   aActivated = pObj.mCsData:GetBool("IsActivated");
end


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
   aActivated = pObj.mCsData:GetBool("IsActivated");
end


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

	cm.ZoomOutUpdate(pObj);
	-- See if the activation condition is met
   if not aActivated then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj.mCsData:SetVal("IsActivated", true);
         aActivated = true;
      end

   else

      if pObj.mState == 0 then
         if cm.GetGlobalBoolIndirect(pObj, "VisibleFlag") then

            pObj.mState = pObj.mState + 1;
   			pObj:SetActive(0, 0, true);
   			pObj:SetActive(1, 0, true);

   			pObj.mCsData:SetVal("Visible",    true);
   			pObj.mCsData:SetVal("IsFadingIn", true);
   			pObj.mAlpha:AncSetupChange(0.0, 255.0, PhMaterializeTime,
               IcAutoNumChgr.IC_ANCM_LINEAR);

         end
      else

         if pObj.mCsData:GetBool("IsFadingIn") then
            if not pObj.mAlpha:AncIsChanging() then
               pObj.mCsData:SetVal("IsFadingIn", false);
            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
			_G[aTmpVal].GetBigger(pObj, PhGetBigTime);
			return true;
		else
			assert(pObj.mState == 1);
			pObj.mState		 = pObj.mState   + 1;
			pObj.mCurCell	 = pObj.mCurCell + 1;
			pObj.mDiscovered= false;
			pObj:SetActive(0, pPolyNum, false);
			pObj:SetActive(1, pPolyNum, false);
			pObj:StopParticleEmission(false);

			_G[aTmpVal].NormalSize(pObj);
			pObj.mCb:Disappear();
			cm.SpawnObject(pObj, 0, pObj.mBigPos, "SpawnObject");
			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)

	if not pObj.mCsData:GetBool("Visible") then
      return;
   end


	local aImg  = IcImageWrap(pObj.mSmallImage);
	assert(aImg);

	local aMaxCells = aImg:GetNumCells();
	local aCell = pObj.mCurCell;
	if aCell >= aMaxCells then
      aCell = aMaxCells - 1;
   end

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

	local aRect = aImg:GetCelRect(aCell);
	local aRpos = pObj.mSmPos;
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

	if (pObj.mCurImg == 1) then

		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));

		aImg = IcImageWrap(pObj.mBigImage);
		aRpos= pObj.mBigPos;
		assert(aImg);
		aRect = aImg:GetCelRect(pObj.mCurCell);
		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

		local aBoxImg = IcImageWrap("PH_IMG_CMN_ZOOM_BOX");
		assert(aBoxImg);
		aRect = aBoxImg:GetCelRect(0);
		aRpos = pObj.mFrmPos;
		pG:DrawImage(aBoxImg:GetImage(), aRpos.x, aRpos.y, aRect);

	end


end



-- Called on moused
_G[gTable].OnMoused =  function(pObj)
	print(2+2);
end

-- Called on Unmoused
_G[gTable].OnUnMoused =  function(pObj)
	print(2+2);
end

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


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

