_G[gTable] = {};

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


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
	cm.SetGlobalValIndirect(pObj, "PuzzleFlag", false);
end


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

end


-- Called to fetch mouse over info
_G[gTable].GetCursorInfo = function(pObj, pPolyNum, pName)
   local aCursorTable	= {};
   aCursorTable[0]		= true;
   aCursorTable[1]		= IcGobj.CURSOR_POINTER;

   if pObj.mState >= 1 then
      aCursorTable[1] = IcGobj.CURSOR_FOOT;
   end

   return aCursorTable;
end




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

	-- See if it is within the player's reach
   if not pObj.mCsData:GetBool("IsActivated") then
      if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj.mCsData:SetVal("IsActivated", true);
      else
         return;
      end
   end

	cm.ZoomOutUpdate(pObj);
   if pObj.mState == 0 then

		if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag") then

         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
         pObj.mState   = pObj.mState + 1;
			pObj.mCurCell = pObj.mCurCell + 1;
         pObj:StopParticleEmission(false);

			if pObj.mCsData:IfExistsString("ObjSfxId1") then
			   local aStr = pObj.mCsData:GetString("ObjSfxId1");
			   IcPlaySample(aStr);
			else
			   IcPlaySample("PH_SFX_OBJ_USED");
			end

      end

	elseif pObj.mState == 1 then

	   if not pObj.mAlpha:AncIsChanging() then
      	local aImg = IcImageWrap(pObj.mSmallImage);
       	local aNumCells = aImg:GetNumCells();

       	if pObj.mCurCell < aNumCells - 1 then
	         pObj:StartFadeRects(PhFadeAnotherTimeMedium);
	         pObj:StopParticleEmission(false);
	         pObj.mCurCell = pObj.mCurCell + 1;
			else
			   pObj.mState = pObj.mState + 1;
				local aNum = pObj:GetFirstActive(0);
				pObj:SetActive(0, aNum, false);
				pObj:SetActive(0, aNum + 1, true);

			end

		end

	end


end


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

	pObj.mDiscovered = true;
   -- If not active, show a message & exit
	if not pObj.mCsData:GetBool("IsActivated") then

   	cm.GetShowInfo(pObj, "InactiveStr1", "InactiveTime");
   	if pName == "" then
         return true;
      end
   	return false;
	end

	if pName == "" then

		if pObj.mState == 0 then
         cm.LaunchPuzzle(pObj);
		   return true;

		elseif pObj.mState == 2 then

			local aNextLevel = pObj.mCsData:GetInt("NextLevel");
			local aNextStage = pObj.mCsData:GetBool("NextStage");
			pObj.mPdata:SetNextLevel(aNextLevel);
			pObj.mPdata:LevelComplete(aNextStage);

			if pObj.mCsData:IfExistsString("ObjSfxId2") then
			   local aStr = pObj.mCsData:GetString("ObjSfxId2");
			   IcPlaySample(aStr);
			else
			   IcPlaySample("PH_SFX_OBJ_USED");
			end

			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
      if pObj.mCsData:GetBool("FadingToNext") then
         local aOldRect = aImg:GetCelRect(pObj.mCurCell - 1);
         cm.FadeImageRects(pObj, pG, aImg, aRpos, aOldRect, aRect);
      else
         pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);
      end
   else
      pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);
   end

end


