/**
 * CVS $Id: defaultMap.js,v 1.62 2010/06/11 14:29:39 marbjo Exp $
 */

// Couldn't place these within PlatsR_map as this.scalesAll isn't available
// until the object is completely initialized and they are needed in
// PlatsR_map.mapInit()
var	scalesAll = [16000000,12000000,4000000,2000000,1000000,500000,250000,75000,20000,10000,5000];
var scalesWFS = [500000,250000,75000,20000,10000,5000];
var scalesPlace = [250000,75000,20000,10000,5000];
var maxSearchZoom = 7;

var wgs84 = new OpenLayers.Projection("WGS84");
var sweref99 = new OpenLayers.Projection("EPSG:3006");
 
var PlatsR_map = {

    map: null,
    drawControls: null,
    mapSize: '',
    OBJECT_ZOOM_SMALL: 0,
    OBJECT_ZOOM_LARGE: 8,
	addPlaceTitle: '',
	movingFeature: null,
	moveToXReceiver: null,
	moveToYReceiver: null,
	openPopup: null,
	objectPopups: {},
	objectId: null,
	bottomLeftWgs84: null,
	topRightWgs84: null,
	
	bounds: new OpenLayers.Bounds(200000, 6100000, 1000000, 7700000),
	bounds_ext: new OpenLayers.Bounds(-2238400, 5287200, 3438400, 8512800),
	origin: "200000, 6100000",
	origin_ext: "-2238400, 5287200",

	options_large: {
		controls: [
	        new OpenLayers.Control.PanZoomBar(),
	        new OpenLayers.Control.Navigation(),
	        new OpenLayers.Control.OverviewMap( {
	        			size: new OpenLayers.Size(100, 90)
	        		})
	    ],
	    projection: "EPSG:3006",
	    units: "m",
	    scales: scalesAll
	},
	
	options_medium: {
	    controls: [
	       new OpenLayers.Control.Navigation()
	    ],
	    projection: "EPSG:3006",
	    units: "m",
	    scales: scalesAll
	},

	options_small: {
	    controls: [
	       new OpenLayers.Control.Navigation()
	    ],
	    projection: "EPSG:3006",
	    units: "m",
	    scales: scalesPlace
	},

	mapInit: function(mapSize, objectId, searchHits, x, y) {
		var options;
		this.mapSize = mapSize;
		this.objectId = objectId;
		switch(mapSize) {
			case 'SMALL_MAP':
				options = this.options_small;
				break;
			case 'MEDIUM_MAP':
				options = this.options_medium;
				break;
			default:
				options = this.options_large;
				break;
		}
	    this.map = new OpenLayers.Map('map', options);
	    OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
	    OpenLayers.DOTS_PER_INCH = 96;
	    //OpenLayers.ProxyHost= "/proxyservlet/proxy.cgi?url=";
	    
	    var sverige = new OpenLayers.Layer.WMS(
	        "Sverige",
	        wmsUrls,
	        {
	            srs: "EPSG:3006",
	            layers: wmsLayers,
	            styles: "",
	            format: "image/png",
	            tiled: "true",
	            tilesOrigin: PlatsR_map.origin_ext,
	            isBaseLayer: "true"
	        },
	        {
	            maxExtent: PlatsR_map.bounds_ext,
	            projection: "EPSG:3006",
	            buffer: 0
	        }
	    );
	    this.map.addLayer(sverige);

		var placeStyle = new OpenLayers.Style(
			{
				graphicWidth: 31, 
				graphicHeight: 44,
				graphicYOffset: -40,
				fillOpacity: 1,
				externalGraphic: "/platsr/img/icon/map/pin.png"
			}, 
			{
			}
		);

		var selectedStyle = new OpenLayers.Style(
			{
				graphicWidth: 31, 
				graphicHeight: 44,
				graphicYOffset: -40,
				fillOpacity: 1,
				externalGraphic: "/platsr/img/icon/map/pin-selected.png"
			}, 
			{
			}
		);
		
		var filter = null;

		if(searchHits == null && (objectId == null || objectId < 0)) {
			// Include cluster layers only when no search hits to highlight
			this.addClusterLayer({
				name: "PlatsR kluster 16M", 
				featureType: "GEOCLUSTER_16M",
				scales: [16000000]
			});
			this.addClusterLayer({
				name: "PlatsR kluster 12M", 
				featureType: "GEOCLUSTER_12M",
				scales: [12000000]
			});
			this.addClusterLayer({
				name: "PlatsR kluster 4M", 
				featureType: "GEOCLUSTER_4M",
				scales: [4000000]
			});
			this.addClusterLayer({
				name: "PlatsR kluster 2M", 
				featureType: "GEOCLUSTER_2M",
				scales: [2000000]
			});
			this.addClusterLayer({
				name: "PlatsR kluster 1M", 
				featureType: "GEOCLUSTER_1M",
				scales: [1000000]
			});
		} 

		if(searchHits != null) {
			filter = new OpenLayers.Filter.Logical({
				type: OpenLayers.Filter.Logical.OR
			});
			var filters = new Array();
			var index = 0;
			var objId;
			for(objId in searchHits) {
				filters[index] = new OpenLayers.Filter.Comparison({
					type: OpenLayers.Filter.Comparison.EQUAL_TO,
					property: "OBJEKT_ID",
					value: searchHits[objId]
				});
				index++;
			}
			filter.filters = filters;
		}
		
		var platsr_wfs = new OpenLayers.Layer.Vector(
			"PlatsR",
			{
				strategies: [new OpenLayers.Strategy.BBOX()],
				protocol: new OpenLayers.Protocol.WFS(
					{
						url: wfsUrl,
						featureType: "PLATSR_WMS",
						featureNS: "http://raa.se/KMS/PLATSR",
						featurePrefix: "PLATSR",
						geometryName: "GEOMETRI"
					}
				),
				filter: filter,
				extractAttributes: true,
				maxExtent: PlatsR_map.bounds,
				projection: "EPSG:3006",
				scales: (searchHits == null) ? scalesWFS : scalesAll,
				styleMap: new OpenLayers.StyleMap(
					{
	                    "default": (filter == null) ? placeStyle : selectedStyle,
	                    "select": selectedStyle
					}
				)
			}
		);

		if(searchHits != null || objectId == null || objectId < 0) {
			this.map.addLayer(platsr_wfs);
		}

		var platsr_edit = new OpenLayers.Layer.Vector(
				"PlatsREdit",
				{
					maxExtent: PlatsR_map.bounds,
					projection: "EPSG:3006",
					scales: scalesAll,
					styleMap: new OpenLayers.StyleMap(
							{
								"default": selectedStyle
							}
					)
				}
		);
		this.map.addLayer(platsr_edit);
		
		var selectControl = new OpenLayers.Control.SelectFeature(
				platsr_wfs,
				{
					onSelect: function(feature) {
						PlatsR_map.onSelectPlace(feature);
					}
				}
		);
		this.map.addControl(selectControl);
		selectControl.activate();
	
		drawControls = {
			/* 
			 * Currently not used.
			 * 
	        addPlace: new OpenLayers.Control.DrawFeature(
	            platsr_edit, 
	            OpenLayers.Handler.Point,
	            {
	            	featureAdded: function(feature) {
	            		PlatsR_map.onAddPlace(feature);
	            	},
	            	cursor: 'crosshair'
	            }
	        ),
	        */
	        movePlace: new OpenLayers.Control.DrawFeature(
	        	platsr_edit, 
	            OpenLayers.Handler.Point,
	            {
	            	featureAdded: function(feature) {
	            		PlatsR_map.onMovePlace(feature);
	            	},
	            	cursor: 'crosshair'
	            }
	        )
		};
	
	    for(var key in drawControls) {
	        PlatsR_map.map.addControl(drawControls[key]);
	    }

		this.map.events.register("moveend", this.map, function() {
			if (PlatsR_map.movingFeature) {
				var layer = PlatsR_map.map.getLayersByName("PlatsREdit")[0];
				var screenBounds = layer.getExtent();
				var featureBounds = PlatsR_map.movingFeature.geometry.getBounds();
				if (screenBounds.intersectsBounds(featureBounds)) {
					if(!PlatsR_map.getFeatureByLayerAndFeatureId(layer.id, PlatsR_map.movingFeature.id)) {
						layer.addFeatures([PlatsR_map.movingFeature]);
					}
				}
			}
			if (PlatsR_map.mapSize == 'FULL_MAP' && PlatsR_map.map.getScale() < 100000) {
				PlatsR_map.searchStats(PlatsR_map.map.getExtent());
			} else {
				$(".ksamsoksection").hide();
				$(".tagsection").removeClass("tagsectionLeft");				
			}
		});
		
		/*
		 * NB.
		 * Features are destroyed when zooming, popups are not - thus popups 
		 * will become orphans after zoom. If popups need to be reassigned to
		 * new features (corresponding to the same object) register this listener: 
		 */
		// platsr_wfs.events.register("featureadded", this, PlatsR_map.reassignObjectPopup);

		if(searchHits != null) {
			platsr_wfs.events.register("loadend", platsr_wfs, PlatsR_map.zoomToSearchResult);
		} 
		
		if(objectId >= 0) {
			if (x != null) {
				this.mapCenter(x, y, objectId);
				var objectFeature = new OpenLayers.Feature.Vector(
						new OpenLayers.Geometry.Point(x,y), selectedStyle);				
				this.movingFeature = objectFeature;
				platsr_edit.addFeatures([objectFeature]);
			}
		}

		if (!this.mapZoomByPageParameters() && x == null) {
			this.map.zoomToMaxExtent();
		}
		
		if((mapSize == 'LARGE_MAP') || (mapSize == 'FULL_MAP')) {
			var overviewMap = this.map.getControlsByClass("OpenLayers.Control.OverviewMap")[0];
			overviewMap.maximizeControl();
		}
	},

	// ##################### POPUPS ##########################

	addPopup: function(popupId, feature, html, closeFunction, isObjectPopup) {
		if(feature != null) {
			PlatsR_map.closePopup(this.openPopup);
			var popup = new OpenLayers.Popup.FramedCloud(
					popupId,
					new OpenLayers.LonLat(feature.geometry.x, feature.geometry.y),
					new OpenLayers.Size(200, 200),
					html,
					null,
					true,
		            function() {PlatsR_map.closePopup(popup); feature.popup = null; closeFunction();}
				);
			feature.popup = popup;
			if (isObjectPopup) {
				this.objectPopups[popup.id] = popup;
			}
			this.map.addPopup(popup);		
			this.openPopup = popup;
		}
	},
	
	closePopup: function(popup) {
		if (popup) {
			PlatsR_map.objectPopups[popup.id] = null;
			popup.destroy();
			popup = null;
		}
		this.openPopup = null;
	},	

	reassignObjectPopup: function(event) {
		var objectId = event.feature.attributes.OBJEKT_ID;
		var popup = PlatsR_map.objectPopups["popup." + objectId];
		if(popup) {
			event.feature.popup = popup;
		}
	},

	// TODO: Platsr future development: this function to input some initial
	// information in a popup before creating a place is not used - it can be 
	// removed together with any dependent functions that is used only for this
	// purpose.	
	/*
	 * Currently not used.
	 * 
	getAddPlaceHTML: function(feature) {
	    return '<form name="addPlaceNoSubmitForm" onsubmit="return false;">'
            + '    <label for="title">Titel</label>'
            + '    <input type="text" name="title" value="' + PlatsR_map.addPlaceTitle + '"/>'
	    	+ '</form>'
	    	+ '<form name="addPlaceForm" method="GET" action="/platsr/ny/plats" onSubmit="label.value=encodeURI(addPlaceNoSubmitForm.title.value)" accept-charset="UTF-8">'
            + '    <input type="hidden" name="label" value="">'
            + '    <input type="hidden" name="x" value="' + Math.round(feature.geometry.x) + '">'
            + '    <input type="hidden" name="y" value="' + Math.round(feature.geometry.y) + '">'
	    	+ '    <input type="submit" value="Skapa" />'
	    	+ '    <input type="button" value="Flytta position" onClick="PlatsR_map.onAddPlaceMove(addPlaceNoSubmitForm.title.value);" />'
	    	+ '</form>';
	},
	 */
	
	// ##################### EVENTS ##########################

	onSelectPlace: function(feature) {
		var popupId = "popup." + feature.attributes.OBJEKT_ID;
		var loadingHtml = PlatsR_REST.getPopupLoadingHtml();

		PlatsR_map.addPopup(popupId, feature, loadingHtml, function() {}, true);
		
		var popupHtml = PlatsR_REST.getPopupHtml(feature.attributes.OBJEKT_ID);
		this.openPopup.setContentHTML(popupHtml);		
	},

	onSelectCluster: function(feature) {
		var popupId = "popup." + feature.attributes.ID;
		var places = feature.attributes.CONTENT.split(";");
		var data;
		var popupHtml;
		if (feature.attributes.FCOUNT == "1") {
			var loadingHtml = PlatsR_REST.getPopupLoadingHtml();
			PlatsR_map.addPopup(popupId, feature, loadingHtml, function() {}, true);
			data = places[0].split("$");			
			var popupHtml = PlatsR_REST.getPopupHtml(data[0]);
			this.openPopup.setContentHTML(popupHtml);
		}
		else {
			popupHtml = 
				"<div class='mapClusterPopup'>" +
				"<div class='txtcontainer'>" +
				"<h1>" +
				feature.attributes.FCOUNT +
				" platser</h1>";
				
			if(parseInt(feature.attributes.FCOUNT) > 5) {
				popupHtml = popupHtml + "<p>Fem f&ouml;rsta platserna:</p>";
			}
			
			for(var ii = 0; ii < places.length; ii++) {
				data = places[ii].split("$");
				popupHtml = 
					popupHtml +
					"<a href='/platsr/visa/plats/id/" + data[0] + "'>" + data[1] + "</a><br/>" 
			}
			
			popupHtml = 
				popupHtml +
				"</div>" +
				"</div>";

			PlatsR_map.addPopup(popupId, feature, popupHtml, function() {}, true);
		}
	},

	/*
	 * Currently not used.
	 * 
	onAddPlace: function(feature) {
		PlatsR_map.movingFeature = feature;
		var popupId = "addplacepopup";
		var popupHtml = PlatsR_map.getAddPlaceHTML(feature);
		PlatsR_map.addPopup(popupId, feature, popupHtml, function() {PlatsR_map.onAddPlaceCancel()}, false);
	    PlatsR_map.activateControl('none');
	},
	
	onAddPlaceCancel: function() {
		PlatsR_map.closeFeature(PlatsR_map.movingFeature);
	    PlatsR_map.addPlaceTitle = '';
	    PlatsR_map.activateControl('none');
	},
	
	onAddPlaceMove: function(title) {
		PlatsR_map.closeFeature(PlatsR_map.movingFeature);
	    PlatsR_map.addPlaceTitle = title;
	    PlatsR_map.activateControl('addPlace');
	},
	*/
	
	onMovePlace: function(feature) {
		if (this.movingFeature) {
			PlatsR_map.closeFeature(this.movingFeature);
		}		
		this.movingFeature = feature;

		if (this.moveToXReceiver && this.moveToYReceiver) {
			this.moveToXReceiver.value = Math.round(feature.geometry.x);
			this.moveToYReceiver.value = Math.round(feature.geometry.y);
		}
	    PlatsR_map.activateControl('none');		
	},

	// ##################### MAP FUNCTIONS ##########################

	activateControl: function(name) {
        PlatsR_map.map.div.style.cursor = "";
	    for(key in drawControls) {
	        var control = drawControls[key];
	        if(name == key) {
	            control.activate();
	            if(control.cursor) {
	                PlatsR_map.map.div.style.cursor=control.cursor;
	            }
	        } else {
	            control.deactivate();
	        }
	    }
	},
	
	mapCenter: function(x, y, objectId) {
		this.objectId = objectId;
		var zoomLevel = (this.mapSize == 'SMALL_MAP') ? 
				this.OBJECT_ZOOM_SMALL : this.OBJECT_ZOOM_LARGE;
		PlatsR_map.map.setCenter(new OpenLayers.LonLat(x, y), zoomLevel, false, false);
	},
	
	zoomToSearchResult: function(event) {
		if (PlatsR_map.objectId == null) {
			// Don't change zoom if objectId is set (place presentation)
			var layers = PlatsR_map.map.getLayersByName("PlatsR");
			var dataExtent = layers[0].getDataExtent();
			if (dataExtent == null) {
				PlatsR_map.map.zoomToMaxExtent();
			}
			else {
				// Expand extent with some extra space for the map pins (ticket #2055).
				dataExtent.extend(new OpenLayers.LonLat(dataExtent.left * 1.01, dataExtent.bottom * 1.001))
				dataExtent.extend(new OpenLayers.LonLat(dataExtent.right * 1.01, dataExtent.top * 1.001))
				
				PlatsR_map.map.zoomToExtent(dataExtent, false);
			}
			if(PlatsR_map.map.getZoom() > maxSearchZoom) {
				PlatsR_map.map.zoomTo(maxSearchZoom);
			}
		}
	},

	closeFeature: function(feature) {
	    if (feature) {
	    	if (feature.popup) {
	    		PlatsR_map.closePopup(feature.popup);
	    	}
	    	if (PlatsR_map.movingFeature && feature.id == PlatsR_map.movingFeature.id) {
		    	PlatsR_map.movingFeature = null;
	    	}
	    	feature.destroy();
	    }
	}, 

	getFeatureByObjectId: function(objectId) {
		var ii;
		var layer = this.map.getLayersByName("PlatsR_map")[0];
		for(var ii in layer.features) {
			var feature = layer.features[ii];
			if(feature.attributes.OBJEKT_ID == objectId) {
				return feature;
			}
		}
		return null;
	},
	
	getFeatureByLayerAndFeatureId: function(layerId, featureId) {
		return PlatsR_map.map.getLayersBy('id',layerId)[0].getFeatureById(featureId);
	},
	
	mapZoomByPageParameters: function() {
		var isMapZoomed = false;		
		var qstr = location.search.replace(/\?/,"&");
		var pair = qstr.split("&");
		var extentString;
		var zoomLevel;
		for (var i=1; i<pair.length; i++) {
			var item = pair[i].split("=");
			if (item.length == 2) {
				if (item[0] == "mapExtent") {
					extentString = item[1];
				} else if (item[0] == "mapZoom") {
					zoomLevel = parseInt(item[1]);
				} 
			}
		}
		if (extentString) {
			if (extentString.contains("%2C")) {
				var bounds = extentString.split("%2C");
			} else {
				var bounds = extentString.split(",");
			}
			if (bounds.length == 4) {
				PlatsR_map.map.zoomToExtent(new OpenLayers.Bounds(bounds[0],bounds[1],bounds[2],bounds[3]));
				isMapZoomed = true;
			}
		} else if (zoomLevel) {
			PlatsR_map.map.zoomToMaxExtent();
			PlatsR_map.map.zoomTo(zoomLevel);
			isMapZoomed = true;
		}
		return isMapZoomed;
	},

	addClusterRule: function(rules, attrName, minCount, maxCount, iconUrl) {
		if (minCount == null) {
			rules.push(new OpenLayers.Rule({
				filter: new OpenLayers.Filter.Comparison({
					type: OpenLayers.Filter.Comparison.EQUAL_TO,
					property: attrName,
					value: maxCount
				}),
				symbolizer: {
					externalGraphic: iconUrl
				}
			}));
		}
		else if (maxCount == null) {
				rules.push(new OpenLayers.Rule({
					filter: new OpenLayers.Filter.Comparison({
						type: OpenLayers.Filter.Comparison.GREATER_THAN,
						property: attrName,
						value: minCount
					}),
					symbolizer: {
						externalGraphic: iconUrl
					}
				}));
		} else {
			var filter = new OpenLayers.Filter.Logical({
				type: OpenLayers.Filter.Logical.AND
			});
			
			filter.filters.push(new OpenLayers.Filter.Comparison({
				type: OpenLayers.Filter.Comparison.GREATER_THAN,
				property: attrName,
				value: minCount
			}));
			filter.filters.push(new OpenLayers.Filter.Comparison({
				type: OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO,
				property: attrName,
				value: maxCount
			}));
			
			rules.push(new OpenLayers.Rule({
				filter: filter,
				symbolizer: {
					externalGraphic: iconUrl
				}
			}));
		}
	},

	addClusterLayer: function(options) {
		var iconRules = [];
		this.addClusterRule(iconRules, "FCOUNT", null, 1, 
			"/platsr/img/icon/map/pin.png");
		this.addClusterRule(iconRules, "FCOUNT", 1, CLUSTER_SMALL_MAX, 
			"/platsr/img/icon/map/pin-cluster-small.png");
		this.addClusterRule(iconRules, "FCOUNT", CLUSTER_SMALL_MAX, CLUSTER_MEDIUM_MAX, 
			"/platsr/img/icon/map/pin-cluster-medium.png");
		this.addClusterRule(iconRules, "FCOUNT", CLUSTER_MEDIUM_MAX, null, 
			"/platsr/img/icon/map/pin-cluster-big.png");
		
		var clusterStyle = new OpenLayers.Style(
			{
				graphicWidth: 31, 
				graphicHeight: 44,
				graphicYOffset: -40,
				fillOpacity: 1
			}, 
			{
			}
		);

		clusterStyle.addRules(iconRules);

		var iconRules = [];
		this.addClusterRule(iconRules, "FCOUNT", null, 1, 
			"/platsr/img/icon/map/pin-selected.png");
		this.addClusterRule(iconRules, "FCOUNT", 1, CLUSTER_SMALL_MAX, 
			"/platsr/img/icon/map/pin-cluster-small-selected.png");
		this.addClusterRule(iconRules, "FCOUNT", CLUSTER_SMALL_MAX, CLUSTER_MEDIUM_MAX, 
			"/platsr/img/icon/map/pin-cluster-medium-selected.png");
		this.addClusterRule(iconRules, "FCOUNT", CLUSTER_MEDIUM_MAX, null, 
			"/platsr/img/icon/map/pin-cluster-big-selected.png");
		
		var selectedClusterStyle = new OpenLayers.Style(
			{
				graphicWidth: 31, 
				graphicHeight: 44,
				graphicYOffset: -40,
				fillOpacity: 1
			}, 
			{
			}
		);

		selectedClusterStyle.addRules(iconRules);
		
		var clusterStyleMap = new OpenLayers.StyleMap(
			{
                "default": clusterStyle,
                "select": selectedClusterStyle
			}
		);
		
		var clusterLayer = new OpenLayers.Layer.Vector(
			options.name,
			{
				strategies: [
					new OpenLayers.Strategy.BBOX()
				],
				protocol: new OpenLayers.Protocol.WFS(
					{
						url: wfsUrl,
						featureType: options.featureType,
						featureNS: "http://raa.se/KMS/PLATSR",
						featurePrefix: "PLATSR",
						geometryName: "GEOMETRY"
					}
				),
				extractAttributes: true,
				maxExtent: PlatsR_map.bounds,
				projection: "EPSG:3006",
				scales: options.scales,
				styleMap: clusterStyleMap
			}
		);
		this.map.addLayer(clusterLayer);
		
		var selectControl = new OpenLayers.Control.SelectFeature(
			clusterLayer,
			{
				onSelect: function(feature) {
					PlatsR_map.onSelectCluster(feature);
				}
			}
		);
		this.map.addControl(selectControl);
		selectControl.activate();
	},
	
	searchStats: function(extent) {
		var bottomLeft = new OpenLayers.Geometry.Point(extent.left, extent.bottom);
		var topRight = new OpenLayers.Geometry.Point(extent.right, extent.top);
		OpenLayers.Projection.transform(bottomLeft, sweref99, wgs84);
		OpenLayers.Projection.transform(topRight, sweref99, wgs84);
		PlatsR_map.bottomLeftWgs84 = bottomLeft;
		PlatsR_map.topRightWgs84 = topRight;
		var ksamsokUrl = "/ksamsok/api?x-api=platsr361&startRecord=1&query=boundingBox+%3D+%2FWGS84%22" +
			bottomLeft.x + "+" + bottomLeft.y + "+" + topRight.x + "+" + topRight.y + "%22&method=search&hitsPerPage=1";
		jQuery.get(ksamsokUrl, null, PlatsR_map.updateSearchStats, "text/xml");
	},
	
	updateSearchStats: function(data, textStatus) {
		var totalHits = null;
		if(textStatus == "success") {
			var xmlParser = new OpenLayers.Format.XML();
			var xmlData = xmlParser.read(data);
			var totalHitsElem = xmlParser.getElementsByTagNameNS(xmlData, "", "totalHits");
			if(totalHitsElem != null) {
				totalHits = totalHitsElem[0].firstChild.data;
			}
		}
		if(totalHits != null && totalHits != "0") {
			$(".ksamsoksection #count").text(totalHits);	
			$(".ksamsoksection").show();
			$(".tagsection").addClass("tagsectionLeft");
			var bbox = PlatsR_map.bottomLeftWgs84.x + "," 
						+ PlatsR_map.bottomLeftWgs84.y + "," 
						+ PlatsR_map.topRightWgs84.x + "," 
						+ PlatsR_map.topRightWgs84.y;
			$("#archiveLink").attr({href: $("#kringlaBboxUrl").val() + bbox });
			$("#helpLink").attr({href: "/platsr/sok/typ/arkiv/omrade/" + bbox });
		} else {
			$(".ksamsoksection").hide();
			$(".tagsection").removeClass("tagsectionLeft");
		}				
	}
	
}