var 
 z, zz, max_zoom: integer;
 width, height, total_tiles, tile_group, t: Longint;
 VResponseCode: Cardinal;
 VResponseHeader, VResponseData, BasePath, ManifestURL: AnsiString; 

function TilesX(z: Longint): Longint;
 begin
//  result:=Ceil(width/(256.0*IntPower(2, z)));
  result:=Ceil(trunc(width/IntPower(2, z))/256.0);
 end;

function TilesY(z: Longint): Longint;
 begin
//  result:=Ceil(height/(256.0*IntPower(2, z)));
  result:=Ceil(trunc(height/IntPower(2, z))/256.0);
 end;

function TilesAtZoom(z: Longint): Longint;
 begin
  result:=TilesX(z)*TilesY(z);
 end;

begin

// On the first invocation get the ImageProperties.xml manifest
if GetBefore('<W>', ScriptBuffer)<>GetURLBase then
 begin

  // This algorithm is stolen from the 'dezoomify.php' script

  // At first, assume GetURLBase is the address of the page with a zoomify player
  VResponseHeader:='';
  VResponseData:='';
  if Assigned(Downloader) then
   VResponseCode:=Downloader.DoHttpRequest(GetURLBase, '', '', VResponseHeader, VResponseData);

  if VResponseCode=200 then
   begin

    // Test if the webpage contains a Zoomify flash player
    //	We search a string of the form zoomifyImagePath=str
    //	preg_match ( '/zoomifyImagePath=([^\'"&]*)[\'"&]/i', $html , $addr );
    BasePath:=GetBetween(VResponseData, 'zoomifyImagePath=', '"');

    // If no flash player was found, search a Zoomify HTML5 player
    //	The searched url is within a javascript script. It's the second argument of the showImage function.*/
    //	preg_match("#showImage\([^),]*,[^\"']*[\"']([^'\"]*)[^)]*\)#", $html, $addr);
	
    if BasePath='' then
     BasePath:=GetBetween(GetAfter(',', GetBetween(VResponseData, 'showImage(', ')')), '"', '"');

    if GetBefore('://', BasePath)='' then
     begin
      BasePath:=RegExprReplaceMatchSubStr(GetURLBase, '/[^/]*$', '/'+BasePath);
     end;
   end
  // At second, assume GetURLBase is the directory where the manifest resides
  else BasePath:=GetURLBase;

  VResponseHeader:='';
  VResponseData:='';
  ManifestURL:=BasePath+'/ImageProperties.xml';
  if Assigned(Downloader) then
   VResponseCode:=Downloader.DoHttpRequest(ManifestURL, '', '', VResponseHeader, VResponseData);
 
  if (VResponseCode<>200) or (GetBefore('_', VResponseData)<>'<IMAGE') then
   begin
    // Last chance - use the Version variable as a manifest
    VResponseData:=Version;
    if GetBefore('_', VResponseData)<>'<IMAGE' then
     begin
      ResultURL:='Cannot get '+ManifestURL+' - error '+inttostr(VResponseCode);
      exit;
     end
   end;

// Calculate the maximum zoom level
  width:=strtoint(GetNumberAfter('WIDTH="', VResponseData));
  height:=strtoint(GetNumberAfter('HEIGHT="', VResponseData));
  t:=strtoint(GetNumberAfter('NUMTILES="', VResponseData));
  z:=0;
  repeat
   t:=t-TilesAtZoom(z);
   inc(z);
  until t<=0;

// Save all info into ScriptBuffer
// Save GetURLBase to trigger execution of this init block again on URL change
  ScriptBuffer:=GetURLBase+'<W>'+inttostr(width)+'<H>'+inttostr(height)+'<Z>'+inttostr(z);

 end;

width:=strtoint(GetNumberAfter('<W>', ScriptBuffer));
height:=strtoint(GetNumberAfter('<H>', ScriptBuffer));
max_zoom:=strtoint(GetNumberAfter('<Z>', ScriptBuffer));

if GetZ>max_zoom then
 begin
  ResultURL:='Too high zoom level';
  exit;
 end;

z:=max_zoom-GetZ;

if (GetX>=TilesX(z)) or (GetY>=TilesY(z)) then
 begin
  ResultURL:='';
  exit;
 end;

// Get the number of tiles for all zooms < current
t:=0;
for zz:=z+1 to max_zoom-1 do t:=t+TilesAtZoom(zz);

tile_group:=(t+GetY*TilesX(z)+GetX) div 256;
ResultURL:=BasePath+'/TileGroup'+inttostr(tile_group)+'/'+inttostr(GetZ-1)+'-'+inttostr(GetX)+'-'+inttostr(GetY)+'.jpg';

end.
