_G[gTable] = {};

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

-- Called on Init
_G[gTable].OnInit = function(pObj)
   aState = pObj.mState;
end


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

-- Called after level is loaded.
_G[gTable].OnLevelLoad = function(pObj)
   aState = pObj.mState;
end

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

	if aState == 3 then
		if not pObj.mAlpha:AncIsChanging() then
         aState      = aState + 1;
			pObj.mState = aState;
		end
	end

end


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

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

      if aState == 0 then

         aState         = aState + 1;
			pObj.mState    = aState;
			pObj.mCurCell  = pObj.mCurCell + 1;
			pObj:SetActive(0, pPolyNum, false);
			pObj:SetActive(0, pPolyNum + 1, true);
			pObj:StopParticleEmission(false);
			cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
         IcPlaySample("PH_SFX_OBJ_USED");

      elseif aState == 1 then

         aState         = aState + 1;
			pObj.mState    = aState;
			pObj.mCurCell  = pObj.mCurCell + 1;
			pObj:SetActive(0, pPolyNum, false);
			pObj:SetActive(0, pPolyNum + 1, true);
			pObj:StopParticleEmission(false);
			cm.SetGlobalValIndirect(pObj, "SetFlag2", true);
         IcPlaySample("PH_SFX_OBJ_USED");

      elseif aState == 2 then

         aState         = aState + 1;
			pObj.mState		= aState;
			pObj:SetActive(0, pPolyNum, false);
			cm.SetGlobalValIndirect(pObj, "SetFlag3", true);

			pObj.mDrawer   = "DrawFadeAppearDisappear";
   		pObj.mAlpha:AncSetupChange(255.0, 0.0, PhVanish50,
            IcAutoNumChgr.IC_ANCM_LINEAR);

			pObj:StopParticleEmission(false);
         IcPlaySample("PH_SFX_OBJ_USED");

		end

		return true;

	else
      pObj.mPdata:UnMouseObject();
		cm.ObjDoesNotWork(pObj);
		return true;
	end

	return false;
end



-- Called on draw
_G[gTable].OnDraw =  function(pObj, pG)

	local aImg  = IcImageWrap(pObj.mSmallImage);
	assert(aImg);
	local aRpos = pObj.mSmPos;
	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;

