_G[gTable] = {};

local aTmpVal   = gTable;
local aTargetCell = {};
aTargetCell[0] = 0;
aTargetCell[1] = 6;
aTargetCell[2] = 10;
aTargetCell[3] = 14;
aTargetCell[4] = 18;
aTargetCell[5] = 24;
local aCurFace = 0;

cm.SetupTable(_G[aTmpVal]);

-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
	aCurFace		= 0;
   cm.SetGlobalValIndirect(pObj, "CurrentFace", aCurFace);
   pObj.mCsData:SetVal("aCurFace", aCurFace);
   pObj.mBigPos = pObj.mSmPos;
	cm.SetGlobalValIndirect(pObj, "PuzzlesSolved", 0);

end


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
   aCurFace = pObj.mCsData:GetInt("aCurFace");
end


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

   _G[aTmpVal].MoveShila(pObj);

   local aCurCell 	= 0 + pObj.mCurCell;
   local aLastCell	= pObj.mCsData:GetInt("LastCell");
   local aState   	= 0 + pObj.mState;


   if aState < 2 then

      local aNextFace = cm.GetGlobalIntIndirect(pObj, "CurrentFace");
      
      -- Rotate the shila
      if aNextFace ~= aCurFace then

			if not pObj.mAlpha:AncIsChanging() then
	         local aTrgCell = aTargetCell[aNextFace];
				local aNextCell= 1 + aCurCell;
				if aNextCell > aLastCell then
				   aNextCell = 0;
				end

	   		pObj:SetActive(0, aCurFace, false);
				if aTrgCell == aNextCell then
				   aCurFace = aNextFace;
				   pObj.mCsData:SetVal("aCurFace", aCurFace);
				end

	         pObj:StartFadeRects(PhFadeAnotherTimeTotallyReallyShort);
	   		pObj.mCurCell   = aNextCell;
	   		pObj:StopParticleEmission(false);
	   		IcPlaySample("PH_SFX_ICESLABSLIDES2");
   		end

      else
      
         -- Enable the puzzles if necessary
         if cm.GetGlobalBoolIndirect(pObj, "FaceChange")
            and (not pObj.mAlpha:AncIsChanging()) then
            
            cm.SetGlobalValIndirect(pObj, "FaceChange", false);
            local aStr = string.format("PuzzleFlag%d", aCurFace);
            
            --IcPrintLog("Checking "..aStr.."\n");
				-- Enable the puzzles
            if aCurFace == 4 then
	            if cm.GetGlobalIntIndirect(pObj, aStr) ~= 1 then
	               pObj:SetActive(0, aCurFace, true);
	               --IcPrintLog(string.format("Activated Face %d\n", aCurFace));
	            end
				else

	            if not cm.GetGlobalBoolIndirect(pObj, aStr) then
	               pObj:SetActive(0, aCurFace, true);
	               --IcPrintLog(string.format("Activated Face %d\n", aCurFace));
	            end

				end

			end

		end
	end

	-- Enable the path
	if aCurFace == 0 then
      if cm.GetGlobalBoolIndirect(pObj, "SetFlag1") then
         pObj.mState = aState + 1;
         pObj:SetActive(0, aCurFace, true);
         cm.SetGlobalValIndirect(pObj, "SetFlag2", true);
      end
   end

   if aState == 0 then

      if aCurFace == 1 then

         if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag1")
         and pObj:GetFirstActive(0) == aCurFace then

            pObj:StopParticleEmission(false);
            pObj:SetActive(0, aCurFace, false);
            local aCounter = 1 + pObj.mCsData:GetInt("PuzzlesSolved");
            pObj.mCsData:SetVal("PuzzlesSolved", aCounter);
            cm.SetGlobalValIndirect(pObj, "OnePuzzleSolved", true);
            IcPlaySample("PH_SFX_CLOSER_LOOK");

         end

      elseif aCurFace == 2 then

         if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag2")
         and pObj:GetFirstActive(0) == aCurFace then

            pObj:StopParticleEmission(false);
            pObj:SetActive(0, aCurFace, false);
            local aCounter = 1 + pObj.mCsData:GetInt("PuzzlesSolved");
            pObj.mCsData:SetVal("PuzzlesSolved", aCounter);
            cm.SetGlobalValIndirect(pObj, "OnePuzzleSolved", true);
            IcPlaySample("PH_SFX_CLOSER_LOOK");
            
         end

      elseif aCurFace == 3 then

         if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag3")
         and pObj:GetFirstActive(0) == aCurFace then

            pObj:StopParticleEmission(false);
            pObj:SetActive(0, aCurFace, false);
            local aCounter = 1 + pObj.mCsData:GetInt("PuzzlesSolved");
            pObj.mCsData:SetVal("PuzzlesSolved", aCounter);
            cm.SetGlobalValIndirect(pObj, "OnePuzzleSolved", true);
            IcPlaySample("PH_SFX_CLOSER_LOOK");

         end

      elseif aCurFace == 4 then

         if cm.GetGlobalIntIndirect(pObj, "PuzzleFlag4") == 1
         and pObj:GetFirstActive(0) == aCurFace then

            pObj:StopParticleEmission(false);
            pObj:SetActive(0, aCurFace, false);
            local aCounter = 1 + pObj.mCsData:GetInt("PuzzlesSolved");
            pObj.mCsData:SetVal("PuzzlesSolved", aCounter);
            cm.SetGlobalValIndirect(pObj, "OnePuzzleSolved", true);
            IcPlaySample("PH_SFX_CLOSER_LOOK");

         end

      elseif aCurFace == 5 then

         if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag5")
         and pObj:GetFirstActive(0) == aCurFace then

            pObj:StopParticleEmission(false);
            pObj:SetActive(0, aCurFace, false);
            local aCounter = 1 + pObj.mCsData:GetInt("PuzzlesSolved");
            pObj.mCsData:SetVal("PuzzlesSolved", aCounter);
            cm.SetGlobalValIndirect(pObj, "OnePuzzleSolved", true);
            IcPlaySample("PH_SFX_CLOSER_LOOK");

         end

      end


      if pObj.mCsData:GetInt("PuzzlesSolved") == 5 then
         pObj.mState = aState + 1;
         IcPlaySample("PH_SFX_GOOD_JOB");
      end
      
   end

end


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


   local aState   = 0 + pObj.mState;
   local aCurCell = 0 + pObj.mCurCell;
	pObj.mDiscovered = true;

	if pName == "" then

      if pPolyNum == 0 then

         if aState < 2 then
            cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");

         else
            if not cm.SpawnObject(pObj, 0, pObj.mSmPos, "SpawnObject") then
               return true;
            end
            cm.SetGlobalValIndirect(pObj, "SetFlag4", true);
            cm.DisableObject(pObj);
            IcPlaySample("PH_SFX_OBJ_PICKUP");
         end

      elseif pPolyNum == 1 then
         local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag1");
         local aPuzzIndex  = pObj.mCsData:GetInt("PuzzleNum1");
         AddJigsawWidget(pObj.mPdata, aPuzzleFlag, aPuzzIndex);
         IcPlaySample("PH_SFX_CLOSER_LOOK");

      elseif pPolyNum == 2 then
   	   local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag2");
   	   local aPuzzIndex = pObj.mCsData:GetInt("PuzzleNum2");
   	   AddSliderWidget(pObj.mPdata, aPuzzleFlag, aPuzzIndex);
         IcPlaySample("PH_SFX_CLOSER_LOOK");

      elseif pPolyNum == 3 then
   	   local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag3");
   	   AddMemoryWidget(pObj.mPdata, aPuzzleFlag);
         IcPlaySample("PH_SFX_CLOSER_LOOK");

      elseif pPolyNum == 4 then
         local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag4");
         local aPuzzIndex  = pObj.mCsData:GetInt("PuzzleNum4");
         local aPuzzImg    = pObj.mCsData:GetString("PuzzleImage4");
         pObj.mPdata.mCsData:SetVal(aPuzzleFlag, 0);
   		AddRiddleWidget(pObj.mPdata, aPuzzleFlag, aPuzzImg, aPuzzIndex);
         IcPlaySample("PH_SFX_CLOSER_LOOK");

      elseif pPolyNum == 5 then
   	   local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag5");
   	   local aPuzzIndex = pObj.mCsData:GetInt("PuzzleNum5");
   	   AddSequenceWidget(pObj.mPdata, aPuzzleFlag, aPuzzIndex);
         IcPlaySample("PH_SFX_CLOSER_LOOK");

      end

	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

-- Called to move the shila up & down
local aUpdateCount = 0;
_G[gTable].MoveShila =  function(pObj)

	if cm.GetGlobalBoolIndirect(pObj, "SetFlag3") then
	   return;
	end

   aUpdateCount	= (aUpdateCount + 1)%360;
	if (aUpdateCount == 0 or aUpdateCount == 180)
	 and cm.GetGlobalBoolIndirect(pObj, "SetFlag1") then
	   cm.SetGlobalValIndirect(pObj, "SetFlag3", true);
	   pObj.mSmPos = pObj.mBigPos;
	   return;
	end


	local aAngle   = (aUpdateCount/180.0)*math.pi;
   local aSinVal	= math.sin(aAngle);
   local aYoffset = aSinVal*10;

	local aRpos = IcVector2d(0, aYoffset) + pObj.mBigPos;
	pObj.mSmPos = aRpos;

end

