_G[gTable] = {};

local aTmpVal = gTable;
local aButtons= {
	{ aPos = IcVector2d(212, 151), aImg = "PH_IMG_STG5_SC4_WIDGET_BTN_00", aWeight = "0", mClicked = 0 },
	{ aPos = IcVector2d(259, 151), aImg = "PH_IMG_STG5_SC4_WIDGET_BTN_01", aWeight = "1", mClicked = 0 },
	{ aPos = IcVector2d(311, 151), aImg = "PH_IMG_STG5_SC4_WIDGET_BTN_02", aWeight = "2", mClicked = 0 },
	{ aPos = IcVector2d(358, 151), aImg = "PH_IMG_STG5_SC4_WIDGET_BTN_03", aWeight = "3", mClicked = 0 },
};
local aWrongAnswer = false;

cm.SetupTable(_G[aTmpVal]);


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState  = 0;
	aWrongAnswer = false;
	pObj.mCsData:SetVal("aWrongAnswer", aWrongAnswer);
end

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

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

   aWrongAnswer = pObj.mCsData:GetBool("aWrongAnswer");

end

_G[gTable].GetParticleSystemInfo = function(pObj, pPolyNum, pName)

	local gMouseOverTable	= {};
   gMouseOverTable[0] = true;
   if pPolyNum == 6 then
      gMouseOverTable[0] = false;
	end
   gMouseOverTable[1] = "PH_PSYS_UNDYING_SPARKLE";
   return gMouseOverTable;

end


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

	cm.ZoomOutUpdate(pObj);

   local aState = 0 + pObj.mState;
   if aState == 0 then

      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
      	pObj.mState   = aState        + 1;
      	pObj.mCurCell = pObj.mCurCell + 1;
		   pObj:StopParticleEmission(false);
			pObj:SetActive(0, 0, true);
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
         IcPlaySample("PH_SFX_CUPBOARDOPENS");
      end

   elseif aState >= 2 then

      for aI = 1, 4 do
         if aButtons[aI].mClicked > 0 then
            aButtons[aI].mClicked = aButtons[aI].mClicked - 1;
			end
		end

   end
   
   if aWrongAnswer then

      aCount = aCount + 1;
      if aCount == 10 then
         aCount = 0;
         aWrongAnswer = false;
         pObj.mCsData:SetVal("PressedButtonz", "");
         pObj.mCsData:SetVal("aWrongAnswer", aWrongAnswer);
		end

	end


end


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

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

	if pName == "" then
	
	   if pObj.mCurImg == 0 then
	      _G[aTmpVal].GetBigger(pObj, PhGetBigTime);
	      return true;
	      
		elseif pPolyNum == 6 then
		   return true;

      elseif aState == 1 then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         return true;

      elseif aState == 2 then

         aButtons[pPolyNum].mClicked = 8;
         local aPressed = pObj.mCsData:GetString("PressedButtonz");
         aPressed = aPressed..aButtons[pPolyNum].aWeight;
         local aSol = pObj.mCsData:GetString("PuzzleSolution");
         
         if aSol ~= aPressed then
            if #aPressed >= #aSol then
               aWrongAnswer = true;
               pObj.mCsData:SetVal("aWrongAnswer", aWrongAnswer);
               IcPlaySample("PH_SFX_DOES_NOT_WORK");
					cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
				end
				pObj.mCsData:SetVal("PressedButtonz", aPressed);
				IcPlaySample("PH_SFX_MACHINEBUTTONPRESSED2");
				return true;
			end
			
			pObj.mCsData:SetVal("PressedButtonz", aPressed);
			pObj.mState	= aState + 1;
			pObj:SetActive(1, 1, false);
			pObj:SetActive(1, 2, false);
			pObj:SetActive(1, 3, false);
			pObj:SetActive(1, 4, false);
			pObj:SetActive(1, 5, true);
			cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
			IcPlaySample("PH_SFX_GOOD_JOB");

			return true;

      elseif aState == 3 then

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

   		pObj.mState		= aState + 1;
   		pObj.mCurCell	= pObj.mCurCell - 1;
   		cm.DisableObject(pObj);
			_G[aTmpVal].NormalSize(pObj);
			pObj.mCb:Disappear();
   		IcPlaySample("PH_SFX_OBJ_PICKUP");
   		return true;

      end
		return true;

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

	   if pObj.mCurImg == 0 then
	      _G[aTmpVal].GetBigger(pObj, PhGetBigTime);
	      return true;
		end

      if aState == 1 then
   		pObj.mState		= aState + 1;
   		pObj.mCurCell	= pObj.mCurCell + 1;
   		pObj:StopParticleEmission(false);
   		pObj:SetActive(1, pPolyNum, false);
   		pObj:SetActive(1, pPolyNum + 1, true);
   		pObj:SetActive(1, pPolyNum + 2, true);
   		pObj:SetActive(1, pPolyNum + 3, true);
   		pObj:SetActive(1, pPolyNum + 4, true);
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
         cm.GetShowInfo(pObj, "InfoStr6", "InfoTime6");
   		return true;
      end
      return _G[aTmpVal].MaybeLater(pObj);

	else
		cm.ObjDoesNotWork(pObj);
		return false;
	end

	return false;

end


-- Called on draw
_G[gTable].OnDraw =  function(pObj, pG)

	local aRpos = IcVector2d(0, 0) + pObj.mSmPos;
	local aImg  = IcImageWrap(pObj.mSmallImage);
	assert(aImg);
	
	local aCell = 0 + pObj.mCurCell;
	if aCell > aImg:GetNumCells() - 1 then
	   aCell = aImg:GetNumCells() - 1;
	end
	local aRect = aImg:GetCelRect(aCell);
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

	if pObj.mCurImg == 1 then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));

      -- Draw zoomed image
		aImg = IcImageWrap(pObj.mBigImage);
		assert(aImg);
		aRpos = pObj.mBigPos;
		aRect = aImg:GetCelRect(-1 + pObj.mCurCell);
		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);
		
		-- Draw Buttons
		local aMPoly = pObj:GetMouseOverPoly();
		for aI = 1, 4 do

			local aBtnImg = IcImageWrap(aButtons[aI].aImg);
			aCell = 0;
			if aButtons[aI].mClicked > 0 then
			   aCell = 2;
			elseif aMPoly == aI then
			   aCell = 1;
			end

			aRect = aBtnImg:GetCelRect(aCell);
			aRpos = pObj.mBigPos + aButtons[aI].aPos;
			pG:DrawImage(aBtnImg:GetImage(), aRpos.x, aRpos.y, aRect);
		end
		
		-- Draw Markers
		local aMarkers		= pObj.mCsData:GetString("PressedButtonz");
		if aMarkers ~= "" then
			local aMarkerImg = IcImageWrap("PH_IMG_STG5_SC4_WIDGET_MARK");
			local aPos		  = IcVector2d(225, 80) + pObj.mBigPos;
			local aWidth     = aMarkerImg:GetCelWidth();

			for aI in string.gmatch(aMarkers, "%d") do
			   local aTheCell = 0 + aI;
			   aRect = aMarkerImg:GetCelRect(aTheCell);
			   pG:DrawImage(aMarkerImg:GetImage(), aPos.x, aPos.y, aRect);
			   aPos.x = aPos.x + aWidth;
			end

		end

	end

end


-- Show some info
_G[gTable].MaybeLater =  function(pObj)

   pObj.mPdata:UnMouseObject();
   cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
   IcPlaySample("PH_SFX_DOES_NOT_WORK");
   return true;

end


-- Called on draw
_G[gTable].GetBigger =  function(pObj, pTime1)

	pObj.mCurImg = 1;
	pObj.mAlpha:AncEndChange();
	pObj.mAlpha:AncSetupChange(0.0, 255.0, pTime1, IcAutoNumChgr.IC_ANCM_LINEAR);
	pObj:StopParticleEmission(false);

	local aImg1 	= IcImageWrap(pObj.mBigImage);
   local aRect1	= aImg1:GetCelRect(0);
	pObj.mBigPos.x = (gGobj.mWidth - aRect1.mWidth)/2;
	pObj.mBigPos.y = gGobj.mHeight - pObj.mPdata.mBar:GetWorkingHeight() - aRect1.mHeight;
	pObj.mFrmPos	= pObj.mBigPos;

   local aImg3 	= IcImageWrap("PH_IMG_CMN_ZOOM_BOX_CLOSE");
   local aRect3	= aImg3:GetCelRect(0);
	local aCbPos	= IcVector2d(aRect1.mWidth - aRect3.mWidth, 0);

	pObj.mCb:SetPos(aCbPos);
	pObj.mCb:StartAppearing(pTime1, pTime1);

	pObj.mCsData:SetValf("CbPosX", aCbPos.x);
	pObj.mCsData:SetValf("CbPosY", aCbPos.y);
	pObj.mCsData:SetVal("Time1", pTime1);
	IcPlaySample("PH_SFX_CLOSER_LOOK");

end


-- Called to reduce the size
_G[gTable].NormalSize =  cm.ZoomOut;

