SolnaStad = typeof SolnaStad === 'undefined' ? {} : SolnaStad;

SolnaStad.Map = function (el, options) {
	var t = this;
	this.options = $.extend({}, this.defaultOptions, options || {});
	this.container = $(el);
	this.googlemap = new google.maps.Map(this.container.find('.map')[0], this.options.googleMapOptions);

	this.mapMode = this.options.mapMode;

	if (this.options.mapDataContainer) {
		this.parseItemList(this.options.mapDataContainer);
	}
};
SolnaStad.Map.prototype = {
	defaultOptions: {
		// Default options for Google Map
		googleMapOptions: {
			zoom: 1,
			center: new google.maps.LatLng(20, 10),
			mapTypeControlOptions: {
				mapTypeIds: [
					google.maps.MapTypeId.ROADMAP,
					google.maps.MapTypeId.HYBRID
				]
			},
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			// disableDefaultUI: true,
			// navigationControl: false,
			scrollwheel: false
		},
		// Container to update with data from map
		mapDataContainer: null
	},
	zoomToWorld: function () {
		this.showWorld();
		this.googlemap.setZoom(1);
		this.googlemap.setCenter(this.options.googleMapOptions.center);
		this.updateDataContainer();
	},
	showWorld: function () {
		$.each(this.continents, function () {
			this.hideAllMarkers();
			this.hideAllInfoWindows();
			// Show continent marker
			this.showMarker();
		});
		this.buildBreadcrumbs();
	},
	scrollToTopOfMapContainer: function () {
		$('html, body').animate({
			scrollTop: this.container.offset().top
		}, 400);
	},
	parseItemList: function (element) {
		var t = this;
		var continentElements;
		var zoomChangeListener;
		var bounds = new google.maps.LatLngBounds();

		this.pois = [];

		element.find('div.item').each(function () {
			var item = $(this);
			var name = item.find('.name:first');
			var poi = new SolnaStad.Map.POI({
				name: name.text(),
				coords: SolnaStad.Map.Utils.parseCoords(item.find('.coords:first').text()),
				map: t,
				infoElement: item,
				markerText: item.find('.num:first').text()
			});
			bounds.extend(poi.coords);
			poi.showMarker();
			t.pois.push(poi);
			t.fireEvent('poiAdded', t, [poi, item]);
		});

		// When we set the bounds, we don't want to zoom in more than zoom level 3
		zoomChangeListener = google.maps.event.addListener(this.googlemap, 'zoom_changed', function () {
			if (t.googlemap.getZoom() > 15) {
				t.googlemap.setZoom(15);
			}
			google.maps.event.removeListener(zoomChangeListener);
		});

		// Shrink to fit all office markers
		this.googlemap.fitBounds(bounds);
	}
};

SolnaStad.mixins.augment(SolnaStad.Map, SolnaStad.mixins.Events);

//
// Marker icons
//
SolnaStad.Map.MarkerIcons = {
	MARKER: new google.maps.MarkerImage(SolnaStad.themeBasePath + 'i/mapmarker.png',
		// Size
		new google.maps.Size(32, 42),
		// Origin
		new google.maps.Point(0,0),
		// Anchor
		new google.maps.Point(16, 42)
	),
	SHADOW: new google.maps.MarkerImage(SolnaStad.themeBasePath + 'i/mapmarkershadow.png',
		// Size
		new google.maps.Size(51, 42),
		// Origin
		new google.maps.Point(0,0),
		// Anchor
		new google.maps.Point(16, 42)
	)
};
