_G[gTable] = {};

local aTmpVal     = gTable;
local aInfoShown  = false;
local aCount      = 0;
local aHelpShown  = false;

cm.SetupTable(_G[aTmpVal]);

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

   pObj.mState	= 0;
	aCount     	= 0;
	aInfoShown	= false;
	aHelpShown	= false;

	pObj.mCsData:SetVal("aInfoShown", aInfoShown);
	pObj.mCsData:SetVal("aHelpShown", aHelpShown);

end

-- Called after data being loaded from disk
_G[gTable].OnLoad = function(pObj)
	aInfoShown	= pObj.mCsData:GetBool("aInfoShown");
	aHelpShown	= pObj.mCsData:GetBool("aHelpShown");
	aCount		= 0;
end

-- Called after a level is loaded
_G[gTable].OnLevelLoad = function(pObj)
   pObj.mGobj.mSoundPlayer:PlayMusic("IcMusicOutsides2");
  	pObj.mGobj.mSoundPlayer:PlayAmbient("Ambient_FairyGarden", 5);
  	aInfoShown = pObj.mCsData:GetBool("aInfoShown");
	aHelpShown	= pObj.mCsData:GetBool("aHelpShown");
end


-- Called after a level is exited
_G[gTable].OnLevelExit = function(pObj)
   pObj.mGobj.mSoundPlayer:ClearMusic(0);
   pObj.mGobj.mSoundPlayer:FadeoutLoopingSfx(20);
   pObj.mGobj.mSoundPlayer:FadeoutAmbient(20);
end


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

	if not aInfoShown then

		aCount = aCount + 1;
		if aCount == 25 then
		   aCount      = 0;
			aInfoShown	= true;
			pObj.mCsData:SetVal("aInfoShown", aInfoShown);
			--pObj.mPdata:ShowCurLevelInfo();
			pObj.mPdata:ShowCurLevelInfo("PH_SFX_INGAME_13");
		end

	elseif not aHelpShown and (not pObj.mPdata.mInfo:IsAlive()) then

		aCount = aCount + 1;
		if aCount == 25 then
			aHelpShown	= true;
			pObj.mCsData:SetVal("aHelpShown", aHelpShown);
			local aInfoStr  = pObj.mCsData:GetString("HelpInfoStr");
			pObj.mPdata:ShowHelp(pObj.mGobj.mDataManager:GetString(aInfoStr),
				IcVector2d(770, 13), false, true);
		end

	end

end

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

	if pName == "" then

   	if pPolyNum == 0 then
         cm.GetShowInfo(pObj, "InfoStr1", "InfoTime1");
      elseif pPolyNum == 1 then
         cm.GetShowInfo(pObj, "InfoStr2", "InfoTime2");
      elseif pPolyNum == 2 then
         cm.GetShowInfo(pObj, "InfoStr3", "InfoTime3");
      elseif pPolyNum == 3 then
         cm.GetShowInfo(pObj, "InfoStr4", "InfoTime4");

      elseif pPolyNum == 4 or pPolyNum == 5 then
      	local aNextLevel = pObj.mCsData:GetInt("NextLevel1");
      	pObj.mPdata:SetNextLevel(aNextLevel);
      	pObj.mPdata:LevelComplete(pObj.mCsData:GetBool("NextStage"));
   	end

      return true;

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

end

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

	local aImg = IcImageWrap(pObj.mSmallImage);
	if (pObj.mCurImg == 1) then
		aImg = IcImageWrap(pObj.mBigImage);
	end
	assert(aImg);

	aRect = aImg:GetCelRect(pObj.mCurCell);
	if pObj:SmIsMoving() then
		pG:DrawImageF(aImg:GetImage(), pObj.mSmPos.x, pObj.mSmPos.y, aRect);
	else
		pG:DrawImage(aImg:GetImage(), pObj.mSmPos.x, pObj.mSmPos.y, aRect);
	end

end


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


-- Called to fetch cursor data
_G[gTable].GetCursorInfo = function(pObj, pPolyNum, pName)
	local aCursorTable	= {};
   aCursorTable[0]		= true;
   aCursorTable[1]		= IcGobj.CURSOR_POINTER;

   if pPolyNum == 4 or pPolyNum == 5 then
      aCursorTable[1] = IcGobj.CURSOR_FOOT;
	end
   return aCursorTable;

end

