_G[gTable] = {};

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


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


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


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

   if aState == 2 then
      if cm.GetGlobalBoolIndirect(pObj, "DeActivatorFlag") then
   		pObj.mState  = aState + 1;
   		aState       = pObj.mState;
         cm.DisableObject(pObj);
      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
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");

      elseif aState == 1 then

      	pObj.mState		  = aState + 1;
   		pObj.mCurCell	  = pObj.mCurCell + 1;
   		aState           = pObj.mState;

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

   		pObj:StopParticleEmission(false);
   		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);

      elseif aState == 2 then

      	pObj.mState		  = aState - 1;
   		pObj.mCurCell	  = pObj.mCurCell - 1;
   		aState           = pObj.mState;

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

   		pObj:StopParticleEmission(false);
   		cm.SetGlobalValIndirect(pObj, "SetFlag1", false);

      end
   	return true;

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

		pObj.mState		  = aState + 1;
		pObj.mCurCell	  = pObj.mCurCell + 1;
		pObj.mDiscovered = false;
		aState           = pObj.mState;

      -- Disable polygons
		pObj:SetActive(0, pPolyNum, false);
		pObj:SetActive(0, pPolyNum + 1, true);

		-- Remove the key
		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		pObj:StopParticleEmission(false);

		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

-- Zooms Out
_G[gTable].NormalSize = cm.ZoomOut;


-- Zooms In
_G[gTable].GetBigger = cm.ZoomIn;


