/* <![CDATA[ */

// return a GIcon configured to display the mybonitos marker Icons
// use only this function to set custom Icons
// params:
//  bitflag int   type:
//                  1: been there
//                  2: want to go there
//                  4: live here
//                  8: restaurant
//                  16: hotel
//                  32: bar
//                  64: reserve
//                  128: in my pf
//                  256: in my DCMs pf
//                  512: in my FOFs pf
//                  1024: in anyones pf
//                  2048: draggable
// -------------------------------------------------------------------------------------------------
function createMbGIcon( type )
{
  var path_images = '/images/icons/markers';

  var basename_icon = 'mybonitos' + ( ( type & 2048 ) ? '_draggable' : '' ) + '.png';
  var basename_shadow = 'shadow' + ( ( type & 2048 ) ? '_draggable' : '' ) + '.png';

  var icon = new GIcon();

  icon.iconSize = new GSize( 48, 44 );
  icon.shadowSize = new GSize( 48, 44 );
  icon.iconAnchor = new GPoint( 35, 35 );
  icon.infoWindowAnchor = new GPoint( 40, 38 );

  if ( type & 8 )
  {
    icon.image        = path_images + '/' + 'restaurant' + ( ( type & 2048 ) ? '_draggable' : '' ) + '.png';
    icon.shadow       = path_images + '/' + 'shadow' + ( ( type & 2048 ) ? '_draggable' : '' ) + '.png';
    icon.printImage   = path_images + '/' + 'restaurant.png';
    icon.printShadow  = path_images + '/' + 'shadow.png';
  }
  else if ( type & 16 )
  {
    icon.image        = path_images + '/' + 'hotel' + ( ( type & 2048 ) ? '_draggable' : '' ) + '.png';
    icon.shadow       = path_images + '/' + 'shadow' + ( ( type & 2048 ) ? '_draggable' : '' ) + '.png';
    icon.printImage   = path_images + '/' + 'hotel.png';
    icon.printShadow  = path_images + '/' + 'shadow.png';
  }
  else if ( type & 32 )
  {
    icon.image        = path_images + '/' + 'bar' + ( ( type & 2048 ) ? '_draggable' : '' ) + '.png';
    icon.shadow       = path_images + '/' + 'shadow' + ( ( type & 2048 ) ? '_draggable' : '' ) + '.png';
    icon.printImage   = path_images + '/' + 'bar.png';
    icon.printShadow  = path_images + '/' + 'shadow.png';
  }
  else
  {
    icon.image        = path_images + '/' + basename_icon;
    icon.shadow       = path_images + '/' + basename_shadow;
    icon.printImage   = path_images + '/' + basename_icon;
    icon.printShadow  = path_images + '/' + basename_shadow;
  }

  return icon;
}

// return a GMarker with the matching mbIcon
// params:
//  bitflag int   type:
//                  1: been there
//                  2: want to go there
//                  4: live here
//                  8: restaurant
//                  16: hotel
//                  32: bar
//                  64: reserve
//                  128: in my pf
//                  256: in my DCMs pf
//                  512: in my FOFs pf
//                  1024: in anyones pf
//                  2048: draggable
//  float         latitude
//  float         longitude
//  string        info_window
// -------------------------------------------------------------------------------------------------
function createMbGMarker( type, latitude, longitude )
{
  var marker = new GMarker( new GLatLng( latitude, longitude ), { icon: createMbGIcon( type ), draggable: ( ( type & 2048 ) ? true : false ) } );

  return marker;
}

function bindEvents( marker, events )
{
  for ( var event in events )
  {
    GEvent.addListener( marker, event, events[event] );
  }
}

// in order for to be able to unset markers in the marker manager, we need to store them outside in
// markers_objects, using a unique key. this function is used to copy the markers from that object to the manager
// zoom levels are not stored in the markers object and must be supplied as function parameters
function setupMarkers( markers_object, minZoom, maxZoom )
{
  var markers = [];
  var i;
  for ( i in markers_object )
  {
    markers.push( markers_object[i] );
  }

  markermanager.addMarkers( markers, minZoom, maxZoom );
  markermanager.refresh();
}

// this adds a marker to a marker object
function addMarkerToManager( marker, markers_object, key, minZoom, maxZoom )
{
  markermanager.addMarker( marker, minZoom, maxZoom );
  markermanager.refresh();
  markers_object[key] = marker;
}

// this function can remove a marker from these marker objects, using the unique key
function removeMarkerFromManager( markers_object, key )
{
  if ( markers_object[key] )
  {
    markermanager.removeMarker( markers_object[key] );
    markermanager.refresh();
    markers_object[key] = null;
  }
}

function toggleLocality( collection, latitude, longitude, member_id, locality_id, type, text )
{
  var newMarkerData = new Object();
  newMarkerData['id']       = locality_id;
  newMarkerData['text']     = text;
  newMarkerData['GLatLng']  = new GLatLng( longitude, latitude );

  var parameterString = 'member=' + member_id + '&locality=' + locality_id + '&type=' + type;
  var toggleRequest = new Ajax.Request( '/index.php/de_CH/maps/toggleMemberLocality',
    {
      method: 'get',
      parameters: parameterString,
      onComplete: showLocality
    } );
}

function showLocality( ajaxRequest )
{
  // types is a bitflag: 1 = been there, 2 = want to go, 3 = 1 + 2 = both
  var types = ajaxRequest.responseText;

  if ( GMarkers[newMarkerData['id']] )
  {
    map.removeOverlay( GMarkers[newMarkerData['id']] );
    GMarkers[newMarkerData['id']] = null;
  }

  if ( types > 0 )
  {
    addMarker( newMarkerData['id'], types, newMarkerData['text'], newMarkerData['GLatLng'] );
    map.panTo( newMarkerData['GLatLng'] );
  }
}

function addMarker( id, type, text, GLatLng )
{
  markerOptions = { icon:Icons[( type )] };
  GMarkers[id] = new GMarker( GLatLng, markerOptions );
  map.addOverlay( GMarkers[id] );
}

/* ]]> */
