_G[gTable] = {};

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


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

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

end


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

   local aState = 0 + pObj.mState;
   if aState == 0 then

      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj:StartFadeRects(PhFadeAnotherTimeRatherLong);
   		pObj.mState		  = aState + 1;
   		pObj.mCurCell	  = pObj.mCurCell + 1;

         pObj:StopParticleEmission(false);
         local aNum = pObj:GetFirstActive(0);
         pObj:SetActive(0, aNum,     false);
         pObj:SetActive(0, aNum + 1, true);
      end

	elseif aState == 1 then
		if not pObj.mAlpha:AncIsChanging() then
         pObj.mDrawer   = "DrawFrontFadeMulti";
         pObj.mState    = aState + 1;
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
		end

	elseif aState == 3 then
	end

end


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

   local aState = 0 + pObj.mState;
   if aState >= 7 then
      if pName ~= "" then
         pObj.mPdata:UnMouseObject();
		end
      return true;
	end

   pObj.mDiscovered = true;

	if pName == "" then

      if aState == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      else
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
      end
		return true;

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

      if aState == 2 or aState == 3 then

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
   		pObj.mState		  = aState + 1;
   		pObj.mCurCell	  = pObj.mCurCell + 1;
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		return true;
		end

      _G[aTmpVal].MaybeLater(pObj);
		return true;

   elseif pName == pObj.mCsData:GetString("KeyObject3")
		or pName == pObj.mCsData:GetString("KeyObject4") then

      if aState == 4 or aState == 5 then

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
   		pObj.mState		  = aState + 1;
   		pObj.mCurCell	  = pObj.mCurCell + 1;
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
   		return true;
		end

      _G[aTmpVal].MaybeLater(pObj);
		return true;

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

      if aState == 6 then

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
   		pObj.mState		  = aState + 1;
   		pObj.mCurCell	  = pObj.mCurCell + 1;
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);

			pObj.mDiscovered = false;
			pObj:StopParticleEmission(false);
   		cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
   		cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
   		return true;
		end

      _G[aTmpVal].MaybeLater(pObj);
		return true;

	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 (aState == 3 or aState == 1) then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
	end
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end



-- Tell the player that this will not work
_G[gTable].MaybeLater =  function(pObj)
   pObj.mPdata:UnMouseObject();
   IcPlaySample("PH_SFX_DOES_NOT_WORK");
   cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
end

-- Mouse over particle system info
_G[gTable].GetParticleSystemInfo = function(pObj, pPolyNum, pName)
   local gMouseOverTable	= {};
   gMouseOverTable[0]		= true;
   gMouseOverTable[1]		= "PH_PSYS_UNDYING_SPARKLE";
   
   if pObj.mState >= 7 then
      gMouseOverTable[0] = false;
	end

   return gMouseOverTable;
end
