$(function() {
	init();

	$("div[class^=google-map]").unload(function() {
		GUnload();
	});
});

function init() {
	if (GBrowserIsCompatible()) {
		// save markers array (for each div with .marker class)
		$('div[class^=google-map]').each(function(n, value) {
			applygMap(value);
		});
	}
};

function applygMap(map_element) {
	var map_id = "#" + map_element.attributes['id'].value;
	var markers = $(map_id + " .marker");

	// create google map object
	var map = new GMap2(map_element);

	GEvent.addListener(map, "load", function() {
		$(map_id + ' > .gmnoprint').remove();
		$(map_id + " > div:last").attr("id", "info");

		/*if (map_id == '#small_map')
		$('#gmap-small-panel').hide();*/
	});

	var latLng;
	if ($(map_id).attr('lat')) {
		latLng = new GLatLng($(map_id).attr('lat'), $(map_id).attr('lng'));
	} else {
		latLng = new GLatLng($(markers[0]).attr('lat'), $(markers[0]).attr('lng'));
	}

	map.setCenter(latLng, parseInt($(map_id).attr('zoom')), G_NORMAL_MAP);

	if ($(map_id).attr('scroll-zoom'))
		map.enableScrollWheelZoom();

	if ($(map_id).attr('map-control'))
		map.addControl(new GSmallMapControl());

	if ($(map_id).attr('map-type-control')) {
		var mapControl = new GMapTypeControl(true);
		map.addControl(mapControl);
	}

	var bounds = new GLatLngBounds;
	var lastMarker;

	var LastEventIcon = new GIcon();
	LastEventIcon.image = 'client/default/images/last-event-marker.png';
	LastEventIcon.iconSize = new GSize(25, 28);

	var NextEventIcon = new GIcon();
	NextEventIcon.image = 'client/default/images/next-event-marker.png';
	NextEventIcon.iconSize = new GSize(25, 28);

	//add markers to the map
	markers.each(function(n, value) {
		var point = new GLatLng(value.attributes['lat'].value,
			value.attributes['lng'].value);

		// extend markers bounds
		bounds.extend(point);

		var zIndexes = function() {
			return 100 - n;
		}
		var marker;
		if (value.attributes['href']) {
			if (value.attributes['icon']) {
				var icon = new GIcon(G_DEFAULT_ICON);
				icon.image = value.attributes['icon'].value;
				icon.iconSize = new GSize(32, 33);
				iconAnchor = new GSize(5, 10);
				icon.shadow = '/client/default/images/marker-shadow.png';
				icon.shadowSize = new GSize(32, 33);
				markerOptions = {
					icon: icon,
					title: value.attributes['title'].value,
					zIndexProcess: zIndexes
				};
			} else {
				markerOptions = {
					title: value.attributes['title'].value,
					zIndexProcess: zIndexes
				};
			}

			// Set up our GMarkerOptions object
			marker = new GMarker(point, markerOptions);
			GEvent.addListener(marker, "click", function() {
				window.location.href = value.attributes['href'].value;
			});

			$('div[event-id=' + value.attributes['event-id'].value + ']').bind('click', function(event) {
				map.panTo(point);
			});
		} else {
			marker = new GMarker(point,
			{
				zIndexProcess: zIndexes
			});
		}

		map.addOverlay(marker);
	});

	/*markers.each(function(n, value) {

	});*/

	//set zoom level
	if (markers.length > 1) {
		var zoom = $(map_id).attr('zoom');
		if (!zoom || (zoom > map.getBoundsZoomLevel(bounds)))
			zoom = map.getBoundsZoomLevel(bounds);
		map.setCenter(bounds.getCenter(), zoom);
	}
};
