;var Maps = {
	_map: false,
	_hasPanorama: false,
	
	createAddingMap: function(elem) {
		this._map = new GMap2(elem);
		this._map.setCenter(new GLatLng(63.221782,14.602227), 3);
		this._map.enableDoubleClickZoom();
		this._map.enableScrollWheelZoom();
	},
	create: function(elem, options) {
		if (options && options.position)
			position = options.position;
		else
			position = {lat: 63.1792, lng: 14.6357};
		
		if (options && options.zoom)
			zoom     = options.zoom;
		else
			zoom     = 4;
		
		this._map = new GMap2(elem);
		this._map.setCenter(new GLatLng(position.lat, position.lng), zoom);
		this._map.enableDoubleClickZoom();
		this._map.enableScrollWheelZoom();
	},
	createPanorama: function(elem, info) {
		this._hasPanorama = true;
		
		var panorama = new GStreetviewPanorama(elem, {
			'latlng': new GLatLng(info.lat, info.lng),
			'features': {
				streetView: true,
				userPhotos: false
			},
			'enableFullScreen': false
		});
		GEvent.addListener(panorama, 'error', handleError);
		
		var me = this;
		function handleError(err) {
			me._hasPanorama = false;
			
			me.create(elem, {
				position: {
					lat: info.lat,
					lng: info.lng
				},
				zoom: 15
			});
			me.setMarkers(bars);
			me._map.setMapType(G_SATELLITE_MAP);
			(function() { $('maptype-satellite').fireEvent('click', $('maptype-satellite')); me._map.setZoom(17); }).delay(1200);
		}
	},
	createControls: function(elem, info) {
		var controlBuffer = '<ul><li><a id="maptype-map" href="javascript:void(0);"><img src="/images/maps/map.map-active.png" /></a></li><li><img src="/images/maps/map.type-separator.png" /></li><li><a id="maptype-satellite" href="javascript:void(0);"><img src="/images/maps/map.satellite';
		
		if (this._hasPanorama) {
			controlBuffer  = controlBuffer.replace(/map\.map-active/g, 'map.map-inactive');
			controlBuffer += '-inactive" /></a></li><li><img src="/images/maps/map.type-separator.png" /></li><li><a id="maptype-streetview" href="javascript:void(0);"><img src="/images/maps/map.streetview-active.png" />';
		} else {
			controlBuffer += '-end-inactive" />';
		}
		
		controlBuffer += '</a></li></ul>';

		var container = new Element('div')
			.setStyles({
				position: 'absolute',
				top: '15px',
				right: '12px'
			})
			.inject($('mapPositioner'));
		
		container.set('html', controlBuffer);
		
		var me = this;
		$('maptype-map').addEvent('click', function(e) {
			new Event(e).stop();
			
			$('zoom-controller').setStyle('display', 'block');
			
			this.getElement('img').set('src', '/images/maps/map.map-active.png');
			if (me._hasPanorama) {
				$('maptype-satellite').getElement('img').set('src', '/images/maps/map.satellite-inactive.png');
				$('maptype-streetview').getElement('img').set('src', '/images/maps/map.streetview-inactive.png');
				
				elem.empty();
				me.create(elem, {
					position: {
						lat: info[0].lat,
						lng: info[0].lng
					},
					zoom: 14
				});
				me.setMarkers(info);
			} else {
				$('maptype-satellite').getElement('img').set('src', '/images/maps/map.satellite-end-inactive.png');
				me._map.setMapType(G_NORMAL_MAP);
			}
		});
		$('maptype-satellite').addEvent('click', function(e) {
			new Event(e).stop();
			
			$('zoom-controller').setStyle('display', 'block');
			
			$('maptype-map').getElement('img').set('src', '/images/maps/map.map-inactive.png');
			if (me._hasPanorama) {
				this.getElement('img').set('src', '/images/maps/map.satellite-active.png');
				$('maptype-streetview').getElement('img').set('src', '/images/maps/map.streetview-inactive.png');
				
				elem.empty();
				me.create(elem, {
					position: {
						lat: info[0].lat,
						lng: info[0].lng
					},
					zoom: 14
				});
				me.setMarkers(info);
			} else {
				this.getElement('img').set('src', '/images/maps/map.satellite-end-active.png');
			}
			me._map.setMapType(G_SATELLITE_MAP);
		});
		if (this._hasPanorama) {
			$('maptype-streetview').addEvent('click', function(e) {
				new Event(e).stop();
				
				$('zoom-controller').setStyle('display', 'none');
				
				this.getElement('img').set('src', '/images/maps/map.streetview-active.png');
				$('maptype-map').getElement('img').set('src', '/images/maps/map.map-inactive.png');
				$('maptype-satellite').getElement('img').set('src', '/images/maps/map.satellite-inactive.png');
				
				elem.empty();
				me.createPanorama(elem, info[0]);
			});
		}
		
		controlBuffer = '<ul><li><a id="map-zoomin" href="javascript:void(0);"><img src="/images/maps/map.zoom-in.png" /></a></li><li><img src="/images/maps/map.type-separator.png" /></li><li><img src="/images/maps/map.zoom.png" /></li><li><img src="/images/maps/map.type-separator.png" /></li><li><a id="map-zoomout" href="javascript:void(0);"><img src="/images/maps/map.zoom-out.png" /></a></li></ul>';

		var zooms = new Element('div')
			.set('id', 'zoom-controller')
			.setStyles({
				position: 'absolute',
				top: '15px',
				left: '12px'
			})
			.inject($('mapPositioner'));
		
		zooms.set('html', controlBuffer);
		
		$('map-zoomin').addEvents({
			'mousedown': function() {
				this.getElement('img').set('src', '/images/maps/map.zoom-in-click.png');
			},
			'mouseup': function() {
				this.getElement('img').set('src', '/images/maps/map.zoom-in.png');
			},
			'click': function(e) {
				new Event(e).stop();
				
				me._map.setZoom(me._map.getZoom() + 1);
			}
		});
		$('map-zoomout').addEvents({
			'mousedown': function() {
				this.getElement('img').set('src', '/images/maps/map.zoom-out-click.png');
			},
			'mouseup': function() {
				this.getElement('img').set('src', '/images/maps/map.zoom-out.png');
			},
			'click': function(e) {
				new Event(e).stop();
				
				me._map.setZoom(me._map.getZoom() - 1);
			}
		});
		
		if (this._hasPanorama)
			$('zoom-controller').setStyle('display', 'none');
	},
	_createMarker: function(position, options) {
		if (!options)
			options     = {};
		
		var pinIcon        = new GIcon();
		if (options.slug && options.slug == 'sia-glass-fabriksforsaljning') {
			pinIcon.image      = "/images/maps/pin.sloinge.png";
			pinIcon.shadow     = "/images/maps/pin.sloinge-shadow.png";
		} else {
			pinIcon.image      = "/images/maps/pin.png";
			pinIcon.shadow     = "/images/maps/pin.shadow.png";
		}
		pinIcon.iconSize   = new GSize(35, 42);
		pinIcon.iconAnchor = new GPoint(10, 27);
		
		var marker         = new GMarker(position, {
			icon: pinIcon,
			draggable: false,
			zIndexProcess: (options.zIndexProcess) ? options.zIndexProcess : 0
		});
		
		if (options.overlay) {
			GEvent.addListener(marker, 'mouseover', function() {
				options.overlay.draw(marker);
			});
			GEvent.addListener(marker, 'mouseout', function() {
				options.overlay.remove();
			});
		}
		if (options.infoWindow) {
			var _isOpen = false;
			GEvent.addListener(marker, 'click', function() {
				if (_isOpen) {
					options.infoWindow.close();
					_isOpen = false;
				} else {
					options.infoWindow.toggle();
					_isOpen = true;
				}
			});
		} else if (options.event) {
			GEvent.addListener(marker, 'click', options.event);
		}
		
		return marker;
	},
	_createInfoWindow: function(title, description, url, address, phone) {
		var moreInfo = '';
		if (address && phone)
			moreInfo = '<p style="font-style: italic; line-height: 12px;">' + address + '<br />Tel: ' + phone + '</p>';
		else
			moreInfo = '<p style="line-height: 12px;"> <br /> </p>';
		
		moreInfo += '<div style="position: absolute; bottom: 10px; right: 10px;"><a href="' + url + '" style="color: #4a8ad4; text-decoration: underline;">Mer information</a></div>';
		
		var outer = new Element('div');
		outer.set('html', '<div class="info-window-top"></div><div class="info-window-middle"><h2><a href="' + url + '">' + title + '</a></h2><p>' + description + '</p>' + moreInfo + '</div><div class="info-window-bottom"></div>');
		
		return outer;
	},
	setMarkers: function(markers, options) {
		var me = this;
		var bounds = new GLatLngBounds();
		
		$each(markers, function(element) {
			bounds.extend(new GLatLng(element.lat, element.lng));
			
			var marker = me._createMarker(new GLatLng(element.lat, element.lng), {
				slug: element.slug,
				overlay: new Overlay(me._map, element.name, 'overlay').adjust(new OverlayPosition(15, -45)),
				zIndexProcess: (element.slug == 'sia-glass-fabriksforsaljning') ? function() { return 1001; } : false,
				event: function() {
					me._map.setZoom(14);
					
					var n = new InfoWindow(me._map, me._createInfoWindow(element.name, element.description, '/' + element.county + '/' + element.slug, element.address + ', ' + element.city, element.phone), {
						cssClass: 'info-window',
						panTo: new GLatLng(element.lat, element.lng)
					});
					n.toggle();
				}
			});
			
			if ($defined($('searched'))) {
				if (element.slug != 'sia-glass-fabriksforsaljning') {
					$('searched').getElement('.bar-' + element.slug).addEvents({
						mouseenter: function() {
							if (options && options.highlight) {
								marker.setImage('/images/maps/pin.alert.png');
							}
						},
						mouseleave: function() {
							if (options && options.highlight) {
								marker.setImage('/images/maps/pin.png');
							}
						}
					});
				}
			}
			
			me._map.addOverlay(marker);
		});
		
		if (markers.length > 1 && !$('content').getElement('.right').hasClass('start')) {
			me._map.setCenter(bounds.getCenter());
			me._map.setZoom(me._map.getBoundsZoomLevel(bounds) - 1);
		} else if (markers.length == 1) {
			me._map.setCenter(bounds.getCenter());
			me._map.setZoom(12);
		}
	}
};
