$().ready(function() {

  if ( typeof(GMap2) == 'function' ){

    map = new GMap2( $("#map_canvas")[0] );
    geocoder = new GClientGeocoder();

    map.setCenter(new GLatLng( map_lat, map_lng), map_zoom);
    map.setUIToDefault();

    mgr = new MarkerManager(map);

    show_streetview = function(){

      var node = $("#streetview")[0];
      var pano = new GStreetviewPanorama( node );
      GEvent.addListener(pano, 'error', function(errorCode) {
        if (errorCode == 603) {
          node.innerHTML = 'StreetView requires flash plugin. Click <a href="http://get.adobe.com/flashplayer/" target="_blank"> here</a> to download';
        }
      });
      pano.setLocationAndPOV( map.getInfoWindow().getPoint() );
      node.pano = pano;

      $("#streetview").show();
      map.updateInfoWindow()

      return false;

    };

    process_properties = function( data ){

      markers_16 = [];
      markers_14 = [];
      markers_12 = [];
      markers_11 = [];

      count = 0;

      $.each( data, function( i, property ){

        var point = new GLatLng( property.lat,property.lng )
        var marker = new GMarker( point );
//         map.addOverlay( marker );

        markers_16.push( marker );
        if ( count % 10 == 0 ) markers_14.push( marker );
        if ( count % 50 == 0 ) markers_12.push( marker );
        if ( count % 75 == 0 ) markers_11.push( marker );

        count++;

        GEvent.addListener(marker,"infowindowopen",function(){
          $("a[href='#streetview']").click( show_streetview );
        });


        /*
          // if map memory usage becomes a problem, figure out how to close the streetview window here
          GEvent.addListener(marker,"infowindowbeforeclose",function(){
            var node = $("#streetview")[0];
            if ( node.pano ){
              // release streetview memory here
            }
          });
        */

        if ( highlight_id == property.id ) marker.openInfoWindowHtml( property.html );

        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(property.html);
        });

      });

      mgr.addMarkers(markers_16, 16);
      mgr.addMarkers(markers_14, 14, 15);
      mgr.addMarkers(markers_12, 12, 13);
      mgr.addMarkers(markers_11, 0, 11);
      mgr.refresh();

    };

    $.get('properties.php?limit=5000&offset=0',{},process_properties,'json');
    $.get('properties.php?limit=5000&offset=5000',{},process_properties,'json');
    $.get('properties.php?limit=5000&offset=10000',{},process_properties,'json');
    $.get('properties.php?limit=5000&offset=15000',{},process_properties,'json');

  }

  $("#address_search_button").click( function(){
    address = $("#address_search_query").val();

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

  });

  $("a[href=#twitter]").click( function(){
    m = "<?= $property->twitter_message() ?>";
    window.open( 'http://twitter.com/home?status=' + encodeURIComponent(m) );
    return false;
  });

  $("a[href=#facebook]").click( function(){
    u = "<?= $property->url() ?>";
    t = "LA Hoodwinked";
    window.open(
      'http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),
      'sharer','toolbar=0,status=0,width=626,height=436'
    );
    return false;
  })

});


