_G[gTable] = {};
local aTmpVal = gTable;

local aButtonInfo =
                  {
                  {mActive = false, mX =  40, mY = 30, mFlip = 6},
						{mActive = false, mX =  84, mY = 30, mFlip = 3},
						{mActive = false, mX = 129, mY = 30, mFlip = 5},
						{mActive = false, mX = 173, mY = 30, mFlip = 6},
						{mActive = false, mX = 218, mY = 30, mFlip = 8},
						{mActive = false, mX = 263, mY = 30, mFlip = 5},

						{mActive = false, mX =  61, mY = 56, mFlip = 8},
						{mActive = false, mX = 109, mY = 56, mFlip = 0},
						{mActive = false, mX = 154, mY = 56, mFlip = 3},
						{mActive = false, mX = 201, mY = 56, mFlip = 2},
						{mActive = false, mX = 246, mY = 56, mFlip = 9},
						{mActive = false, mX = 292, mY = 56, mFlip = 11}
					  };

local aNumSpots	= 12;
local aClickCount	= 0;
cm.SetupTable(_G[aTmpVal]);




-- Called on Init
_G[gTable].OnInit = function(pObj)

	for i = 1, aNumSpots do
		aButtonInfo[i].mActive = false;
	end

	_G[aTmpVal].LoadInfo(pObj);
	_G[aTmpVal].SaveInfo(pObj);
	pObj.mAlpha:AncSetCurrent(255);

end


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

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

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


end


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

	if pName == "" then

      if pPolyNum == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         return true;
      else
      
         aClickCount = aClickCount + 1;
         if aClickCount >= 20 then
            if aClickCount == 20 or ((aClickCount - 20)%10 == 0) then
               cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
               IcPlaySample("PH_SFX_DOES_NOT_WORK");
				end
			end

         IcPlaySample("PH_SFX_WOODMACHINEBUTTON");
         local aI = 0;
         aI = pPolyNum;
         aButtonInfo[aI].mActive = not aButtonInfo[aI].mActive;
         aI = aButtonInfo[aI].mFlip;
         if aI > 0 then
            aButtonInfo[aI].mActive = not aButtonInfo[aI].mActive;
         end
         _G[aTmpVal].SaveInfo(pObj);

         aI = 0;
      	for i = 1, aNumSpots do
      		if aButtonInfo[i].mActive then
               aI = aI + 1;
            end
      	end
      	
      	if aI < aNumSpots then
            return true;
         end

   		pObj.mState   = pObj.mState   + 1;
   		pObj.mCurCell = pObj.mCurCell + 1;
         cm.DisableObject(pObj);
         cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         IcPlaySample("PH_SFX_WOODMACHINEOPENS");
         return true;
      end
		return true;

	else
		cm.ObjDoesNotWork(pObj);
		IcPlaySample("PH_SFX_DOES_NOT_WORK");
	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);
   pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

   aImg = IcImageWrap("PH_IMG_STG3_SC2_SCR_COMBI_MACHINE_PEGS");
   assert(aImg);

	local aMPoly = pObj:GetMouseOverPoly();
   local aRect0 = aImg:GetCelRect(0);
   local aRect1 = aImg:GetCelRect(1);
   local aRect2 = aImg:GetCelRect(2);
   local aRect3 = aImg:GetCelRect(3);
   local aPos   = IcVector2d(0, 0) + pObj.mSmPos;
   for i = 1, aNumSpots do

      aRect = aRect0;
   	if aButtonInfo[i].mActive then
   	   aRect = aRect2;
   	   if aMPoly == i then
   	   	aRect = aRect3;
			end

      elseif aMPoly == i then
   	   aRect = aRect1;
      end

		aRpos = IcVector2d(aButtonInfo[i].mX, aButtonInfo[i].mY) + aPos;
		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);
   end


end



-- Called to fetch mouse over info
_G[gTable].GetCursorInfo = function(pObj, pPolyNum, pName)

   local aCursorTable = {};
   aCursorTable[0]	 = true;
   aCursorTable[1]	 = IcGobj.CURSOR_HAND;

   if pPolyNum == 0 then
      aCursorTable[1] = IcGobj.CURSOR_POINTER;
   end
   return aCursorTable;

end



-- Loads info from the object
_G[gTable].LoadInfo =  function(pObj)

	local aKey;
	for i = 1, aNumSpots do
		aKey = string.format("Active%d", i);
		if pObj.mCsData:IfExistsBool(aKey) then
			aButtonInfo[i].mActive = pObj.mCsData:GetBool(aKey);
		end
	end
	aClickCount = pObj.mCsData:GetInt("aClickCount");

end

-- Saves info to the object
_G[gTable].SaveInfo =  function(pObj)

	local aKey;
	for i = 1, aNumSpots do
		aKey = string.format("Active%d", i);
		pObj.mCsData:SetVal(aKey, aButtonInfo[i].mActive);
	end
	pObj.mCsData:SetVal("aClickCount", 0);

end

