app/soc/content/js/map-090730.js
changeset 2800 cd9eed2b787e
parent 2700 40cf7eaa032e
--- a/app/soc/content/js/map-090730.js	Tue Aug 25 15:55:26 2009 +0100
+++ b/app/soc/content/js/map-090730.js	Tue Aug 25 15:58:30 2009 +0100
@@ -1,10 +1,13 @@
-role_profile_gmap = new function(){
+(function () {
+  var role_profile_gmap = window.role_profile_gmap = function () {
+  };
+
   // Create global variables
   var map;
   var marker;
   var geocoder;
 
-  // The following strings can be customized to reflect ids in the page. 
+  // The following strings can be customized to reflect ids in the page.
   // You can also add or remove fields used for GMap Geocoding in 
   // the JSON address object
 
@@ -48,62 +51,75 @@
       id: "#id_res_postalcode",
       old_value: ""
     }
-  }
+  };
 
   // Save current address fields in the JSON Object
   function saveOldAddress() {
-    for (var a in address) {
-      address[a].old_value = $(address[a].id).val();
-    }
+    jQuery.each(address, function (level, level_details) {
+      level_details.old_value = jQuery(level_details.id).val();
+    });
   }
 
   // Return true if the user has edited address fields
   function isNewAddress() {
-    for (var a in address) {
-      if ($(address[a].id).val() != address[a].old_value) return true;
-    }
-    return false;
+    var is_new = false;
+    jQuery.each(address, function (level, level_details) {
+      if (jQuery(level_details.id).val() !== level_details.old_value) {
+        is_new = true;
+        return false;
+      }
+    });
+    return is_new;
   }
 
   // Write saved lat and lng values to page fields
   function setLatLngFields() {
-    $(field_lat).val(current_lat);
-    $(field_lng).val(current_lng);
+    jQuery(field_lat).val(current_lat);
+    jQuery(field_lng).val(current_lng);
   }
 
   // Read lat and lng fields and store them
   function readLatLngFields() {
-    current_lat = $(field_lat).val();
-    current_lng = $(field_lng).val();
+    current_lat = jQuery(field_lat).val();
+    current_lng = jQuery(field_lng).val();
   }
 
-  // This function reads address fields, merge them and uses 
+  // This function reads address fields, merge them and uses
   // GMap API geocoding to find the first hit
-  // Using geocoding http://code.google.com/intl/it-IT/apis/maps/documentation/services.html#Geocoding
+  // Using geocoding
+  // http://code.google.com/intl/it-IT/apis/maps/documentation/
+  // services.html#Geocoding
   function calculateAddress() {
     // If the user has really edited address fields...
     if (isNewAddress()) {
       // Merge address fields
       var address_string = "";
-      for (var a in address) {
-        address_string+=$(address[a].id).val();
-        if (a!=address.length-1) {address_string+=","};
-      }
+      jQuery.each(address, function (level, level_details) {
+        address_string += jQuery(level_details.id).val() + ",";
+      });
 
       // Ask GMap API for geocoding
       geocoder.getLatLng(
         address_string,
-        function(point) {
+        function (point) {
           // If a point is found
           if (point) {
             // Save the current address in the JSON object
             saveOldAddress();
             // Set the new zoom, map center and marker coords
             var zoom_set = world_zoom;
-            if ($(address.street.id).val()!="") zoom_set = address_zoom;
-            else if ($(address.city.id).val()!="") zoom_set = city_zoom;
-            else if ($(address.state.id).val()!="") zoom_set = state_zoom;
-            else if ($(address.country.id).val()!="") zoom_set = country_zoom;
+            if (jQuery(address.street.id).val() !== "") {
+              zoom_set = address_zoom;
+            }
+            else if (jQuery(address.city.id).val() !== "") {
+              zoom_set = city_zoom;
+            }
+            else if (jQuery(address.state.id).val() !== "") {
+              zoom_set = state_zoom;
+            }
+            else if (jQuery(address.country.id).val() !== "") {
+              zoom_set = country_zoom;
+            }
             map.setCenter(point, zoom_set);
             marker.setPoint(point);
             map.clearOverlays();
@@ -120,12 +136,12 @@
   }
 
   // Public function to load the map
-  this.map_load = function() {
+  role_profile_gmap.map_load = function () {
     // All can happen only if there is gmap compatible browser.
     // TODO: Fallback in case the browser is not compatible
-    if (GBrowserIsCompatible()) {
-      // Save the address fields. This is useful if the page is being edited 
-      // to not update blindly the lat/lng fields with GMap geocoding if 
+    if (window.GBrowserIsCompatible()) {
+      // Save the address fields. This is useful if the page is being edited
+      // to not update blindly the lat/lng fields with GMap geocoding if
       // blurring an address field
       saveOldAddress();
       var starting_point;
@@ -133,36 +149,38 @@
       var show_marker = true;
 
       // Create the map and add small controls
-      map = new GMap2(document.getElementById(map_div));
-      map.addControl(new GSmallMapControl());
-      map.addControl(new GMapTypeControl());
+      map = new window.GMap2(document.getElementById(map_div));
+      map.addControl(new window.GSmallMapControl());
+      map.addControl(new window.GMapTypeControl());
 
       // Instantiate a global geocoder for future use
-      geocoder = new GClientGeocoder();
+      geocoder = new window.GClientGeocoder();
 
-      // If lat and lng fields are not void (the page is being edited) then 
-      // update the starting coords, modify the zoom level and tells following 
+      // If lat and lng fields are not void (the page is being edited) then
+      // update the starting coords, modify the zoom level and tells following
       // code to show the marker
-      if ($(field_lat).val()!="" && $(field_lng).val()!="") {
+      if (jQuery(field_lat).val() !== "" && jQuery(field_lng).val() !== "") {
         readLatLngFields();
         zoom_selected = address_zoom;
         show_marker = true;
       }
-      
+
       // Set map center, marker coords and show it if this is an editing
-      starting_point = new GLatLng(current_lat,current_lng);
+      starting_point = new window.GLatLng(current_lat, current_lng);
       map.setCenter(starting_point, zoom_selected);
-      marker = new GMarker(starting_point, {draggable:true});
-      if (show_marker) map.addOverlay(marker);
-      
+      marker = new window.GMarker(starting_point, {draggable: true});
+      if (show_marker) {
+        map.addOverlay(marker);
+      }
+
       // Adds a new event listener to geocode the address when an address 
       // field is blurred
-      for (var a in address) {
-        $(address[a].id).blur(calculateAddress);
-      }
-      
+      jQuery.each(address, function (level, level_details) {
+        jQuery(level_details.id).blur(calculateAddress);
+      });
+
       // Adds a new event listener: if the marker has been dragged around...
-      GEvent.addListener(marker, "dragend", function() {
+      window.GEvent.addListener(marker, "dragend", function () {
         // Update internal variables with current marker coords...
         current_lat = marker.getPoint().lat();
         current_lng = marker.getPoint().lng();
@@ -170,54 +188,56 @@
         setLatLngFields();
       });
     }
-  }
-};
+  };
+}());
 
-org_home_gmap = new function(){
+(function () {
+  var org_home_gmap = window.org_home_gmap = function () {
+  };
   // Global variables
   var map;
 
   // HTML div tag where map needs to be inserted
   var map_div = "org_home_map";
-  
+
   // Setup required icons
-  var base_icon = new GIcon();
+  var base_icon = new window.GIcon();
   base_icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
-  base_icon.iconSize = new GSize(20, 34);
-  base_icon.shadowSize = new GSize(37, 34);
-  base_icon.iconAnchor = new GPoint(9, 34);
-  base_icon.infoWindowAnchor = new GPoint(9, 2);
-  base_icon.infoShadowAnchor = new GPoint(18, 25);
-  var student_icon = new GIcon(base_icon);
+  base_icon.iconSize = new window.GSize(20, 34);
+  base_icon.shadowSize = new window.GSize(37, 34);
+  base_icon.iconAnchor = new window.GPoint(9, 34);
+  base_icon.infoWindowAnchor = new window.GPoint(9, 2);
+  base_icon.infoShadowAnchor = new window.GPoint(18, 25);
+  var student_icon = new window.GIcon(base_icon);
   student_icon.image = "http://www.google.com/mapfiles/marker.png";
-  var mentor_icon = new GIcon(base_icon);
+  var mentor_icon = new window.GIcon(base_icon);
   mentor_icon.image = "/soc/content/images/mentor-marker.png";
 
   // Map load function
-  this.map_load = function(map_data) {
+  org_home_gmap.map_load = function (map_data) {
 
-    if (GBrowserIsCompatible()) {
+    if (window.GBrowserIsCompatible()) {
       // Create the map and add small controls
-      map = new GMap2(document.getElementById(map_div));
-      map.addControl(new GLargeMapControl());
-      map.addControl(new GMapTypeControl());
+      map = new window.GMap2(document.getElementById(map_div));
+      map.addControl(new window.GLargeMapControl());
+      map.addControl(new window.GMapTypeControl());
 
       // Set map center and initial zoom level
-      map.setCenter(new GLatLng(0, 0), 1);
+      map.setCenter(new window.GLatLng(0, 0), 1);
 
       var mentors = {};
       var students = {};
       var projects = {};
       var polylines = [];
 
-      jQuery.each(map_data.people, function(key, person) {
+      jQuery.each(map_data.people, function (key, person) {
         if (person.type === "student") {
           students[key] = {
             "name": person.name,
             "lat": person.lat,
             "long": person.long,
             "projects": person.projects
-          }
+          };
         }
         if (person.type === "mentor") {
           mentors[key] = {
@@ -239,31 +259,41 @@
             current_student.long !== null &&
             current_mentor.lat !== null &&
             current_mentor.long !== null) {
-              polylines.push([[current_student.lat,current_student.long],[current_mentor.lat,current_mentor.long]]);
+              /*jslint white: false */
+              polylines.push([
+                [current_student.lat, current_student.long],
+                [current_mentor.lat, current_mentor.long]
+              ]);
+              /*jslint white: true */
         }
       });
 
       // Iterate over students
-      jQuery.each(students, function(key, person) {
+      jQuery.each(students, function (key, person) {
         var html = "";
         var marker = null;
 
-        if (person.lat!==null && person.long!==null) {
-          point = new GLatLng(person.lat,
-                              person.long);
+        if (person.lat !== null && person.long !== null) {
+          var point = new window.GLatLng(person.lat, person.long);
 
-          marker = new GMarker(point, student_icon);
-          html = "<strong>" + person.name + "</strong><br />";
-          html += "<span style='font-style:italic;'>Student</span><br />";
-          html += "<div style='height:100px;width:300px;overflow:auto;font-size:70%'>";
+          marker = new window.GMarker(point, student_icon);
+          html = [
+            "<strong>", person.name, "</strong><br />",
+            "<span style='font-style:italic;'>Student</span><br />",
+            "<div style='height:100px;width:300px;",
+            "overflow:auto;font-size:70%'>"
+          ].join("");
           // iterate through projects
           jQuery.each(person.projects, function () {
             var current_project = map_data.projects[this];
-            html += "<a href='"+ current_project.redirect + "'>" + current_project.title + "</a><br />";
-            html += "Mentor: " + current_project.mentor_name + "<br />";
+            html += [
+              "<a href='", current_project.redirect, "'>",
+              current_project.title, "</a><br />",
+              "Mentor: ", current_project.mentor_name, "<br />"
+            ].join("");
           });
-          html+= "</div>";
-          GEvent.addListener(marker, "click", function() {
+          html += "</div>";
+          window.GEvent.addListener(marker, "click", function () {
             marker.openInfoWindowHtml(html);
           });
 
@@ -272,27 +302,32 @@
       });
 
       // Iterate over mentors
-      jQuery.each(mentors, function(key, person) {
+      jQuery.each(mentors, function (key, person) {
         var html = "";
         var marker = null;
 
-        if (person.lat!==null && person.long!==null) {
-          point = new GLatLng(person.lat,
-                              person.long);
+        if (person.lat !== null && person.long !== null) {
+          var point = new window.GLatLng(person.lat, person.long);
 
-          marker = new GMarker(point, mentor_icon);
-          html = "<strong>" + person.name + "</strong><br />";
-          html += "<span style='font-style:italic;'>Mentor</span><br />";
-          html += "<div style='height:100px;width:300px;overflow:auto;font-size:70%'>";
+          marker = new window.GMarker(point, mentor_icon);
+          html = [
+            "<strong>", person.name, "</strong><br />",
+            "<span style='font-style:italic;'>Mentor</span><br />",
+            "<div style='height:100px;width:300px;",
+            "overflow:auto;font-size:70%'>"
+          ].join("");
           // iterate through projects
           jQuery.each(person.projects, function () {
             var current_project = map_data.projects[this];
-            html += "<a href='"+ current_project.redirect + "'>" + current_project.title + "</a><br />";
-            html += "Student: " + current_project.student_name + "<br />";
+            html += [
+              "<a href='", current_project.redirect, "'>",
+              current_project.title, "</a><br />",
+              "Student: ", current_project.student_name, "<br />"
+            ].join("");
           });
-          html+= "</div>";
+          html += "</div>";
 
-          GEvent.addListener(marker, "click", function() {
+          window.GEvent.addListener(marker, "click", function () {
             marker.openInfoWindowHtml(html);
           });
 
@@ -301,12 +336,12 @@
       });
 
       // Draw all polylines
-      jQuery.each(polylines, function() {
-        var from = new GLatLng(this[0][0],this[0][1]);
-        var to = new GLatLng(this[1][0],this[1][1]);
-        var polyline = new GPolyline([from, to], "#ff0000", 3);
+      jQuery.each(polylines, function () {
+        var from = new window.GLatLng(this[0][0], this[0][1]);
+        var to = new window.GLatLng(this[1][0], this[1][1]);
+        var polyline = new window.GPolyline([from, to], "#ff0000", 3);
         map.addOverlay(polyline);
       });
     }
-  }
-};
+  };
+}());