
_G[gTable] = {};

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


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

   aUsed            = false;
	pObj.mState      = 0;
   aState           = pObj.mState;
	pObj.mDiscovered = true;
	pObj.mCsData:SetVal("Used", aUsed);

   pObj.mAlpha:AncSetupChange(0, 255.0, PhMaterializeTime, IcAutoNumChgr.IC_ANCM_LINEAR);
	_G[aTmpVal].MoveToSlot(pObj, PhKeyToSlotTime, true);
   cm.SetGlobalValIndirect(pObj, "SetFlag1", false);

end


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
   aUsed    = pObj.mCsData:GetBool("Used");
   aState   = pObj.mState;
end


-- Called on use
_G[gTable].OnUse = function(pObj, pOtherObj)


   if aState == 0 then
      if pOtherObj.mName == pObj.mCsData:GetString("ActivatorObj1") then
         aState         = aState + 1;
         pObj.mState    = aState;
         pObj.mCurCell  = pObj.mCurCell + 1;

         cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
        	IcPlaySample("PH_SFX_OBJ_USED");
        	pObj.mPdata:UnMouseObject();
        	return;
     	end
   elseif aState == 1 then
      if pOtherObj.mName == pObj.mCsData:GetString("ActivatorObj2") then
         aState         = aState + 1;
         pObj.mState    = aState;

         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
         aUsed = true;
      	pObj.mCsData:SetVal("Used", aUsed);
      	pObj.mPdata.mBar:RemoveSlot(pObj.mId);
      	pObj.mPdata:UnMouseObject();
      	pObj.mAlpha:AncSetupChange(255.0, 0.0, PhKeyVanishAfterUse,
          IcAutoNumChgr.IC_ANCM_LINEAR);
      	IcPlaySample("PH_SFX_OBJ_USED");
        	return;
     	end
   end

   cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
   IcPlaySample("PH_SFX_DOES_NOT_WORK");

end


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

   cm.MoveToSlotUpdate(pObj);

	if aUsed then
		if not pObj.mAlpha:AncIsChanging() then
			pObj.mIsDone = true;
		end
	end

end


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

	if pName == "" then
		pObj.mPdata:MouseTheObject(pObj);
		IcPlaySample("PH_SFX_OBJ_PICKUP");
		return true;
	end
	cm.ObjDoesNotWork(pObj);
	return false;


end


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

	if pObj.mIsDone then
		return;
	end
	pG:PushState();

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

	pG:PopState();

end


-- Called on moused
_G[gTable].OnMoused =  function(pObj)
	print(2+2);
end

-- Called on Unmoused
_G[gTable].OnUnMoused =  function(pObj)
	if not pObj.mCsData:GetBool("Used") then
		_G[aTmpVal].MoveToSlot(pObj, PhKeyToSlotTime, false);
	end
end


-- Called to reduce the size
_G[gTable].NormalSize =  function(pObj)
	print(2+2);
end


-- Called on draw
_G[gTable].GetBigger =  function(pObj, aTime1)
	print(2+2);
end


-- Moves the key to a slot
_G[gTable].MoveToSlot = cm.MoveToSlot;

