_G[gTable] = {};

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


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


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


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

	cm.ZoomOutUpdate(pObj);

	-- See if the activation condition is met
   if not aActivated then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj.mCsData:SetVal("IsActivated", true);
         aActivated = true;
      end

   else

      if pObj.mState == 0 then
         if cm.GetGlobalBoolIndirect(pObj, "VisibleFlag") then

            pObj.mState = pObj.mState   + 1;
   			pObj:SetActive(0, 0, true);
   			pObj:SetActive(1, 0, true);
   			
   			pObj.mCsData:SetVal("Visible",    true);
   			pObj.mCsData:SetVal("IsFadingIn", true);
   			pObj.mAlpha:AncSetupChange(0.0, 255.0, PhMaterializeTime,
               IcAutoNumChgr.IC_ANCM_LINEAR);
         end

      -- Check if the puzzle is solved
      elseif pObj.mState == 1 then
         if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag") then

            pObj.mState   = pObj.mState   + 1;
   			pObj.mCurCell = pObj.mCurCell + 1;

   			local aNum = pObj:GetFirstActive(0);
   			pObj:SetActive(0, aNum, false);
   			pObj:SetActive(1, aNum, false);
   			pObj:SetActive(0, aNum + 1, true);
   			pObj:SetActive(1, aNum + 1, true);
            pObj:StopParticleEmission(false);
            _G[aTmpVal].GetBigger(pObj, PhGetBigTime);

         end
         
         if pObj.mCsData:GetBool("IsFadingIn") then
            if not pObj.mAlpha:AncIsChanging() then
               pObj.mCsData:SetVal("IsFadingIn", false);
            end
         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, "InactiveStr", "InactiveTime");
   	if pName == "" then
         return true;
      end
   	return false;
	end

	if pName == "" then

		if pObj.mCurImg == 0 then

         -- If the puzzle is solved, allow zoom in
         if pObj.mState == 2 then
            _G[aTmpVal].GetBigger(pObj, PhGetBigTime);
			   return true;
			end

			-- Spawn Puzzle
			assert(pObj.mState == 1);
		   local aPuzzleType = pObj.mCsData:GetString("PuzzleType");

		   if aPuzzleType == "Jigsaw" then
            local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag");
            local aPuzzIndex  = pObj.mCsData:GetInt("PuzzleNum");
            AddJigsawWidget(pObj.mPdata, aPuzzleFlag, aPuzzIndex);
         elseif aPuzzleType == "Sequence" then
   		   local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag");
   		   local aPuzzIndex = pObj.mCsData:GetInt("PuzzleNum");
   		   AddSequenceWidget(pObj.mPdata, aPuzzleFlag, aPuzzIndex);
         elseif aPuzzleType == "Sliders" then
   		   local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag");
   		   local aPuzzIndex = pObj.mCsData:GetInt("PuzzleNum");
   		   AddSliderWidget(pObj.mPdata, aPuzzleFlag, aPuzzIndex);
         elseif aPuzzleType == "Memory" then
   		   local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag");
   		   AddMemoryWidget(pObj.mPdata, aPuzzleFlag);
         elseif aPuzzleType == "Password" then
   		   local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag");
   		   local aPuzzIndex = pObj.mCsData:GetInt("PuzzleNum");
   		   AddPasswordPuzzleWidget(pObj.mPdata, aPuzzleFlag, aPuzzIndex);
         elseif aPuzzleType == "Question" then
   		   local aPuzzleFlag = pObj.mCsData:GetString("PuzzleFlag");
   		   local aPuzzAnswer = pObj.mCsData:GetString("PuzzleAnswer");
   		   local aPuzzQuestion = pObj.mCsData:GetString("PuzzleQuestion");
   		   AddAnswerWidget(pObj.mPdata, aPuzzleFlag, aPuzzAnswer, aPuzzQuestion);
		   end
		   return true;

		else

			if pObj.mState == 2 then
				pObj.mState		  = pObj.mState   + 1;
				pObj.mCurCell	  = pObj.mCurCell + 1;

				pObj:SetActive(0, pPolyNum, false);
				pObj:SetActive(1, pPolyNum, false);
				pObj.mDiscovered = false;
				pObj:StopParticleEmission(false);

				_G[aTmpVal].NormalSize(pObj);
				pObj.mCb:Disappear();

				cm.SpawnObject(pObj, 0, pObj.mBigPos, "SpawnObject");
				IcPlaySample("PH_SFX_OBJ_PICKUP");
				return true;
			end
			return true;
		end

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

	return false;

end



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

	if not pObj.mCsData:GetBool("Visible") then
      return;
   end


	local aRpos = pObj.mSmPos;
	local aImg  = IcImageWrap(pObj.mSmallImage);
	assert(aImg);
	local aRect = aImg:GetCelRect(pObj.mCurCell);

	if pObj.mCsData:GetBool("IsFadingIn") then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
   end

	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(pObj.mCurCell);
		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

      -- Draw containing box/circle
		local aBoxImg = IcImageWrap("PH_IMG_CMN_ZOOM_BOX");
		assert(aBoxImg);
		aRect = aBoxImg:GetCelRect(0);
		aRpos = pObj.mFrmPos;
		pG:DrawImage(aBoxImg:GetImage(), aRpos.x, aRpos.y, aRect);
	end


end


-- Called on moused
_G[gTable].OnMoused =  function(pObj)
	print(2+2);
end

-- Called on Unmoused
_G[gTable].OnUnMoused =  function(pObj)
	print(2+2);
end

-- Zooms Out
_G[gTable].NormalSize = cm.ZoomOut;


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


