_G[gTable] = {};

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


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

   aUsed = false;
	pObj.mState = 0;
	pObj.mDiscovered = true;
	pObj.mCsData:SetVal("Used", false);
	pObj.mCsData:SetVal("NumUsed", 0);
	pObj.mAlpha:AncSetupChange(0, 255.0, PhMaterializeTime, IcAutoNumChgr.IC_ANCM_LINEAR);
	_G[aTmpVal].MoveToSlot(pObj, PhKeyToSlotTime, true);

end


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


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

   local aNumUses = pObj.mCsData:GetInt("NumUsed");
   aNumUses = aNumUses + 1;
	pObj.mCsData:SetVal("NumUsed", aNumUses);
  	IcPlaySample("PH_SFX_OBJ_USED");

   -- Used enough times?
	if aNumUses == pObj.mCsData:GetInt("MaxUses") then
      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);
      pObj:DisablePolys();
   end

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;

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

      if pObj.mState == 0 then
   		pObj.mState   = pObj.mState   + 1;
   		pObj.mCurCell = pObj.mCurCell + 1;

   		pObj:StopParticleEmission(false);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		return true;
		else
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
		   return false;
		end
		return false;

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

      if pObj.mState == 1 then
   		pObj.mState   = pObj.mState   + 1;
   		pObj.mCurCell = pObj.mCurCell + 1;

   		pObj:StopParticleEmission(false);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
   		return true;
		else
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
		   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 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);


end


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

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



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

