_G[gTable] = {};

local aTmpVal  = gTable;
local aClicked = false;
local aTimer   = 0;
cm.SetupTable(_G[aTmpVal]);


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

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


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

	if pObj.mState == 2 then
		if not pObj.mAlpha:AncIsChanging() then
			pObj.mIsDone = true;
		end
	end
	
	if aClicked then
	   aTimer = aTimer + 1;
	end

end


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

   aClicked = true;
   pObj.mDiscovered = true;
   if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
   
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag2") then
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
      else
      
         if aTimer > 3*60*50 then
            cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         else
            cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
         end
      	IcPlaySample("PH_SFX_DOES_NOT_WORK");
      end

		if pName ~= "" then
         pObj.mPdata:UnMouseObject();
		end

      return true;
	end

	if pName == "" then

		if pObj.mState == 0 then

			if not cm.SpawnObjectMouse(pObj, 0, pX, pY, "SpawnObject1") then
	         return true;
	      end

         pObj:StartFadeRects(PhFadeAnotherTimeReallyShort);
			pObj.mState		= pObj.mState + 1;
			pObj.mCurCell	= pObj.mCurCell + 1;
			cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
			IcPlaySample("PH_SFX_OBJ_PICKUP");

		elseif pObj.mState == 1 then

			if not cm.SpawnObjectMouse(pObj, 0, pX, pY, "SpawnObject2") then
	         return true;
	      end

         pObj:StartFadeRects(PhFadeAnotherTimeReallyShort);
			pObj.mState		= pObj.mState + 1;
			pObj.mCurCell	= pObj.mCurCell + 1;
			IcPlaySample("PH_SFX_OBJ_PICKUP");

		end
		return true;

	else

      pObj.mDiscovered = false;
		cm.ObjDoesNotWork(pObj);
		return false;

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

	if (pObj.mState == 2) then
		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
	end
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end





