// <summary>
	// A method called when the job is done, successfully or not.
	// </summary>
	public void JobTerminationEvent(object job, JobEventArgs e)
	{
#if DEBUG_LOG
		Debug.Log("DEBUG: TileDownloader.JobTerminationEvent: Tile download complete, but was it murdered? " + e.WasKilled);
#endif
		TileEntry entry = e.Owner as TileEntry;
		tilesLoading.Remove(entry);
		
#if !UNITY_WEBPLAYER
		if (e.WasKilled == false)
		{
			if (entry.error && entry.cached)
			{
                if (entry.cached)
                {
#if DEBUG_LOG
				    Debug.Log("DEBUG: TileDownloader.JobTerminationEvent: loading cached tile failed, trying to download it: " + entry.url);
#endif
    				// try downloading the tile again
    				entry.cached = false;
					cacheSize -= entry.size;
    				tiles.Remove(entry);
                }
                else
                {
#if DEBUG_LOG
                     Debug.Log("DEBUG: TileDownloader.JobTerminationEvent: downloading tile failed, trying to download it again: " + entry.url);
#endif
                }

				Get(entry.url, entry.tile);
				
				return ;
			}
			
			tileURLLookedFor = entry.url;
			TileEntry existingEntry = tiles.Find(tileURLMatchPredicate);
			if (existingEntry != null)
			{
				tiles.Remove(existingEntry);
				cacheSize -= existingEntry.size;
			}
			
			entry.timestamp = (DateTime.Now.ToLocalTime() - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
			tiles.Add(entry);
			cacheSize += entry.size;
			
			// if the cache is full, erase the oldest entry
			// FIXME: find a better way to handle the cache (cf. iPhone Maps app)
			// FIXME: one aspect might be to erase tiles in batch, 10 or 20 at a time, a significant number anyway
			if (cacheSize > MaxCacheSize)
			{
                // beware the year 3000 bug :)
				double oldestTimestamp = (new DateTime(3000, 1, 1) - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
				TileEntry entryToErase = null;
				foreach (TileEntry tile in tiles)
				{
					if (tile.timestamp < oldestTimestamp
						&& tile != entry)
					{
						oldestTimestamp = tile.timestamp;
						entryToErase = tile;
					}
				}
				if (entryToErase == null)
				{
#if DEBUG_LOG
					Debug.LogWarning("WARNING: TileDownloader.JobTerminationEvent: no cache entry to erase (should not happen)");
#endif
					return ;
				}

                DeleteCachedTile(entryToErase);
#if DEBUG_LOG
				Debug.Log("DEBUG: TileDownloader.JobTerminationEvent: erased from cache: " + entryToErase.url + " [" + entryToErase.guid + "]");
#endif
			}
		}
#endif
	}
        // <summary>
        // A method called when the job is done, successfully or not.
        // </summary>
        public void JobTerminationEvent(object job, JobEventArgs e)
        {
#if DEBUG_LOG
            Debug.Log("DEBUG: TileDownloader.JobTerminationEvent: Tile download complete, but was it murdered? " + e.WasKilled);
#endif
            TileEntry entry = e.Owner as TileEntry;
            tilesLoading.Remove(entry);

#if !UNITY_WEBPLAYER
            if (e.WasKilled == false)
            {
                if (entry.error && entry.cached)
                {
                    if (entry.cached)
                    {
#if DEBUG_LOG
                        Debug.Log("DEBUG: TileDownloader.JobTerminationEvent: loading cached tile failed, trying to download it: " + entry.url);
#endif
                        // try downloading the tile again
                        entry.cached = false;
                        cacheSize   -= entry.size;
                        tiles.Remove(entry);
                    }
                    else
                    {
#if DEBUG_LOG
                        Debug.Log("DEBUG: TileDownloader.JobTerminationEvent: downloading tile failed, trying to download it again: " + entry.url);
#endif
                    }

                    Get(entry.url, entry.tile);

                    return;
                }

                tileURLLookedFor = entry.url;
                TileEntry existingEntry = tiles.Find(tileURLMatchPredicate);
                if (existingEntry != null)
                {
                    tiles.Remove(existingEntry);
                    cacheSize -= existingEntry.size;
                }

                entry.timestamp = (DateTime.Now.ToLocalTime() - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
                tiles.Add(entry);
                cacheSize += entry.size;

                // if the cache is full, erase the oldest entry
                // FIXME: find a better way to handle the cache (cf. iPhone Maps app)
                // FIXME: one aspect might be to erase tiles in batch, 10 or 20 at a time, a significant number anyway
                if (cacheSize > MaxCacheSize)
                {
                    // beware the year 3000 bug :)
                    double    oldestTimestamp = (new DateTime(3000, 1, 1) - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
                    TileEntry entryToErase    = null;
                    foreach (TileEntry tile in tiles)
                    {
                        if (tile.timestamp < oldestTimestamp &&
                            tile != entry)
                        {
                            oldestTimestamp = tile.timestamp;
                            entryToErase    = tile;
                        }
                    }
                    if (entryToErase == null)
                    {
#if DEBUG_LOG
                        Debug.LogWarning("WARNING: TileDownloader.JobTerminationEvent: no cache entry to erase (should not happen)");
#endif
                        return;
                    }

                    DeleteCachedTile(entryToErase);
#if DEBUG_LOG
                    Debug.Log("DEBUG: TileDownloader.JobTerminationEvent: erased from cache: " + entryToErase.url + " [" + entryToErase.guid + "]");
#endif
                }
            }
#endif
        }