// $Id: gmap_background.js,v 1.5 2009-01-29 08:06:53 fini Exp $

/**
 * Main JavaScript file for GMap background.
 *
 * Handles initializing, etc. of the Google Maps and changing of overlays.
 */

// A global var for keeping track of our stuff.
var gmap_background = {
  markers: {mediacom:[], groupm:[], friendspartners:[]},
  icons: {},
  regions: {},
  map: undefined // We'll define this in the init.
}

$(document).ready(function () {
  if (GBrowserIsCompatible()) {
    mediacom_typecontrol_init();
    gmap_background_init();
    gmap_background_load_markers();

    // Region select box
    $("#gmap-background-region-select").change(function () {
      gmap_background_set_region($(this).val());
    });

    // Mediacom marker groups checkboxes
    $("#mapOptions :checkbox").click(function (){
      gmap_background_toggle_markers(this.checked, $(this).val());
    });


    // Delay before setting initial state so the Google Maps has time to do its stuff.
    setTimeout(function(){
      // Region select box, initial state
      $("#gmap-background-region-select").each(function (i){
        gmap_background_set_region($(this).val());
      });

      // Mediacom marker groups checkboxes, initial state
      $("#mapOptions :checkbox").each(function (i){
        gmap_background_toggle_markers(this.checked, $(this).val());
      });
    }, 1000);
  }
});

$(document).unload(function () {
  // Unload Google Maps
  GUnload();
});

/**
 * Initial setup
 */
function gmap_background_init() {

  // First, set up the map
  var mapDomElement = document.getElementById("map");
  gmap_background.map = new GMap2(mapDomElement);
  var map = gmap_background.map;
  map.setMapType(G_SATELLITE_MAP);
  map.setCenter(new GLatLng(15, 5), 2);
  map.addControl(new GLargeMapControl());
  mapDomElement.lastChild.id = 'mapZoomControl';
  map.addControl(new MediacomTypeControl());
  map.enableScrollWheelZoom();

  // Add icons for the three categories.
  var mediacom_icon = new GIcon();
  mediacom_icon.image = Drupal.settings.gmap_background.image_path + "mediacom_arrows.png";
  mediacom_icon.iconSize = new GSize(18, 13);
  mediacom_icon.iconAnchor = new GPoint(5, 10);
  mediacom_icon.infoWindowAnchor = new GPoint(5, 4);
  gmap_background.icons['mediacom'] = mediacom_icon;

  var groupm_icon = new GIcon();
  groupm_icon.image = Drupal.settings.gmap_background.image_path + "groupm_arrows.png";
  groupm_icon.iconSize = new GSize(18, 13);
  groupm_icon.iconAnchor = new GPoint(5, 10);
  groupm_icon.infoWindowAnchor = new GPoint(5, 4);
  gmap_background.icons['groupm'] = groupm_icon;

  var fp_icon = new GIcon();
  fp_icon.image = Drupal.settings.gmap_background.image_path + "partners_arrows.png";
  fp_icon.iconSize = new GSize(18, 13);
  fp_icon.iconAnchor = new GPoint(5, 10);
  fp_icon.infoWindowAnchor = new GPoint(5, 4);
  gmap_background.icons['friendspartners'] = fp_icon;

  // Set up regions.
  // List of LatLng object plus zoomlevel.
  gmap_background.regions = {
    'global': [new GLatLng(15, 5), 2],
    'asia': [new GLatLng(42.098222,88.505859), 3],
    'europe': [new GLatLng(48.341646,14.150391), 4],
    'latinamerica': [new GLatLng(-9.622414,-59.941406), 4],
    'northamerica': [new GLatLng(32.805745,-97.294922), 3]
  }
}

/**
 * Loads markers from the JSON callback URL
 */
function gmap_background_load_markers() {
  // Set up markers
  $.getJSON(Drupal.settings.gmap_background.callback_url, function (data, textStatus) {
    $.each(data, function(group,items) {
      $.each(items, function(i, item) {
        gmap_background_marker_create(new GLatLng(item.latitude, item.longitude),
                                      group, item.title, item.info_html);
      });
    });
  });
}

/**
 * Set up a marker
 */
function gmap_background_marker_create(point, group, title, info_html) {
  var marker = new GMarker(point, {title: title, icon: gmap_background.icons[group]});
  GEvent.addListener(marker, "click", function() {
    gmap_background.map.setCenter(marker.getPoint());
    marker.openInfoWindowHtml(info_html);
    setTimeout("gmap_background.map.panBy(new GSize(-100, 0))", 1);
  });


  // Add it to our overlay
  gmap_background.map.addOverlay(marker);

  // Hide the marker by default
  marker.hide();

  // And to our marker registry
  gmap_background.markers[group].push(marker);
}

/**
 * Change the map view to center and zoom on a region.
 */
function gmap_background_set_region(region) {
  gmap_background.map.setCenter(gmap_background.regions[region][0],
                                gmap_background.regions[region][1]);
}

/**
 * Toggle marker visibility
 */
function gmap_background_toggle_markers(show, group) {
  if (show) {
    $.each(gmap_background.markers[group], function (i, marker) {
      marker.show();
    });
  } else {
    $.each(gmap_background.markers[group], function (i, marker) {
      marker.hide();
    });
  }
}

/**
 * Define our own type control widget
 */
function MediacomTypeControl() {
}

/**
 * Set up the type control widget
 */
function mediacom_typecontrol_init() {
  MediacomTypeControl.prototype = new GControl();

  // Creates a one DIV for each of the buttons and places them in a container
  // DIV which is returned as our control element. We add the control to
  // to the map container and return the element for the map class to
  // position properly.
  MediacomTypeControl.prototype.initialize = function(map) {

    var mapTypeStreet = document.createElement("div");
    mapTypeStreet.appendChild(document.createTextNode("Map"));
    GEvent.addDomListener(mapTypeStreet, "click", function() {map.setMapType(G_NORMAL_MAP);});

    var mapTypeSatellite = document.createElement("div");
    mapTypeSatellite.appendChild(document.createTextNode("Satellite"));
    GEvent.addDomListener(mapTypeSatellite, "click", function() {map.setMapType(G_SATELLITE_MAP);});

    var mapTypeHybrid = document.createElement("div");
    mapTypeHybrid.appendChild(document.createTextNode("Hybrid"));
    GEvent.addDomListener(mapTypeHybrid, "click", function() {map.setMapType(G_HYBRID_MAP);});

    var container = document.createElement("div");
    container.id = 'mapTypeControl';
    container.appendChild(mapTypeStreet);
    container.appendChild(mapTypeSatellite);
    container.appendChild(mapTypeHybrid);
    map.getContainer().appendChild(container);
    return container;
  }

  // By default, the control will appear in the top left corner of the
  // map with 7 pixels of padding.
  MediacomTypeControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
  }
}
