_G[gTable] = {};

local aTmpVal  = gTable;
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 compass has accomplished its purpose
   if pObj.mState == 0 and (not pObj.mCsData:GetBool("FadingToNext")) then
      if cm.GetGlobalBoolIndirect(pObj, "ActivatorFlag1") then
         pObj.mState = pObj.mState + 1;
         cm.DisableObject(pObj);

         if pObj.mCurImg == 1 then
   		   _G[aTmpVal].NormalSize(pObj);
   			pObj.mCb:Disappear();
	         IcPlaySample("PH_SFX_CLOSER_LOOK");
   	   end

      end
   end

	cm.ZoomOutUpdate(pObj);

end


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

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

      -- Reject click if the compass is changing
      if pObj.mCsData:GetBool("FadingToNext") then
         return true;
      end

      -- Zoom in if necessary
		if pObj.mCurImg == 0 then
			_G[aTmpVal].GetBigger(pObj, PhGetBigTime);
			return true;
		else

         local aImg = IcImageWrap(pObj.mBigImage);
         assert(aImg);

         -- Update the image cell
         pObj:StartFadeRects(PhFadeAnotherTimeShort);
         local aPrevCell = pObj.mCurCell;
         pObj.mCurCell   = aPrevCell + 1;
         if pObj.mCurCell >= aImg:GetNumCells() then
            pObj.mCurCell = 0;
         end

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

         -- Update the global flag with the weight of this lever
         local aNum    = pObj.mCsData:GetInt("Sc4_Compass_Weight");
         local aSolDir = pObj.mCsData:GetInt("SolDirection");

         if aSolDir == pObj.mCurCell then
            aGlobalNum = aGlobalNum + aNum;
         elseif aSolDir == aPrevCell then
            aGlobalNum = aGlobalNum - aNum;
         end
         pObj.mPdata.mCsData:SetVal(aSetFlag, aGlobalNum);
         IcPlaySample("PH_SFX_COMPASSNEEDLEMOVED");
         return true;

      end
      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 aRect = aImg:GetCelRect(0);
	local aRpos = pObj.mSmPos;
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

	if (pObj.mCurImg == 1) then

		pG:SetColorizeImages(true);
		pG:SetColor(Color(255, 255, 255, pObj.mAlpha:AncGetVal()));
		aImg = IcImageWrap(pObj.mBigImage);
		assert(aImg);
		aRpos = pObj.mBigPos;

      if pObj.mCsData:GetBool("CompassChanging") then

   		pG:SetColor(Color(255, 255, 255));
   		local aPrevCell = pObj.mCsData:GetInt("CompassOldCell");
   		aRect = aImg:GetCelRect(aPrevCell);
   		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

		   pG:SetColor(Color(255, 255, 255, 255 - pObj.mAlpha:AncGetVal()));
   		aRect = aImg:GetCelRect(pObj.mCurCell);
   		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);
   		pG:SetColor(Color(255, 255, 255));

      else
   		aRect = aImg:GetCelRect(pObj.mCurCell);
   		pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);
      end

		local aBoxImg = IcImageWrap("PH_IMG_CMN_ZOOM_BOX");
		assert(aBoxImg);
		aRect = aBoxImg:GetCelRect(0);
		aRpos = pObj.mFrmPos;
		pG:DrawImage(aBoxImg:GetImage(), aRpos.x, aRpos.y, aRect);

	end


end



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


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


-- Returns the sparkle info
_G[gTable].GetParticleSystemInfo = function(pObj, pPolyNum, pName)
   local aParticleSystemTable	= {};
   aParticleSystemTable[0]		= true;
   aParticleSystemTable[1]		= "PH_PSYS_UNDYING_SPARKLE_BG";

   if (pObj.mCurImg == 1) then
      aParticleSystemTable[0]	= false;
   end

   return aParticleSystemTable;
end


