var map;

function addMarker(marker)
{
	map.addOverlay(marker);

	var element = new Element('div');
	element.id = 'title_' + marker.id;
	element.name = 'title[' + marker.id + ']';
	element.inject($('markers_data'));
	var element = new Element('div');
	element.id = 'description_' + marker.id;
	element.name = 'description[' + marker.id + ']';
	element.inject($('markers_data'));

	GEvent.addListener(marker, 'click', function(markerPoint){
		nameValue = $('title_' + this.id).innerHTML;
		descriptionValue = $('description_' + this.id).innerHTML;
		this.openInfoWindowHtml('<div style="width: 200px"><strong>' + nameValue + '</strong></div><div>' + descriptionValue + '</div>');
	});
}

function mapPanTo(lat, lng)
{
	map.panTo(new GLatLng(lat, lng));
}

function addIcon(image, gsize, gpoint, gpoint2, elementId)
{
	var icon = new GIcon();
	icon.image = image;
	icon.iconSize = gsize;
	icon.iconAnchor = gpoint;
	icon.infoWindowAnchor = gpoint2;
	iconList[elementId] = icon;
}

function showAddress(address)
{
	geocoder.getLatLng(
		'Polska, Łódź, ' + address, function(point) {
			if (point) {
				map.addOverlay(searchMarker);
				searchMarker.setLatLng(point);
				map.setCenter(point, 15);
			} else {
				// Unable to find address
			}
		}
	);
}

function checkMarkers()
{
	bounds = map.getBounds();
	for (var n = 0; n < markersArray.length; n++) {
		var marker = markersArray[n];
		if (true == bounds.containsLatLng(marker.getLatLng())) {
			$('description_' + marker.id).setStyles({
				'color': '#5D5D5D'
			});
		} else {
			$('description_' + marker.id).setStyles({
				'color': '#ADADAD'
			});
		}
	}
}

function loadMap(mapId)
{
	document.getElementById(mapId).innerHTML = "Wczytywanie mapy...";
	if (!GBrowserIsCompatible()) {
		alert('Sorry. Your browser is not Google Maps compatible.');
	}

	map = new GMap2(document.getElementById(mapId));

	iconList = new Array();
	markersArray = new Array();
	geocoder = new GClientGeocoder();
	num = 0;

	map.addControl(new GLargeMapControl());
	map.setCenter(new GLatLng(51.95, 19.25), 6);
	map.addControl(new GMapTypeControl());
	GEvent.addListener(map, 'moveend', function(markerPoint){
		checkMarkers();
	});
}

