
-- Code for the big head 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 to fetch cursor data
_G[gTable].GetCursorInfo = function(pObj, pPolyNum, pName)
	local aCursorTable	= {};
   aCursorTable[0]		= true;
   aCursorTable[1]		= IcGobj.CURSOR_POINTER;

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

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    = pObj.mState + 1;
		   pObj.mCurCell  = 2;

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

         -- 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);
         
         -- Update global flag
         cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
			IcPlaySample("PH_SFX_GOOD_JOB");

      -- 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
	      local aCounter = 0;
	      local aCounterFlag = pObj.mCsData:GetString("CounterFlag");
	      if pObj.mPdata.mCsData:IfExistsInt(aCounterFlag) then
	         aCounter = pObj.mPdata.mCsData:GetInt(aCounterFlag);
	      end

	      if aCounter < 8 then
	         local aStr2 = string.format("There are %d others that wish to ask you a question.", 8 - aCounter);
	         if aCounter == 7 then
	            aStr2 = "There is one more that would ask a question of you.";
	         end
	         cm.ShowInfo(pObj, aStr2, 200);
	         return true;
	      end

	      pObj:StopParticleEmission(false);
	      pObj:StartFadeRects(PhFadeAnotherTimeLong);
	      pObj.mState     = pObj.mState + 1;
			pObj.mCurCell   = pObj.mCurCell + 1;
   		IcPlaySample("PH_SFX_OBJ_USED");
			return true;

		elseif pObj.mState == 3 then

      	local aNextLevel = pObj.mCsData:GetInt("NextLevel");
      	pObj.mPdata:SetNextLevel(aNextLevel);
      	pObj.mPdata:LevelComplete(pObj.mCsData:GetBool("NextStage"));
			IcPlaySample("PH_SFX_FOOTSTEPSONGROUND");
      	return true;

		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

   if pObj.mState ~= 2 then
	  pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);
	end

end


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


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



