
-- Code for the talking heads in Stage 2, Level 2

_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 == 2 then
      local aPuzzleStatus = cm.GetGlobalIntIndirect(pObj, "PuzzleFlag");
      if aPuzzleStatus == 0 then
			pObj.mState    = 0;
			pObj.mCurCell  = 0;
      end
	end


end

-- Called after a level is exited
_G[gTable].OnLevelExit = function(pObj)

   if pObj.mState == 2 then
      local aPuzzleStatus = cm.GetGlobalIntIndirect(pObj, "PuzzleFlag");
      if aPuzzleStatus == 0 then
			pObj.mState    = 0;
			pObj.mCurCell  = 0;
      end
	end

end


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


	if pObj.mState == 1 then
	
	   if not pObj.mAlpha:AncIsChanging() then
	      pObj.mState = pObj.mState + 1;
	      local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag");
	      local aPuzzIndex = pObj.mCsData:GetInt("PuzzleNum");
	      pObj.mPdata.mCsData:SetVal(aPuzzleFlag, 0);
	      AddRiddleWidget(pObj.mPdata, aPuzzleFlag, pObj.mSmallImage, aPuzzIndex);
	      IcPlaySample("PH_SFX_CLOSER_LOOK");
	   end

	elseif pObj.mState == 2 then

      local aPuzzleStatus = cm.GetGlobalIntIndirect(pObj, "PuzzleFlag");

      -- Riddle successfully solved
      if aPuzzleStatus == 1 then

	      pObj:StartFadeRects(PhFadeAnotherTimeLong);
         pObj.mState    = 3;
		   pObj.mCurCell  = 2;
		   cm.DisableObject(pObj);
         
         -- Update puzzle counters
         local aCounter = 0;
         local aCounterFlag = pObj.mCsData:GetString("CounterFlag");
         if pObj.mPdata.mCsData:IfExistsInt(aCounterFlag) then
            aCounter = pObj.mPdata.mCsData:GetInt(aCounterFlag);
         end
         aCounter = aCounter + 1;
         pObj.mPdata.mCsData:SetVal(aCounterFlag, aCounter);

      -- Riddle not solved
      elseif aPuzzleStatus == -1 then

	      pObj:StartFadeRects(PhFadeAnotherTimeLong);
         pObj.mState    = 0;
		   pObj.mCurCell  = 0;
		   pObj:StopParticleEmission(false);

		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

	      pObj:StopParticleEmission(false);
	      pObj:StartFadeRects(PhFadeAnotherTimeLong);
	      pObj.mState     = pObj.mState + 1;
			pObj.mCurCell   = pObj.mCurCell + 1;
			IcPlaySample("PH_SFX_OBJ_USED");
		end
		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 pObj.mCsData:GetBool("Changing") then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
		local aRectNum = pObj.mCsData:GetInt("OldRectCell");
		local aRect2   = aImg:GetCelRect(aRectNum);
		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect2);
		pG:SetColor(Color(255, 255, 255, 255 - pObj.mAlpha:AncGetVal()));
	end

	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end


-- Zooms Out
_G[gTable].NormalSize = function(pObj)
   pObj.mCurCell = pObj.mCurCell - 1;
end


-- Zooms In
_G[gTable].GetBigger = cm.ZoomIn;



