jQuery(function(){

	if (jQuery('#busstops_map').length == 0)
		return;

	var map = new GMap2(document.getElementById("busstops_map"));
	map.setCenter(new GLatLng(52.203609,19), 6);
	map.addControl(new GLargeMapControl());
	map.enableScrollWheelZoom();

	function refreshPins() {
		jQuery.ajax({
			type: "POST",
			data: {'reg': jQuery('#busstops_map').attr('woj')},
			processData: true,
			url: "/pl/euroticket/index/stops-list",
			dataType: "xml",
			success: function(xml) {
				var bounds = null;
				jQuery(xml).find('stop').each(function(){
					var elt = jQuery(this);
                                        var opt;
                                        opt = {};
                                        opt.draggable = false  ;
                                        
					var pos = new GLatLng(elt.attr('lat'),elt.attr('lon'))
					var marker = new GMarker(pos,opt);

					if (parseFloat(elt.attr('lat')) && parseFloat(elt.attr('lon'))) {
						if (bounds == null)
							bounds = new GLatLngBounds(pos, pos);
						else
							bounds.extend(pos);
					}

					map.addOverlay(marker);

					GEvent.addListener(marker, "click", function() {
						if (map.getZoom() < 12) 
							map.setCenter(this.getLatLng(), 12);

						this.openInfoWindowHtml('<b>' + elt.attr('city') + '</b><br />' 
									+ elt.attr('location') 
									+ "<br/><br/>" 
									+ elt.attr('description')
									+ "<br /><br />Lokalizacja GPS: " + Math.round(elt.attr('lat')*1000)/1000 
									+ ', ' + Math.round(elt.attr('lon')*1000)/1000);
					});
				})
				map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
			}
		});
	};
	
	refreshPins();
});
