_G[gTable] = {};

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


-- Called on Init
_G[gTable].OnInit = function(pObj)
	pObj.mState = 0;
end

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

end


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

   -- See if the lever has accomplished its purpose
   if cm.GetGlobalBoolIndirect(pObj, "DeActivatorFlag1") then
      cm.DisableObject(pObj);
   end

end


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

   if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      IcPlaySample("PH_SFX_DOES_NOT_WORK");
      return true;

   -- See if the button has been activated
   elseif not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
      pObj.mDiscovered = true;
      if pName == "" then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         IcPlaySample("PH_SFX_DOES_NOT_WORK");
      else
         cm.ObjDoesNotWork(pObj);
         pObj.mPdata:UnMouseObject();
      end
      return true;
   end

	pObj.mDiscovered = true;
	if pName == "" then

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

         -- Fetch the global flag
         local aGlobalNul = 0;
         local aSetFlag = pObj.mCsData:GetString("SetFlag1");
         if pObj.mPdata.mCsData:IfExistsInt(aSetFlag) then
            aGlobalNul = pObj.mPdata.mCsData:GetInt(aSetFlag);
         end

         -- Update the global flag with the weight of this lever
         local aNum = pObj.mCsData:GetInt("LeverWeight");
         aGlobalNul = aGlobalNul + aNum;
         pObj.mPdata.mCsData:SetVal(aSetFlag, aGlobalNul);

         -- Update other states
			pObj:SetActive(0, pPolyNum, false);
			pObj:SetActive(0, pPolyNum + 1, true);
         pObj:StopParticleEmission(false);
         IcPlaySample("PH_SFX_MACHINEBUTTONPRESSED2");

      elseif pObj.mState == 1 then
         pObj.mState    = 0;
         pObj.mCurCell  = 0;

         -- Fetch the flags
         local aNum        = pObj.mCsData:GetInt("LeverWeight");
         local aSetFlag    = pObj.mCsData:GetString("SetFlag1");
         local aGlobalNul  = pObj.mPdata.mCsData:GetInt(aSetFlag);

         -- Update the global flag
         aGlobalNul = aGlobalNul - aNum;
         pObj.mPdata.mCsData:SetVal(aSetFlag, aGlobalNul);

         -- Update other states
			pObj:SetActive(0, pPolyNum, false);
			pObj:SetActive(0, pPolyNum - 1, true);
         pObj:StopParticleEmission(false);
         IcPlaySample("PH_SFX_MACHINEBUTTONPRESSED2");

      end
      
      aPressCount = aPressCount + 1;
      if aPressCount == 20 then
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
		elseif aPressCount == 32 then
		   cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
		elseif (aPressCount > 32) and (aPressCount%12 == 0) then
		   cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
		end
      return true;

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

	return false;

end


-- Called when mouse enters the object
_G[gTable].OnMouseEnter = function(pObj)

	if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
		return;
	end
   pObj.mCurCell = 1;

end

-- Called when mouse leaves object
_G[gTable].OnMouseLeave = function(pObj)
	if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
		return;
	end

	if pObj.mState == 0 then
	   pObj.mCurCell = 0;
	elseif pObj.mState == 1 then
	   pObj.mCurCell = 2;
	end

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);

end




