var status = 'empty';

function toggleAroundThis(url, showtext, hidetext) {
  if (status == 'empty') {
    jQuery('#map-togglearoundthis').addClass('loading');
    status = 'loading';

    jQuery.getJSON(
      url,
      function(data) {
        pgMappy.removeAllPoints();
        jQuery.each(data, function(i, item) {
          var pgPoint = new PGPoint(item);
          pgMappy.pointAdder(pgPoint);
        });
        pgMappy.setCenterAndZoom();

        initMapPoints();

        jQuery('#map-togglearoundthis').removeClass('loading');
        jQuery('#map-togglearoundthis a').text(hidetext);

        status = 'visible';
      }
    );
  } else if (status == 'visible') {
    jQuery('div.map-point-aroundthis').parent().hide();
    jQuery('div.map-popup').parent().parent().hide();
    jQuery('#map-togglearoundthis a').text(showtext);

    status = 'hidden';
  } else if (status == 'hidden') {
    jQuery('div.map-point-aroundthis').parent().show();
    jQuery('div.map-popup').parent().parent().show();
    jQuery('#map-togglearoundthis a').text(hidetext);

    status = 'visible';
  }
}

function initMapPoints() {
  jQuery('div.map-point').hover(
    function() {
      jQuery(this).parent().css('z-index', 300);
    },
    function() {
      jQuery(this).parent().css('z-index', 250);
    }
  );
}

function toggleMapSize(map, mapcontainer) {
  var mapcontainer = jQuery('#' + mapcontainer);
  var mapbox = mapcontainer.parent();

  mapbox.toggleClass('expanded');

  if (mapbox.hasClass('expanded')) {
    mapbox.insertBefore(mapbox.parent());
    jQuery(window).resize(function() {
      map.setSize(mapcontainer.width(), map.getSize().height);
    });
  } else {
    mapbox.prependTo(mapbox.next());
    jQuery(window).resize(function() {});
  }

  map.setSize(mapcontainer.width(), map.getSize().height);
}

