--- \old\gmap_plugin_style_gmap.inc	Thu Feb 05 15:25:23 2009
+++ \new\gmap_plugin_style_gmap.inc	Thu Feb 05 15:08:36 2009
@@ -1,5 +1,5 @@
 <?php
-// $Id: gmap_plugin_style_gmap.inc,v 1.3 2008/11/24 22:47:12 bdragon Exp $ 
+// $Id: gmap_plugin_style_gmap.inc,v 1.3 2008/11/24 22:47:12 bdragon Exp $
 
 /**
  * @file
@@ -12,32 +12,42 @@
  * @ingroup views_style_plugins
  */
 class gmap_plugin_style_gmap extends views_plugin_style {
+  var $name_lat = '';
+  var $name_lon = '';
+
   /**
    * Set default options
    */
   function option_definition() {
     $options = parent::option_definition();
 
-    $options['macro'] = array(
-      'default' => '[gmap ]',
-    );
-
-    $options['datasource'] = array(
-      'default' => 'location',
-    );
-
+    $options['macro'] = array('default' => '[gmap ]');
+    $options['datasource'] = array('default' => 'location');
+    $options['field_lat'] = array('default' => '');
+    $options['field_lon'] = array('default' => '');
     $options['markers'] = array('default' => 'static');
     $options['markertype'] = array('default' => 'drupal');
+	$options['marker_tooltip'] = array('default' => "<Use Node's title>");
+	$options['marker_action'] = array('default' => 'use_GMap_setting');
+	$options['marker_link'] = array('default' => "<Link to current Node>");
 
     return $options;
   }
 
   function query() {
     if ($this->options['datasource'] == 'location') {
-      $this->view->query->add_field('location', 'latitude', 'gmap_lat');
-      $this->view->query->add_field('location', 'longitude', 'gmap_lon');
+      $this->name_lat = $this->view->query->add_field('location', 'latitude', 'gmap_lat');
+      $this->name_lon = $this->view->query->add_field('location', 'longitude', 'gmap_lon');
+    }
+    if ($this->options['datasource'] == 'autodetect') {
+      $handlers = $this->display->handler->get_handlers('field');
+      $this->name_lat = $handlers[$this->options['field_lat']]->field_alias;
+      $this->name_lon = $handlers[$this->options['field_lon']]->field_alias;
     }
 
+	$this->name_tooltip = $handlers[$this->options['marker_tooltip']]->field_alias;
+	$this->name_link    = $handlers[$this->options['marker_link']]->field_alias;
+	
     if ($this->options['markers'] == 'nodetype') {
       $this->view->query->add_field('node', 'type', 'gmap_node_type');
     }
@@ -52,8 +62,16 @@ class gmap_plugin_style_gmap extends vie
       return;
     }
 
+	$defaults = gmap_defaults();
+
+    if (empty($this->name_lat) || empty($this->name_lon)) {
+      vpr('gmap_plugin_style_gmap: Missing lat/lon field');
+      return;
+    }
+
     $markername = isset($this->options['markertype']) ? $this->options['markertype'] : 'drupal';
 
+    // FIXME: What's the purpose of this?
     $markertypes = variable_get('gmap_node_markers', array());
     if ($this->options['markers'] == 'nodetype') {
       $markertypes = variable_get('gmap_node_markers', array());
@@ -68,9 +86,10 @@ class gmap_plugin_style_gmap extends vie
     foreach ($sets as $title => $records) {
       $markers = array();
       $offsets = array();
-        foreach ($records as $label => $row) {
-        $lat = (float)$row->gmap_lat;
-        $lon = (float)$row->gmap_lon;
+      foreach ($records as $label => $row) {
+	  
+        $lat = (float)$row->{$this->name_lat};
+        $lon = (float)$row->{$this->name_lon};
         if (!empty($lat) && !empty($lon)) {
           if ($this->options['markers'] == 'nodetype') {
             if (isset($markertypes[$row->gmap_node_type])) {
@@ -85,16 +104,56 @@ class gmap_plugin_style_gmap extends vie
           if (!isset($offsets[$markername])) {
             $offsets[$markername] = 0;
           }
-          $markers[] = array(
+
+		  $marker = array(
             'latitude' => $lat,
             'longitude' => $lon,
             'markername' => $markername,
             'offset' => $offsets[$markername],
-            'text' => $this->row_plugin->render($row),
           );
+
+		  // MARKER TOOLTIP
+          // Set marker's title so that it will be displayed as tooltip for this marker
+		  if ($this->options['marker_tooltip'] == "<Use Node's title>") {
+  		    $node = node_load($row->nid);
+		    $marker['opts']['title'] = $node->title;	
+          } 
+		  else {
+		    // read it from the specified column
+		    $marker['opts']['title'] = $row->{$this->name_tooltip};
+		  }
+	  
+	      // MARKER ACTION
+		  $view_markermode = 0; // default, ie no action
+		  if ($this->options['marker_action'] == 'use_GMap_setting'){
+		    $view_markermode = $defaults['markermode'];
+		  } 
+		  elseif ($this->options['marker_action'] == 'open_window'){
+		    $view_markermode = 1;
+		  } 
+		  elseif ($this->options['marker_action'] == 'open_link'){
+		    $view_markermode = 2;
+		  }
+		  
+          if ($view_markermode == 1) { // popup
+            $marker['text'] = $this->row_plugin->render($row);
+          }
+          elseif ($view_markermode == 2) { // link
+		    if ($this->options['marker_link'] == "<Link to current Node>") {
+              $marker['link'] = url('node/'. $row->nid);
+            } 
+			else {
+		      // read it from the specified column
+		      $marker['link'] = url('node/'. $row->{$this->name_link});
+   		    }
+	      }
+	  
+          $markers[] = $marker;
+
           $offsets[$markername]++;
         }
       }
+
       if (!empty($markers)) { // Don't draw empty maps.
         $map = gmap_parse_macro($this->options['macro']);
         $map['markers'] = $markers;
@@ -122,11 +181,38 @@ class gmap_plugin_style_gmap extends vie
       '#title' => t('Data Source'),
       '#options' => array(
         'location' => t('Location.module'),
-        'autodetect' => t('Fields named "latitude" and "longitude" @@@TODO'),
+        'autodetect' => t('Fields named "latitude" and "longitude"'),
       //'geocode' => t('Just-in-time geocoding on field named "address"'),
       ),
       '#default_value' => $this->options['datasource'],
-      '#multiple' => FALSE,
+    );
+
+    // Create an array of allowed columns from the data we know:
+    $field_names = array();
+    $handlers = $this->display->handler->get_handlers('field');
+    foreach ($handlers as $field => $handler) {
+      if ($label = $handler->label()) {
+        $field_names[$field] = (string) $label;
+      }
+      else {
+        $field_names[$field] = (string) $handler->ui_name();
+      }
+    }
+    $form['field_lat'] = array(
+      '#type' => 'select',
+      '#title' => t('Latitude field'),
+      '#options' => $field_names,
+      '#default_value' => $this->options['field_lat'],
+      '#process' => array('views_process_dependency'),
+      '#dependency' => array('radio:style_options[datasource]' => array('autodetect')),
+    );
+    $form['field_lon'] = array(
+      '#type' => 'select',
+      '#title' => t('Longitude field'),
+      '#options' => $field_names,
+      '#default_value' => $this->options['field_lon'],
+      '#process' => array('views_process_dependency'),
+      '#dependency' => array('radio:style_options[datasource]' => array('autodetect')),
     );
 
     $form['markers'] = array(
@@ -151,5 +237,50 @@ class gmap_plugin_style_gmap extends vie
       '#title' => t('Marker / fallback marker to use'),
       '#default_value' => $this->options['markertype'],
     );
+	
+	$tooltip_field_names = array();
+	$tooltip_field_names = array_merge( array("<Use Node's title>" => "<Use Node's title>"), $field_names) ;
+	
+	$form['marker_tooltip'] = array(
+      '#type' => 'select',
+      '#title' => t('Marker tooltip field'),
+      '#options' => $tooltip_field_names,
+      '#default_value' => $this->options['marker_tooltip'],
+	  '#description' => t("Default works for most cases. If your view uses Relationships, you may specify the field which contains the node title to show as a tooltip."),
+    );
+	
+	$form['marker_action'] = array(
+      '#type' => 'radios',
+      '#title' => t('Marker action'),
+      '#options' => array(
+        'use_GMap_setting' => t('Use setting from GMap module'),
+        'do_nothing' => t('Do nothing'),
+        'open_window' => t('Open info window'),
+        'open_link' => t('Open link'),
+      ),
+      '#default_value' => $this->options['marker_action'],
+	  '#description' => t("Select the first option to use the setting from the GMap module's settings page, or select one of the other options to specify the action for this view."),
+    );
+	
+	$link_field_names = array();
+	$link_field_names = array_merge( array("<Link to current Node>" => "<Link to current Node>"), $field_names) ;
+	
+    $form['marker_link'] = array(
+      '#type' => 'select',
+      '#title' => t('Field containing NID of node to link to'),
+      '#options' => $link_field_names,
+      '#default_value' => $this->options['marker_link'],
+      '#process' => array('views_process_dependency'),
+      '#dependency' => array('radio:style_options[marker_action]' => array('open_link')),
+	  '#description' => t("Default works for most cases. If your view uses Relationships, you may specify the field which contains the node's NID. This will be used to build the link from the marker to the correct node."),
+    );
+  }
+
+  function uses_fields() {
+    // The 'audodetech' option requires that we have lat and lon fields.
+    if ($this->options['datasource'] == 'autodetect') {
+      return TRUE;
+    }
+    return parent::uses_fields();
   }
 }
