_G[gTable] = {};

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

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


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

end


-- Called to fetch cursor data
_G[gTable].GetCursorInfo = function(pObj, pPolyNum, pName)
	local aCursorTable	= {};
   aCursorTable[0]		= true;
   aCursorTable[1]		= IcGobj.CURSOR_POINTER;

   if pObj.mState == 3 then
      aCursorTable[1] = IcGobj.CURSOR_FOOT;
	end
   return aCursorTable;

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 pObj.mState == 0 then
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState		 = pObj.mState   + 1;
   		pObj.mCurCell   = pObj.mCurCell + 1;

         -- Disable polygons
   		pObj:SetActive(0, pPolyNum, false);
   		pObj:SetActive(0, pPolyNum + 1, true);
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         pObj:StopParticleEmission(false);

         -- Set global flag
   		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);

		elseif pObj.mState == 1 then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
		else
         -- Warp to next stage or level
   		local aNextLevel = pObj.mCsData:GetInt("NextLevel");
   		pObj.mPdata:SetNextLevel(aNextLevel);

         local aNextStage = pObj.mCsData:GetBool("NextStage");
         pObj.mPdata:LevelComplete(aNextStage);
         pObj:StopParticleEmission(false);
         IcPlaySample("PH_SFX_FOOTSTEPSONLADDER");
   		return true;
		end
   	return true;

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

      if pObj.mState == 1 then

         if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
            cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
            return false;
         end

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState		 = pObj.mState   + 1;
   		pObj.mCurCell   = pObj.mCurCell + 1;

         -- 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;
		end
		
		cm.ObjDoesNotWork(pObj);
		return true;

	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



