#!/usr/bin/perl
require "kisrael.pl";

&chugCGI(*input);

use POSIX qw(ceil floor);


use LWP::Simple;





print "Content-type:text/html\n\n";


$url = $input{"url"};
$showdiv = $input{"showdiv"};
$wantwidth=$input{"wantwidth"};
$mapname=$input{"mapname"};

if($url eq ""){
    $showdiv = "checked";
    $mapname= "MAPNAME";
}

print<<__EOQ__;
<head><title>flickrnotes2imagemap</title></head>

<a href="/">kisrael.com</a> | <a href="/tools/">tools</a> |
<b>flickrnotes2imagemap</b><br>
This is an HTML "screenscraper" that takes a flickr image URL that has annotative "notes"
and tries to browbeat it into being a plain ol' HTML image map. (I didn't try very 
    hard to get the nifty CSS box effect flickr uses to indicate where the notes are; however I do optionally update a caption underneath
the photo.)
<br><br>
I couldn't find a "proper" way of extracting this information through the API,
which means this will break horribly if/when flickr changes how it does HTML.


<br><br>

    Options include setting a new width (scaling map coordinates accordingly); 
I wanted this because my blog uses a ~400px width column) It dumps the information 
three ways: as HTML, in a table, and then the HTML source.
<form>
<b>flickr url:</b><input name="url" value="$url" size=60><br>
<b>imagemap name:</b><input name="mapname" value="$mapname"><br>
<b>desired image width:</b><input name="wantwidth" value="$wantwidth" size=6> <i>(optional)</i><br>
<b><input type="checkbox" name="showdiv" value="checked" $showdiv>show div caption beneath</b>
<input type="submit" value="generate image map">
</form>

__EOQ__



if($url ne ""){
my $buffer = get($url);

$buffer =~ s/\<div/\n\<div/g;

#before we do anything, we need to scan through and try and find the img...
foreach $line (split(/\n/,$buffer)){
    if($line =~ /class=\"photoImgDiv\"/){

	$line =~ /src\=\"([^\"]*)\"/;
	$globimgsrc = $1;
	if($line =~ /width\=\"([^\"]*)\"/){
	$rawimgwidth = $1;
    }
	if($line =~ /height\=\"([^\"]*)\"/){
	$rawimgheight = $1;
    }

if($wantwidth ne ""){
$multiplier = $wantwidth / $rawimgwidth ;

$globimgwidth = $wantwidth;
$globimgheight = floor($rawimgheight * $multiplier);

} else {
$multiplier = 1;
$globimgwidth =  $rawimgwidth;
$globimgheight =  $rawimgheight;


}


    }




}


foreach $line (split(/\n/,$buffer)){

    if($line =~  /class\=\"photo\_note\"/){


    if($line =~ /left\:(\d*)px\; top\:(\d*)px\;/){
	$globleft = $1;
	$globtop = $2;
    } else {
	err("ERROR in format.... $line is photo_note without left: top: format I expected");
    }
}

    if($line =~  /style\=\"overflow\:/){

	if($line =~   /width\:(\d*)px\; height\:(\d*)px\;/){
	    $globwidth = $1;
	    $globheight = $2;
	} else {
	    err("ERROR in format.... $line is style=\"overflow: without width: height: format I expected");
	}
    }


    if($line =~ /\_decorate\(/){
	
	$line =~ s/\\\'/\[\[QUOTE\]\]/g;
	$line =~ /\'([^\']*)\'\)\;\<\/script/;
	$globcontent = $1;



	$globright = $globleft + $globwidth;
	$globbottom = $globtop+$globheight;


$globleft = floor($multiplier * $globleft);
$globtop = floor($multiplier * $globtop);
$globright = floor($multiplier * $globright);
$globbottom = floor($multiplier * $globbottom);


	$globcontentnoescape = $globcontent;
	$globcontentnoescape =~  s/\[\[QUOTE\]\]/\\\'/g;
	$globcontentnoescape =~ s/\\\'/\'/g;
	$globcontentnoescape =~ s/\\n/\n/g;


	$globcontentwithbr = $globcontent;
	$globcontentwithbr =~ s/\\n/\<br\> /g;
	$globcontentwithbr =~ s/\[\[QUOTE\]\]/\\\'/g;

	$globcontentnoescapewithbr = $globcontent;
	$globcontentnoescapewithbr  =~ s/\[\[QUOTE\]\]/\\\'/g;
	$globcontentnoescapewithbr =~ s/\\\'/\'/g;
	$globcontentnoescapewithbr =~ s/\\n/\<br\> /g;





	if($globleft ne "" && $globtop ne "" && $globwidth ne "" && $globheight ne ""){


	$globbuffer .= <<__EOQ__;
<area shape="rect"
coords="$globleft,$globtop,$globright,$globbottom"  
href="#"
onClick="return false;" 
title="$globcontentnoescape"
onMouseOver="document.getElementById('caption_$mapname').innerHTML = '$globcontentwithbr';"
onFocus="document.getElementById('caption_$mapname').innerHTML = '$globcontentwithbr';" 
 />
__EOQ__


$globtable .= <<__EOQ__;
<tr>
<td>$globcontentnoescapewithbr</td>
<td>$globleft</td>
<td>$globtop</td>
<td>$globright</td>
<td>$globbottom</td>

</tr>
__EOQ__


$globleft = "";
	$globtop = "";
	$globwidth = "";
	$globheight = "";
	    
    }


    }


}



$usemap = "#".$mapname;
$result = <<__EOQ__;

<img src="$globimgsrc"  usemap="$usemap" width="$globimgwidth" height="$globimgheight">
<div id="caption_$mapname"></div>
<br><br>
<map id="$mapname" name="$mapname">
$globbuffer
</map>
__EOQ__

    print $result;
print<<__EOQ__;
<table border=1>
<tr>
<th>content
<th>x1
<th>y1
<th>x2
<th>y2

</tr>

$globtable
</table>
__EOQ__

    $resulthtml = $result;
$resulthtml =~ s/\</\&lt\;/g;
$resulthtml =~ s/\>/\&gt\;/g;

print "<pre>$resulthtml</pre>\n";

}

sub err {
    my($msg)=@_;
    print $msg,"\n";
}
