var _maps = new Array();
var _mapQueue = new Array();
var _lastMap; 


function initMap(config) {
	
    loadMap(config.id);
    config.mapElements.each(function(element){
    	addIcon(element[1], new GSize(71, 39), new GPoint(6, 37), new GPoint(50, 1), element[2]);
    });
    var bounds = new GLatLngBounds();
    var num = 0;
    config.markers.each(function(mark){
    	var latLng = new GLatLng(mark.lat,mark.lng);
    	
	    bounds.extend(latLng);
	    marker = new GMarker(latLng,{icon:iconList[mark.icon]});
	
	    marker.id = mark.id;
	    addMarker(marker,config.id);
	    markersArray[num++] = marker;
    });
    var newZoom = _maps[config.id].getBoundsZoomLevel(bounds);
    if (newZoom > 14) {
    	newZoom = 14;
    }
    var center = bounds.getCenter();
    _maps[config.id].setCenter(center, newZoom); 
    var searchMarker = new GMarker(0, 0,{zIndexProcess: -1000});	
}


function addMarker(marker, mapid)
{
	map = _lastMap;
	if (mapid) {
		map = _maps[mapid];
	}
	map.addOverlay(marker);
	var element = new Element('div');
	element.id = 'title_el_' + marker.id;
	element.name = 'title[' + marker.id + ']';
	element.inject($('markers_data'));
	
	var element = new Element('div');
	element.id = 'description_el_' + 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, mapid)
{
	map = _lastMap;
	if (mapid) {
		map = _maps[mapid];
	}	
	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, mapid)
{
	map = _lastMap;
	if (mapid) {
		map = _maps[mapid];
	}
	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(map)
{
	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(map);
	});
	_maps[mapId] = map;
	_mapQueue.push(map);
	_lastmap = map;
}
