LocationMap = {
	useSearch : false
	,
	criteria : {}
	,
	prevRequestsQueue : 0
	,
	requestsQueue : 0
	,
	activeRequests : 0
	,
	existingMarkers : []
	,
	init : function (map) {
		if ($('#SpotSearch').val() != '') { LocationMap.useSearch = true; LocationMap.criteria['data[Location][search]'] = $('#SpotSearch').val(); }
		if ($('#SpotLocality').val() != '') { LocationMap.useSearch = true; LocationMap.criteria['data[Location][locality]'] = $('#SpotLocality').val(); }
		if ($('#SpotSport').val() != '') { LocationMap.useSearch = true; LocationMap.criteria['data[Location][sport]'] = $('#SpotSport').val(); }
		if ($('#SpotFacility').val() != '') { LocationMap.useSearch = true; LocationMap.criteria['data[Location][facility]'] = $('#SpotFacility').val(); }

		GEvent.addListener(map,'moveend',LocationMap.loadExistingMarkers);
		GEvent.addListener(map,'zoomend',LocationMap.loadExistingMarkers);
		LocationMap.requestsQueue++;
		LocationMap.checkRequestQueue();
	}
	,
	showExistingMarkers : function (points) {
		LocationMap.activeRequests--;
		var existingMarkerIcon = new GIcon(G_DEFAULT_ICON);
		existingMarkerIcon.image = '/img/spot.png';
		existingMarkerIcon.shadow = false;
		existingMarkerIcon.iconSize = new GSize(24,24);
		existingMarkerIcon.iconAnchor = new GPoint(12,12);

		var existingPrivateMarkerIcon = new GIcon(G_DEFAULT_ICON);
		existingPrivateMarkerIcon.image = '/img/spot-private.png';
		existingPrivateMarkerIcon.shadow = false;
		existingPrivateMarkerIcon.iconSize = new GSize(24,24);
		existingPrivateMarkerIcon.iconAnchor = new GPoint(12,12);

		for (l in LocationMap.existingMarkers) {
			try { LocationMap.existingMarkers[l].hide(); } catch (e) { }
		}
		for (p in points) {
			try {
				var key = points[p].Location.urn;
				if (!LocationMap.existingMarkers[key]) {
					if (points[p].Location.public == 1) {
						LocationMap.existingMarkers[key] = new GMarker(new GLatLng(points[p].Location.latitude,points[p].Location.longitude),{draggable:false,icon:existingMarkerIcon});
					} else {
						LocationMap.existingMarkers[key] = new GMarker(new GLatLng(points[p].Location.latitude,points[p].Location.longitude),{draggable:false,icon:existingPrivateMarkerIcon});
					}
					map.addOverlay(LocationMap.existingMarkers[key]);
					LocationMap.existingMarkers[key].ssData = points[p].Location;
					LocationMap.existingMarkers[key].ssObject = points[p];
					GEvent.addListener(LocationMap.existingMarkers[key], "click", function() {
						this.openInfoWindowHtml('<div class="spots-infoWindow">'+
'<h2>'+this.ssData.name+'</h2>'+
'<table>'+
'<tr><th>Adres : </th><td>'+this.ssData.locality+'<br />'+this.ssData.thoroughfare+'</td>'+
((this.ssObject.Thumbnail.filename != null) ? '<td rowspan="5"><img src="/img/upl/c/120x120/'+this.ssObject.Thumbnail.dir+'/'+this.ssObject.Thumbnail.filename+'" width="120" height="120" /></td>' : '')+
'</tr>'+
'<tr><th>Obiekt : </th><td>'+this.ssObject.Facility.name+'</td></tr>'+
'<tr><th>Sporty : </th><td>'+this.ssData.agr_sport_names+'</td></tr>'+
'<tr><th>Zdjęcia : </th><td>'+this.ssData.agr_images_count+'</td></tr>'+
'<tr><th>Komentarze : </th><td>'+this.ssData.agr_comments_count+'</td></tr>'+
'</table>'+
'<p class="more"><a href="/spoty/widok/'+this.ssData.urn+'">więcej</a></p>'+
'</div>');
					});
				} else {
					LocationMap.existingMarkers[key].show();
				}
			} catch (e) {}
		}
	}
	,
	checkRequestQueue : function () {
		if (LocationMap.requestsQueue > 0 && LocationMap.prevRequestsQueue == LocationMap.requestsQueue) {
			LocationMap.requestsQueue = 0;
			LocationMap.doLoadExistingMarkers();
		}
		LocationMap.prevRequestsQueue = LocationMap.requestsQueue;
		setTimeout(LocationMap.checkRequestQueue,800);
	}
	,
	doLoadExistingMarkers : function () {
		LocationMap.activeRequests++;
		if (LocationMap.useSearch) {
			var baseUrl = '/spots/searchLocationMapPoints';
			$.ajax({type:'POST',url:baseUrl+'/'+map.getZoom()+'/'+Math.floor(map.getBounds().getSouthWest().lat())+'/'+Math.floor(map.getBounds().getSouthWest().lng())+'/'+Math.ceil(map.getBounds().getNorthEast().lat())+'/'+Math.ceil(map.getBounds().getNorthEast().lng())+'.json',data:LocationMap.criteria,dataType:'json',success:LocationMap.showExistingMarkers});
		} else {
			var baseUrl = '/spots/locationMapPoints';
			$.ajax({type:'POST',url:baseUrl+'/'+map.getZoom()+'/'+Math.floor(map.getBounds().getSouthWest().lat())+'/'+Math.floor(map.getBounds().getSouthWest().lng())+'/'+Math.ceil(map.getBounds().getNorthEast().lat())+'/'+Math.ceil(map.getBounds().getNorthEast().lng())+'.json',dataType:'json',success:LocationMap.showExistingMarkers});
		}
	}
	,
	loadExistingMarkers : function () {
		LocationMap.requestsQueue++;
	}
};
jQuery(document).ready(function() {
	$("#SpotExtendedSearch").click(function() {if($(this).is(':checked')) { $('#SpotExtendedSearchInputs').show(); } else { $('#SpotExtendedSearchInputs').hide(); } });
	if($("#SpotExtendedSearch").is(':checked')) { $('#SpotExtendedSearchInputs').show(); } else { $('#SpotExtendedSearchInputs').hide(); }
	$.ajaxSetup({async:false});
	LocationMap.init(map);
});
