#!/usr/bin/perl -w
use strict;
use CGI;
use HTTP::Request::Common qw(GET);
use LWP::UserAgent;

my $q= new CGI;

my $ua= new LWP::UserAgent;

my $phi_east= $q->param('x') || 491679;
my $phi_north= $q->param('y') || 5235293;
my $zoom= $q->param('s') || 0;
my $map= $q->param('m') || "EU"; # or NL

my $shift= 0.25;
my $PI= 4*atan2(1,1);
my $height= int(600*exp($zoom*log(2)));

my $equator= 2140; # y=5235293
#my $equator= 1970; # y=5043293
#my $equator= 1820; # y=4843293
#my $equator= 1685; # y=4643293
#my $equator= 1565; # y=4443293
#my $equator= 1250; # y=3683293
my $widthtop= int($equator*cos(($phi_north+$height)/18000000*$PI)*exp($zoom*log(2)));
my $widthupper= int($equator*cos(($phi_north+$height/2)/18000000*$PI)*exp($zoom*log(2)));
my $width= int($equator*cos($phi_north/18000000*$PI)*exp($zoom*log(2)));
my $widthlower= int($equator*cos(($phi_north-$height/2)/18000000*$PI)*exp($zoom*log(2)));
my $widthbottom= int($equator*cos(($phi_north-$height)/18000000*$PI)*exp($zoom*log(2)));

my $fixedparams= "s=$zoom&a=$map";

print $q->header;
print $q->start_html('kaart van europa');

print <<__EOF__;
height: $height<BR>
top   : $widthtop<BR>
upper : $widthupper<BR>
center: $width<BR>
lower : $widthlower<BR>
bottom: $widthbottom<BR>
center : $phi_east, $phi_north<BR>

<A HREF="?x=491679&y=5635293">56</A>
<A HREF="?x=491679&y=5435293">54</A>
<A HREF="?x=491679&y=5235293">amsterdam</A>
<A HREF="?x=491679&y=5043293&s=3">namen</A>
<A HREF="?x=491679&y=4843293&s=3">ergens</A>
<A HREF="?x=491679&y=4643293&s=3">46</A>
<A HREF="?x=491679&y=4443293&s=3">valreas</A>
<A HREF="?x=491679&y=4243293&s=3">42</A>
<A HREF="?x=491679&y=4043293&s=3">40</A>
<A HREF="?x=491679&y=3683293&s=3">36</A>
<BR>
<A HREF="?y=5198612&x=438758&s=2">thijsseweg11</A>
<A HREF="?y=5336274&x=521939&s=2">west terschelling</A>
<A HREF="?y=5251800&x=1332600&s=2">berlin d-10587</A>
<HR>
<TABLE cellpadding=0 cellspacing=0>
<TR>
    <TD><A HREF="?x=@{[int($phi_east-$width*$shift)]}&y=@{[int($phi_north+$height*$shift)]}&@{[$fixedparams]}">*</A></TD>
    <TD align=center><A HREF="?x=@{[int($phi_east)]}&y=@{[int($phi_north+$height*$shift)]}&@{[$fixedparams]}">*</A></TD>
    <TD><A HREF="?x=@{[int($phi_east+$width*$shift)]}&y=@{[int($phi_north+$height*$shift)]}&@{[$fixedparams]}">*</A></TD>
</TR>
<TR>
    <TD><A HREF="?x=@{[int($phi_east-$width*$shift)]}&y=@{[int($phi_north)]}&@{[$fixedparams]}">*</A></TD>
    <TD>
		<TABLE cellpadding=0 cellspacing=0>
		<TR>
        <TD align=right><IMG WIDTH=@{[int(400*$widthupper/$width)]} HEIGHT=300 SRC="@{[getMapUrl($zoom, $phi_east-$widthupper/2, $phi_north+$height/2)]}"></TD>
		<TD align=left><IMG WIDTH=@{[int(400*$widthupper/$width)]} HEIGHT=300 SRC="@{[getMapUrl($zoom, $phi_east+$widthupper/2, $phi_north+$height/2)]}"></center></TD>
		</TR>
		<TR>
        <TD align=right><IMG WIDTH=@{[int(400*$widthlower/$width)]} HEIGHT=300 SRC="@{[getMapUrl($zoom, $phi_east-$widthlower/2, $phi_north-$height/2)]}"></TD>
		<TD align=left><IMG WIDTH=@{[int(400*$widthlower/$width)]} HEIGHT=300 SRC="@{[getMapUrl($zoom, $phi_east+$widthlower/2, $phi_north-$height/2)]}"></TD>
		</TR>
		</TABLE>
    </TD>
    <TD><A HREF="?x=@{[int($phi_east+$width*$shift)]}&y=@{[int($phi_north)]}&@{[$fixedparams]}">*</A></TD>
</TR>
<TR>
    <TD><A HREF="?x=@{[int($phi_east-$width*$shift)]}&y=@{[int($phi_north-$height*$shift)]}&@{[$fixedparams]}">*</A></TD>
    <TD align=center><A HREF="?x=@{[int($phi_east)]}&y=@{[int($phi_north-$height*$shift)]}&@{[$fixedparams]}">*</A></TD>
    <TD><A HREF="?x=@{[int($phi_east+$width*$shift)]}&y=@{[int($phi_north-$height*$shift)]}&@{[$fixedparams]}">*</A></TD>
</TR>
</TABLE>
__EOF__

for my $newzoom (-10..15) {
    print <<__EOF__;
&nbsp;&nbsp;<A HREF="?x=@{[$phi_east]}&y=@{[$phi_north]}&s=@{[$newzoom]}&a=@{[$map]}">x@{[$newzoom]}</A>
__EOF__
}

print $q->end_html;

exit(0);

sub getMapUrl {
	my ($zoom, $phi_east, $phi_north)= @_;

	my $base= "http://www.routenet.nl/asp/euroroute/map.asp";
    
	my $params= "task=zoom&zoom=$zoom&cx=$phi_east&cy=$phi_north&address=_______";

	my $rq= GET "$base?$params";

	my $response= $ua->request($rq);

	my ($imgurl)= ($response->content =~ m{<div id="kaart"><IMG src=/(maps/[A-Z0-9_]+.gif) WIDTH});

	return "http://www.routenet.nl/$imgurl";
}
