_G[gTable] = {};

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


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

	pObj.mState = 1;
	aTimer      = 0;

	aTimer  		= 0;
	aClicked		= false;
	pObj.mCsData:SetVal("aTimer", 	aTimer);
	pObj.mCsData:SetVal("aClicked",	aClicked);

end

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


-- Called after a level is loaded
_G[gTable].OnLevelLoad = function(pObj)
	aTimer  		= pObj.mCsData:GetInt("aTimer");
	aClicked		= pObj.mCsData:GetBool("aClicked");
end


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

	if aClicked then
	   aTimer = aTimer + 1;
		pObj.mCsData:SetVal("aTimer",	aTimer);
	end

end

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

	pObj.mDiscovered = true;
	if pName == "" then
	   _G[aTmpVal].ShowInfo(pObj);
		aClicked	= true;
		pObj.mCsData:SetVal("aClicked",	aClicked);
      return true;

	elseif pName == pObj.mCsData:GetString("KeyObject1") then
	
      if not cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
         return false;
      end

      if pObj.mCsData:IfExistsString("SpawnObject") then
         if not cm.SpawnObjectMouse(pObj, 0, pX, pY, "SpawnObject") then
            return true;
         end
      end

      cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
      cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
		pObj.mState = pObj.mState + 1;
		cm.DisableObject(pObj);

		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);
		return true;

	else
		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


-- Called to show info
_G[gTable].ShowInfo =  function(pObj)

	if not aClicked then
      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
	else

	   if aTimer < 8*60*100 then
	      cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
		elseif aTimer < 16*60*100 then
	      cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");
		else
	      cm.GetShowInfo(pObj, "InfoStr5", "InfoTime5");
		end

	end
	
end

