function debug( $obj )
{
  if ( window.console && window.console.log )
  {
    window.console.log( $obj );
  }
}

function initializeMap( mapbox, monthYear, filter, events )
{
  if ( GBrowserIsCompatible() )
  {
    var map = new GMap2( document.getElementById( mapbox ) );
    map.setCenter( new GLatLng( 51.838613, 12.244778 ), 5 );
    map.enableScrollWheelZoom();
    var mapControl = new GLargeMapControl();
    map.addControl( mapControl );
    addEventsToMap( map, monthYear, filter, events );

    return map;
  }
}

function initializeMapForEvent( mapbox, address, ident, isFriend, premium, isOver )
{
  if ( GBrowserIsCompatible() )
  {
    var map = new GMap2( document.getElementById( mapbox ) );
    var geocoder = new GClientGeocoder();

    geocoder.getLatLng(
      address,
      function( point )
      {
        if ( !point )
        {
          alert( address + ' not found' );
        }
        else
        {
          map.setCenter( point, 13 );
        }
      }
    );

    var mapControl = new GSmallMapControl();
    map.addControl( mapControl );
    addAddressToMap( map, address, ident, isFriend, premium, isOver );

    return map;
  }
}

function addAddressToMap( map, address, ident, isFriend, premium, isOver )
{
  if ( !address )
  {
    return;
  }

  var response = null;
  geocoder = new GClientGeocoder();
  geocoder.getLocations( address,  function( response )
  {
    if ( !response || response.Status.code != 200 )
    {
    }
    else
    {
      point = new GLatLng( response.Placemark[0].Point.coordinates[1], response.Placemark[0].Point.coordinates[0] );
      addMarker( point, map, ident, isFriend, premium, isOver );
    }
  } );
}

function addEventsToMap( map, monthYear, filter, events )
{
  var debug = new jQuery.debug();
  var submit_url = '';

  if ( typeof( events ) == 'object' && events.length > 0 )
  {
    $.each( events, function( i, item )
    {
      addAddressToMap( map, item.address, item.event_id, item.isFriend, item.type, item.over );
    } );
  }
  else
  {
//    $.post( '/shared/ajax/tooltip',
//            { getInfo: 'events',
//              _cal: monthYear,
//              _filter_map: filter },
//            function( data )
//            {
//              $.each( data, function( i, item )
//              {
//                var address = item.street + ' ' + item.zipcode + ' ' + item.event_city;
//                addAddressToMap( map, address, item.event_id, item.isFriend, item.type );
//              } );
//            },
//            'json' );
  }

}

function addMarker( point, map, ident, isFriend, premium, isOver )
{
  var baseIcon = new GIcon( G_DEFAULT_ICON );
  var letteredIcon = new GIcon( baseIcon );

  if ( premium == 1 )
  {
    if ( isOver )
    {
      letteredIcon.image = '/projects/fetenhits/img/icons/marker_premium_inactive.gif';
    }
    else
    {
      letteredIcon.image = '/projects/fetenhits/img/icons/marker_premium.gif';
    }

    letteredIcon.iconSize = new GSize( 20, 25 );
  }
  else
  {
    if ( !isFriend )
    {
      if ( isOver )
      {
        letteredIcon.image = '/projects/fetenhits/img/icons/marker_user_inactive.gif';
      }
      else
      {
        letteredIcon.image = '/projects/fetenhits/img/icons/marker_user.gif';
      }

      letteredIcon.iconSize = new GSize( 20, 25 );
    }
    else
    {
      if ( isOver )
      {
        letteredIcon.image = '/projects/fetenhits/img/icons/marker_user_inactive.gif';
      }
      else
      {
        letteredIcon.image = '/projects/fetenhits/img/icons/marker_user.gif';
      }

      letteredIcon.iconSize = new GSize( 20, 25 );
    }
  }

  var markerOptions = { icon : letteredIcon };
  var marker = new GMarker( point, markerOptions );
//  var gmimap = '#gmimap' + ( marker.bq.slice( 6 ) - 1 );

  GEvent.addListener(
    marker,
    'click',
    function()
    {
      marker.openExtInfoWindow(
        map,
        'map_tip',
        '<div class="map_tip_loading"><span>loading...</span></div>',
        { beakOffset: 0,
          ajaxUrl: '/shared/ajax/tooltip?getInfo=event&id=' + ident }
      );
/*
    var markerPosition = map.fromLatLngToDivPixel(
      marker.getPoint()
    );
    debug(markerPosition);
    showInfoGoogleMap( $(map.C).attr('id'), markerPosition, ('u_id24996'));
*/
    }
  );

  map.addOverlay( marker );
}