﻿var map = null;
var count = 0;
var newcount = 0;
var totalcount = 0;
var addedMarkers = new Array();

var tinyIcon = new GIcon();
tinyIcon.image = "marker_red.png";
tinyIcon.iconSize = new GSize(8, 8);
tinyIcon.iconAnchor = new GPoint(4, 4);
markerOptions = { icon:tinyIcon };

$(document).ready(function() {
  initialize();
});

function initialize() 
{
  map = new GMap2(document.getElementById("map_canvas"));
  map.setCenter(new GLatLng(45, 0), 1);
  map.setUIToDefault();
  map.setMapType(G_PHYSICAL_MAP);
  
  GEvent.addListener(map, 'moveend', updateMarkers);
  GEvent.addListener(map, 'click', function() { $('#map_cmenu').hide(); });
  GEvent.addListener(map, 'movestart', function() { $('#map_cmenu').hide(); });
  GEvent.addListener(map, 'singlerightclick', rightclick);
  
  updateMarkers();
}

function rightclick(point,src,overlay) 
{
  if (overlay) 
  {
    if (overlay instanceof GMarker) 
    {
      return;
    }
  } 

  clickedPixel = point;
  var x=point.x;
  var y=point.y;
  var offset = $('#map_canvas').offset();
  x+=offset.left;
  y+=offset.top;
  $('#map_cmenu').css('left', x)
  $('#map_cmenu').css('top', y)
  $('#map_cmenu').fadeIn('fast');
}

function updateMarkers()
{
  var bounds = map.getBounds();
  var southWest = bounds.getSouthWest();
  var northEast = bounds.getNorthEast();

  // get random selection of lie-downs in visible area
  $.ajax({
    type:       "GET",
    url:        "markers.aspx",
    cache:      false,
        data:       "mode=map&minlat="+southWest.lat()+"&maxlat="+northEast.lat()+"&minlng="+southWest.lng()+"&maxlng="+northEast.lng(),
        beforeSend: function(XMLHttpRequest) { $('#map_loading').fadeIn() },
        success:    function(xml) { loadMarkers(map, xml); $('#map_loading').fadeOut(); }
  });
}

function loadMarkers(map, xmlData)
{
  map.clearOverlays();

  count = $(xmlData).find("marker").length;
  totalcount = $(xmlData).attr("totalMarkers");
  $('#map_info').text(count + " lie downs visible - " + totalcount + " on map in total");

  $(xmlData).find("marker").each(function() {
    var marker = $(this);
    var point = new GLatLng(parseFloat(marker.attr("lat")), parseFloat(marker.attr("lng")));
    if(marker.attr("countryName") != undefined) 
    {
        markerOptions.title = marker.attr("name") + " ("+ marker.attr("countryName") + ")";
    }
    else 
    {
        markerOptions.title = marker.attr("name");
    }
    var marker = new GMarker(point, markerOptions);
    map.addOverlay(marker);
    GEvent.addListener(marker, 'dblclick', function(latlng) { map.zoomIn(latlng,true);updateMarkers(); });
    attachClickEventToMarker(marker);
  });
}

function add()
{
  var point = map.fromContainerPixelToLatLng(clickedPixel);
  $('#map_cmenu').hide();
}

function submitliedown() 
{
  var point = map.fromContainerPixelToLatLng(clickedPixel);
  $.ajax({
    type:       "POST",
    url:        "markers.aspx",
    cache:      false,
        data:       "lat="+point.lat()+"&lng="+point.lng()+"&name="+$('#name').val(),
        beforeSend: function(XMLHttpRequest) { $('#map_loading').fadeIn() },
        success:    function(xml) { $('#map_loading').fadeOut(); }
  });

  markerOptions.title = $('#name').val() + " - NEW!";
  map.addOverlay(new GMarker(point, markerOptions));

  $('#map_cmenu').hide();
  count++;
  newcount++;
  totalcount++;
  $('#map_info').text(count + " lie downs visible (" + newcount + " new) - " + totalcount + " on map in total");
  return false;
}

function showCountryRankings() 
{
  $.ajax({
    type:       "GET",
    url:        "stats.aspx",
    cache:      false,
        data:       "mode=countries",
        beforeSend: function(XMLHttpRequest) { },
        success:    function(html) { $('#map_stats').html(html); }
  });
}