#!/usr/bin/perl

#http://www.greatschools.net/modperl/bycity/ca//?city=Mountain+View&level=e

 print "Content-type: text/html\n";
 $infile="school.db";
 $markersize="20";
 $sbwidth="300px";
 $sbheight="450px";
# $tmp=`cat $infile`;
 open(DB,"<$infile");
 while(<DB>){
 $tmp=$tmp . $_;
 }
# $dd=`date \"+%B %d, %Y\"`; chomp $dd;
$ddd=localtime;
@ddd=split(/ +/,$ddd);
$dd="$ddd[1] $ddd[2], $ddd[4]";
 @tmp=split(/\n/,$tmp);
 $sumlong=$sumlat=$num=0;
$part0="
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>Emily Chiang's Real Estate in Palo Alto, Atherton, Stanford, Menlo Park, Los Altos, Mountain View, Sunnyvale, Cupertino</title>
  </head>
  <body bgcolor=#ffffff>
    <!--font size=+1>Multiple Listings Maps in Palo Alto, Atherton, Stanford, Menlo Park, Los Altos, Mountain View, Sunnyvale, Cupertino</font-->
    <a href=school.cgi?city=PA><img src=images/pa.jpg border=0></a>
    <a href=school.cgi?city=LAH><img src=images/lah.jpg border=0></a>
    <a href=school.cgi?city=MP><img src=images/mp.jpg border=0></a>
    <a href=school.cgi?city=AT><img src=images/at.jpg border=0></a>
    <a href=school.cgi?city=LA><img src=images/la.jpg border=0></a>
    <a href=school.cgi?city=MV><img src=images/mv.jpg border=0></a>
    <a href=school.cgi?city=SV><img src=images/sv.jpg border=0></a>
    <a href=school.cgi?city=CU><img src=images/cu.jpg border=0></a>
    <a href=school.cgi?city=ST><img src=images/st.jpg border=0></a><p>
<br><b>Please consult with <b><br><a href=\"http://www.sccoe.k12.ca.us/resourcesfamilies/districtlocator\"><font color=green>Santa Clara County School District Locator</font></a> 
or <br><br>click on desired cities listed above for more details.<br>
</body></html>
";

 $inputstring = $ENV{"QUERY_STRING"};
 @data=split(/&/,$inputstring);
 foreach $data (@data){
 ($string,$val)=split(/=/,$data);
 if ($string eq "level"){$lev=$val;}
 if ($string eq "city"){$ctn=$val;}
 }
 if ($ctn eq ""){print $part0;exit;}

 if ($ctn eq "PA"){$cityctr="(-122.140174625, 37.4316916041667)";$cityname="Palo Alto";}
 if ($ctn eq "LAH"){$cityctr="(-122.137451, 37.379639)";$cityname="Los Altos Hills";}
 if ($ctn eq "MP"){$cityctr="(-122.180969, 37.45348)";$cityname="Menlo Park";}
 if ($ctn eq "CU"){$cityctr="(-122.032303, 37.32304)";$cityname="Cupertino";}
 if ($ctn eq "LA"){$cityctr="(-122.112701, 37.384338)";$cityname="Los Altos";}
 if ($ctn eq "MV"){$cityctr="(-122.081451, 37.385349)";$cityname="Mountain View";}
 if ($ctn eq "SV"){$cityctr="(-122.035019, 37.369019)";$cityname="Sunnyvale";}
 if ($ctn eq "AT"){$cityctr="(-122.197922, 37.4603)";$cityname="Atherton";}
 if ($ctn eq "ST"){$cityctr="(-122.022034, 37.263489)";$cityname="Saratoga";}

 foreach $property (@tmp){
  ($coord[$num],$name[$num],$level[$num],$street[$num],$city[$num],$phone[$num])=split(/\|/,$property);
  if ($city[$num] ne $cityname){next;}
  $coo=$coord[$num];
  $coo=~s/[(|)| ]//g;
  $num++;
 }

$part1="
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>Emily Chiang's Real Estate in Palo Alto, Atherton, Stanford, Menlo Park, Los Altos, Mountain View, Sunnyvale, Cupertino</title>
    <script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAA0MtzEFjLaG4crpHBBqc32BRZuVooGxRgxAuUjE5yBgN3gYy3sBTEwgWAXG1xutZ6N7GF8yRaU5-5xg\" type=\"text/javascript\"></script>
  </head>
  <body bgcolor=#ffffff>
    <!--font size=+1>Multiple Listings Maps in Palo Alto, Atherton, Stanford, Menlo Park, Los Altos, Mountain View, Sunnyvale, Cupertino</font-->
    <a href=school.cgi?city=PA><img src=images/pa.jpg border=0></a>
    <a href=school.cgi?city=LAH><img src=images/lah.jpg border=0></a>
    <a href=school.cgi?city=MP><img src=images/mp.jpg border=0></a>
    <a href=school.cgi?city=AT><img src=images/at.jpg border=0></a>
    <a href=school.cgi?city=LA><img src=images/la.jpg border=0></a>
    <a href=school.cgi?city=MV><img src=images/mv.jpg border=0></a>
    <a href=school.cgi?city=SV><img src=images/sv.jpg border=0></a>
    <a href=school.cgi?city=CU><img src=images/cu.jpg border=0></a>
    <a href=school.cgi?city=ST><img src=images/st.jpg border=0></a>
    <br><br>There are <font color=#ff0000>$num</font> Public Schools in <font color=#ff7f00>$cityname</font>:
    <table border=1>
    <tr>
    <td>
    <table border=0>
    <tr>
    <td><img src=\"images/marker_20_green.png\"><a href=school.cgi?city=$ctn&level=green>Elementary</a></td>
    <td><img src=\"images/marker_20_blue.png\"><a href=school.cgi?city=$ctn&level=blue>Middle</a></td>
    <td><img src=\"images/marker_20_red.png\"><a href=school.cgi?city=$ctn&level=red>High</a></td>
    </tr>
    </table>
    </td>
    <td width=\"150\">
    <center><a href=school.cgi?city=$ctn>Show All Public Schools in $cityname</a></center></td>
</font></td>
    </td>
    <td>
<form name=f1>
<input type=button value=\"Resize Window\"
onClick=\"if(parseInt(navigator.appVersion)>3)top.resizeTo(1000,1000)\">
</form>
    </td>
    </tr>
    </table>
    <table border=1>
      <tr>
        <td>
           <div id=\"map\" style=\"width: 600px; height: $sbheight\"></div>
        </td>
        <td width = $sbwidth valign=\"top\" style=\"text-decoration: underline; color: #4444ff;\">
           <div id=\"sidebar\" style=\"overflow: auto; width: $sbwidth; height: $sbheight\"></div>
        </td>
      </tr>

    </table>
    <script type=\"text/javascript\">
    //<![CDATA[

var icon = new GIcon();
icon.image = \"http://labs.google.com/ridefinder/images/mm_20_red.png\";
icon.shadow = \"http://labs.google.com/ridefinder/images/mm_20_shadow.png\";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);

    if (GBrowserIsCompatible()) {

      function GPoint2(x,y) { return new GLatLng(y,x); }

      // this variable will collect the html which will eventualkly be placed in the sidebar
      var sidebar_html = \"\";

      // arrays to hold copies of the markers and html used by the sidebar
      // because the function closure trick doesnt work there
      var gmarkers = [];
      var htmls = [];
      var to_htmls = [];
      var from_htmls = [];
      var i = 0;

      // A function to create the marker and set up the event window
      function createMarker(point,name,html,icon,street,city) {
        var marker = new GMarker(point,icon);

      // The info window version with the to here form open
      to_htmls[i] = html + '<br>Directions: <b>To here</b> - <a href=\"javascript:fromhere(' + i + ')\">From here</a>' + '<br>Start address:<form action=\"http://maps.google.com/maps\" method=\"get\" target=\"_blank\">' + '<input type=\"text\" SIZE=40 MAXLENGTH=40 name=\"saddr\" id=\"saddr\" value=\"\" /><br>' + '<INPUT value=\"Get Directions\" TYPE=\"SUBMIT\">' + '<input type=\"hidden\" name=\"daddr\" value=\"' + street + ',' + city + ',CA'  + '\"/>';


      // The info window version with the to here form open
       from_htmls[i] = html + '<br>Directions: <a href=\"javascript:tohere(' + i + ')\">To here</a> - <b>From here</b>' + '<br>End address:<form action=\"http://maps.google.com/maps\" method=\"get\" target=\"_blank\">' + '<input type=\"text\" SIZE=40 MAXLENGTH=40 name=\"daddr\" id=\"daddr\" value=\"\" /><br>' + '<INPUT value=\"Get Directions\" TYPE=\"SUBMIT\">' + '<input type=\"hidden\" name=\"saddr\" value=\"' + street + ',' + city + ',CA' + '\"/>'; 

        // The inactive version of the direction info
        html = html + '<br>Directions: <a href=\"javascript:tohere('+i+')\">To here</a> - <a href=\"javascript:fromhere('+i+')\">From here</a>';

        GEvent.addListener(marker, \"click\", function() {
          marker.openInfoWindowHtml(html);
        });
        // save the info we need to use later for the sidebar
        gmarkers[i] = marker;
        htmls[i] = html;

        // add a line to the sidebar html
        sidebar_html += '<a href=\"javascript:myclick(' + i + ')\">' + name + '</a><br>';
        i++;
        return marker;
      }

      // functions that open the directions forms
      function tohere(i) {
        gmarkers[i].openInfoWindowHtml(to_htmls[i]);
      }
      function fromhere(i) {
        gmarkers[i].openInfoWindowHtml(from_htmls[i]);
      }

      // This function picks up the click and opens the corresponding info window
      function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
      }

      // This function zooms in or out
      // its not necessary to check for out of range zoom numbers, because the API checks
      function myzoom(a) {
        map.setZoom(map.getZoom() + a);
      }

      // create the map
      var map = new GMap2(document.getElementById(\"map\"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GPoint2$cityctr, 13);
";

$part2="
      // And finnaly create sidebar
      // put the assembled sidebar_html contents into the sidebar div
      document.getElementById(\"sidebar\").innerHTML = sidebar_html;

    }

    else {
      alert(\"Sorry, the Google Maps API is not compatible with this browser\");
    }

    //]]>
    </script>
  </body>

</html>
";
 print "$part1";

  for ($j=0;$j<$num;$j++){
  if ($level[$j] eq "Elementary"){$marker[$j]="marker_34_green.png";$lcode[$j]="e";}
  if ($level[$j] eq "Middle"){$marker[$j]="marker_34_blue.png";$lcode[$j]="m";}
  if ($level[$j] eq "High"){$marker[$j]="marker_34_red.png";$lcode[$j]="h";}
  }

  for ($jj=0;$jj<$num;$jj++){
   $color=$marker[$jj];
   $color=~s/.png//gi;
   $color=~s/marker_.._//gi;
  if ($lev eq $color | $lev eq "") {
    if ($name[$jj] eq "Lynbrook"){$city[$jj]="San Jose";}
    &findcoord(($jj,$coord[$jj],$street[$jj],$level[$jj],$marker[$jj],$city[$jj],$name[$jj],$phone[$jj],$lcode[$jj]));
#   print "haha $color $lev $level\n";
   print $loc;
   }
 }

 print "$part2";

sub findcoord{
$infohtml="<li> $_[6] $_[3]";
$_[5]=~s/ /+/g;
#$opmsg="http://www.greatschools.net/modperl/bycity/ca//?city=$_[5]&level=$_[8]";
$opmsg="http://www.greatschools.net/schools.page?city=$_[5]&lc=$_[8]&state=CA";
$_[5]=~s/\+/ /g;
$loc="
      // add Locations
      var point = new GPoint2$_[1];
      var InfoHTML = '$infohtml';
      var description = '$_[6] $_[3]<br>$_[2],&nbsp;$_[5]<br>$_[7]<br><a href=$opmsg target=\"_blank\">Compare Schools</a><br><a href=\"javascript:myzoom(1)\">Zoom In</a>&nbsp;&nbsp;<a href=\"javascript:myzoom(-1)\">Zoom Out</a>';
      icon.image = \"images/$_[4]\";
      var marker = createMarker(point,InfoHTML,description,icon,'$_[2]','$_[5]')
      map.addOverlay(marker);
";
}
