
_G[gTable] = {};

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


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState= 0;
	aState     = pObj.mState;
	aCurCell   = pObj.mCurCell;
	pObj.mCsData:SetVal("FillCount", 0);
end


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

--Called after level being reloaded
_G[gTable].OnLevelLoad = function(pObj)
	aState   = pObj.mState;
	aCurCell = pObj.mCurCell;
end

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

end


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

	pObj.mDiscovered = true;
	if pName == "" then
	
      if aState == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         return true;

      elseif aState == 3 then

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

         pObj:StartFadeRects(PhFadeAnotherTimeLong);
   		aCurCell        = aCurCell + 1;
   		pObj.mCurCell   = aCurCell;

   		cm.DisableObject(pObj);
   		IcPlaySample("PH_SFX_OBJ_PICKUP");

      else
         if aAlternatePick == 4 then
            aAlternatePick = 0;
         	cm.GetShowInfo(pObj, "InfoStr6", "InfoTime6");
         else
            aAlternatePick = aAlternatePick + 1;
         	cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         end
         return true;
   	end

   	return true;

	elseif pName == pObj.mCsData:GetString("KeyObject1") then

      if aState == 0 then
         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
   		aState          = aState + 1;
   		pObj.mState		 = aState;
   		aCurCell        = aCurCell + 1;
   		pObj.mCurCell   = aCurCell;
   		pObj:SetActive(0, pPolyNum, false);
   		pObj:SetActive(0, pPolyNum + 1, true);
   		pObj:StopParticleEmission(false);

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

		cm.ObjDoesNotWork(pObj);
		return false;

	elseif pName == pObj.mCsData:GetString("KeyObject2") then

      if aState == 1 or aState == 2 then

         if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
            cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
            IcPlaySample("PH_SFX_DOES_NOT_WORK");
            return false;
         end

         if aState == 2 then
            pObj:StartFadeRects(PhFadeAnotherTimeLong);
      		aCurCell        = aCurCell + 1;
      		pObj.mCurCell   = aCurCell;

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

	   		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
	   		cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
         end

   		aState          = aState + 1;
   		pObj.mState		 = aState;
   		pObj:StopParticleEmission(false);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		return true;

      else
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
         return false;
   	end

      return false;

	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);
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end



