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

      elseif aState == 1 or aState == 2 then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");

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

	elseif pName == pObj.mCsData:GetString("KeyObject1") then
	
      if aState == 0 then
         if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
            _G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
            return false;
         end

         pObj:StartFadeRects(PhFadeAnotherTimeLong);
         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);
   		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
		end
	   return true;

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

      if aState == 3 then
         pObj:StartFadeRects(PhFadeAnotherTimeLong);
         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

		pObj.mPdata:UnMouseObject();
      cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
      IcPlaySample("PH_SFX_DOES_NOT_WORK");
	   return true;

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

      if aState == 1 or aState == 2 then

         if aState == 1 then
            cm.SetGlobalValIndirect(pObj, "SetFlag2", true);
         else
            cm.SetGlobalValIndirect(pObj, "SetFlag3", true);
         end

         pObj:StartFadeRects(PhFadeAnotherTimeLong);
         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);

		elseif aState == 4 then
		   cm.DisableObject(pObj);
         cm.SetGlobalValIndirect(pObj, "SetFlag4", true);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		pObj.mPdata:UnMouseObject();
   		return true;
      else
         cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
         return false;
		end

	   return true;


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

	return false;

end



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

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

	pG:PopState();
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


