
_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;
	if aState >= 2 and aState < 4 then
	   local aNext = false;
	   if aState == 3 then
		 aNext = true;
		end
	   _G[aTmpVal].StartGlow(pObj, aNext);
	end
end


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

   if aState == 2 then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
	      pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         aState      	= aState + 1
         pObj.mState 	= aState;
	      pObj.mCurCell	= pObj.mCurCell + 1;
         _G[aTmpVal].StartGlow(pObj, true);

         cm.ShowInfo(pObj, "The spell worked!", 140);
      end
   end

end


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

	pObj.mDiscovered = true;

	if pName == "" then
   	cm.ShowInfo(pObj, "This pot was probably used to brew something", 180);
   	return true;

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

		if aState == 0 then
	      pObj:StartFadeRects(PhFadeAnotherTimeMedium);
	      aState      	= aState + 1;
	      pObj.mState 	= aState;
	      pObj.mCurCell	= pObj.mCurCell + 1;
			_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
			pObj:StopParticleEmission(false);
		else
		   cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
		   pObj.mPdata:UnMouseObject();
		   IcPlaySample("PH_SFX_DOES_NOT_WORK");
		end

		return true;

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

		if aState == 1 then
	      pObj:StartFadeRects(PhFadeAnotherTimeMedium);
	      aState      	= aState + 1;
	      pObj.mState 	= aState;
	      pObj.mCurCell = pObj.mCurCell + 1;
			_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
			pObj:StopParticleEmission(false);
	      cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
	      _G[aTmpVal].StartGlow(pObj, false);
		else
		   cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
		   pObj.mPdata:UnMouseObject();
		   IcPlaySample("PH_SFX_DOES_NOT_WORK");
		end
		return true;

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

      if(aState == 0) then
         cm.ShowInfo(pObj, "The pot is empty");
         pObj.mPdata:UnMouseObject();
         return true;

      elseif(aState < 3) then
         cm.ShowInfo(pObj, "The liquid in the pot is currently not useful", 160);
         pObj.mPdata:UnMouseObject();
         return true;
      end
      
      if not cm.SpawnObjectMouse(pObj, 0, pX, pY, "SpawnObject") then
         pObj.mPdata:UnMouseObject();
         return true;
      end

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

	else

		cm.ObjDoesNotWork(pObj);
		IcPlaySample("PH_SFX_DOES_NOT_WORK");
		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



-- Starts particle system glow
_G[gTable].StartGlow = function(pObj, pOther)

   local aStart = Color(34,  36,  123, 50);
	local aMid	 = Color(253, 244, 236, 255);
	local aEnd	 = Color(156, 116, 22,  50);
	pObj.mDiscovered	= true;
   pObj:SetupParticleSystem("PH_PSYS_S01_L03_BUBBLES");
   
   if pOther then
      pObj:SetEmitterColors(aStart, aMid, aEnd);
	end
   pObj:StartParticleEmission(1);

end

