_G[gTable] = {};

local aTmpVal    = gTable;
local aStartTime = 0;
local aStartTick = false;
cm.SetupTable(_G[aTmpVal]);

-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
	aStartTime	= 0;
	aStartTick	= false;
   pObj.mCsData:SetVal("aStartTime", aStartTime);
   pObj.mCsData:SetVal("aStartTick", aStartTick);
end


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


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

   if pObj.mState == 5 then
		if not pObj.mAlpha:AncIsChanging() then
         pObj.mState  = pObj.mState + 1;
			pObj.mIsDone = true;
		end
	end
	
	if (not pObj.mCsData:GetBool("HelpShown1")) and cm.GetGlobalBoolIndirect(pObj, "HelpActivator1") then

		if (not aStartTick) then

		   aStartTick = true;
		   aStartTime = pObj.mUpdateCount;
		   pObj.mCsData:SetVal("aStartTime", aStartTime);
		   pObj.mCsData:SetVal("aStartTick", aStartTick);

		elseif aStartTick then
		   if pObj.mUpdateCount - aStartTime > 100*7 then
		      _G[aTmpVal].ShowHelp(pObj);
		   end
		end

	end

end


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

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

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

	if pName == "" then
	
	   if aState == 0 or aState == 1 or aState == 2 then

			pObj:StartFadeRects(PhFadeAnotherTimeMedium);
   		pObj.mState		  = aState + 1;
   		pObj.mCurCell	  = pObj.mCurCell + 1;

			pObj:SetActive(0, pPolyNum, 		false);
			pObj:SetActive(0, pPolyNum + 1,	true);
			pObj:StopParticleEmission(false);
			IcPlaySample("PH_SFX_OBJ_USED");
   		return true;


   	elseif aState == 4 then

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

   		pObj.mState		= aState + 1;
			pObj.mDrawer	= "DrawFadeAppearDisappear";
			pObj.mCsData:SetVal("DisappearStart", aState + 1);
			pObj.mCsData:SetVal("DisappearEnd", aState + 2);

   		pObj.mAlpha:AncSetupChange(255.0, 0.0, PhSpawnerVanishAfterUse,
            IcAutoNumChgr.IC_ANCM_LINEAR);
   		cm.DisableObject(pObj);
   		IcPlaySample("PH_SFX_OBJ_PICKUP");
   		return true;

   	end

      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      if pObj.mCsData:GetBool("HelpShown1") and (not pObj.mCsData:GetBool("HelpShown2")) then
			local aInfoStr  = pObj.mCsData:GetString("InfoStr3");
			pObj.mPdata:ShowHelp(pObj.mGobj.mDataManager:GetString(aInfoStr),
				IcVector2d(492, 473), false, false);
			pObj.mCsData:SetVal("HelpShown2", true);
			cm.SetGlobalValIndirect(pObj, "SetFlag2", true);
		end
   	return true;

   -- The chest is unlocked
	elseif pName == pObj.mCsData:GetString("KeyObject") then

      if aState == 3 then

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
   		pObj.mState		  = aState + 1;
   		pObj.mCurCell	  = pObj.mCurCell + 1;

   		pObj:SetActive(0, pPolyNum, false);
   		pObj:SetActive(0, pPolyNum + 1, true);
   		pObj:StopParticleEmission(false);

   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);

			local aInfoStr  = pObj.mCsData:GetString("InfoStr4");
			local aX, aY	 = cm.GetSpawnPos(pObj, 0, pObj.mSmPos);
			pObj.mPdata:ShowHelp(pObj.mGobj.mDataManager:GetString(aInfoStr),
				IcVector2d(aX, aY), false, false);
			pObj.mCsData:SetVal("HelpShown3", true);
			cm.SetGlobalValIndirect(pObj, "SetFlag3", true);

   		return true;
		end

		cm.ObjDoesNotWork(pObj);
		return false;

	else
		cm.ObjDoesNotWork(pObj);
		return false;
	end

	return false;
end

-- Called on mouse enter
--_G[gTable].OnMouseEnter =  function(pObj, pMousedObj)

-- 	if cm.GetGlobalBoolIndirect(pObj, "HelpActivator1") then
-- 		if not pObj.mCsData:GetBool("HelpShown1") then
-- 		   _G[aTmpVal].ShowHelp(pObj);
-- 	 	end
--  	end

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

-- Shows help
_G[gTable].ShowHelp =  function(pObj)

	local aInfoStr  = pObj.mCsData:GetString("InfoStr2");
	pObj.mPdata:ShowHelp(pObj.mGobj.mDataManager:GetString(aInfoStr),
		IcVector2d(534, 436), false, false);
	pObj.mCsData:SetVal("HelpShown1", true);
	cm.SetGlobalValIndirect(pObj, "SetFlag1", true);

end


