/// <summary>
		/// Initialize this instance.
		/// </summary>
		public void Initialize()
		{
			m_pendingLeaderboardsUpdate = new List<SGNLeaderboard>();
			m_pendingAchievementsUpdate = new List<SGNAchievement>();
			
			// Player.
			SGNService.PlayerManager.LoggedIn += delegate(object sender, PlayerLoggedInEventArgs e) 
			{
				Log("User logged. Processing pending updates...");
				RestorePendingUpdates();
				ProcessPendingLeaderboardsUpdate();
				ProcessPendingAchievementsUpdate();
			};
			
			// Leaderboards.
			SGNService.LeaderboardManager.LeaderboardUpdating += delegate(object sender, LeaderboardUpdatingEventArgs e) 
			{
				AddPendingLeaderboardUpdate(e.Leaderboard);
			};
			
			SGNService.LeaderboardManager.LeaderboardUpdatingFailed += delegate(object sender, LeaderboardUpdatingFailedEventArgs e) 
			{
				m_currentProcessingLeaderboard = null; 
			};
			
			SGNService.LeaderboardManager.LeaderboardUpdated += delegate(object sender, LeaderboardUpdatedEventArgs e) 
			{
				RemovePendingLeaderboardUpdate(e.Leaderboard);
			};
			
			// Achievements.
			SGNService.AchievementManager.AchievementUpdating += delegate(object sender, AchievementUpdatingEventArgs e) 
			{ 
				AddPendingAchievementUpdate(e.Achievement); 
			};
			
			SGNService.AchievementManager.AchievementUpdatingFailed += delegate(object sender, AchievementUpdatingFailedEventArgs e) 
			{
				m_currentProcessingAchievement = null;
			};
			
			SGNService.AchievementManager.AchievementUpdated += delegate(object sender, AchievementUpdatedEventArgs e) 
			{
				RemovePendingAchievementUpdate(e.Achievement);
			};
			
			AppService.BackgroundBegin += delegate {
				SavePendingUpdates();
			};
		}
		void ProcessPendingLeaderboardsUpdate()
		{
			lock (this)
			{
				var count = m_pendingLeaderboardsUpdate.Count;
				
				Log("There are {0} pending leaderboards update.", count);
				
				if (m_currentProcessingLeaderboard == null && count > 0)
				{
					m_currentProcessingLeaderboard = m_pendingLeaderboardsUpdate[0];
					
					Log("Updating pending leaderboard {0} update.", m_currentProcessingLeaderboard);
					
					SGNService.LeaderboardManager.UpdateLeaderboard(m_currentProcessingLeaderboard);
				}
			}
		}
		void RemovePendingLeaderboardUpdate(SGNLeaderboard leaderboard)
		{
			lock (this)
			{
				Log("Removing leaderboard {0} as pending update...", leaderboard);
				
				m_pendingLeaderboardsUpdate.Remove(leaderboard);
				
				if (leaderboard.Equals(m_currentProcessingLeaderboard))
				{
					m_currentProcessingLeaderboard = null;
				}
			}
		}
		void AddPendingLeaderboardUpdate(SGNLeaderboard leaderboard)
		{
			lock (this)
			{
				Log("Adding leaderboard {0} as pending update...", leaderboard);
				
				if (m_pendingLeaderboardsUpdate.Contains(leaderboard))
				{
					m_pendingLeaderboardsUpdate.Remove(leaderboard);
				}
				
				m_pendingLeaderboardsUpdate.Add(leaderboard);
			}
		}
		void RestorePendingUpdates()
		{
			if (m_pendingUpdatesRestored)
			{
				return;
			}
			
			// Leaderboards.
			var data = UserService.GetPreference(GetLeaderboardsPlayerPrefsKey(), "").Value.ToString();
			
			if (!String.IsNullOrEmpty(data))
			{
				var leaderboards = data.Split(';');
				
				foreach (var l in leaderboards)
				{
					var parts = l.Split('|');
					if (parts.Length == 2)
					{
						var leaderboard = new SGNLeaderboard(parts[0]);
						leaderboard.Score = Convert.ToInt64(parts[1]);
						AddPendingLeaderboardUpdate(leaderboard);
					}
				}
			}
			
			// Achievements.
			data = UserService.GetPreference(GetAchievementsPlayerPrefsKey(), "").ToString();
			
			if (!String.IsNullOrEmpty(data))
			{
				var achievements = data.Split(';');
				
				foreach (var l in achievements)
				{
					var parts = l.Split('|');
					
					if (parts.Length == 2)
					{
						var achievement = new SGNAchievement(parts[0]);
						achievement.Percent = Convert.ToSingle(parts[1]);
						m_pendingAchievementsUpdate.Add(achievement);
					}
				}
			}
			
			m_pendingUpdatesRestored = true;
		}
		/// <summary>
		/// Initializes a new instance of the
		/// <see cref="Skahal.Infrastructure.Framework.SocialGamingNetwork.LeaderboardUpdatingEventArgs"/> class.
		/// </summary>
		/// <param name="leaderboard">Leaderboard.</param>
		public LeaderboardUpdatingEventArgs(SGNLeaderboard leaderboard)
		{
			Leaderboard = leaderboard;
		}