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

	if pObj.mState > 1 and pObj.mState <= 6 then
	   _G[aTmpVal].StartParticleSystem(pObj, 2, pObj.mState - 1);
	elseif pObj.mState > 6 then
		_G[aTmpVal].StartParticleSystem(pObj, 2, 5);
	end

end

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

	if pObj.mState > 1 and pObj.mState <= 6 then
	   _G[aTmpVal].StartParticleSystem(pObj, 2, pObj.mState - 1);
	elseif pObj.mState > 6 then
		_G[aTmpVal].StartParticleSystem(pObj, 2, 5);
	end

end

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

		if pObj.mState == 5 then
         if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
            pObj.mState = pObj.mState + 1;
            _G[aTmpVal].StartParticleSystem(pObj, 2, pObj.mState - 1);
			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 pObj.mState == 0 then
         cm.GetShowInfo(pObj, "InactiveStr1", "InactiveTime");
      else
         cm.GetShowInfo(pObj, "InactiveStr4", "InactiveTime4");
      end
		return true;

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

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

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

      if pObj.mState > 0 then

   		pObj.mState  = pObj.mState + 1;
   		pObj:StopParticleEmission(false);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);

         if pObj.mState == 5 then
   		   cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
         end

			_G[aTmpVal].StartParticleSystem(pObj, 2, pObj.mState - 1);
		   return true;

      else
         cm.GetShowInfo(pObj, "InactiveStr2", "InactiveTime");
         return false;
      end
      return true;

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

		if pObj.mState == 6 then

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

			if not cm.SpawnObjectMouse(pObj, 0, pX, pY, "SpawnObject") then
            return true;
         end

   		pObj.mState	= pObj.mState + 1;
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		   return true;

      else
         cm.GetShowInfo(pObj, "InactiveStr3", "InactiveTime3");
         return false;
      end

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

		if not cm.SpawnObjectMouse(pObj, 0, pX, pY, "SpawnObject2") then
         return true;
      end

		cm.DisableObject(pObj);
		_G[pActorObj.mScriptTag].OnUse(pActorObj, 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

-- Starts particle system
_G[gTable].StartParticleSystem =  function(pObj, pPolyNum, pIndex)

	local aColorVec =
	{
	   { Start = Color(51,21,79, 100), Mid = Color(115,46,207, 255), End = Color(50,45,96, 100) },
	   { Start = Color(80,20,43, 100), Mid = Color(192,78,101, 255), End = Color(97,45,44, 100) },
	   { Start = Color(19,20,81, 100), Mid = Color(21, 27,232, 255), End = Color(37,39,103,100) },
	   { Start = Color(37,56,63, 100), Mid = Color(23,193,230, 255), End = Color(54,78,86, 100) },
	   { Start = Color(64,51,36, 100), Mid = Color(238,196,49, 255), End = Color(114,101,27,100) }
	}

	local aTable    = aColorVec[pIndex];
   local aPsysName = pObj.mCsData:GetString("ParticleSystemName");
   pObj:SetupParticleSystem(aPsysName);
   pObj:SetEmitterColors(aTable.Start, aTable.Mid, aTable.End);
   pObj:StartParticleEmission(pPolyNum);

end

