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

end


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

   local aState = 0 + pObj.mState;

	if pName == "" then

      if aState < 7 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");

      elseif aState == 7 then

         local aSolStr = pObj.mCsData:GetString("CurSolutionStr");
         local aNewSol = string.format("%s%d", aSolStr, pPolyNum);
         local aSol    = pObj.mCsData:GetString("Solution");
         pObj.mCsData:SetVal("CurSolutionStr", aNewSol);
         
         if string.find(aSol, aNewSol, 1, true) ~= 1 then
            pObj.mCsData:SetVal("CurSolutionStr", "");
            cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
            IcPlaySample("PH_SFX_DOES_NOT_WORK");
            return true;
         end

			local aSfxStr = string.format("PH_SFX_BELL%d", pPolyNum);
         if #aSol == #aNewSol then
            cm.DisableObject(pObj);
            cm.SetGlobalValIndirect(pObj, "SetFlag1", true);
            cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
            IcPlaySample("PH_SFX_GOOD_JOB");
            return true;
         end

      	IcPlaySample(aSfxStr);

      end

   	return true;

   else
      local aPrefStr = pObj.mCsData:GetString("KeyObjectPref");
      local aStr     = "";
      local aMatch   = false;
      
      for aI = 1, 7 do
         aStr = string.format("%s%d", aPrefStr, aI);
         if aStr == pName then
            aMatch = true;
            break;
         end
      end
      
      if aMatch then

         pObj:StartFadeRects(PhFadeAnotherTimeShort);
   		pObj.mState		 = aState        + 1;
   		pObj.mCurCell   = pObj.mCurCell + 1;
   		_G[pActorObj.mScriptTag].OnUse(pActorObj, pObj);

         if pObj.mState == 7 then
            pObj:SetActive(0, pPolyNum, false);
            for aI = 1, 7 do
               pObj:SetActive(0, pPolyNum + aI, true);
            end
   		end
   		return true;

		end

		cm.ObjDoesNotWork(pObj);
		pObj.mPdata:UnMouseObject();
		return true;
	end

   return false;
end



-- Called on draw
_G[gTable].OnDraw =  function(pObj, pG)

	local aRpos = pObj.mSmPos;
	local aImg  = IcImageWrap(pObj.mSmallImage);
	assert(aImg);
	local aRect = aImg:GetCelRect(pObj.mCurCell);
	pG:DrawImage(aImg:GetImage(), aRpos.x, aRpos.y, aRect);

end


