<?php
/* $Id$ */

/**
 * @file
 * GMap Filters is a module to include Google Map in a module
 *
 * GMap filter allows the insertion of a googlemap in a module.  It has
 * a page to creat a macro and then a filter to convet the macro into the
 * html and javascript code required to insert a google map.
 */

$gmap_initialized=FALSE;

function gmap_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Filter to allow insertion of a google map into a node');
      break;
  }
}

function gmap_perm() {
  return array('create macro', 'override defaults', 'show user map', 'user locations');
}

/**
 *
 * Returns the html required to insert a map from a gmap associative array.
 *
 * @param $gmap
 * An associative array with the following variables set:
 *
 *  id - the id of the map every map on a page must have a unique id
 *  width - width of the map
 *  height - height of the map
 *  center - a string of the longitude and latitude of the centre of the map
 *  zoom - the zoom factor of the google map
 *  align - the alignment of the map 'right', 'left' or 'center'
 *  control - the control shown on the map 'Large', 'Small', or 'None'
 *  tcontrol - whether the type control is on the map or not: 'off' or 'on'
 *  type - 'Map', 'Hybrid' or 'Satellite'
 *  drag - 'yes' or 'no' map is draggable.  Default is 'yes'
 *  points - an array of associative arrays for the points.
 *  shapes - an array of associative arrays for the overlays.
 *  track - a file containing a series of points in .plt format to be
 *  inserted into the node.
 *
 *  Xmaps must be enabled for circle and polygon to work.
 *
 * @param $javascript
 * Some javascript to insert into function after drawing the map.
 * note that '{id}' will be replaced with the map id.
 *
 * @return
 * A string with the google map ready to be inserted into a node.
 *
 */


function gmap_from_var ($gmap,$javascript='') {
  global $base_url;
    _gmap_doheader();
    (!isset($gmap['width'])) ? $gmap['width']=variable_get('gmap_default_width', 300): NULL;
    (!isset($gmap['height'])) ? $gmap['height']=variable_get('gmap_default_height', 200):NULL;
    (!isset($gmap['zoom'])) ? $gmap['zoom']=variable_get('gmap_default_zoom', 7):NULL;
    (!isset($gmap['align'])) ? $gmap['align']='':NULL;
    (!isset($gmap['id'])) ? $gmap['id']='map':NULL;
    (!isset($gmap['control'])) ? $gmap['control']='Small':NULL;
    (!isset($gmap['type'])) ? $gmap['type']='Map':NULL;
    (!isset($gmap['tcontrol'])) ? $gmap['tcontrol']='off':NULL;
    (!isset($gmap['drag'])) ? $gmap['drag']='yes':NULL;

    if (!isset($gmap['center'])&& isset($gmap['latlong'])){
      //backwards compatible with macros created by previous version of this program
      $gmap['center']=$gmap['latlong'];
    }
    elseif (!isset($gmap['center'])){
      $gmap['center']=variable_get('gmap_default_latlong', '-123.1, 49.2');
    }

    $style='width: '.gmap_todim($gmap['width']).'; height: '.gmap_todim($gmap['height']).';';


    switch (strtolower($gmap['align'])) {
      case 'left':
        $style .= ' float: left;';
        break;
      case 'right':
        $style .= ' float: right;';
        break;
      case 'center':
      case 'centre':
        $style .= ' margin-left: auto; margin-right: auto;';
    }

    $outtext.='<div id="'.$gmap['id'].'" style="'.$style.'"></div>
              <script type="text/javascript" type="text/javascript">
              //<![CDATA[
              //initialize gmap variables
              var mycontrol=null;
              var mytypecontrol=null;
              var '.$gmap['id'].'=null;

              gmap_torun.push(\'gmap_load_'.$gmap['id'].'();\');
              function gmap_load_'.$gmap['id'].'() {
                 '.$gmap['id'].' = new GMap(document.getElementById("'.$gmap['id'].'"));
                 '.$gmap['id'].'.centerAndZoom(new GPoint('.$gmap['center'].'), '.$gmap['zoom'].');
                 ';
    switch (strtolower($gmap['control'])) {
      case 'small':
        $outtext .='mycontrol=new GSmallMapControl();
                '.$gmap['id'].'.addControl(mycontrol);
                 ';
        break;
      case 'large':
      $outtext .='mycontrol=new GLargeMapControl();
              '.$gmap['id'].'.addControl(mycontrol);
              ';
    }

    if (strtolower($gmap['tcontrol'])=='on' || strtolower($gmap['tcontrol'])=='yes') {
      $outtext .= $gmap['id'].'.addControl(mytypecontrol=new GMapTypeControl());
                ';
    }

    switch (strtolower($gmap['type'])) {
      case 'hybrid':
        $outtext .= $gmap['id'].".setMapType(G_HYBRID_TYPE); \n";
        break;
      case 'satellite':
        $outtext .= $gmap['id'].".setMapType(G_SATELLITE_TYPE); \n";
    }
    if (isset($gmap['drag']) && strtolower($gmap['drag'])=='no') {
      $outtext .= $gmap['id'].".disabledragging(); \n";
    }
    $outext .= $gmap['id'].'.centerAndZoom(new GPoint('.$gmap['center'].'), '.$gmap['zoom'].");\n";

    if (isset($gmap['track'])) {
      foreach ($gmap['track'] as $value) {
        $tt=$value;
        $tt['type']='line';
        //open file parse into points
        $tt['points']=array();
        if($trackFH = fopen($tt['filename'], "r")) {
          while(!feof($trackFH)) {
            $line = fgets($trackFH, 4096);
            $line_exploded = explode(",", $line);
            if(count($line_exploded) == 7) {
              $tt['points'][]= trim($line_exploded[0]).', '.trim($line_exploded[1]);
            }
          }
          fclose($trackFH);
        }
        $gmap['shape'][]=$tt;
      }
    }

    if (isset($gmap['markers'])) {
      $lastmarker='';
      foreach ($gmap['markers'] as $item) {
        $mymarker='';
        $mytext='';
        if (isset($item['markername'])){
          if ($item['markername']==$lastmarker) {
            $i++;
          }
          else {
            $lastmarker=$item['markername'];
            $i=1;
          }
          if (file_exists(variable_get('gmap_markerfiles','modules/gmap/markers').'/'.$item['markername'].$i.'.png')) {
            $mymarker=$base_url.'/'.variable_get('gmap_markerfiles','modules/gmap/markers').'/'.$item['markername'].$i.'.png';
          }
          elseif (file_exists(variable_get('gmap_markerfiles','modules/gmap/markers').'/'.$item['markername'].'.png')) {
            $mymarker=$base_url.'/'.variable_get('gmap_markerfiles','modules/gmap/markers').'/'.$item['markername'].'.png';
          }
        }
        if (isset($item['label']) && strlen($item['label'])>0) {
          $mytext=$item['label'];
        }
        $outtext .=$gmap['id'].'.addOverlay(createGMarker(new GPoint('.$item['point'].'),\''.$mytext.'\',\''.$mymarker."'));\n";
      }
    }
    if (isset($gmap['shape']) && !variable_get('gmap_xmaps',0)) {
      //if xmaps is not enabled then just show the lines using gma;
      foreach ($gmap['shape'] as $value) {
        $style ='';
        if (trim($value['type'])=='line') {
          if (isset($value['color'])) {
            $style .=",'".$value['color']."'";
            if (isset($value['width'])) {
              $style .=','.$value['width'];
              if (isset($value['opacity'])) {
                $style .=','.$value['opacity'];
              }
            }
          }
          foreach ($value['points'] as $lvalue) {
            if (strlen($linetxt)>0) {
              $linetxt .= ', ';
            }
            $linetxt .="new GPoint(".$lvalue.")";
          }
          $outtext .="\n".$gmap['id'].".addOverlay(new GPolyline([$linetxt] $style));";
        }
      }
    }
    elseif (isset($gmap['shape'])) {
      foreach ($gmap['shape'] as $value) {
        $linestyle=array();
        $fillstyle=array();
        if (isset($value['color'])){
          $linestyle[] = 'color: "'.$value['color'].'" ';
        }
        if (isset($value['width'])){
          $linestyle[] = 'weight: '.$value['width'];
        }
        if (isset($value['opacity'])){
          $linestyle[] = 'opacity: '.$value['opacity'];
        }
        if (isset($value['pattern'])){
          $linestyle[] = 'pattern: ['.$value['pattern'].'] ';
        }
        if (isset($value['text'])){
          $linestyle[] = 'text: "'.$value['text'].'" ';
        }
        if (isset($value['fillcolor'])){
          $fillstyle[] = 'color: "'.$value['fillcolor'].'" ';
        }
        if (isset($value['fillopacity'])){
          $fillstyle[] = 'opacity: '.$value['fillopacity'];
        }
        $outtext .= 'var lineStyle = {'.implode(',',$linestyle)."};\n";
        if (count($fillstyle)>0) {
          $outtext .= 'var fillStyle = {'.implode(',',$fillstyle)."};\n";
        }
        else {
          $outtext .= "var fillStyle = null;\n";
        }

        switch ($value['type']) {
          case 'line':
            $outtext .= "var points = []; \n";

            foreach ($value['points'] as $lvalue) {
              $outtext .="points.push(new GPoint(".$lvalue."));\n";
            }
            $outtext .= $gmap['id'].".addOverlay(new XPolyline(points, lineStyle));\n";
            break;
          case 'circle':
            //create a circle by using a radius in km
            if (empty($value['sides'])){
              $value['sides']=36;
            }
            $outtext .= 'var radius = new XDistance('.$value['radius'].",XDistance.KM);\n";
            $outtext .= 'var centre = new GPoint('.$value['center'].");\n";
            $outtext .= $gmap['id'].".addOverlay(XPolygon.createRegularPolygonFromRadius(centre, radius, ".$value['sides'].", 0, lineStyle, fillStyle));\n";
            break;
          case 'rpolygon':
            if (isset($value['sides'])){
              $value['sides']=36;
            }
            $outtext .= "var centre = new GPoint(".$value['center'].");\n";
            $outtext .= $gmap['id'].".addOverlay(XPolygon.createRegularPolygonFromPoint( new GPoint(".$value['center']."), new GPoint(".$value['point']."), 36, lineStyle, fillStyle));\n";
            break;
          case 'polygon':
            $outtext .= "var points=[];\n";
            foreach ($value['points'] as $pvalue) {
              $outtext .= "points.push(new GPoint($pvalue));\n";
            }
            $outtext .= $gmap['id'].".addOverlay(new XPolygon(points, lineStyle, fillStyle));\n";
        }
      }
    }
    if (strlen($javascript)>0) {
      $javascript=str_replace('{id}',$gmap['id'],$javascript);
      $outtext .=$javascript;
    }
    $outtext .="\n } \n   //]]>
               </script>";
    return $outtext;
}

/**
 *
 * Cleans the gmap variables to prevent javascript interjection
 *
 * @param $gmap
 * A Gmap variable
 *
 * @return
 * A GMap variable with any dangerous text removed.
 *
 * This does not really do much of anything right now.
 */

function gmap_sanitize($gmap){
  //sanitizes the gmap variables to reduce the possibility of javascript inserts
  reset($gmap);
  $value=current($gmap);
  do {
    if (key($gmap)=='id') {
      preg_match('([a-zA-Z1-9_-]*)', $value,$out);
      if (strlen($out[0])==0) $out[0]='map';
      $gmap[key($gmap)]=$out[0];
    }
    else {
      $gmap[key($gmap)]=str_replace(';','',$value);
    }
  } while ($value=next($gmap));
  return $gmap;
}

/**
 *
 * Returns the html required to insert a map from a gmap associative array.
 *
 * @param $instring
 * A string with the settings of gmap insertion in the format var=setting|var2=setting2
 *  The possible variables are
 *  id - the id of the map every map on a page must have a unique id
 *  width - width of the map
 *  height - height of the map
 *  center - a string of the longitude and latitude of the centre of the map
 *  zoom - the zoom factor of the google map
 *  align - the alignment of the map 'right', 'left' or 'center'
 *  control - the control shown on the map 'Large', 'Small', or 'None'
 *  type - 'Map', 'Hybrid' or 'Satellite'
 *  markers - a string of points to mark on the map with + between
 *          each point
 *  line - the line is defined by a set of points separated by a +
 *  track - Draws a line based on the points in the .plt file
 *  The following shape types require XMaps:
 *  circle - a circle based on a center point and a radius in km separated
 *          by a + and optionally can include the number of sizes.
 *  rpolygon - a regular polygon is defined by the center point and a point
 *          on the permiter separated by a +
 *  polygon - a polygon is defined by a set of points
 *
 *  Each of the shapes types can optionally have charecteristics of colour,
 *  width, opacity, pattern, text, fill colour, fill opacity.  Pattern, text
 *  and fill are all only used by xmaps.
 *      color - hexadecimal for the colour include the '#'
 *
 * @return
 * A string with the google map ready to be inserted into a node.
 *
 */


function gmap_parse_text($instring, $javascript='') {
  $statements=explode('|', $instring);
  $j=0;

  while (isset($statements[$j])) {
    unset($tt);
    $t=explode('=', $statements[$j],2);
    switch (trim($t[0])) {

      case 'points':
      case 'markers':
        unset($markername);
        if (strpos($t[1],'::')) { // note: we don't care about case starting with ':'
          list($markername,$t[1])=explode('::',$t[1],2);
        }
        unset($ttt);
        $ttt = explode('+',$t[1]);
        for ($i =0; $i<count($ttt); $i++) {
          unset($tt);
          if (strpos($ttt[$i],':')) {
            list($tt['point'],$tt['label']) = explode(':',$ttt[$i],2);
          }
          else {
            $tt['point']=$ttt[$i];
          }
          if (isset($markername)){
            $tt['markername']=$markername;
          }
          $gmap['markers'][]=$tt;
        }
        break;

      case 'track':
        $tt['color']=variable_get('gmap_default_line1_color', '#00cc00');
        if (strpos($t[1],':')) { // note: we don't care about case starting with ':'
          list($configs,$t[1])=explode(':',$t[1],2);
          list($tt['color'],$tt['width'],$tt['opacity'],$tt['pattern'],$tt['text'])=explode('/',$configs);
        }
        $tt['filename'] =$t[1];
        $gmap['track'][] = $tt;
        break;

      case 'line1':
        $tt['color']=variable_get('gmap_default_line1_color', '#00cc00');
      case 'line2':
        if (empty($tt['color'])){
          $tt['color']=variable_get('gmap_default_line2_color', '#ff0000');
        }
      case 'line3':
        if (empty($tt['color'])){
          $tt['color']=variable_get('gmap_default_line3_color', '#0000ff');
        }
      case 'line':
        if (strpos($t[1],':')) { // note: we don't care about case starting with ':'
          list($configs,$t[1])=explode(':',$t[1],2);
          list($tt['color'],$tt['width'],$tt['opacity'],$tt['pattern'],$tt['text'])=explode('/',$configs);
        }
        $tt['points'] = explode('+',$t[1]);
        $tt['type'] = 'line';
        $gmap['shape'][] = $tt;
        break;

      case 'rpolygon':
        if (strpos($t[1],':')) { // note: we don't care about case starting with ':'
          list($configs,$t[1])=explode(':',$t[1],2);
          list($tt['color'],$tt['width'],$tt['opacity'],$tt['pattern'],$tt['text'],$tt['fillcolor'],$tt['fillopacity'])=explode('/',$configs);
        }
        list($tt['center'],$tt['point'],$tt['sides']) = explode('+',$t[1]);
        $tt['type']='rpolygon';
        $gmap['shape'][] = $tt;
        break;

      case 'circle':
        if (strpos($t[1],':')) { // note: we don't care about case starting with ':'
          list($configs,$t[1])=explode(':',$t[1],2);
          list($tt['color'],$tt['width'],$tt['opacity'],$tt['pattern'],$tt['text'],$tt['fillcolor'],$tt['fillopacity'])=explode('/',$configs);
        }
        list($tt['center'],$tt['radius'],$tt['sides'])= explode('+',$t[1]);
        $tt['type']='circle';
        $gmap['shape'][] = $tt;
        break;

      case 'polygon':
        $tt['color']=variable_get('gmap_default_line1_color', '#00cc00');
        if (strpos(':',$t[1])) { // note: we don't care about case starting with ':'
          list($configs,$t[1])=explode(':',$t[1],2);
          list($tt['color'],$tt['width'],$tt['opacity'],$tt['pattern'],$tt['text'],$tt['fillcolor'],$tt['fillopacity'])=explode('/',$configs);
        }
        $tt['points'] = explode('+',$t[1]);
        $tt['type']='polygon';
        $gmap['shape'][] = $tt;
        break;


      default:
        $gmap[trim($t[0])] = trim($t[1]);
      }
    $j++;
  }
  return $gmap;
}

function gmap_from_text($instring, $javascript='') {
  $gmap=gmap_parse_text($instring,$javascript);
  $gmap=gmap_sanitize($gmap);
  $outtext = gmap_from_var($gmap,$javascript);
  return $outtext;
}


function _gmap_doheader(){

  global $gmap_initialized;
  if (variable_get('gmap_method','Static')=='None' || $gmap_initialized) {

    drupal_set_html_head('<!--gmap_initialized already -->');
    return;
  }

  $header_text = '<style type="text/css">
               v\:* {
                 behavior:url(#default#VML);
               }
               </style>
                <script src="http://maps.google.com/maps?file=api&amp;v=1&amp;key='.variable_get('googlemap_api_key', '').'" type="text/javascript"></script>
                <script type="text/javascript">
                 //<![CDATA[
                 var gmap_torun= new Array();

                 // note: this is to allow the page to finish loading before it tries to modify the DOM
                   function gmap_onload() {

                     for (i=0; i < gmap_torun.length; i++) {
                        eval(gmap_torun[i]);
                     }
                   }
                   var oldOnload = window.onload;
                   if (typeof window.onload != \'function\') {
                     window.onload = gmap_onload;
                   }else {
                     window.onload = function() {
                       oldOnload();
                       gmap_onload();
                     }
                   }

              var baseIcon = new GIcon();
              baseIcon.image = "http://www.google.com/mapfiles/marker.png";
              baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
              baseIcon.iconSize = new GSize(20, 34);
              baseIcon.shadowSize = new GSize(37, 34);
              baseIcon.iconAnchor = new GPoint(9, 34);
              baseIcon.infoWindowAnchor = new GPoint(9, 2);
              baseIcon.infoShadowAnchor = new GPoint(18, 25);

              function createGMarker(point, htmltext, marker) {
                if (marker.length >0) {
                  var markerIcon = new GIcon(baseIcon);
                  markerIcon.image = marker;
                  var returnMarker = new GMarker(point, markerIcon);
                }
                else {
                  var returnMarker = new GMarker(point);
                }

                // Show this htmltext  info window when it is clicked.
                if (htmltext.length>0) {
                  GEvent.addListener(returnMarker, \'click\', function() {
                    returnMarker.openInfoWindowHtml(htmltext);
                  });
                }
                return returnMarker;
              }
                  // ]]>
               </script>
              ';
  if (variable_get('gmap_xmaps',0)) {
    $header_text .= '<script src="'.variable_get('gmap_xmaps_script','misc/xmaps.1c.js').'" type="text/javascript"> </script>'."\n";
  }
  drupal_set_html_head($header_text);
  $gmap_initialized = true;
}

function _gmap_prepare($intext) {
  $out=FALSE;
  $mapexp = '/\[gmap([^\[\]]+ )* \] /x';
  preg_match_all($mapexp, $intext, $matches);
  $i=0;

  while (isset($matches[1][$i])) {
    $out[0][$i] = $matches[0][$i];
    $out[1][$i] = gmap_from_text($matches[1][$i]);
    $i++;
  } // endwhile process macro
  return $out;
}

function gmap_todim($instring) {
  if (strpos($instring,'px')) {
    return intval($instring).'px';
  }
  elseif (strpos($instring,'%')) {
    return intval($instring).'%';
  }
  else {
    return intval($instring).'px';
  }
}

function gmap_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return (array(0 => t('GMap filter')));
      break;

    case 'name':
      return t('Google map filter');
      break;

    case 'description':
      return t('converts a google map  macro into the html required for inserting a google map.');
      break;

    case 'process':
      $gmaps=_gmap_prepare($text);   //returns an array of $tables[0] = table macro $table[1]= table html
      if ($gmaps) {                    // there are table macros in this node
        return str_replace($gmaps[0], $gmaps[1], $text);
      }
      else {
        return $text;
      }
      break;

    case 'prepare':
      return $text;
      break;

    case 'no cache':
      return variable_get('gmap_method', 'Static')=='Dynamic';
      break;
  }
}

function gmap_filter_tips($delta, $format, $long = false) {
  return t('Insert Google Map macro. <a href="gmapmacro" target="_blank" >Create a macro</a>');
}

function gmap_menu($may_cache) {
  $items=array();

  if ($may_cache) {
    $items[] = array('path' => 'gmapmacro',
                     'type' => MENU_SUGGESTED_ITEM,
                     'title' => t('Create a Google Map Macro'),
                     'access' => user_access('create macro'),
                     'callback' => 'gmap_macro_page' );
    if (variable_get('gmap_user',0)) {
      $items[] = array('path' => 'users/map',
                       'type' => MENU_NORMAL_ITEM,
                       'title' => t('User Locations'),
                       'access' => user_access('show user map'),
                       'callback' => 'gmap_users_page' );
    }
  }
  if (!$may_cache && (strlen(variable_get('googlemap_api_key', '')) > 0)) {
    if (variable_get('gmap_method', 'Static')=='Static') {
      _gmap_doheader();
    }
  }
  return $items;
}

function gmap_user($op, &$edit, &$user, $category = NULL) {
  if (variable_get('gmap_user',0)) {
    switch ($op) {
      case 'categories':
        return array(array('name'=>'gmap_user', 'title'=> t('Location Map'),'weight'=>5));
      case 'validate':
        if (isset($edit['gmap_latitude'])){
          if (!is_numeric($edit['gmap_latitude']) || abs($edit['gmap_latitude'])>90) {
            form_set_error('gmap_latitude', t('Latitude must be between -90 and 90'));
          }
        }
        if (isset($edit['gmap_longitude'])){
          if (!is_numeric($edit['gmap_longitude']) || abs($edit['gmap_longitude'])>180) {
            form_set_error('gmap_longitude', t('Longitude must be between -180 and 180'));
          }
        }
        return;
      case 'load':
        $res=db_query("SELECT * FROM {gmap_user} WHERE uid=%d",$user->uid);
        if ($gmap_user=db_fetch_array($res)) {
          $user->latitude=$gmap_user['latitude'];
          $user->longitude=$gmap_user['longitude'];
          $user->gmap_zoom=$gmap_user['zoom'];
        }
        return;
      case 'insert':
      case 'update':
        if ((isset($edit['gmap_latitude']) && $edit['gmap_latitude'] !=0) && (isset($edit['gmap_longitude']) && $edit['gmap_longitude'] !=0)) {
          db_query('DELETE from {gmap_user} WHERE uid=%d',$user->uid);
          db_query("INSERT INTO {gmap_user} (uid, latitude, longitude, zoom) VALUES (%d, %f, %f, %d)", $user->uid, $edit['gmap_latitude'],$edit['gmap_longitude'], $edit['gmap_zoom']);
        }
        return;

        //db_query ("UPDATE {gmap_user} SET latitude='%f', longitude='%f', zoom=%d WHERE uid=%d", $user->uid, $edit['gmap_latitude'],$edit['gmap_longitude'], $edit['gmap_zoom']);
        //return;
      case 'delete':
        db_query ('DELETE from {gmap_user} WHERE uid=%d', $user->uid);
        return;
      case 'form':
        if ($category=='gmap_user') {
          if (isset($user->latitude) && isset($user->longitude)) {
            $usermap=_gmap_user_form($user->longitude,$user->latitude, $user->gmap_zoom);
          }
          else {
            $usermap = _gmap_user_form();
          }

          $form['map']=array('#type' => 'fieldset','#title' => t('Google Map'));
          $form['map']['gmap_user']=array('#type'=>'markup','#value'=>$usermap);
          $form['coordinates']=array('#type' => 'fieldset','#title' => t('Coordinates'));
          $form['coordinates']['gmap_latitude']=array('#type'=>'textfield', '#title'=>t('Latitude'), '#default_value'=>$user->latitude, '#size'=>30, '#maxlength'=>120, '#description'=>t('The latitude will automatically be entered here (or you can do it manually).'),'#onchange'=>'gmap_textchange();');
          $form['coordinates']['gmap_longitude']=array('#type'=>'textfield', '#title'=>t('Longitude'), '#default_value'=>$user->longitude, '#size'=>30, '#maxlength'=>120, '#description'=>t('The longitude will automatically be entered here (or you can do it manually).'),'#onchange'=>'gmap_textchange();');
          $form['coordinates']['gmap_user_zoom']=array('#type'=>'hidden', '#default_value'=>variable_get('gmap_user_zoom', 15));
          return  $form;
 /*
          $form2 = form_textfield(t('Latitude'), 'gmap_latitude', $user->latitude, 30,120, t('The latitude will automatically be entered here (or you can do it manually).'), array('onchange'=>'gmap_textchange();') );
          $form2 .= form_textfield(t('Longitude'), 'gmap_longitude', $user->longitude, 30,120, t('The longitude will automatically be entered here (or you can do it manually).'), array('onchange'=>'gmap_textchange();') );
          $form2 .= form_hidden('gmap_zoom', variable_get('gmap_user_zoom', 15));

          return array(array('title'=>t('Google Map'), 'data'=>$form, 'weight'=>0),array('title'=>t('Coordinates'), 'data'=>$form2, 'weight'=>1));
*/
        }
    }
  }
}

/* Not used in Drupal 4.7 due to a bug in Drupal 4.7
function gmap_onload() {
  if (strlen(variable_get('googlemap_api_key', ''))>0) return array('gmap_onload()');
}
*/
function gmap_settings() {
  //note the same google api key variable name as in the googlemap module is used
  //note the name of the variable for center of the map is latlong although the format is actually longitude, latitude

  $form['initialization']=array('#type' => 'fieldset','#title' => t('Google Map Initialize'));
  $form['initialization']['googlemap_api_key']=array('#type' => 'textfield', '#title' => t('Google map API key'), '#default_value'=>variable_get('googlemap_api_key', ''), '#size' => 50, '#maxlength' => 255,'#description' => t('Your personal Googlemaps API key.  You must get this for each separate website at <a href="http://www.google.com/apis/maps/">Google Map API website</a>.'));
  $form['initialization']['gmap_method']=array('#type' => 'select', '#title' => t('Google map method'),  '#options'=> array('Static'=>t('Static'), 'Dynamic'=>t('Dynamic'), 'None'=>t('None')),'#default_value'=>variable_get('gmap_method','Static'),'#description' => t('How is the Google Map initialization script run:<br /><strong>Static</strong> - Loaded on every page, <br /><strong>Dynamic</strong> - Initialization script runs only on pages with google maps, but all pages with the gmap filter will not be cached. <br /><strong>None</strong> - Google map initialization script must be loaded some other way.'));
  $form['initialization']['gmap_xmaps']=array('#type'=>'checkbox', '#title'=>t('Enable Xmaps features (circles and polygons)'), '#default_value'=>variable_get('gmap_xmaps',0),'#description'=>t('Enables XMaps features.  XMaps is currently a <em>beta</em> release available from <a href="http://xmaps.busmonster.com/">xmaps.busmonser.com</a>. '));
  $form['initialization']['gmap_xmaps_script']=array('#type' => 'textfield', '#title' => t('Location of XMaps script'), '#default_value'=>variable_get('gmap_xmaps_script','misc/xmaps.1c.js'), '#size' => 25, '#maxlength' => 50);


  $form['defaults']=array('#type' => 'fieldset', '#title' => t('Default map settings'));
  $form['defaults']['gmap_default_width']=array('#type'=>'textfield', '#title'=>t('Default width'), '#default_value'=>variable_get('gmap_default_width', 300), '#size'=>25, '#maxlength'=>6, '#description'=>t('The default width of a Google map.'));
  $form['defaults']['gmap_default_height']=array('#type'=>'textfield', '#title'=>t('Default height'), '#default_value'=>variable_get('gmap_default_height', 200), '#size'=>25, '#maxlength'=>6, '#description'=>t('The default height of a Google map.'));
  $form['defaults']['gmap_default_latlong']=array('#type'=>'textfield', '#title'=>t('Default center'), '#default_value'=>variable_get('gmap_default_latlong', '-123.1, 49.2'), '#size'=>50, '#maxlength'=>255, '#description'=>t('The default longitude, latitude of a Google map.'));
  $form['defaults']['gmap_default_zoom']=array('#type'=>'textfield', '#title'=>t('Default zoom'), '#default_value'=>variable_get('gmap_default_zoom', 7), '#size'=>25, '#maxlength'=>4, '#description'=>t('The default zoom level of a Google map (between 1 and 24).'));
  $form['defaults']['gmap_default_control']=array('#type'=>'select', '#title'=>t('Default control type'), '#default_value'=>variable_get('gmap_default_control', 'Small'), '#options'=>array('None'=>t('None'),'Small'=>t('Small'),'Large'=>t('Large')));
  $form['defaults']['gmap_default_type']=array('#type'=>'select', '#title'=>t('Default map type'), '#default_value'=>variable_get('gmap_default_type', 'Map'), '#options'=>array('Map'=>t('Map'),'Hybrid'=>t('Hybrid'),'Satellite'=>t('Satellite')));
  $form['defaults']['gmap_default_line1_color']=array('#type'=>'textfield', '#title'=>t('Default Line 1 Color'), '#default_value'=>variable_get('gmap_default_line1_color', '#00cc00'), '#size'=>25, '#maxlength'=>7 );
  $form['defaults']['gmap_default_line2_color']=array('#type'=>'textfield', '#title'=>t('Default Line 2 Color'), '#default_value'=>variable_get('gmap_default_line2_color', '#ff0000'), '#size'=>25, '#maxlength'=>7 );
  $form['defaults']['gmap_default_line3_color']=array('#type'=>'textfield', '#title'=>t('Default Line 3 Color'), '#default_value'=>variable_get('gmap_default_line3_color', '#0000ff'), '#size'=>25, '#maxlength'=>7 );

  $form['user']=array('#type' => 'fieldset', '#title' => t('User map settings'));
  $form['user']['gmap_user']=array('#type'=>'checkbox', '#title'=>t('Enable Mapping of user locations'), '#default_value'=>variable_get('gmap_user',0),'#description'=>t('Ensure that the database table gmap_user is created before turning on the user location mapping functions.'));
  $form['user']['gmap_user_map']=array('#type'=>'textfield', '#title'=>t('Default user map'), '#default_value'=>variable_get('gmap_user_map', '[gmap|id=usermap|center=0,30|zoom=16|width=100%|height=400px]'), '#size'=>50, '#description'=>t('The gmap macro where the user information will be diplayed on.'));
  $form['user']['gmap_user_map_header']=array('#type'=>'textarea', '#title'=>t('Text at the top of the users/map page'), '#default_value'=>variable_get('gmap_user_map_header', t('This map illustrates the extent of users of this website. Each marker indicates a user that has entered their locations.')), '#cols'=>50, '#rows'=>6 );

  return $form;
  /*  from 4.6 settings
  $output  = form_group(t('Google map initializations'),
             form_textfield(t('Google API Key'), 'googlemap_api_key', variable_get('googlemap_api_key', ''), 100, 255, t('Your personal Googlemaps API key.  You must get this for each separate website at <a href="http://www.google.com/apis/maps/">Google Map API website</a>.')).
             form_select(t('Google map method'), 'gmap_map_method', variable_get('gmap_map method', 'Static'), array('Static'=>t('Static'), 'Dynamic'=>t('Dynamic'), 'None'=>t('None')),t('How is the Google Map initialization script run:<br /><strong>Static</strong> - Loaded on every page, <br /><strong>Dynamic</strong> - Initialization script runs only on pages with google maps, but all pages with the gmap filter will not be cached. <br /><strong>None</strong> - Google map initialization script must be loaded some other way.')));
  $output .= form_group(t('Default map settings'),
             form_textfield(t('Default width'), 'gmap_default_width', variable_get('gmap_default_width', 300), 100,4,t('The default width of a Google map.')).
             form_textfield(t('Default height'), 'gmap_default_height', variable_get('gmap_default_height', 200), 100,4,t('The default height of a Google map.')).
             form_textfield(t('Default center'), 'gmap_default_latlong', variable_get('gmap_default_latlong', '-123.1, 49.2'), 100,120,t('The default longitude, latitude of a Google map.')).
             form_textfield(t('Default zoom'), 'gmap_default_zoom', variable_get('gmap_default_zoom', 7), 100,2,t('The default zoom level of a Google map.')).
             form_select(t('Default Control type'), 'gmap_default_control', variable_get('gmap_default_control', 'Small'), array('None'=>t('None'), 'Small'=>t('Small'), 'Large'=>t('Large')),t('The default control type for the map.')).
             form_select(t('Default map type'), 'gmap_map_type', variable_get('gmap_default_type', 'Map'),array('Map'=>t('Map'), 'Hybrid'=>t('Hybrid'), 'Satellite'=>t('Satellite')),t('The default map type.')).
             form_textfield(t('Line1 color'), 'gmap_default_line1_color', variable_get('gmap_default_line1_color', '#00cc00'), 100,7).
             form_textfield(t('Line2 color'), 'gmap_default_line2_color', variable_get('gmap_default_line2_color', '#ff0000'), 100,7).
             form_textfield(t('Line3 color'), 'gmap_default_line3_color', variable_get('gmap_default_line3_color', '#0000ff'), 100,7));
  $output .= form_group(t('User map settings'),
             form_checkbox(t('Enable mapping of user locations.'),'gmap_user', 1, variable_get('gmap_user',0),t('Ensure that the database table gmap_user is created before turning on the user location mapping functions.')).
             form_textfield(t('User Map View'), 'gmap_user_map', variable_get('gmap_user_map', '[gmap|id=usermap|center=0,30|zoom=16|width=100%|height=400px]'), 100,255));

  return $output;
  */
}

/**
 *
 * Creates a page that has all of the javascript required for the macro-creation tool.
 *
 */

function gmap_macro_page() {

   $line_colour=array(1=>variable_get('gmap_default_line1_color', '#00cc00'), variable_get('gmap_default_line2_color', '#ff0000'),variable_get('gmap_default_line3_color', '#0000ff'));
  (isset($_REQUEST['width'])) ? $newwidth=$_REQUEST['width'] : $newwidth= variable_get('gmap_default_width', 300);
  (isset($_REQUEST['height'])) ? $newheight=$_REQUEST['height'] : $newheight= variable_get('gmap_default_height', 200);
  (isset($_REQUEST['longlat'])) ? $newlonglat=$_REQUEST['longlat'] : $newlonglat= variable_get('gmap_default_latlong', '-123.1, 49.2');
  (isset($_REQUEST['zoom'])) ? $newzoom=$_REQUEST['zoom'] : $newzoom= variable_get('gmap_default_zoom', 7);
  (isset($_REQUEST['control'])) ? $newcontrol=$_REQUEST['control'] : $newcontrol= variable_get('gmap_default_control', 'Small');
  $control=''; $small=''; $large='';
  (variable_get('clean_url', true)) ? $qurl='' : $qurl='?q=';
  $none= ($newcontrol=='None') ? 'SELECTED ' :'';
  $initiate = "//initiate variables\n";
  if ($newcontrol=='Small') {
    $small = 'SELECTED ';
    $initiate .=  "map.addControl(mycontrol = new GSmallMapControl());\n";
  }
  if ($newcontrol=='Large') {
    $large = 'SELECTED ';
    $initiate .=  "map.addControl(mycontrol = new GLargeMapControl());\n";
  }

  for ($i=1; $i<=3; $i++) {
    if (isset($_REQUEST['point'.$i]) && strlen($_REQUEST['point'.$i])>0) {
      $newpoint[$i]=$_REQUEST['point'.$i];
      $initiate .= 'point'.$i.'overlay=new GMarker(new GPoint('.$_REQUEST['point'.$i].'));'."\n";
      $initiate .= 'map.addOverlay(point'.$i.'overlay);'."\n";
    }
    else {
      $newpoint[$i]='';
    }

    if (isset($_REQUEST['line'.$i]) && strlen($_REQUEST['line'.$i])>0) {
      $newline[$i]=$_REQUEST['line'.$i];
      $initiate .= 'line'.$i.'overlay = new GPolyline([new GPoint('.str_replace(' + ','), new GPoint(',$newline[$i]).')],"'.$line_colour[$i].'", 5);'."\n";
      $initiate .= 'map.addOverlay(line'.$i.'overlay);'."\n";
    }
    else {
      $newline[$i]='';
    }

  }

  $output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
    <title>GMap Macro Creator</title>
    <script src="http://maps.google.com/maps?file=api&v=1&key='.variable_get('googlemap_api_key', 'abcdef').'" type="text/javascript"></script>
    <style type="text/css">
      v\:* {
        behavior:url(#default#VML);
      }
      body {
        font-family: Helvetica, Arial, Lucida, Verdana, sans-serif;
      }
      h1 {
        font-size: 1.2em;
        font-weight: 700;
      }
    </style>
  </head>
  <body onload="makemacro(); gmapload();">
    <h1>GMap Macro creation tool</h1>
    <p>Using the controls on this page, set the map as to how you would like it to appear and then use the gmap macro filter to insert this
    into a node.  Select the contents of the macro form (by clicking on macro text field) and then copy and paste it into a node where the
    gmap filter has been enabled.
    <p><div id="map" style="width: '.$newwidth.'; height: '.$newheight.'"></div>
    <script type="text/javascript">
    //<![CDATA[

    function makemacro() {
      var zooml = \' |zoom=\' + document.gmapform.zoom.value;
      var center = \' |center=\' + document.gmapform.longlat.value;
      var width = \' |width=\' + document.gmapform.width.value;
      var height = \' |height=\' + document.gmapform.height.value;
      var id = \' |id=\' + document.gmapform.id.value;
      var control = \' |control=\' + document.gmapform.controltype.value;
      var type = \' |type=\' + document.gmapform.maptype.value;
      var alignment = \' |align=\' + document.gmapform.alignment.value;
      if (points.length >0) {
        var outpoints = \' |markers=\' + points.join(\' + \');
      } else var outpoints=\'\';
      if (document.gmapform.line1.value.length >0) {var line1 = \' |line1=\' + document.gmapform.line1.value} else line1=\'\';
      if (document.gmapform.line2.value.length >0) {var line2 = \' |line2=\' + document.gmapform.line2.value} else line2=\'\';
      if (document.gmapform.line3.value.length >0) {var line3 = \' |line3=\' + document.gmapform.line3.value} else line3=\'\';

      document.gmapform.macro.value = \'[gmap\' + id + center + zooml + width + height +  alignment + control + type + outpoints + line1 + line2 + line3 + \']\';

    }
function mapat(instring) {
     var splitstring=instring.split(",");
     map.centerAtLatLng(new GPoint(splitstring[0],splitstring[1]));
}

function docontrol(incontrol) {
     map.removeControl(mycontrol);
     if (incontrol == "Small") map.addControl(mycontrol = new GSmallMapControl());
     if (incontrol == "Large") map.addControl(mycontrol = new GLargeMapControl());
     makemacro();
}

function changetype(intype) {
    if (intype == "Map") map.setMapType(G_MAP_TYPE);
    if (intype == "Hybrid") map.setMapType(G_HYBRID_TYPE);
    if (intype == "Satellite") map.setMapType(G_SATELLITE_TYPE);
    makemacro();
}

function doresize () {
     var width = document.gmapform.width.value; //(in pixels, e.g. 500px)
     var height = document.gmapform.height.value; //(in pixels, e.g. 300px)
     if (mapdiv){
           mapdiv.style.width=width;
           mapdiv.style.height=height;
     }
     map.onResize();
     makemacro();
}

var map=null;
var mycontrol=null;
var points=[];
var pointsOverlays=[];

var line1overlay=null;  var line1points=new Array(); var line1string=new String();
var line2overlay=null;  var line2points=new Array(); var line2string=new String();
var line3overlay=null;  var line3points=new Array(); var line3string=new String();

    function gmapload() {
      if (GBrowserIsCompatible()) {
        map = new GMap(document.getElementById("map"));

        map.centerAndZoom(new GPoint('.$newlonglat.'), '.$newzoom.');
       '.$initiate.'


        GEvent.addListener(map, "moveend", function() {
          var center = map.getCenterLatLng();
          var latLngStr = center.x + \', \' + center.y ;
          document.gmapform.longlat.value = latLngStr;
          makemacro();
        });

        GEvent.addListener(map, "zoom", function() {
          var zooml = map.getZoomLevel();
          document.gmapform.zoom.value = zooml;
          document.gmapform.longlat.value = latLngStr;
          makemacro();
        });


        GEvent.addListener(map, \'click\', function(overlay, point) {

          if (overlay) {
            var shft=false;
            for (i=0; i<points.length; i++){
              if (overlay==pointsOverlays[i]) {
                shft=true;
              }
              if (shft==true) {
                if (i<points.length) {
                  pointsOverlays[i]=pointsOverlays[i+1];
                  points[i]=points[i+1];
                }
              }
            }
            points.pop();
            pointsOverlays.pop();
            map.removeOverlay(overlay);
          }
          else if (point) {
            if (document.gmapform.clicktype.value==\'Points\') {
              map.addOverlay(marker=new GMarker(point));
              pointsOverlays.push(marker);
              points.push(point.x + \',\' + point.y);
            }
            else if (document.gmapform.clicktype.value==\'Line1\') {
              line1points.push(point);
              if (line1overlay) map.removeOverlay(line1overlay);
              line1overlay=new GPolyline(line1points,"'.$line_colour[1].'", 5);
              map.addOverlay(line1overlay);
              if (line1string.length > 0) line1string += \' + \';
              line1string += point.x + \',\' + point.y;
              document.gmapform.line1.value = line1string;
            }
            else if (document.gmapform.clicktype.value==\'Line2\') {
              line2points.push(point);
              if (line2overlay) map.removeOverlay(line2overlay);
              line2overlay=new GPolyline(line2points,"'.$line_colour[2].'", 5);
              map.addOverlay(line2overlay);
              if (line2string.length > 0) line2string += \' + \';
              line2string += point.x + \',\' + point.y;
              document.gmapform.line2.value = line2string;
            }
            else if (document.gmapform.clicktype.value==\'Line3\') {
              line3points.push(point);
              if (line3overlay) map.removeOverlay(line3overlay);
              line3overlay=new GPolyline(line3points,"'.$line_colour[3].'", 5);
              map.addOverlay(line3overlay);
              if (line3string.length > 0) line3string += \' + \';
              line3string += point.x + \',\' + point.y;
              document.gmapform.line3.value = line3string;
            }

          }
          makemacro();
        });
      }
    }

    function addpoint(formelement) {
     var splitstring=formelement.value.split(",");
     var point=new GPoint(splitstring[0],splitstring[1]);
           if (document.gmapform.clicktype.value==\'Points\') {
              map.addOverlay(marker=new GMarker(point));
              pointsOverlays.push(marker);
              points.push(formelement.value);
            }
            else if (document.gmapform.clicktype.value==\'Line1\') {
              line1points.push(point);
              if (line1overlay) map.removeOverlay(line1overlay);
              line1overlay=new GPolyline(line1points,"'.$line_colour[1].'", 5);
              map.addOverlay(line1overlay);
              if (line1string.length > 0) line1string += \' + \';
              line1string += point.x + \',\' + point.y;
              document.gmapform.line1.value = line1string;
            }
            else if (document.gmapform.clicktype.value==\'Line2\') {
              line2points.push(point);
              if (line2overlay) map.removeOverlay(line2overlay);
              line2overlay=new GPolyline(line2points,"'.$line_colour[2].'", 5);
              map.addOverlay(line2overlay);
              if (line2string.length > 0) line2string += \' + \';
              line2string += point.x + \',\' + point.y;
              document.gmapform.line2.value = line2string;
            }
            else if (document.gmapform.clicktype.value==\'Line3\') {
              line3points.push(point);
              if (line3overlay) map.removeOverlay(line3overlay);
              line3overlay=new GPolyline(line3points,"'.$line_colour[3].'", 5);
              map.addOverlay(line3overlay);
              if (line3string.length > 0) line3string += \' + \';
              line3string += point.x + \',\' + point.y;
              document.gmapform.line3.value = line3string;
            }

          makemacro();

}

    function newid  ()
    {
      var newvalue = document.gmapform.id.value;
      newvalue=newvalue.match(/^[0-9A-Za-z_-]+/);
      if (newvalue.length==0) newvalue=\'map\';
      makemacro();
    };

    //]]>
    </script>

    <form action="'.$qurl.'gmapmacro" name="gmapform" method="post">
    <table>
      <tr><td><a title="When multiple maps are to be shown on the same page, each one must have a unique id. (only letters numbers and - and _ are permitted)">ID:</a></td><td><input type="text" size="25" name="id" value="map"  onchange="newid(); " />
          <td>Map type:</td><td><SELECT NAME="maptype" onchange="changetype(document.gmapform.maptype.value);">
                                  <OPTION VALUE="Map" />Map
                                  <OPTION VALUE="Hybrid" />Hybrid
                                  <OPTION VALUE="Satellite" />Satellite
                                </SELECT> </td> </tr>
      <tr><td><a title="The Longitude and Latitude of the centre of the map">Long, Lat:</a></td><td colspan=3><input type="text" size="50" name="longlat" value="'.$newlonglat.'" onchange="mapat(document.gmapform.longlat.value); " />  </td>
      <tr><td><a title="Map size">Width:</a></td><td><input type="text" size="25" name="width" value="'.$newwidth.'"  onchange="doresize();" /></td>
          <td>Alignment:</td><td><SELECT NAME="alignment" onchange="makemacro();">
                                  <OPTION VALUE="None" />None
                                  <OPTION VALUE="Right" />Right
                                  <OPTION VALUE="Left" />Left
                                  <OPTION VALUE="Center" />Center
                                </SELECT></td></tr>

      <tr><td><a title="Map size">Height:</a></td><td><input type="text" size="25" name="height" value="'.$newheight.'"  onchange="doresize();" />  </td>
          <td>Controls:</td><td><SELECT NAME="controltype" onchange="docontrol(document.gmapform.controltype.value);">
                                  <OPTION VALUE="None" '.$none.'/>None
                                  <OPTION VALUE="Small" '.$small.'/>Small
                                  <OPTION VALUE="Large" '.$large.'/>Large
                                </SELECT>

      <tr><td><a title="What happens when you click on the map">Click type:</a></td><td><SELECT NAME="clicktype" >
                                  <OPTION VALUE="Points" TITLE="Add Marker to map"/>Markers
                                  <OPTION VALUE="Line1" />Line1
                                  <OPTION VALUE="Line2" />Line2
                                  <OPTION VALUE="Line3" />Line3
                                </SELECT> </td>
          <td><a title="The current magnification of the map">Magnification:</a></td><td><input type="text" size="5" name="zoom" value="'.$newzoom.'"  onchange="map.zoomTo(document.gmapform.zoom.value); " />  </td></tr>
      <tr><td><a title="Manually enter a Long, Latitude for a marker or the line">Add Point:</a></td><td colspan=3><input type="text" size="50" name="newpoint" value="" onchange="addpoint(this); " />  </td>


      <input type="hidden" size="50" name="line1" value="'.$newline[1].'" />
      <input type="hidden" size="50" name="line2" value="'.$newline[2].'" />
      <input type="hidden" size="50" name="line3" value="'.$newline[3].'" />

    </table>
    <textarea name="macro" cols="60" rows="10" onclick="document.gmapform.macro.select();">
    </textarea>
    </form>
  </body>
</html> ';
  print $output;
}
function _gmap_user_form($longitude=false, $latitude=false, $zoom=false) {
  if (!$zoom || $zoom<1 || $zoom>20) {
    $zoom=16;
  }
  $myjava = '';
  if ($longitude && $latitude) {
    $myjava .= '{id}.centerAndZoom(new GPoint('.$longitude.', '.$latitude.'), zoomfield.value);'."\n".
             '{id}.addOverlay(mypoint=new GMarker(new GPoint('.$longitude.', '.$latitude.')));';
  }
//  else {
//    $startat='map.centerAndZoom(new GPoint(0, 30), zoom);';
//  }

//  $out='<div id="map" style="width: 100%; height: 300px;"></div> ';


  $header='          <script type="text/javascript">
              //<![CDATA[
              var mypoint=false;
              gmap_torun.push("gmap_user()");
              function gmap_user() {
                var areas = document.getElementsByTagName("input");
                for (var i=0; i<areas.length; i++)  {
                        if (areas[i].name.match("latitude"))  {
                                latfield = areas[i];
                        }
                        if (areas[i].name.match("longitude"))  {
                                longfield = areas[i];
                        }
                        if (areas[i].name.match("zoom"))  {
                                zoomfield = areas[i];

                        }
                }
                zoomfield.value='.$zoom.';
              }
              function gmap_textchange() {
                if (mypoint) {
                  mymap.removeOverlay(mypoint);
                }
                usermap.recenterOrPanToLatLng(newpoint=new GPoint(longfield.value, latfield.value));
                usermap.addOverlay(mypoint=new GMarker(newpoint));
              }
              //]]>
            </script>
            ';


  $myjava .=  '
                if (mycontrol) {
                  {id}.removeControl(mycontrol);
                }
                mycontrol=new GLargeMapControl();
                {id}.addControl(mycontrol);
                if (!mytypecontrol) {
                  {id}.addControl(mytypecontrol=new GMapTypeControl());
                }
                GEvent.addListener({id}, \'click\', function(overlay, point) {
                  if (point) {
                    if (mypoint) {
                      {id}.removeOverlay(mypoint);
                    }
                    {id}.addOverlay(mypoint=new GMarker(point));
                    zoomfield.value =zoomfield.value-2;
                    if (zoomfield.value<1) zoomfield.value=1;
                    {id}.centerAndZoom(point, zoomfield.value);
                    latfield.value=point.y;
                    longfield.value=point.x;
                  }
                });
                GEvent.addListener({id}, "zoom", function() {
                  zoomfield.value={id}.getZoomLevel();
                });


              ';
  $out=gmap_from_text(variable_get('gmap_user_map', '[gmap|id=usermap|center=0,30|zoom=16|width=100%|height=400px]'),$myjava);

  drupal_set_html_head($header);
  return $out;
}

function gmap_users_page() {
  // get array of users for lookups.  There is probably a better way of doing this, but I'm not
  // sure what it is.  Only necessary if user has view
 /* if (user_access('user locations')) {
    $result=db_query('SELECT uid, name, picture FROM {users}');
    $usernames=array();
    while ($u=db_fetch_array($result)) {
      $username[$u['uid']]=$u['name'];
      $userpicture[$u['uid']]=$u['picture'];
    }
  } */
  $output .= variable_get('gmap_user_map_header', t('This map illustrates the extent of users of this website. Each marker indicates a user that has entered their locations.'))."</p>\n";


  $result=db_query('SELECT * FROM {gmap_user} WHERE longitude !=0 OR latitude !=0');
  $thismap = gmap_parse_text(variable_get('gmap_user_map', '[gmap|id=usermap|center=0,30|zoom=16|width=100%|height=400px]'));
  if (empty($thismap['markers'])) {
    $thismap['markers']=array();
  }
  while ($u=db_fetch_object($result)) {
    if (user_access('user locations')) {
      $account = user_load(array('uid' => $u->uid));
      
      $newmarker['label'] = theme('user_picture', $account);
      $newmarker['label'] .= theme('username', $account);
      $newmarker['point']= $u->longitude.','.$u->latitude;
//      $newmarker['markername']='usermarker';
    }
    else {
      $newmarker['point']= $u->longitude.','.$u->latitude;
    }
    $thismap['markers'][]=$newmarker;
  }
  $output .= '<p>'.gmap_from_var($thismap);
  return $output;
}