#!/usr/bin/perl -wT

use CGI qw(:standard);
#use CGI::Carp qw(fatalsToBrowser set_message);

$cgi = new CGI;
print $cgi->header();

print $cgi->start_html(
	-head => [	meta({ -http_equiv => 'pragma',  -content => 'no-cache' }),
	            meta({ -http_equiv => 'expires', -content => '-1' }) ],
	-title => 'Cartographers Guild Member Locations', 
	-meta => { 'keywords' => 'Gartographers,Guild,Member,Locations', 'description' => 'Cartographers Guild member locations' } );

my $world_width = 1024;
my $world_height = 768;
my $world_lat_top = 61.2;
my $world_lat_bot = 13.0;
my $world_lng_left = -126.93;
my $world_lng_right = -58.5;

$loc_dir = "../MembersMap";
open LOCATIONS, "<$loc_dir/locations.txt";
my @valid_entries = <LOCATIONS>;
close LOCATIONS;

print "\n";
print '<div id="map" style="position: absolute; left: 0px; top: 0px;">'."\n";
print '<img src="http://www.cartographersguild.com/utilities/MembersMap/USA.png">'."\n";
print '<a href="http://www.cartographersguild.com/utilities/cgi-bin/WhereInWashington.pl">'."\n";
print '<img style="position: absolute; left: 13px; top: 169px;" width="81" height="71" src="http://www.cartographersguild.com/utilities/MembersMap/Clear.png">'."\n";
print '</a>'."\n";
print '</div>'."\n";

for( my $i = (@valid_entries - 1); $i >= 0; $i-- )
{
	$entry = $valid_entries[$i];
	chomp( $entry );
	
	if( $entry =~ /([0-9\.\-]+),([0-9\.\-]+),([a-zA-Z0-9_\s]+)/ )
	{
		my $flat = $1;
		my $flng = $2;
		my $tip = $3;

		if( $flat <= $world_lat_top &&
			$flat >= $world_lat_bot &&
			$flng <= $world_lng_right &&
			$flng >= $world_lng_left )
		{
			my $px = int( ($flng - $world_lng_left) / ($world_lng_right - $world_lng_left) * ($world_width - 1) );
			my $py = ($world_height - 1) - int( ($flat - $world_lat_bot) / ($world_lat_top - $world_lat_bot) * ($world_height - 1) );
		
			print '<div style="position: absolute; left: '.$px.'px; top: '.$py.'px; width: 5px; height: 5px; style="border-style: none;">'."\n";
			print '<a href="#" title="'.$3.'" style="border-style: none;">';
			print '<img src="http://www.cartographersguild.com/utilities/MembersMap/Marker.png" style="border-style: none;"';
			print '</a>';
			print '</div>'."\n";
		}
	}
}

print '</body></html>'."\n";

1;
