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

	if aState == 0 then

		if pObj.mPdata.mCsData:GetBool("Sc1_Stairs_Appear_Start") then
         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         aState         = aState + 1;
			pObj.mState    = aState;
			pObj.mCurCell  = pObj.mCurCell + 1;
			pObj:StopParticleEmission(false);
		end

   elseif aState == 1 then

		if not pObj.mAlpha:AncIsChanging() then
         aState         = aState + 1;
			pObj.mState    = aState;

         local aNum = pObj:GetFirstActive(0);
			pObj:SetActive(0, aNum, false);
			pObj:SetActive(0, aNum + 1, true);
			cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
		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.ShowInfo(pObj, "The door is out of reach", 160);
			IcPlaySample("PH_SFX_DOES_NOT_WORK");
			return true;

		elseif aState == 2 then
			cm.ShowInfo(pObj, "The door is locked", 120);
			return true;

		elseif aState == 3 then
			pObj.mDiscovered = false;
			pObj.mPdata:SetNextLevel(1);
			pObj.mPdata:LevelComplete(false);
			IcPlaySample("PH_SFX_FOOTSTEPSONGROUND");
			return true;
		end

		return true;

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

		if aState == 0 or aState == 1 then
			cm.ShowInfo(pObj, "The door is out of reach", 180);
			IcPlaySample("PH_SFX_DOES_NOT_WORK");
			return false;

		elseif aState == 2 then

         aState         = aState + 1;
			pObj.mState    = aState;
			pObj.mCurCell	= pObj.mCurCell + 1;
			pObj:SetActive(0, pPolyNum, false);
			pObj:SetActive(0, pPolyNum + 1, true);
			pObj:StopParticleEmission(false);
			_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
			_G[aTmpVal].ShowHelp(pObj);
			IcPlaySample("PH_SFX_DOOROPENS");
			return true;
		end

		return false;

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

	if pObj.mState == 1 then

		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, (255 - pObj.mAlpha:AncGetVal())));
   	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

	   local aRect2 = aImg:GetCelRect(pObj.mCurCell + 1);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
   	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect2);

   else
   	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);
	end


end

-- Called to show help
_G[gTable].ShowHelp =  function(pObj)

	if (not pObj.mCsData:GetBool("HelpShown1")) then
		local aInfoStr  = pObj.mCsData:GetString("InfoStr2");
		pObj.mPdata:ShowHelp(pObj.mGobj.mDataManager:GetString(aInfoStr),
			IcVector2d(496, 72), false, true);
		pObj.mCsData:SetVal("HelpShown1", true);
	end

end

