var maxMarkers = 40;
var currentPage = 1;
var mapState; 
var staticFilter;

function openMarkerWindow(ID)
{
	MapAPI.openMarker(ID);
}

function onMarkerMouseOver()
{
  var geoposID = this._geopos_ID;

  MapAPI.highlightMarkerForGeoPosition(geoposID);

  var id = "geopos" + geoposID;

  var f = $(id);
  if (f)
    f.addClassName("hover");
}

function onMarkerMouseOut()
{
  var geoposID = this._geopos_ID;

  MapAPI.unhighlightMarkerForGeoPosition(geoposID);


  var id = "geopos" + geoposID;

  var f = $(id);
  if (f)
    f.removeClassName("hover");
}

function onFilterChange()
{
  MapAPI.closeInfoWindows();
  loadVisibleMarkers(1);
  storeMapState();
}

function onSortChange()
{
  MapAPI.closeInfoWindows();
  loadVisibleMarkers(1);
  storeMapState();
}

function onMapChange(x)
{
  if (!MapAPI.refreshDisabled)
    loadVisibleMarkers(1);
  MapAPI.enableMarkerRefresh();
  storeMapState();
}

function getSearchDefinition(page)
{
  var def = new Object;

  def.filter = { refclass: "Product" };
  def.sort = "distance";

  def.adapter  = "mapsindex";
  def.listTemplate = "list";
  def.markerTemplate = "mapinfowindow";
  def.maxRows  = maxMarkers;
  def.skipRows = page ? ((page-1) * def.maxRows) : 0;

  def.bounds = MapAPI.getBounds();
  def.center = MapAPI.getCenter();

  return def;   
}

function showNext()
{
  MapAPI.closeInfoWindows();
  if (currentPage + 1 <= totalPages)
	{
	  currentPage++;
	  loadVisibleMarkers(currentPage);
	  storeMapState();
	}
}

function showPrev()
{
  MapAPI.closeInfoWindows();
  if (currentPage > 1)
	{
	  currentPage--;
	  loadVisibleMarkers(currentPage);
	  storeMapState();
	}
}

function loadVisibleMarkers(page)
{
  if (page)
	currentPage = page; 

  asyncRpcCall("maps.getPositions", getSearchDefinition(currentPage),
			   function(data) { MapAPI.log("getPositions"); showMarkers(data); MapAPI.enableEvents(); });
}

function restoreMapState(state,ts)
{
  if (!state)
	{
	  asyncRpcCall("maps.getMapState",
				   function(ms) { if (ms) restoreMapState(ms); });
    }
  else
	{
	  var myData; 

	  mapState = state;

	  MapAPI.setType(mapState.type);
	  MapAPI.setCenter(mapState.center[0], mapState.center[1], mapState.zoom);

	  if (mapState.adapter && mapState.adapter["mapsindex"])
		myData = mapState.adapter["mapsindex"];
	  else
		myData = { };

	  var page = myData.page;
	  var def  = myData.searchDef;
	  
	  if (!def)
		def = { filter: "" };

	  var refclass = "Product";

	  var sort = def.sort;
	  if (!sort || sort == "")
	    sort = "distance";

	  loadVisibleMarkers(page ? page : 1);
	}
}

function storeMapState(reload)
{
  if (!mapState)
	mapState = new Object;	

  if (!mapState.adapter)
	mapState.adapter = new Object;

  if (!mapState.adapter["mapsindex"])
	mapState.adapter["mapsindex"] = new Object;

  mapState.adapter["mapsindex"].searchDef = getSearchDefinition();
  mapState.adapter["mapsindex"].page = currentPage;

  mapState.center = MapAPI.getCenter();
  mapState.zoom = MapAPI.getZoom();

  mapState.type = MapAPI.getType();

  MapAPI.log("storing map state");

  if (reload)	
    asyncRpcCall("maps.storeMapState", mapState, function() { reloadPage() });
  else
    asyncRpcCall("maps.storeMapState", mapState);  
}

function showMarkers(arr)
{
  var newMarkers = new Object();
  var positions = arr.positions;
  var totalrows = arr.totalrows;
  var rows = arr.rows;
  var overflow = arr.overflow;

  totalPages = Math.ceil(totalrows / maxMarkers);

  var cbs = { mouseover: onMarkerMouseOver, mouseout: onMarkerMouseOut };

  MapAPI.replaceGeoPositionMarkers(positions, cbs);
}


