_G[gTable] = {};

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

-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
   cm.SetGlobalValIndirect(pObj, "PuzzleFlag", false);
end


-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
end


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

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

      if cm.GetGlobalBoolIndirect(pObj, "PuzzleFlag") then

         pObj.mState = aState + 1;
         cm.DisableObject(pObj);
         cm.SetGlobalValIndirect(pObj, "SetFlag4", true);
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         IcPlaySample("PH_SFX_GOOD_JOB");

      elseif cm.GetGlobalBoolIndirect(pObj, "PuzzleFailFlag") then

         pObj.mState = aState + 1;
         cm.SetGlobalValIndirect(pObj, "SetFlag1", false);
         cm.SetGlobalValIndirect(pObj, "SetFlag2", false);
         cm.SetGlobalValIndirect(pObj, "SetFlag3", false);
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");

      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

      local aDisabled = true;
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1")
         and cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2")
         and cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag3")
      then
         aDisabled = false;
      end
      
      pObj.mState = 0;
      pObj.mCsData:SetVal("PuzzleDisabled", aDisabled);
      cm.SetGlobalValIndirect(pObj, "PuzzleFailFlag", false);
      cm.LaunchPuzzle(pObj);
		return true;

	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



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


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


