/*
 * Cooreea Map class using Prototype and GMap2
 */
var CooreeaMap = Class.create();

CooreeaMap.prototype = {

  // array of waypoints
  waypoints:[],

  // array of consignment markers
  cons_markers: [],

  // array of journey markers
  jour_markers: [],

  // array of ghost markers
  ghost_markers: [],
	
  // gmap Directions object
  gdir: null,
  
  jourMarkerCluster: null,
  consMarkerCluster: null,
  ghostMarkerCluster: null,

  /************************************************************************
   * constructor
   */
  initialize : function() {

    this.options = Object.extend({
      map_id:          0,
      lat:            30,
      lng:            10,
      zoom:           1,
      zoomMin:        1,
      zoomMax:        15,
      control:        GLargeMapControl,
      width:          '100%',
      height:          '200px',
      matchesFromPointUrl: '/',
      matchesFromRouteUrl: '/',
      useCluster:			false
    }, arguments[0] || {} );

    this.createMap(this.options.map_id);
    this.createDir();

  },

  /************************************************************************
   * create a map
   */
  createMap : function(map_id) {

    if (!GBrowserIsCompatible()) return false;

    // write the div holding the map
//    document.write('<div id="map'+map_id+'_canvas" style="overflow: hidden; width: '+this.options.width+'; height: '+this.options.height+'"></div>');

    // create the map with controls
    this.map = new GMap2($('map'+map_id+'_canvas'), { /* draggableCursor: 'crosshair' */ });
    control = (this.options.control == 'GSmallMapControl') ? new GSmallMapControl() : new GLargeMapControl();
    this.map.addControl(control);

    // this.map.enableContinuousZoom();
    // this.map.addMapType(G_SATELLITE_3D_MAP);

    // this.map.addControl(new GMapTypeControl());
    this.map.addControl(new GScaleControl());

     // map.enableScrollWheelZoom();

    // set the center
    this.map.setCenter(new GLatLng(this.options.lat, this.options.lng), this.options.zoom);

    // display window
    if ($('mapInfo')) {
      var pos = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
      pos.apply($('mapInfo'));
      this.map.getContainer().appendChild($('mapInfo'));
      $('mapInfo').style.display = 'block';
    }

    // initialize MarkerFrom and MarkerTo
    this.markerFrom = null;
    this.markerTo = null;

  },

  /************************************************************************
   * create the GDirection object
   */
  createDir : function() {

    this.gdir = new GDirections(this.map);
    GEvent.addListener(this.gdir, "load", this.onGDirectionsLoad);
    GEvent.addListener(this.gdir, "error", this.handleErrors);

  },

  /************************************************************************
   * set an array of match markers on the map
   */
  setMatchMarkers : function(markers, erase_before, result_tabs, convert_buttons) {

    // erase current markers
    if (erase_before) {
      for(i=0; i < this.cons_markers.length; i++) {
        this.map.removeOverlay(this.cons_markers[i]);
      }
      this.cons_markers = [];

      for(i=0; i < this.jour_markers.length; i++) {
        this.map.removeOverlay(this.jour_markers[i]);
      }
      this.jour_markers = [];

      for(i=0; i < this.ghost_markers.length; i++) {
        this.map.removeOverlay(this.ghost_markers[i]);
      }
      this.ghost_markers = [];
    }
    // first add ghost markers ...
    for(i=0; i < markers.length; i++) {
      if (markers[i].from_to == 'ghost')
        this.addMarker(markers[i], false);
    }

    // ... then add real matches (to have them on top of ghosts
    for(i=0; i < markers.length; i++) {
      if (markers[i].from_to == 'match') {
        this.addMarker(markers[i], false);
      }
    }
    
    if (this.options.useCluster) {
    	this.addMarkerClusters();
	  }

    this.zoomPosAdjust();

    if (result_tabs) {
      this.updateResultTabs();
    }

    if (convert_buttons) {
      $('j_convert_buttons').show();
      $('c_convert_buttons').show();
    }

  },
  
  /************************************************************************
   * add clusters from the existing gmarker arrays
   */
  addMarkerClusters : function() {
  
	  if (this.jourMarkerCluster) this.jourMarkerCluster.clearMarkers();
	    this.jourMarkerCluster = new MarkerClusterer(this.map, this.jour_markers, 
	    	{maxZoom: 10, gridSize: 20, styles: this.clusterStyle('jour')}
	    );
    	if (this.ghostMarkerCluster) this.ghostMarkerCluster.clearMarkers();
	    this.ghostMarkerCluster = new MarkerClusterer(this.map, this.ghost_markers, 
	    	{maxZoom: 10, gridSize: 20, styles: this.clusterStyle('ghost')}
	    );
    	if (this.consMarkerCluster) this.consMarkerCluster.clearMarkers();
	    this.consMarkerCluster = new MarkerClusterer(this.map, this.cons_markers, 
	    	{maxZoom: 10, gridSize: 20, styles: this.clusterStyle('cons')}
	    );
	},
	
  /************************************************************************
   * add an array of waypoint markers to the map
   */
  addWaypoints : function(markers, erase_before, load_directions) {

    // erase current markers
    if (erase_before) {
      for(i=0; i < this.waypoints.length; i++) {
        this.map.removeOverlay(this.waypoints[i]);
      }
      this.waypoints = [];
      this.markerFrom = null;
      this.markerTo = null;
    }

    // set new markers
    for(i=0; i < markers.length; i++) {
      this.addMarker(markers[i], false);
    }

    if (this.waypoints.length >= 2) {
      if (load_directions) this.loadDirections(); else this.zoomPosAdjust();
    }
    else {
      if (this.waypoints.length == 1) this.loadMatches();
      if (load_directions) this.removeDirections();
    }
  },

  /************************************************************************
   * remove all waypoint markers from the map
   */
  removeWaypoints : function() {
    for(i=0; i < this.waypoints.length; i++) {
      this.map.removeOverlay(this.waypoints[i]);
    }
    this.waypoints = [];
    this.loadAllMatches();
    this.removeDirections();
  },

  /************************************************************************
   * add a marker to the map
   */
  addMarker : function(marker, check_route) {

    // create gmap marker from pint and icon
    var point = new GLatLng(marker.latitude, marker.longitude);
    icon = this.createIcon(marker.type + '_' + marker.from_to);
    var gmarker = new GMarker(point, icon);
    gmarker.type = marker.type;

    if (marker.from_to == 'from' || marker.from_to == 'to') this.zoomPosAdjust();

    // build popup if html is given
    if (marker.popup_html) {
      openPopup = function() {
          if ($('mapInfo')) $('mapInfo').fade({duration: 0.3});
          gmarker.openInfoWindowHtml('<div class="mapWhere">' + marker.popup_html + '</div>');
      }
      GEvent.addListener(gmarker, "click", openPopup);
      gmarker.openPopup = openPopup;
    }

    // remember list html for search result list
    if (marker.list_html) {
      gmarker.list_html = marker.list_html;
    }

    // remember match marker
    if (marker.from_to == 'match') {
    	if (marker.type == 'consignment' || marker.type == 'ride') this.cons_markers.push(gmarker);
    	if (marker.type == 'journey') this.jour_markers.push(gmarker);
    	this.map.addOverlay(gmarker);
    	if (this.options.useCluster) gmarker.hide();
    }
    if (marker.from_to == 'ghost') {
    	this.ghost_markers.push(gmarker);
    	this.map.addOverlay(gmarker);
    	if (this.options.useCluster) gmarker.hide();
   	}
    if (marker.from_to == 'from') {
      this.markerFrom = gmarker;
      this.waypoints.push(gmarker);
      this.map.addOverlay(gmarker);
    }
    if (marker.from_to == 'to') {
      this.markerTo = gmarker;
      this.waypoints.push(gmarker);
      this.map.addOverlay(gmarker);
    }
    if (marker.from_to == 'waypoint') {
      this.waypoints.push(gmarker);
      this.map.addOverlay(gmarker);
    }

    // check if matches for from/to points should be loaded
    if (check_route) {
      this.loadMatches();

      this.zoomPosAdjust();
    }

  },

  /************************************************************************
   * set center and zoom level so that all markers are shown
   */
  zoomPosAdjust : function() {

    minLat = 180;
    minLng = 180;
    maxLat = -180;
    maxLng = -180;

    // find out sw and ne corners out of from-, to- and match-markers
    if (this.markerFrom) {
      check = this.markerFrom.getLatLng();
      if (check.lat() < minLat) minLat = check.lat();
      if (check.lng() < minLng) minLng = check.lng();
      if (check.lat() > maxLat) maxLat = check.lat();
      if (check.lng() > maxLng) maxLng = check.lng();
    }
    if (this.markerTo) {
      check = this.markerTo.getLatLng();
      if (check.lat() < minLat) minLat = check.lat();
      if (check.lng() < minLng) minLng = check.lng();
      if (check.lat() > maxLat) maxLat = check.lat();
      if (check.lng() > maxLng) maxLng = check.lng();
    }
    for(i = 0; i < this.cons_markers.length; i++) {
      check = this.cons_markers[i].getLatLng();
      if (check.lat() < minLat) minLat = check.lat();
      if (check.lng() < minLng) minLng = check.lng();
      if (check.lat() > maxLat) maxLat = check.lat();
      if (check.lng() > maxLng) maxLng = check.lng();
    }
    for(i = 0; i < this.jour_markers.length; i++) {
      check = this.jour_markers[i].getLatLng();
      if (check.lat() < minLat) minLat = check.lat();
      if (check.lng() < minLng) minLng = check.lng();
      if (check.lat() > maxLat) maxLat = check.lat();
      if (check.lng() > maxLng) maxLng = check.lng();
    }
    for(i = 0; i < this.waypoints.length; i++) {
      check = this.waypoints[i].getLatLng();
      if (check.lat() < minLat) minLat = check.lat();
      if (check.lng() < minLng) minLng = check.lng();
      if (check.lat() > maxLat) maxLat = check.lat();
      if (check.lng() > maxLng) maxLng = check.lng();
    }
		
		if (minLat<180||maxLat>-180||minLng<180||maxLng>-180) {
	    bounds = new GLatLngBounds(new GLatLng(minLat, minLng), new GLatLng(maxLat, maxLng));
	    zoom = this.map.getBoundsZoomLevel(bounds);
	    if(zoom < this.options.zoomMin) zoom = this.options.zoomMin;
	    if(zoom > this.options.zoomMax) zoom = this.options.zoomMax;
	    this.map.setZoom(zoom);
	    this.map.panTo(bounds.getCenter());
	  }
  },

  /************************************************************************
   * if the counter field exists, update the value
   */
  updateResultTabs : function() {

    consignmentsCounter = 0;
    journeysCounter = 0;
    $('c_results').innerHTML = '';
    $('j_results').innerHTML = '';

    for(i = 0; i < this.cons_markers.length; i++) {
      consignmentsCounter++;
      $('c_results').innerHTML += '<div class="LSwrap" onclick="cMap_0.cons_markers['+i+'].openPopup();">'+this.cons_markers[i].list_html+'</div>';
		}
    for(i = 0; i < this.jour_markers.length; i++) {
      journeysCounter++;
      $('j_results').innerHTML += '<div class="LSwrap" onclick="cMap_0.jour_markers['+i+'].openPopup();">'+this.jour_markers[i].list_html+'</div>';
    }

    $('j_counter').innerHTML = journeysCounter;
    $('c_counter').innerHTML = consignmentsCounter;
    $('c_loading').hide();
    $('j_loading').hide();
    $('result_tabs').show();
    $('c_results').show();
    $('j_results').show();
    this.adjustMapHeight();
  },


  adjustMapHeight : function() {
    if ($('map_navigation')) {
      $('map'+this.options.map_id+'_canvas').setStyle( {height: $('map_navigation').getHeight()+'px'});
    }
  },


  /************************************************************************
   * set the result tabs to loading mode
   */
  tabLoading : function() {
    if($('result_tabs')) {
      $('c_loading').show();
      $('j_loading').show();
      $('c_results').hide();
      $('j_results').hide();
      $('j_tab_head').innerHTML = 'Matching Journeys';
      $('j_counter').innerHTML = '';
      $('c_counter').innerHTML = '';
    }
  },

  /************************************************************************
   * load matches whether no or all from/to markes are set
   */
  loadMatches : function() {

    // both are set
    if (this.markerFrom && this.markerTo) {
      // this.loadDirections();
    }

    // none is set
    else if (!this.markerFrom && !this.markerTo) {
      this.tabLoading();
      this.loadAllMatches();
    }

    else {
      this.tabLoading();
      if (this.markerFrom) {
        lat_lng = this.markerFrom.getLatLng();
        this.loadMatchesFromPoint('from', lat_lng);
      }
      else if (this.markerTo) {
        lat_lng = this.markerTo.getLatLng();
        this.loadMatchesFromPoint('to', lat_lng);
      }
      this.map.setCenter(lat_lng);
    }
  },

  /************************************************************************
   * load matches from a given point
   */
  loadMatchesFromPoint : function(from_to, lat_lng) {

    if (this.options.matchesFromPointUrl) {
      new Ajax.Request(
        this.options.matchesFromPointUrl+'/'+from_to+'/'+lat_lng.lng()+'/'+lat_lng.lat()+'.json',
        {
          asynchronous: true,
          evalScripts: true,
          onComplete: this.showMatches
        }
      );
    }

  },

  /************************************************************************
   * load matches from a given route
   */
  loadMatchesFromRoute : function(route) {

    if(this.options.matchesFromRouteUrl) {
      new Ajax.Request(
        this.options.matchesFromRouteUrl+'.json',
        {
          asynchronous: true,
          evalScripts: true,
          method: 'post',
          parameters: {route: route},
          onComplete: this.showMatches
        }
      );
    }

  },

  /************************************************************************
   * load all matches after a map reset
   */
  loadAllMatches : function(route) {

    new Ajax.Request(
      this.options.matchesResetUrl+'.json',
      {
        asynchronous: true,
        evalScripts: true,
        onComplete: this.showMatches
      }
    );

  },

  /************************************************************************
   * show matches on the map
   */
  showMatches : function(transport, json) {

    responseText = transport.responseText;

    // remove anything after a '<!--' at end of response text, because ie doesn't like it
    if (responseText.lastIndexOf('<!--') != -1) {
      responseText = responseText.substr(0, responseText.lastIndexOf('<!--'));
    }

    data = eval(responseText);
    cMap_0.setMatchMarkers(data, true, true, true);
  },

  /************************************************************************
   * build query and tell GDirection to build a route
   */
  loadDirections : function() {

    this.tabLoading();

    if (GBrowserIsCompatible() && this.waypoints.length) {

      query = 'from: Start @' + this.waypoints[0].getLatLng().lat() + ', ' + this.waypoints[0].getLatLng().lng();

      for (i = 1; i < this.waypoints.length; i++) {
        query += ' to: End'+i+' @' + this.waypoints[i].getLatLng().lat() + ', ' + this.waypoints[i].getLatLng().lng();
      }

      this.gdir.load(query);
    }
  },


  /************************************************************************
   * remove route from map
   */
  removeDirections : function() {

    this.gdir.clear();

    if (document.forms["JourneyAddForm"]) {
      document.forms["JourneyAddForm"].elements["data[Journey][new_route_polyline]"].value = '';
    }
  },

  /************************************************************************
   * Callback after the GDirection has created a route
   */
  onGDirectionsLoad : function() {

    // on the journey page, build the polyline
    route = cMap_0.buildRoutePolyline();

    if (document.forms["JourneyAddForm"]) {
      document.forms["JourneyAddForm"].elements["data[Journey][new_route_polyline]"].value = route;
    }

    // move google markers to north pole (removeOverlay doesn't work, because it's too early here)
    geocodesNum = cMap_0.gdir.getNumGeocodes();
    i=0;
    while (cMap_0.gdir.getMarker(i)) {
      cMap_0.gdir.getMarker(i).setLatLng(new GLatLng(90, 0));
      i++;
    }

    // load matches
    cMap_0.loadMatchesFromRoute(route);

    // check for createRoute call
    if (cMap_0.options.createRouteUrl) {
      cMap_0.createRoute(route);
    }
  },


  /************************************************************************
   * build a string polyline from the gdir object to post it to the system
   */
  buildRoutePolyline : function() {

    line = this.gdir.getPolyline();
    count = line.getVertexCount();
    var pointsArray = [];

    for (i=0; i < count; i++) {
      pointsArray.push(line.getVertex(i).lng() + ' ' + line.getVertex(i).lat());
    }

    return pointsArray.join("#");

  },


  /************************************************************************
   * create Route call (for create Journey from map)
   */
  createRoute : function(route) {

    new Ajax.Request(
      this.options.createRouteUrl,
      {
        asynchronous: true,
        evalScripts: true,
        method: 'post',
        parameters: {route: route, id: this.options.createRouteId},
        onComplete: popup_requestHandler
      }
    );

  },


  /************************************************************************
   * handle GMap Errors (especially after loading a route
   */
  handleErrors : function() {
    if (cMap_0.gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
      alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + cMap_0.gdir.getStatus().code);
    }
    else if (cMap_0.gdir.getStatus().code == G_GEO_SERVER_ERROR) {
      alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + cMap_0.gdir.getStatus().code);
    }
    else if (cMap_0.gdir.getStatus().code == G_GEO_MISSING_QUERY) {
      alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + cMap_0.gdir.getStatus().code);
    }
    // else if (cMap_0.gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
      // alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + cMap_0.gdir.getStatus().code);
    else if (cMap_0.gdir.getStatus().code == G_GEO_BAD_KEY) {
      alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + cMap_0.gdir.getStatus().code);
    }
    else if (cMap_0.gdir.getStatus().code == G_GEO_BAD_REQUEST) {
      alert("A directions request could not be successfully parsed.\n Error code: " + cMap_0.gdir.getStatus().code);
    }
    else if (cMap_0.gdir.getStatus().code == 604) {
      alert("No Route available.\n Error code: " + cMap_0.gdir.getStatus().code);
    }
    else {
      alert("An unknown error occurred:" + cMap_0.gdir.getStatus().code);
    }
  },
	
	
	clusterStyle : function(style) {
		switch(style) {
			case 'ghost':
				return [{
		        url: webroot+'img/map/cluster_g_sm.png',
		        height: 30,
		        width: 30,
		        opt_anchor: [9, 13],
		        opt_textColor: '#000000'
		      },
		      {
		        url: webroot+'img/map/cluster_g_med.png',
		        height: 40,
		        width: 40,
		        opt_anchor: [13, 17],
		        opt_textColor: '#FFFFFF'
		      },
		      {
		        url: webroot+'img/map/cluster_g_big.png',
		        height: 50,
		        width: 50,
		        opt_anchor: [17, 22],
		        opt_textColor: '#FFFFFF'
		      }];
			case 'cons':
		  	return [{
		        url: webroot+'img/map/cluster_c_sm.png',
		        height: 30,
		        width: 30,
		        opt_anchor: [9, 13],
		        opt_textColor: '#FFFFFF'
		      },
		      {
		        url: webroot+'img/map/cluster_c_med.png',
		        height: 40,
		        width: 40,
		        opt_anchor: [11, 17],
		        opt_textColor: '#FFFFFF'
		      },
		      {
		        url: webroot+'img/map/cluster_c_big.png',
		        height: 50,
		        width: 50,
		        opt_anchor: [15, 22],
		        opt_textColor: '#FFFFFF'
		      }];

			case 'jour':
				return [{
		        url: webroot+'img/map/cluster_j_sm.png',
		        height: 30,
		        width: 30,
		        opt_anchor: [9, 13],
		        opt_textColor: '#000000'
		      },
		      {
		        url: webroot+'img/map/cluster_j_med.png',
		        height: 40,
		        width: 40,
		        opt_anchor: [13, 17],
		        opt_textColor: '#FFFFFF'
		      },
		      {
		        url: webroot+'img/map/cluster_j_big.png',
		        height: 50,
		        width: 50,
		        opt_anchor: [17, 22],
		        opt_textColor: '#FFFFFF'
		      }];
      default:
		  	return [{
		        url: webroot+'img/map/cluster_c_sm.png',
		        height: 30,
		        width: 30,
		        opt_anchor: [9, 13],
		        opt_textColor: '#FFFFFF'
		      },
		      {
		        url: webroot+'img/map/cluster_c_med.png',
		        height: 40,
		        width: 40,
		        opt_anchor: [11, 17],
		        opt_textColor: '#FFFFFF'
		      },
		      {
		        url: webroot+'img/map/cluster_c_big.png',
		        height: 50,
		        width: 50,
		        opt_anchor: [15, 22],
		        opt_textColor: '#FFFFFF'
		      }];
	  }
	},
	
  /************************************************************************
   * create an icon by given marker type
   */
  createIcon : function(type) {

    iconSize = new GSize(12, 20);
    shadowSize = new GSize(22, 20);
    iconAnchor = new GPoint(6, 20);
    infoWindowAnchor = new GPoint(5, 1);

    switch(type) {

      case 'generic_from':
        image = webroot+"img/map/icon_a.png";
        shadow = webroot+"img/map/shadow2.png";
        iconSize = new GSize(20, 34);
        shadowSize = new GSize(36, 26);
        iconAnchor = new GPoint(10, 34);
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'generic_to':
        image = webroot+"img/map/icon_b.png";
        shadow = webroot+"img/map/shadow2.png";
        iconSize = new GSize(20, 34);
        shadowSize = new GSize(30, 26);
        iconAnchor = new GPoint(10, 34);
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'generic_waypoint':
        image = webroot+"img/map/icon_waypoint.png";
        shadow = webroot+"img/map/shadow2.png";
        iconSize = new GSize(20, 34);
        shadowSize = new GSize(30, 26);
        iconAnchor = new GPoint(10, 34);
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'journey_from':
        image = webroot+"img/map/icon_a.png";
        shadow = webroot+"img/map/shadow2.png";
        iconSize = new GSize(20, 34);
        shadowSize = new GSize(30, 26);
        iconAnchor = new GPoint(10, 34);
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'journey_to':
        image = webroot+"img/map/icon_b.png";
        shadow = webroot+"img/map/shadow2.png";
        iconSize = new GSize(20, 34);
        shadowSize = new GSize(30, 26);
        iconAnchor = new GPoint(10, 34);
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'journey_waypoint':
        image = webroot+"img/map/icon_waypoint.png";
        shadow = webroot+"img/map/shadow2.png";
        iconSize = new GSize(20, 34);
        shadowSize = new GSize(30, 26);
        iconAnchor = new GPoint(10, 34);
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'journey_match':
        image = webroot+"img/map/icon_j_match.png";
        shadow = webroot+"img/map/shadow1.png";
        iconSize = new GSize(16, 26);
        shadowSize = new GSize(30, 26);
        iconAnchor = new GPoint(1, 26);           // was 8,26 moved to offset
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'consignment_from':
        image = webroot+"img/map/icon_a.png";
        shadow = webroot+"img/map/shadow2.png";
        iconSize = new GSize(20, 34);
        shadowSize = new GSize(30, 26);
        iconAnchor = new GPoint(10, 34);
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'consignment_to':
        image = webroot+"img/map/icon_b.png";
        shadow = webroot+"img/map/shadow2.png";
        iconSize = new GSize(20, 34);
        shadowSize = new GSize(30, 26);
        iconAnchor = new GPoint(10, 34);
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'consignment_match':
        image = webroot+"img/map/icon_c_match.png";
        shadow = webroot+"img/map/shadow1.png";
        iconSize = new GSize(16, 26);
        shadowSize = new GSize(30, 26);
        iconAnchor = new GPoint(8, 26);
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'consignment_ghost':
        image = webroot+"img/map/icon_c_ghost.png";
        shadow = webroot+"img/map/shadow1.png";
        iconSize = new GSize(16, 26);
        shadowSize = new GSize(30, 26);
        iconAnchor = new GPoint(8, 26);
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'consignment_contract':
        image = webroot+"img/map/icon_c_contract.png";
        shadow = webroot+"img/map/shadow1.png";
        break;

      case 'ride_match':
        image = webroot+"img/map/icon_r_match.png";
        shadow = webroot+"img/map/shadow1.png";
        iconSize = new GSize(16, 26);
        shadowSize = new GSize(30, 26);
        iconAnchor = new GPoint(8, 26);
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'ride_ghost':
        image = webroot+"img/map/icon_r_ghost.png";
        shadow = webroot+"img/map/shadow1.png";
        iconSize = new GSize(16, 26);
        shadowSize = new GSize(30, 26);
        iconAnchor = new GPoint(8, 26);
        infoWindowAnchor = new GPoint(5, 1);
        break;

      case 'ride_contract':
        image = webroot+"img/map/icon_r_contract.png";
        shadow = webroot+"img/map/shadow1.png";
        break;

      default:
        image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
        shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        break;
     }

    var icon = new GIcon();
    icon.image = image;
    icon.shadow = shadow;
    icon.iconSize = iconSize;
    icon.shadowSize = shadowSize;
    icon.iconAnchor = iconAnchor;
    icon.infoWindowAnchor = infoWindowAnchor;

    return icon;
  }

}
