_G[gTable] = {};

local aTmpVal     = gTable;
local aInfoShown  = false;
local aCount      = 0;
cm.SetupTable(_G[aTmpVal]);

-- Called on Init
_G[gTable].OnInit = function(pObj)
	aCount = 0;
	aInfoShown = false;
	pObj.mCsData:SetVal("aInfoShown", aInfoShown);
	pObj.mCsData:SetVal("LfireCounter", 0);

	local aActivatorFlag1 = pObj.mCsData:GetString("ActivatorFlag1");
	pObj.mPdata.mCsData:SetVal(aActivatorFlag1, 0);
end

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

-- Called after a level is loaded
_G[gTable].OnLevelLoad = function(pObj)
   pObj.mGobj.mSoundPlayer:PlayMusic("IcMusicBuilderInsides");
  	pObj.mGobj.mSoundPlayer:PlayAmbient("Ambient_UnderGround", 5);
  	aInfoShown = pObj.mCsData:GetBool("aInfoShown");
end


-- Called after a level is exited
_G[gTable].OnLevelExit = function(pObj)
   pObj.mGobj.mSoundPlayer:ClearMusic(0);
   pObj.mGobj.mSoundPlayer:FadeoutLoopingSfx(40);
   pObj.mGobj.mSoundPlayer:FadeoutAmbient(20);
end


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

	if not aInfoShown then
		aCount = aCount + 1;
		if aCount == 25 then
			aInfoShown = true;
			pObj.mCsData:SetVal("aInfoShown", aInfoShown);
			--pObj.mPdata:ShowCurLevelInfo();
			pObj.mPdata:ShowCurLevelInfo("PH_SFX_INGAME_08");
		end
	end
	
	-- Handle levers
	local aSetFlag1 = pObj.mCsData:GetString("SetFlag1");
	if not pObj.mPdata.mCsData:GetBool(aSetFlag1) then

   	local aActivatorFlag1  = pObj.mCsData:GetString("ActivatorFlag1");
   	local aLeverStates     = pObj.mPdata.mCsData:GetInt(aActivatorFlag1);
   	local aLfireCounter    = pObj.mCsData:GetInt("LfireCounter");

      if aLeverStates == 100 and (not pObj.mCsData:GetBool("GreenTriDone")) then
         pObj.mPdata.mCsData:SetVal("Sc3ShowGreenTri", true);
	      pObj.mCsData:SetVal("GreenTriDone", true);
         aLfireCounter = aLfireCounter + 1;
         IcPrintLog("Sc3ShowGreenTri fired\n");

      elseif aLeverStates == 110 and (not pObj.mCsData:GetBool("BlueTriDone")) then
         pObj.mPdata.mCsData:SetVal("Sc3ShowBlueTri", true);
	      pObj.mCsData:SetVal("BlueTriDone", true);
         aLfireCounter = aLfireCounter + 1;
         IcPrintLog("Sc3ShowBlueTri fired\n");

      elseif aLeverStates == 11 and (not pObj.mCsData:GetBool("RedTriDone")) then
         pObj.mPdata.mCsData:SetVal("Sc3ShowRedTri", true);
	      pObj.mCsData:SetVal("RedTriDone", true);
         aLfireCounter = aLfireCounter + 1;
         IcPrintLog("Sc3ShowRedTri fired\n");
      end
	   pObj.mCsData:SetVal("LfireCounter", aLfireCounter);

	   if aLfireCounter == 3 then
         pObj.mPdata.mCsData:SetVal(aSetFlag1, true);
      end
	end


end

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

	if pName == "" then
	
      if pPolyNum == 0 then
      	local aNextLevel = pObj.mCsData:GetInt("NextLevel1");
      	pObj.mPdata:SetNextLevel(aNextLevel);
      	pObj.mPdata:LevelComplete(pObj.mCsData:GetBool("NextStage"));
   	end
      return true;

	else
		cm.ObjDoesNotWork(pObj);
		return false;
	end

end

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

	local aImg = IcImageWrap(pObj.mSmallImage);
	if (pObj.mCurImg == 1) then
		aImg = IcImageWrap(pObj.mBigImage);
	end
	assert(aImg);

	aRect = aImg:GetCelRect(pObj.mCurCell);
	if pObj:SmIsMoving() then
		pG:DrawImageF(aImg:GetImage(), pObj.mSmPos.x, pObj.mSmPos.y, aRect);
	else
		pG:DrawImage(aImg:GetImage(), pObj.mSmPos.x, pObj.mSmPos.y, aRect);
	end

end

