_G[gTable] = {};

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

-- Called on Init
_G[gTable].OnInit = function(pObj)

	pObj.mState		  = 1;
	pObj.mDiscovered = true;

	_G[aTmpVal].SpawnParticleSystem(pObj, "PH_PSYS_UNDYING_TRAIL_SCROLL");
	local aImg    = IcImageWrap(pObj.mSmallImage);
	local aCentre = IcVector2d(aImg:GetCelWidth()/2, aImg:GetCelHeight()/2);

   assert(pObj.mCsData:IfExistsInt("TargetX"));
   assert(pObj.mCsData:IfExistsInt("TargetY"));
   local aTarget     = IcVector2d(pObj.mCsData:GetInt("TargetX"), pObj.mCsData:GetInt("TargetY")) - aCentre;
   local aDist       = aTarget - pObj.mSmPos;
   local aMoveTime   = PhMoveTime100;
   local aVel        = aDist/aMoveTime;

   aVel.y = aVel.y*2;
   local aAccel  = ((aDist - aVel*aMoveTime)*2)/(aMoveTime*aMoveTime);
   pObj:SmSetTarget(aTarget);
   pObj:SmSetVelAccel(aVel, aAccel)
   pObj:SmSetMovementMode(IcSimpleMovement.IC_SMM_PRESET_TIMED);
   pObj:SmStartMove(aMoveTime);
   
end


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

	if pObj.mState == 1 then
		_G[aTmpVal].SpawnParticleSystem(pObj, "PH_PSYS_UNDYING_TRAIL_SCROLL");
   end

end


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

   if pObj.mState == 1 then
   	if not pObj:SmIsMoving() then
         pObj.mState = pObj.mState + 1;
         pObj:StopParticleEmission(false);
   	end

   elseif pObj.mState == 2 then
      pObj.mState = pObj.mState + 1;
      _G[aTmpVal].SpawnParticleSystemZoom(pObj, "PH_PSYS_BURST_SCROLL");
      local aNum = pObj:GetFirstActive(0);
      pObj:SetActive(0, aNum, false);

	   IcPlaySample("PH_SFX_MAGICTAKESEFFECT");

   elseif pObj.mState == 3 then
      if pObj:ParticleEmitterFinished() then
         pObj.mState = pObj.mState + 1;
      	cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
      end

   elseif pObj.mState == 4 then
      if not pObj:ParticleEmitterOn() then
         pObj.mState = pObj.mState + 1;
      	pObj.mDiscovered = false;
      	pObj.mIsDone     = true;
      end
	end

end


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

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

	if pObj.mCsData:GetBool("Used") or pObj.mCsData:GetBool("MovingToSlot") then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
	end
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end


-- Called to spawn particle system
_G[gTable].SpawnParticleSystem =  function(pObj, pName)

	local aImg    = IcImageWrap(pObj.mSmallImage);
	local aCentre = IcVector2d(aImg:GetCelWidth()/2, aImg:GetCelHeight()/2);
	local aOldPos = IcVector2d(0, 0) + pObj.mSmPos;
	local aNewPos = aCentre + pObj.mSmPos;
	pObj.mSmPos = aNewPos;

   pObj:SetupParticleSystem(pName);
   pObj:StartParticleEmission(0);
	pObj.mSmPos = aOldPos;

end

-- Called to spawn particle system
_G[gTable].SpawnParticleSystemZoom =  function(pObj, pName)

   assert(pObj.mCsData:IfExistsInt("ParticleX"));
   assert(pObj.mCsData:IfExistsInt("ParticleY"));
	local aPos	= IcVector2d(pObj.mCsData:GetInt("ParticleX"), pObj.mCsData:GetInt("ParticleY"));
	pObj.mBigPos= aPos;
	local aCurImg = pObj.mCurImg;
	pObj.mCurImg  = 1;
   pObj:SetupParticleSystem(pName);
   pObj:StartParticleEmission(0);
	pObj.mCurImg = aCurImg;

end

