// bMAP v1.0
// Copyright Byte Brigade AB 2008
// Author: Jonas Lindstrand

var bMAP_MapTileBaseOBJ;
var bMAP_DragAreaOBJ;
var bMAP_IconBaseOBJ;
var bMAP_BaseNODE;
var bMAP_LabelBaseOBJ;

var bMAP_dragStat = false;
var bMAP_start_mouse_x;
var bMAP_start_mouse_y;
var bMAP_start_div_x;
var bMAP_start_div_y;

var bMAP_Positions = new Array();
bMAP_Positions["bMAP_MapTileBase"] = new Array();
bMAP_Positions["bMAP_MapTileBase"].left = 0;
bMAP_Positions["bMAP_MapTileBase"].top = 0;

var bMAP_frameWidth;
var bMAP_frameHeight;
var bMAP_tileSizeWidth;
var bMAP_tileSizeHeight;
var bMAP_TileScale;
var bMAP_TileImgSuffix;

var bMAP_overflowTiles = 1; //Minst detta antal

var bMAP_grid;
var bMAP_TilePositions;
var bMAP_IconPositions;

var bMAP_currentScaleid;
var bMAP_zoomDirection = "in";

function bMAP_init() {
	//if(!bMAP_DragAreaOBJ) { bMAP_DragAreaOBJ = document.getElementById('bMAP-DragArea'); }
	
	bMAP_DragAreaOBJ.onmousemove = bMAP_drag;
	if(!document.all) { 
		bMAP_DragAreaOBJ.onmousedown = bMAP_dragStart;
	}
	document.onmouseup   = bMAP_dragEnd;
	//if(scroll_timer) { clearTimeout(scroll_timer); }
	
	//bMAP_DragAreaOBJ.onselectstart = function(e) { bMAP_stopEvent(e); return false; }
   	//bMAP_DragAreaOBJ.oncontextmenu = function(e) { bMAP_stopEvent(e); return false; }
   	//bMAP_DragAreaOBJ.ondragstart = function(e) { bMAP_stopEvent(e); return false; }
}
function bMAP_stopEvent(e) {
	//e.stopPropagation();
	if (e && e.stopPropagation && e.preventDefault) {
		e.stopPropagation();
		e.preventDefault();
	}
	else if (window.event) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;		
	}
}


//------------------------------------------------------------------------------

//http://www.bilpool.se/maps/display_map_location_frames.asp?map_locationid=1&x=16279408&y=65796260&scale=8&image_size=200x200

function bMAP_launchMap(frameWidth, frameHeight, startPointCoordX, startPointCoordY, scale, image_size, map_locationid) { //image_size används inte längre
	
	
	if(!bMAP_BaseNODE) { // Första gången kartan laddas
		var bMAP_NODE = document.getElementById('bMAP');
		
		// Skapar map base
		bMAP_BaseNODE = document.createElement("div");
			bMAP_BaseNODE.setAttribute('id','bMAP-Base');
		bMAP_NODE.appendChild(bMAP_BaseNODE);
		
		// Skapar label base
		bMAP_LabelBaseOBJ = document.createElement("div");
			bMAP_LabelBaseOBJ.setAttribute('id','bMAP-LabelBase');
		bMAP_NODE.appendChild(bMAP_LabelBaseOBJ);
		
		/*
		//Skapar förklaringsruta
		bMAP_mapInfoBaseOBJ = document.createElement("div");
			bMAP_mapInfoBaseOBJ.setAttribute('id','bMAP-mapInfoBase');
			bMAP_mapInfoBaseOBJ.innerHTML = "Information om kartan";
		bMAP_NODE.appendChild(bMAP_mapInfoBaseOBJ);
		*/
	}
	//bMAP_BaseNODE = document.getElementById('bMAP-Base');
	
	
	// Tömmer kartan
	for(var i = bMAP_BaseNODE.childNodes.length-1; i >= 0; i--) {
		bMAP_BaseNODE.removeChild(bMAP_BaseNODE.childNodes[i]);
	}
	
	var bMAP_LoadingLabel = document.createElement("div");
		bMAP_LoadingLabel.setAttribute('id','bMAP-LoadingLabel');
		bMAP_LoadingLabel.innerHTML = "Laddar karta...";
	bMAP_BaseNODE.appendChild(bMAP_LoadingLabel);
	
	/*
	if(image_size.indexOf("x") != -1) {
		var imageSizeParts = image_size.split("x");
		var tileSizeWidth = imageSizeParts[0];
		var tileSizeHeight = imageSizeParts[1];
	}
	else {
		var tileSizeWidth = 250;
		var tileSizeHeight = 250;
	}
	*/
	
	var xmlHttpA=null; // Defines that xmlHttp is a new variable.
	 // Try to get the right object for different browser
	 try {
	    // Firefox, Opera 8.0+, Safari
	    xmlHttpA = new XMLHttpRequest(); // xmlHttp is now a XMLHttpRequest.
	 } catch (e) {
	    // Internet Explorer
	    try {
	       xmlHttpA=new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	       xmlHttpA=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	 }
	 xmlHttpA.onreadystatechange = function() {
	    if (xmlHttpA.readyState == 4)
	       try { // In some instance, status cannot be retrieve and will produce an error (ex: Port is not responsive)
	          if (xmlHttpA.status == 200) {
	             //Set the main HTML of the body to the info provided by the AJAX Request
	             //document.getElementById("ajax_output").innerHTML = xmlHttp.responseText;
	             
	             	//alert(xmlHttp.responseText);
	             	
	             	var mapStartData = xmlHttpA.responseText.split("¤");
	             	//alert(frameWidth +", " + frameHeight +", " + mapStartData[0] +", " + mapStartData[1] +", " + startPointCoordX +", " + startPointCoordY +", " + mapStartData[2] +", " + mapStartData[3] +", " + mapStartData[4] +", " + mapStartData[5])
	             	bMAP_drawMap(frameWidth, frameHeight, mapStartData[0], mapStartData[1], startPointCoordX, startPointCoordY, parseFloat(mapStartData[2]), parseFloat(mapStartData[3]), parseFloat(mapStartData[4]), parseFloat(mapStartData[5]));
	             	
	             	bMAP_currentScaleid = parseFloat(mapStartData[6]);
	          }
	          // alert(xmlHttp.status);
	          
	       } catch (e) {
	            //document.getElementById("ajax_output").innerHTML = "Error on Ajax return call : " + e.description;
	          //alert("Error on Ajax return call : " + e.description);
	            //¤ return "Error on Ajax return call : " + e.description;
	            //alert(5);
	       }
	
	 }
	 //alert("/bMAP/getMapStartData.aspx?x=" + startPointCoordX + "&y=" + startPointCoordY + "&scale=" + scale + "&imageSize=" + image_size)
	 xmlHttpA.open("get", "/bMAP/getMapStartData.aspx?x=" + startPointCoordX + "&y=" + startPointCoordY + "&scale=" + scale + "&zoomDirection=" + bMAP_zoomDirection); //  + "&imageSize=" + image_size
	 xmlHttpA.send(null); // Since there is no supplied form, null takes its place as a new form.
	
}

//------------------------------------------------------------------------------

function bMAP_zoomIn() {
	bMAP_zoomDirection = "in";
	
	if(bMAP_currentScaleid > 0) {
		
		// Tömmer kartan
		//var bMAP_BaseNODE = document.getElementById('bMAP-Base');
		//for(var i = bMAP_BaseNODE.childNodes.length-1; i >= 0; i--) {
		//	bMAP_BaseNODE.removeChild(bMAP_BaseNODE.childNodes[i]);
		//}
		
		//13973905&y=62128325
		var currentCenterCoords = bMAP_getCurrentCenterCoords();
		bMAP_launchMap(bMAP_frameWidth, bMAP_frameHeight, currentCenterCoords["x"], currentCenterCoords["y"], (bMAP_currentScaleid-1), -2, -2)
		
	}
}

function bMAP_zoomOut() {
	bMAP_zoomDirection = "out";
	
	//if(bMAP_currentScaleid > 0) {
		
		// Tömmer kartan
		//var bMAP_BaseNODE = document.getElementById('bMAP-Base');
		//for(var i = bMAP_BaseNODE.childNodes.length-1; i >= 0; i--) {
		//	bMAP_BaseNODE.removeChild(bMAP_BaseNODE.childNodes[i]);
		//}
		
		
		//13973905&y=62128325
		var currentCenterCoords = bMAP_getCurrentCenterCoords();
		bMAP_launchMap(bMAP_frameWidth, bMAP_frameHeight, currentCenterCoords["x"], currentCenterCoords["y"], (bMAP_currentScaleid+1), -2, -2)
		
	//}
}

function bMAP_getCurrentCenterCoords() {
	// Räknar ut kartans nuvarande fokuspunkts koordinater
	
	var frameMidPointX = Math.round(bMAP_frameWidth/2);
	var frameMidPointY = Math.round(bMAP_frameHeight/2);
	
	var x = (bMAP_Positions["bMAP_MapTileBase"].left * -1) + frameMidPointX;
	var y = (bMAP_Positions["bMAP_MapTileBase"].top * -1) + frameMidPointY;
	
	var xCoord = bMAP_grid.topLeftCoordX + (x * bMAP_TileScale);
	var yCoord = bMAP_grid.topLeftCoordY - (y * bMAP_TileScale);
	
	var currentCenterCoords = new Array();
	
	currentCenterCoords["x"] = xCoord; //12711500;
	currentCenterCoords["y"] = yCoord; //64046190;
	
	return currentCenterCoords;
}

//------------------------------------------------------------------------------

function bMAP_drawMap(frameWidth, frameHeight, tileScale, imgSuffix, startPointCoordX, startPointCoordY, startPointTileTopLeftCoordX, startPointTileBottomLeftCoordY, tileSizeWidth, tileSizeHeight) {
	
	bMAP_frameWidth = frameWidth;
	bMAP_frameHeight = frameHeight;
	
	bMAP_tileSizeWidth = tileSizeWidth;
	bMAP_tileSizeHeight = tileSizeHeight;
	
	bMAP_TileScale = tileScale;
	bMAP_TileImgSuffix = imgSuffix;
	
	
	var startPointTileTopLeftCoordY = startPointTileBottomLeftCoordY + (bMAP_tileSizeHeight * bMAP_TileScale);
	
	//Mittpunkten i kartframen (px):
	var frameMidPointX = Math.round(bMAP_frameWidth/2);
	var frameMidPointY = Math.round(bMAP_frameHeight/2);
	
	//startKoordinatens placering i sin kartbild (Delta px)
	var startPointTileDeltaPxX = Math.round((startPointCoordX - startPointTileTopLeftCoordX) / bMAP_TileScale);
	var startPointTileDeltaPxY = Math.round((startPointTileTopLeftCoordY - startPointCoordY) / bMAP_TileScale);
	
	//alert("startPointCoordX: " + startPointCoordX + "\nstartPointTileTopLeftCoordX: " + startPointTileTopLeftCoordX + "\nstartPointCoordY: " + startPointCoordY + "\nstartPointTileTopLeftCoordY: " + startPointTileTopLeftCoordY);
	
	
	// träffTilens interna position i framen
	var centerTileLeft = frameMidPointX - startPointTileDeltaPxX;
	var centerTileTop = frameMidPointY - startPointTileDeltaPxY;
	
	//alert("frameMidPointX: " + frameMidPointX + "\nframeMidPointY: " + frameMidPointY + "\nstartPointTileDeltaPxX: " + startPointTileDeltaPxX + "\nstartPointTileDeltaPxY: " + startPointTileDeltaPxY);
	//alert("centerTileLeft: " + centerTileLeft + "\ncenterTileTop: " + centerTileTop);
	
	//backa uppåt och åt vänster tills vi överlappar med minst bMAP_overflowTiles
	var tilesLeft = Math.ceil(centerTileLeft/bMAP_tileSizeWidth) + bMAP_overflowTiles;
	var tilesUp = Math.ceil(centerTileTop/bMAP_tileSizeHeight) + bMAP_overflowTiles;
	//alert(" tilesLeft: " + tilesLeft + "\n tilesUp: " + tilesUp);
	
	
	/*
	bMAP-Base
	append <div id="bMAP-MapTileBase">
		append <div id="bMAP-MapTile_0" class="bMAP-MapTile" style="top:0px;left:0px;background-image:url(/maps/sc316/x161/y659/16138230x65915340x200x200x316.mgif);"></div>
	*/
	
	
	
	
	// Justering av startläge så att mapPoint hamnar i mitten
	//var mapTileBaseStartPositionX = frameMidPointX - ((tilesLeft * bMAP_tileSizeWidth) + centerTileLeft);
	//var mapTileBaseStartPositionY = frameMidPointY - ((tilesUp * bMAP_tileSizeHeight) + centerTileTop);
	//alert(" mapTileBaseStartPositionX: " + mapTileBaseStartPositionX + "\n mapTileBaseStartPositionY: " + mapTileBaseStartPositionY);
	//bMAP_Positions["bMAP_MapTileBase"].left = frameMidPointX - ((tilesLeft * bMAP_tileSizeWidth) + centerTileLeft);
	//bMAP_Positions["bMAP_MapTileBase"].top = frameMidPointY - ((tilesUp * bMAP_tileSizeHeight) + centerTileTop);
	
	bMAP_Positions["bMAP_MapTileBase"].left = centerTileLeft - (tilesLeft * bMAP_tileSizeWidth);
	bMAP_Positions["bMAP_MapTileBase"].top = centerTileTop - (tilesUp * bMAP_tileSizeHeight);
	
	//alert(frameMidPointX + " - " + centerTileLeft + " - (" + tilesLeft + " * " + bMAP_tileSizeWidth + ")");
	//alert(centerTileLeft + " - (" + tilesLeft + " * " + bMAP_tileSizeWidth + ")");
	//alert(centerTileTop + " - (" + tilesUp + " * " + bMAP_tileSizeHeight + ")");
	
	//var mapTileBaseStartPositionX = frameMidPointX - ((tilesLeft * bMAP_tileSizeWidth) + centerTileLeft);
	//var mapTileBaseStartPositionY = frameMidPointY - ((tilesUp * bMAP_tileSizeHeight) + centerTileTop);
	
	bMAP_grid = new Array();
		bMAP_grid.rows = new Array(); // Innehåller top-värde
		bMAP_grid.cols = new Array(); // Innehåller left-värde
		bMAP_grid.topLeftCoordX = startPointTileTopLeftCoordX - (tilesLeft * bMAP_tileSizeWidth * bMAP_TileScale); //Ökar österut
		bMAP_grid.topLeftCoordY = startPointTileTopLeftCoordY + (tilesUp * bMAP_tileSizeHeight * bMAP_TileScale); //Minskar söderut
		
	bMAP_TilePositions = new Array();
	bMAP_IconPositions = new Array();
	/*
	var bMAP_grid = new Array();
		bMAP_grid.rows = new Array(); // Innehåller top-värde
		bMAP_grid.rows[0] = 0;
		bMAP_grid.rows[1] = 200;
		bMAP_grid.rows[2] = 400;
		bMAP_grid.rows[3] = 600;
		bMAP_grid.rows[4] = 800;
		
		bMAP_grid.cols = new Array(); // Innehåller left-värde
		bMAP_grid.cols[0] = 0;
		bMAP_grid.cols[1] = 200;
		bMAP_grid.cols[2] = 400;
		bMAP_grid.cols[3] = 600;
		bMAP_grid.cols[4] = 800;
		
		bMAP_grid.topLeftCoordX = 16138230; //Ökar österut
		bMAP_grid.topLeftCoordY = 65915340; //Minskar söderut
		
	
	var bMAP_TilePositions = new Array();
		bMAP_TilePositions[0] = new Array();
		bMAP_TilePositions[0][0] = "bMAP-MapTile_0";
		bMAP_TilePositions[0][1] = "bMAP-MapTile_1";
		bMAP_TilePositions[0][2] = "bMAP-MapTile_2";
		bMAP_TilePositions[0][3] = "bMAP-MapTile_3";
		bMAP_TilePositions[0][4] = "bMAP-MapTile_4";
		
		bMAP_TilePositions[1] = new Array();
		bMAP_TilePositions[1][0] = "bMAP-MapTile_5";
		bMAP_TilePositions[1][1] = "bMAP-MapTile_6";
		bMAP_TilePositions[1][2] = "bMAP-MapTile_7";
		bMAP_TilePositions[1][3] = "bMAP-MapTile_8";
		bMAP_TilePositions[1][4] = "bMAP-MapTile_9";
		
		bMAP_TilePositions[2] = new Array();
		bMAP_TilePositions[2][0] = "bMAP-MapTile_10";
		bMAP_TilePositions[2][1] = "bMAP-MapTile_11";
		bMAP_TilePositions[2][2] = "bMAP-MapTile_12";
		bMAP_TilePositions[2][3] = "bMAP-MapTile_13";
		bMAP_TilePositions[2][4] = "bMAP-MapTile_14";
		
		bMAP_TilePositions[3] = new Array();
		bMAP_TilePositions[3][0] = "bMAP-MapTile_15";
		bMAP_TilePositions[3][1] = "bMAP-MapTile_16";
		bMAP_TilePositions[3][2] = "bMAP-MapTile_17";
		bMAP_TilePositions[3][3] = "bMAP-MapTile_18";
		bMAP_TilePositions[3][4] = "bMAP-MapTile_19";
		
		bMAP_TilePositions[4] = new Array();
		bMAP_TilePositions[4][0] = "bMAP-MapTile_20";
		bMAP_TilePositions[4][1] = "bMAP-MapTile_21";
		bMAP_TilePositions[4][2] = "bMAP-MapTile_22";
		bMAP_TilePositions[4][3] = "bMAP-MapTile_23";
		bMAP_TilePositions[4][4] = "bMAP-MapTile_24";
	
	<div id="bMAP-PanBarWest" class="bMAP-PanBar" onMouseOver="bMAP_PanBar_hoverStart(this)" onMouseOut="bMAP_PanBar_hoverEnd(this)"></div>
	<div id="bMAP-PanBarEast" class="bMAP-PanBar" onMouseOver="bMAP_PanBar_hoverStart(this)" onMouseOut="bMAP_PanBar_hoverEnd(this)"></div>
	<div id="bMAP-PanBarNorth" class="bMAP-PanBar" onMouseOver="bMAP_PanBar_hoverStart(this)" onMouseOut="bMAP_PanBar_hoverEnd(this)"></div>
	<div id="bMAP-PanBarSouth" class="bMAP-PanBar" onMouseOver="bMAP_PanBar_hoverStart(this)" onMouseOut="bMAP_PanBar_hoverEnd(this)"></div>
	
	<div id="bMAP-DragArea" onMouseDown="bMAP_dragStart()" onMouseUp="bMAP_dragEnd()" onDragStart="return false;"></div>
	*/
	
	
	
	//var bMAP_BaseNODE = document.getElementById('bMAP-Base');
	
	
	// Tömmer kartan
	for(var i = bMAP_BaseNODE.childNodes.length-1; i >= 0; i--) {
		bMAP_BaseNODE.removeChild(bMAP_BaseNODE.childNodes[i]);
	}
	
	var bMAP_PanBarWest = document.createElement("div");
		bMAP_PanBarWest.setAttribute('id','bMAP-PanBarWest');
		bMAP_PanBarWest.className = 'bMAP-PanBar';
		bMAP_PanBarWest.onmouseover = function() { bMAP_PanBar_hoverStart(this); };
		bMAP_PanBarWest.onmouseout = function() { bMAP_PanBar_hoverEnd(this); bMAP_stopScroll(); };
		bMAP_PanBarWest.onmousedown = function() { bMAP_scroll(1,0) };
		bMAP_PanBarWest.onmouseup = function() { bMAP_stopScroll() };
	   	bMAP_PanBarWest.onmousewheel = function(e) { bMAP_wheelScroll("west"); bMAP_stopEvent(e); }
	var bMAP_PanBarEast = document.createElement("div");
		bMAP_PanBarEast.setAttribute('id','bMAP-PanBarEast');
		bMAP_PanBarEast.className = 'bMAP-PanBar';
		bMAP_PanBarEast.onmouseover = function() { bMAP_PanBar_hoverStart(this); };
		bMAP_PanBarEast.onmouseout = function() { bMAP_PanBar_hoverEnd(this); bMAP_stopScroll(); };
		bMAP_PanBarEast.onmousedown = function() { bMAP_scroll(-1,0) };
		bMAP_PanBarEast.onmouseup = function() { bMAP_stopScroll() };
	   	bMAP_PanBarEast.onmousewheel = function(e) { bMAP_wheelScroll("east"); bMAP_stopEvent(e); }
	var bMAP_PanBarNorth = document.createElement("div");
		bMAP_PanBarNorth.setAttribute('id','bMAP-PanBarNorth');
		bMAP_PanBarNorth.className = 'bMAP-PanBar';
		bMAP_PanBarNorth.onmouseover = function() { bMAP_PanBar_hoverStart(this); };
		bMAP_PanBarNorth.onmouseout = function() { bMAP_PanBar_hoverEnd(this); bMAP_stopScroll(); };
		bMAP_PanBarNorth.onmousedown = function() { bMAP_scroll(0,1) };
		bMAP_PanBarNorth.onmouseup = function() { bMAP_stopScroll() };
	   	bMAP_PanBarNorth.onmousewheel = function(e) { bMAP_wheelScroll(); bMAP_stopEvent(e); }
	var bMAP_PanBarSouth = document.createElement("div");
		bMAP_PanBarSouth.setAttribute('id','bMAP-PanBarSouth');
		bMAP_PanBarSouth.className = 'bMAP-PanBar';
		bMAP_PanBarSouth.onmouseover = function() { bMAP_PanBar_hoverStart(this) };
		bMAP_PanBarSouth.onmouseout = function() { bMAP_PanBar_hoverEnd(this); bMAP_stopScroll(); };
		bMAP_PanBarSouth.onmousedown = function() { bMAP_scroll(0,-1) };
		bMAP_PanBarSouth.onmouseup = function() { bMAP_stopScroll() };
	   	bMAP_PanBarSouth.onmousewheel = function(e) { bMAP_wheelScroll(); bMAP_stopEvent(e); }
	
	bMAP_BaseNODE.appendChild(bMAP_PanBarWest);
	bMAP_BaseNODE.appendChild(bMAP_PanBarEast);
	bMAP_BaseNODE.appendChild(bMAP_PanBarNorth);
	bMAP_BaseNODE.appendChild(bMAP_PanBarSouth);
	
	bMAP_DragAreaOBJ = document.createElement("div");
		bMAP_DragAreaOBJ.setAttribute('id','bMAP-DragArea');
		bMAP_DragAreaOBJ.onmousedown = function() { bMAP_dragStart(); };
		bMAP_DragAreaOBJ.onmouseup = function() { bMAP_dragEnd(); };
		bMAP_DragAreaOBJ.onselectstart = function(e) { bMAP_stopEvent(e); return false; }
	   	bMAP_DragAreaOBJ.oncontextmenu = function(e) { bMAP_stopEvent(e); return false; }
	   	bMAP_DragAreaOBJ.ondragstart = function(e) { bMAP_stopEvent(e); return false; }
	   	bMAP_DragAreaOBJ.onmousewheel = function(e) { bMAP_wheelScroll(); bMAP_stopEvent(e); }
	   	
	bMAP_BaseNODE.appendChild(bMAP_DragAreaOBJ);
	
	
	bMAP_MapTileBaseOBJ = document.createElement("div");
		bMAP_MapTileBaseOBJ.setAttribute('id','bMAP-MapTileBase');
		bMAP_MapTileBaseOBJ.style.left = bMAP_Positions["bMAP_MapTileBase"].left + 'px';
		bMAP_MapTileBaseOBJ.style.top = bMAP_Positions["bMAP_MapTileBase"].top + 'px';
		
		var row = 0;
		//var curTopPosCoord = bMAP_grid.topLeftCoordY;
		var curTopPosCoord = bMAP_grid.topLeftCoordY - (bMAP_TileScale * bMAP_tileSizeHeight); //(eftersom bildens namn är bottenkoordinaten)
		for(var curTopPosPX = bMAP_Positions["bMAP_MapTileBase"].top; curTopPosPX < (bMAP_frameHeight + (bMAP_overflowTiles * bMAP_tileSizeHeight)); curTopPosPX += bMAP_tileSizeHeight) { // rows
			
			bMAP_TilePositions[row] = new Array();
			bMAP_grid.rows[row] = (row * bMAP_tileSizeHeight);
			
			var col = 0;
			var curLeftPosCoord = bMAP_grid.topLeftCoordX;
			for(var curLeftPosPX = bMAP_Positions["bMAP_MapTileBase"].left; curLeftPosPX < (bMAP_frameWidth + (bMAP_overflowTiles * bMAP_tileSizeWidth)); curLeftPosPX += bMAP_tileSizeWidth) { // cols
				if(row == 0) { //Behöver bara göras på första raden
					bMAP_grid.cols[col] = (col * bMAP_tileSizeWidth);
				}
				var tileID = "bMAP-MapTile_" + String(row) + String(col);
				
				bMAP_TilePositions[row][col] = tileID;
				
				var newMapTile = document.createElement("div");
					newMapTile.setAttribute('id',tileID);
					newMapTile.className = 'bMAP-MapTile';
					newMapTile.style.top =  bMAP_grid.rows[row] + 'px';
					newMapTile.style.left = bMAP_grid.cols[col] + 'px';
					//if(!(curLeftPosCoord == startPointTileTopLeftCoordX && curTopPosCoord == startPointTileBottomLeftCoordY))
					newMapTile.style.backgroundImage = "url(/maps/sc" + bMAP_TileScale + "/x" + String(curLeftPosCoord).substring(0,3) + "/y" + String(curTopPosCoord).substring(0,3) + "/" + curLeftPosCoord + "x" + curTopPosCoord + "x" + bMAP_tileSizeWidth + "x" + bMAP_tileSizeHeight + "x" + bMAP_TileScale + "." + bMAP_TileImgSuffix + ")";
					//alert("url(/maps/sc" + bMAP_TileScale + "/x" + String(curLeftPosCoord).substring(0,3) + "/y" + String(curTopPosCoord).substring(0,3) + "/" + curLeftPosCoord + "x" + curTopPosCoord + "x" + bMAP_tileSizeWidth + "x" + bMAP_tileSizeHeight + "x" + bMAP_TileScale + "." + bMAP_TileImgSuffix + ")")
					//newMapTile.style.backgroundImage = 'url(/maps/sc316/x161/y659/16138230x65915340x200x200x316.mgif)';
				
				bMAP_MapTileBaseOBJ.appendChild(newMapTile);
				
				curLeftPosCoord += (bMAP_tileSizeWidth * bMAP_TileScale);
				col++;
			}
			
			curTopPosCoord -= (bMAP_tileSizeHeight * bMAP_TileScale);
			row++;
		}
		
	bMAP_BaseNODE.appendChild(bMAP_MapTileBaseOBJ);
	
	
	bMAP_IconBaseOBJ = document.createElement("div");
		bMAP_IconBaseOBJ.setAttribute('id','bMAP-IconBase');
		bMAP_IconBaseOBJ.style.left = bMAP_Positions["bMAP_MapTileBase"].left + 'px';
		bMAP_IconBaseOBJ.style.top = bMAP_Positions["bMAP_MapTileBase"].top + 'px';
		
		
		//Läs in alla ikoner
		var bottomRightXCoord = bMAP_grid.topLeftCoordX + (bMAP_grid.cols.length * bMAP_tileSizeWidth * bMAP_TileScale);
		var bottomRightYCoord = bMAP_grid.topLeftCoordY - (bMAP_grid.rows.length * bMAP_tileSizeHeight * bMAP_TileScale);
		bMAP_getIcons(bMAP_grid.topLeftCoordX, bMAP_grid.topLeftCoordY, bottomRightXCoord, bottomRightYCoord);
		
	
	bMAP_BaseNODE.appendChild(bMAP_IconBaseOBJ);
	
	bMAP_init();
	
	
}
//------------------------------------------------------------------------------

function bMAP_getIcons(topLeftXCoord, topLeftYCoord, bottomRightXCoord, bottomRightYCoord) {
	 var xmlHttp=null; // Defines that xmlHttp is a new variable.
	 // Try to get the right object for different browser
	 try {
	    // Firefox, Opera 8.0+, Safari
	    xmlHttp = new XMLHttpRequest(); // xmlHttp is now a XMLHttpRequest.
	 } catch (e) {
	    // Internet Explorer
	    try {
	       xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	 }
	 xmlHttp.onreadystatechange = function() {
	    if (xmlHttp.readyState == 4)
	       try { // In some instance, status cannot be retrieve and will produce an error (ex: Port is not responsive)
	          if (xmlHttp.status == 200) {
	             //Set the main HTML of the body to the info provided by the AJAX Request
	             //document.getElementById("ajax_output").innerHTML = xmlHttp.responseText;
	             
	             	//alert(xmlHttp.responseText);
	             	
	             	var newMapPoints = xmlHttp.responseText.split("¤|");
	             	for(var i in newMapPoints) {
	             		if(newMapPoints[i] != "") { //Sista är tom...
					var newMapPointData = newMapPoints[i].split("¤");
					//alert(newMapPointData[2] + "\n" + newMapPointData[3] + "\n" + newMapPointData[0] + "\n" + newMapPointData[1]);
					bMAP_insertIcon(newMapPointData[0], newMapPointData[1], newMapPointData[2], newMapPointData[3], newMapPointData[4], newMapPointData[5], newMapPointData[6]);
		             	}
	             	}
	             	
	          }
	          // alert(xmlHttp.status);
	          
	       } catch (e) {
	            //document.getElementById("ajax_output").innerHTML = "Error on Ajax return call : " + e.description;
	          //alert("Error on Ajax return call : " + e.description);
	            //¤ return "Error on Ajax return call : " + e.description;
	            //alert(5);
	       }
	
	 }
	 //alert("/bMAP/getMapPoints.aspx?topLeftX=" + topLeftXCoord + "&topLeftY=" + topLeftYCoord + "&bottomRightX=" + bottomRightXCoord + "&bottomRightY=" + bottomRightYCoord)
	 xmlHttp.open("get", "/bMAP/getMapPoints.aspx?topLeftX=" + topLeftXCoord + "&topLeftY=" + topLeftYCoord + "&bottomRightX=" + bottomRightXCoord + "&bottomRightY=" + bottomRightYCoord); // .open(RequestType, Source);
	 xmlHttp.send(null); // Since there is no supplied form, null takes its place as a new form.
}

function bMAP_insertIcon(XCoord, YCoord, mapPointID, groupid, car_sharing, address, status) {
	
	var posX = (XCoord - bMAP_grid.topLeftCoordX) / bMAP_TileScale;
	var posY = (bMAP_grid.topLeftCoordY - YCoord) / bMAP_TileScale;
	
	var newMapPointNODE = document.createElement("div");
		newMapPointNODE.setAttribute('id','bMAP-mapPoint-'+mapPointID);
		newMapPointNODE.className = 'bMAP-mapPoint';
		newMapPointNODE.style.top =  posY + 'px';
		newMapPointNODE.style.left = posX + 'px';
		
		var newMapPointIconNODE = document.createElement("div");
			newMapPointIconNODE.setAttribute('id','bMAP-mapPointIcon-'+mapPointID);
			newMapPointIconNODE.className = 'bMAP-mapPointIcon';
			if(status == "planned") newMapPointIconNODE.className += ' bMAP-mapPointIconD';
			newMapPointIconNODE.onclick = function() { bMAP_clickMapIcon(mapPointID); };
			newMapPointIconNODE.onmouseover = function() { bMAP_focusMapIcon(mapPointID, groupid); };
			newMapPointIconNODE.onmouseout = function() { bMAP_blurMapIcon(mapPointID, groupid); };
	   		newMapPointIconNODE.onmousewheel = function(e) { bMAP_wheelScroll(); bMAP_stopEvent(e); }
		
		newMapPointNODE.appendChild(newMapPointIconNODE);
			
	bMAP_IconBaseOBJ.appendChild(newMapPointNODE);
	
	// Lagrar ikonens position
	var bMAP_IconPositionIndex = bMAP_IconPositions.length;
	bMAP_IconPositions[bMAP_IconPositionIndex] = new Array();
	bMAP_IconPositions[bMAP_IconPositionIndex].x = XCoord;
	bMAP_IconPositions[bMAP_IconPositionIndex].y = YCoord;
	bMAP_IconPositions[bMAP_IconPositionIndex].left = posX;
	bMAP_IconPositions[bMAP_IconPositionIndex].top = posY;
	bMAP_IconPositions[bMAP_IconPositionIndex].id = mapPointID;
	bMAP_IconPositions[bMAP_IconPositionIndex].groupid = groupid;
	bMAP_IconPositions[bMAP_IconPositionIndex].label = "<b>" + car_sharing + "</b><br>" + address;
	if(status == "planned") bMAP_IconPositions[bMAP_IconPositionIndex].label += "<br>(planerad poolbil)";
	bMAP_IconPositions[bMAP_IconPositionIndex].extraClassName = (status == "planned") ? " bMAP-mapPointIconD" : "";
		
}
function bMAP_clickMapIcon(mapPointID) {
	win=window.open("/maps/display_location.asp?act=funFrames&car_locationid=" + mapPointID + "&Extra=nothing", "", "toolbar=no,status=no,scrollbars=yes,resizable,width=480,height=600");
}
//------------------------------------------------------------------------------

var bMAP_scroll_timer;
function bMAP_scroll(scroll_x, scroll_y) {
	//if(!map_layer) { map_layer = document.getElementById('map_layer'); }
	//if(!icon_layer) { icon_layer = document.getElementById('icon_layer'); }
	
	if(bMAP_scroll_timer) { clearTimeout(bMAP_scroll_timer); }
	
	var deltaX = bMAP_Positions["bMAP_MapTileBase"].left += (scroll_x *10);
	var deltaY = bMAP_Positions["bMAP_MapTileBase"].top += (scroll_y *10);
	
	bMAP_moveMap(deltaX, deltaY);
	
	bMAP_scroll_timer = setTimeout("bMAP_scroll("+scroll_x+", "+scroll_y+")", 20);
}

function bMAP_stopScroll() {
	if(bMAP_scroll_timer) { clearTimeout(bMAP_scroll_timer); }
}

function bMAP_wheelScroll(direction) {
	
	if(direction) {
		if(direction == "west") {
			var scroll_x = (event.wheelDelta/120) * 6;
			var scroll_y = 0;
		}
		else if(direction == "east") {
			var scroll_x = (event.wheelDelta/120) * -6;
			var scroll_y = 0;
		}
	}
	else {
		var scroll_x = 0;
		var scroll_y = (event.wheelDelta/120) * 6;
	}
	bMAP_scroll(scroll_x, scroll_y);
	bMAP_stopScroll();
}

//------------------------------------------------------------------------------


function bMAP_PanBar_hoverStart(panBarObj) {
	panBarObj.className = "bMAP-PanBar bMAP-PanBarHover";
	//alert(panBarObj.className);
}
function bMAP_PanBar_hoverEnd(panBarObj) {
	panBarObj.className = "bMAP-PanBar";
}

function bMAP_dragStart(evt) {
	//if(!bMAP_MapTileBaseOBJ) { bMAP_MapTileBaseOBJ = document.getElementById('bMAP-MapTileBase'); }
	//if(!icon_layer) { icon_layer = document.getElementById('icon_layer'); }
	
	if(!document.all) {
		bMAP_start_mouse_x = evt.screenX + document.body.scrollLeft; //layerX
		bMAP_start_mouse_y = evt.screenY + document.body.scrollTop; //layerY
	}
	else {
		bMAP_start_mouse_x = event.x + document.body.scrollLeft;
		bMAP_start_mouse_y = event.y + document.body.scrollTop;
	}
	
	bMAP_start_div_x = bMAP_Positions["bMAP_MapTileBase"].left;
	bMAP_start_div_y = bMAP_Positions["bMAP_MapTileBase"].top;
	
	bMAP_DragAreaOBJ.className = "bMAP-DragAreaActive";
	bMAP_dragStat = true;
	
	/*
	bMAP_start_div_x = parseFloat(map_layer.style.left.replace(/px/g, ""));
	bMAP_start_div_y = parseFloat(map_layer.style.top.replace(/px/g, ""));
	
	bMAP_dragStat = true;
	
	if(placeIcon) {
		//alert(bMAP_start_div_x + "" + bMAP_start_div_y)
		iconXpx = (bMAP_start_div_x*-1) + bMAP_start_mouse_x  - 14; //icon_size:29 -> /2 = 15
		iconYpx = (bMAP_start_div_y*-1) + bMAP_start_mouse_y  - 14; //icon_size:29 -> /2 = 15
		
		iconObj.style.left = iconXpx;
		iconObj.style.top  = iconYpx;
		iconObj.style.display = "block";
		iconButton.style.display = "block";
		
		var x = map_top_x;
		var y = map_top_y;
		iconX = x + (iconXpx+14) * (map_size_x/image_width);
		iconY = y - (iconYpx+14) * (map_size_y/image_height);
	}
	*/
}
function bMAP_dragEnd(evt) {
	bMAP_dragStat = false;
	bMAP_DragAreaOBJ.className = "";
	
}

function bMAP_drag(evt) {
	if(bMAP_dragStat) {
		
		if(!document.all) { // Mozilla
			var x = evt.screenX + document.body.scrollLeft; //layerX
			var y = evt.screenY + document.body.scrollTop; //layerY
		}
		else {
			var x = event.x + document.body.scrollLeft;
			var y = event.y + document.body.scrollTop;
		}	
	
		var deltaX = bMAP_start_div_x + (x - bMAP_start_mouse_x);
		var deltaY = bMAP_start_div_y + (y - bMAP_start_mouse_y);
		
		bMAP_moveMap(deltaX, deltaY);
		
		/*
		div_x = start_div_x + (x - start_mouse_x);
		div_y = start_div_y + (y - start_mouse_y);
		
		if(div_y>reloadbar_size) { bMAP_start_mouse_y = y; }
		else if(div_y<(map_area_height-map_size)-reloadbar_size) { bMAP_start_mouse_y = y; }
		
		if(div_x>reloadbar_size) { bMAP_start_mouse_x = x; }
		else if(div_x<(map_area_width-map_size)-reloadbar_size) { bMAP_start_mouse_x = x; }
		
		placeLayers(div_x, div_y);
		*/
	}
}
function bMAP_moveMap(deltaX, deltaY) {
	bMAP_Positions["bMAP_MapTileBase"].left = deltaX;
	bMAP_Positions["bMAP_MapTileBase"].top = deltaY;
	bMAP_MapTileBaseOBJ.style.left = deltaX + "px";
	bMAP_MapTileBaseOBJ.style.top = deltaY + "px";
	
	//Flyttar ikonerna
	bMAP_IconBaseOBJ.style.left = deltaX + "px";
	bMAP_IconBaseOBJ.style.top = deltaY + "px";
	
	
	//Flyttar plattorna om det behövs
	//if(bMAP_Positions["bMAP_MapTileBase"].left >= 0) {
		
		// Left
		if(bMAP_Positions["bMAP_MapTileBase"].left > (bMAP_grid.cols[0] * -1)) {
			// 2 > (-200 * -1 = 200) = false
			// 328 > (0 * -1 = 0) = true
			// 328 > (-200 * -1 = 200) = true
			// 328 > (-400 * -1 = 400) = false
			bMAP_swapTilesLeft();	
		}
		
		// Right
		else if((bMAP_Positions["bMAP_MapTileBase"].left + bMAP_grid.cols[bMAP_grid.cols.length-1] + bMAP_tileSizeWidth) < bMAP_frameWidth) {
			// -300 + 800 + 200 < 900 = true
			bMAP_swapTilesRight();	
		}
		
		// Up
		if(bMAP_Positions["bMAP_MapTileBase"].top > (bMAP_grid.rows[0] * -1)) {
			bMAP_swapTilesUp();
		}
		
		// Down
		else if((bMAP_Positions["bMAP_MapTileBase"].top + bMAP_grid.rows[bMAP_grid.rows.length-1] + bMAP_tileSizeHeight) < bMAP_frameHeight) {
			bMAP_swapTilesDown();
		}
	//}
}


//------------------------------------------------------------------------------

/*
	var bMAP_frameWidth = 581;
	var bMAP_frameHeight = 521;
	
	var bMAP_tileSizeWidth = 200;
	var bMAP_tileSizeHeight = 200;
	
	var bMAP_TileScale = 316;
	var bMAP_TileImgSuffix = "mgif";
	
	var bMAP_grid = new Array();
		bMAP_grid.rows = new Array(); // Innehåller top-värde
		bMAP_grid.rows[0] = 0;
		bMAP_grid.rows[1] = 200;
		bMAP_grid.rows[2] = 400;
		bMAP_grid.rows[3] = 600;
		bMAP_grid.rows[4] = 800;
		
		bMAP_grid.cols = new Array(); // Innehåller left-värde
		bMAP_grid.cols[0] = 0;
		bMAP_grid.cols[1] = 200;
		bMAP_grid.cols[2] = 400;
		bMAP_grid.cols[3] = 600;
		bMAP_grid.cols[4] = 800;
	
		bMAP_grid.topLeftCoordX = 16138230; //Ökar österut
		bMAP_grid.topLeftCoordY = 65915340; //Minskar söderut
		
	var bMAP_TilePositions = new Array();
		bMAP_TilePositions[0] = new Array();
		bMAP_TilePositions[0][0] = "bMAP-MapTile_0";
		bMAP_TilePositions[0][1] = "bMAP-MapTile_1";
		bMAP_TilePositions[0][2] = "bMAP-MapTile_2";
		bMAP_TilePositions[0][3] = "bMAP-MapTile_3";
		bMAP_TilePositions[0][4] = "bMAP-MapTile_4";
		
		bMAP_TilePositions[1] = new Array();
		bMAP_TilePositions[1][0] = "bMAP-MapTile_5";
		bMAP_TilePositions[1][1] = "bMAP-MapTile_6";
		bMAP_TilePositions[1][2] = "bMAP-MapTile_7";
		bMAP_TilePositions[1][3] = "bMAP-MapTile_8";
		bMAP_TilePositions[1][4] = "bMAP-MapTile_9";
		
		bMAP_TilePositions[2] = new Array();
		bMAP_TilePositions[2][0] = "bMAP-MapTile_10";
		bMAP_TilePositions[2][1] = "bMAP-MapTile_11";
		bMAP_TilePositions[2][2] = "bMAP-MapTile_12";
		bMAP_TilePositions[2][3] = "bMAP-MapTile_13";
		bMAP_TilePositions[2][4] = "bMAP-MapTile_14";
		
		bMAP_TilePositions[3] = new Array();
		bMAP_TilePositions[3][0] = "bMAP-MapTile_15";
		bMAP_TilePositions[3][1] = "bMAP-MapTile_16";
		bMAP_TilePositions[3][2] = "bMAP-MapTile_17";
		bMAP_TilePositions[3][3] = "bMAP-MapTile_18";
		bMAP_TilePositions[3][4] = "bMAP-MapTile_19";
		
		bMAP_TilePositions[4] = new Array();
		bMAP_TilePositions[4][0] = "bMAP-MapTile_20";
		bMAP_TilePositions[4][1] = "bMAP-MapTile_21";
		bMAP_TilePositions[4][2] = "bMAP-MapTile_22";
		bMAP_TilePositions[4][3] = "bMAP-MapTile_23";
		bMAP_TilePositions[4][4] = "bMAP-MapTile_24";
*/		

function bMAP_swapTilesLeft() {
	
	bMAP_grid.cols.unshift(bMAP_grid.cols.pop()); //Sista elementet hamnar först
	
	bMAP_grid.cols[0] = bMAP_grid.cols[1] - bMAP_tileSizeWidth;
	
	for(var row in bMAP_TilePositions) { // Första kolumnen i varje rad
		//Flyttar elementet i arrayen
		bMAP_TilePositions[row].unshift(bMAP_TilePositions[row].pop());
		//Positionerar om objektet
		document.getElementById(bMAP_TilePositions[row][0]).style.left = bMAP_grid.cols[0] + "px";
		
		//Ändrar kartbild (bg)
		var tileTopLeftCoordX = bMAP_grid.topLeftCoordX + (bMAP_grid.cols[0] * bMAP_TileScale);
		//var tileTopLeftCoordY = bMAP_grid.topLeftCoordY - (bMAP_grid.rows[row] * bMAP_TileScale);
		var tileTopLeftCoordY = (bMAP_grid.topLeftCoordY - (bMAP_TileScale * bMAP_tileSizeHeight)) - (bMAP_grid.rows[row] * bMAP_TileScale);  //(eftersom bildens namn är bottenkoordinaten)
		document.getElementById(bMAP_TilePositions[row][0]).style.backgroundImage = "url(/maps/sc" + bMAP_TileScale + "/x" + String(tileTopLeftCoordX).substring(0,3) + "/y" + String(tileTopLeftCoordY).substring(0,3) + "/" + tileTopLeftCoordX + "x" + tileTopLeftCoordY + "x" + bMAP_tileSizeWidth + "x" + bMAP_tileSizeHeight + "x" + bMAP_TileScale + "." + bMAP_TileImgSuffix + ")";
	}
	
	
	//Läs in nya ikoner
	var newAreaTopLeftCoordX = bMAP_grid.topLeftCoordX + (bMAP_grid.cols[0] * bMAP_TileScale);
	var newAreaTopLeftCoordY = bMAP_grid.topLeftCoordY - (bMAP_grid.rows[0] * bMAP_TileScale);
	var newAreaBottomRightCoordX = bMAP_grid.topLeftCoordX + ((bMAP_grid.cols[0] + bMAP_tileSizeWidth) * bMAP_TileScale);
	var newAreaBottomRightCoordY = bMAP_grid.topLeftCoordY - ((bMAP_grid.rows[bMAP_grid.rows.length-1] + bMAP_tileSizeHeight) * bMAP_TileScale);
	bMAP_getIcons(newAreaTopLeftCoordX, newAreaTopLeftCoordY, newAreaBottomRightCoordX, newAreaBottomRightCoordY);
	
	//alert(newAreaTopLeftCoordX + ", " + newAreaTopLeftCoordY + " | " + newAreaBottomRightCoordX + ", " + newAreaBottomRightCoordY);
	
	//Tag bort gamla ikoner
	var threshold = bMAP_grid.topLeftCoordX + ((bMAP_grid.cols[bMAP_grid.cols.length-1] + bMAP_tileSizeWidth) * bMAP_TileScale);
	bMAP_removeIcons("above", "x", threshold);
}
function bMAP_swapTilesRight() {
	
	bMAP_grid.cols.push(bMAP_grid.cols.shift()); //Första elementet hamnar sist
	
	bMAP_grid.cols[bMAP_grid.cols.length-1] = bMAP_grid.cols[bMAP_grid.cols.length-2] + bMAP_tileSizeWidth;
	
	for(var row in bMAP_TilePositions) { // Sista kolumnen i varje rad
		//Flyttar elementet i arrayen
		bMAP_TilePositions[row].push(bMAP_TilePositions[row].shift());
		//Positionerar om objektet
		document.getElementById(bMAP_TilePositions[row][bMAP_TilePositions[row].length-1]).style.left = bMAP_grid.cols[bMAP_grid.cols.length-1] + "px";
		
		//Ändrar kartbild (bg)
		var tileTopLeftCoordX = bMAP_grid.topLeftCoordX + (bMAP_grid.cols[bMAP_grid.cols.length-1] * bMAP_TileScale);
		//var tileTopLeftCoordY = bMAP_grid.topLeftCoordY - (bMAP_grid.rows[row] * bMAP_TileScale);
		var tileTopLeftCoordY = (bMAP_grid.topLeftCoordY - (bMAP_TileScale * bMAP_tileSizeHeight)) - (bMAP_grid.rows[row] * bMAP_TileScale);
		document.getElementById(bMAP_TilePositions[row][bMAP_TilePositions[row].length-1]).style.backgroundImage = "url(/maps/sc" + bMAP_TileScale + "/x" + String(tileTopLeftCoordX).substring(0,3) + "/y" + String(tileTopLeftCoordY).substring(0,3) + "/" + tileTopLeftCoordX + "x" + tileTopLeftCoordY + "x" + bMAP_tileSizeWidth + "x" + bMAP_tileSizeHeight + "x" + bMAP_TileScale + "." + bMAP_TileImgSuffix + ")";
	}
	
	//Läs in nya ikoner
	var newAreaTopLeftCoordX = bMAP_grid.topLeftCoordX + (bMAP_grid.cols[bMAP_grid.cols.length-1] * bMAP_TileScale);
	var newAreaTopLeftCoordY = bMAP_grid.topLeftCoordY - (bMAP_grid.rows[0] * bMAP_TileScale);
	var newAreaBottomRightCoordX = bMAP_grid.topLeftCoordX + ((bMAP_grid.cols[bMAP_grid.cols.length-1] + bMAP_tileSizeWidth) * bMAP_TileScale);
	var newAreaBottomRightCoordY = bMAP_grid.topLeftCoordY - ((bMAP_grid.rows[bMAP_grid.rows.length-1] + bMAP_tileSizeHeight) * bMAP_TileScale);
	bMAP_getIcons(newAreaTopLeftCoordX, newAreaTopLeftCoordY, newAreaBottomRightCoordX, newAreaBottomRightCoordY);
	
	//alert(newAreaTopLeftCoordX + ", " + newAreaTopLeftCoordY + " | " + newAreaBottomRightCoordX + ", " + newAreaBottomRightCoordY);
	
	//Tag bort gamla ikoner
	var threshold = bMAP_grid.topLeftCoordX + (bMAP_grid.cols[0] * bMAP_TileScale);
	bMAP_removeIcons("below", "x", threshold);
}
function bMAP_swapTilesUp() {
	
	bMAP_grid.rows.unshift(bMAP_grid.rows.pop()); //Sista elementet hamnar först
	
	bMAP_grid.rows[0] = bMAP_grid.rows[1] - bMAP_tileSizeHeight;
	
	//Flyttar elementet i arrayen
	bMAP_TilePositions.unshift(bMAP_TilePositions.pop()); //Sista elementet hamnar först
	
	for(var col in bMAP_TilePositions[0]) { //Alla kolumner i översta raden
		//Positionerar om objektet
		document.getElementById(bMAP_TilePositions[0][col]).style.top = bMAP_grid.rows[0] + "px";
		
		//Ändrar kartbild (bg)
		var tileTopLeftCoordX = bMAP_grid.topLeftCoordX + (bMAP_grid.cols[col] * bMAP_TileScale);
		//var tileTopLeftCoordY = bMAP_grid.topLeftCoordY - (bMAP_grid.rows[0] * bMAP_TileScale);
		var tileTopLeftCoordY = (bMAP_grid.topLeftCoordY - (bMAP_TileScale * bMAP_tileSizeHeight)) - (bMAP_grid.rows[0] * bMAP_TileScale);
		document.getElementById(bMAP_TilePositions[0][col]).style.backgroundImage = "url(/maps/sc" + bMAP_TileScale + "/x" + String(tileTopLeftCoordX).substring(0,3) + "/y" + String(tileTopLeftCoordY).substring(0,3) + "/" + tileTopLeftCoordX + "x" + tileTopLeftCoordY + "x" + bMAP_tileSizeWidth + "x" + bMAP_tileSizeHeight + "x" + bMAP_TileScale + "." + bMAP_TileImgSuffix + ")";
	}
	
	//Läs in nya ikoner
	var newAreaTopLeftCoordX = bMAP_grid.topLeftCoordX + (bMAP_grid.cols[0] * bMAP_TileScale);
	var newAreaTopLeftCoordY = bMAP_grid.topLeftCoordY - (bMAP_grid.rows[0] * bMAP_TileScale);
	var newAreaBottomRightCoordX = bMAP_grid.topLeftCoordX + ((bMAP_grid.cols[bMAP_grid.cols.length-1] + bMAP_tileSizeWidth) * bMAP_TileScale);
	var newAreaBottomRightCoordY = bMAP_grid.topLeftCoordY - ((bMAP_grid.rows[0] + bMAP_tileSizeHeight) * bMAP_TileScale);
	bMAP_getIcons(newAreaTopLeftCoordX, newAreaTopLeftCoordY, newAreaBottomRightCoordX, newAreaBottomRightCoordY);
	
	
	//Tag bort gamla ikoner
	var threshold = bMAP_grid.topLeftCoordY - ((bMAP_grid.rows[bMAP_grid.rows.length-1] + bMAP_tileSizeHeight) * bMAP_TileScale);
	bMAP_removeIcons("below", "y", threshold);
}
function bMAP_swapTilesDown() {
	
	bMAP_grid.rows.push(bMAP_grid.rows.shift()); //Första elementet hamnar sist
	
	bMAP_grid.rows[bMAP_grid.rows.length-1] = bMAP_grid.rows[bMAP_grid.rows.length-2] + bMAP_tileSizeHeight;
	
	//Flyttar elementet i arrayen
	bMAP_TilePositions.push(bMAP_TilePositions.shift()); //Första elementet hamnar sist
	
	for(var col in bMAP_TilePositions[bMAP_TilePositions.length-1]) { //Alla kolumner i sista raden
		//Positionerar om objektet
		document.getElementById(bMAP_TilePositions[bMAP_TilePositions.length-1][col]).style.top = bMAP_grid.rows[bMAP_grid.rows.length-1] + "px";
		
		//Ändrar kartbild (bg)
		var tileTopLeftCoordX = bMAP_grid.topLeftCoordX + (bMAP_grid.cols[col] * bMAP_TileScale);
		//var tileTopLeftCoordY = bMAP_grid.topLeftCoordY - (bMAP_grid.rows[bMAP_grid.rows.length-1] * bMAP_TileScale);
		var tileTopLeftCoordY = (bMAP_grid.topLeftCoordY - (bMAP_TileScale * bMAP_tileSizeHeight)) - (bMAP_grid.rows[bMAP_grid.rows.length-1] * bMAP_TileScale);
		document.getElementById(bMAP_TilePositions[bMAP_TilePositions.length-1][col]).style.backgroundImage = "url(/maps/sc" + bMAP_TileScale + "/x" + String(tileTopLeftCoordX).substring(0,3) + "/y" + String(tileTopLeftCoordY).substring(0,3) + "/" + tileTopLeftCoordX + "x" + tileTopLeftCoordY + "x" + bMAP_tileSizeWidth + "x" + bMAP_tileSizeHeight + "x" + bMAP_TileScale + "." + bMAP_TileImgSuffix + ")";
	}
	
	//Läs in nya ikoner
	var newAreaTopLeftCoordX = bMAP_grid.topLeftCoordX + (bMAP_grid.cols[0] * bMAP_TileScale);
	var newAreaTopLeftCoordY = bMAP_grid.topLeftCoordY - (bMAP_grid.rows[bMAP_grid.rows.length-1] * bMAP_TileScale);
	var newAreaBottomRightCoordX = bMAP_grid.topLeftCoordX + ((bMAP_grid.cols[bMAP_grid.cols.length-1] + bMAP_tileSizeWidth) * bMAP_TileScale);
	var newAreaBottomRightCoordY = bMAP_grid.topLeftCoordY - ((bMAP_grid.rows[bMAP_grid.rows.length-1] + bMAP_tileSizeHeight) * bMAP_TileScale);
	bMAP_getIcons(newAreaTopLeftCoordX, newAreaTopLeftCoordY, newAreaBottomRightCoordX, newAreaBottomRightCoordY);
	
	//Tag bort gamla ikoner
	var threshold = bMAP_grid.topLeftCoordY - (bMAP_grid.rows[0] * bMAP_TileScale);
	bMAP_removeIcons("above", "y", threshold);
}



//------------------------------------------------------------------------------------------------------------------

// Anropas från Sverigekartan
function bMAP_focusMapLocation(mapLocationID, mapLocationName, xPos, yPos) {
	//document.getElementById('map_location_point_' + mapLocationID).src = map_location_point_active.src;
	document.getElementById('active_map_location_point_' + mapLocationID).style.visibility = "visible";
	
	if(mapLocationName || xPos || yPos) {
		document.getElementById('map_location_name').innerHTML = mapLocationName; 
		document.getElementById('map_location_name').style.left = (xPos-7) + "px";
		document.getElementById('map_location_name').style.top = (yPos-23) + "px";
		document.getElementById('map_location_name').style.visibility = "visible";
	}
}
function bMAP_blurMapLocation(mapLocationID) {
	//document.getElementById('map_location_point_' + mapLocationID).src = map_location_point.src;
	document.getElementById('active_map_location_point_' + mapLocationID).style.visibility = "hidden";
	document.getElementById('map_location_name').style.visibility = "hidden";
}

//------------------------------------------------------------------------------------------------------------------

function bMAP_focusMapIcon(mapPointID, groupid) {
	
	if(document.getElementById("bMAP-mapPointIcon-"+mapPointID)) {
	
		//document.getElementById("bMAP-mapPoint-"+mapPointID).className = "bMAP-mapPoint bMAP-mapPoint-Active";
		
		//¤document.getElementById("bMAP-mapPointIcon-"+mapPointID).className = "bMAP-mapPointIcon bMAP-mapPointIcon-activeIcon";
		//¤document.getElementById("bMAP-mapPointIcon-"+mapPointID).className += " bMAP-mapPointIcon-activeIcon";
		
		var thisIconPositionIndex;
		for (var bMAP_IconPositionIndex in bMAP_IconPositions) {
			//if (bMAP_IconPositions[bMAP_IconPositionIndex].id != mapPointID && bMAP_IconPositions[bMAP_IconPositionIndex].groupid == groupid) {
			//	document.getElementById("bMAP-mapPointIcon-"+bMAP_IconPositions[bMAP_IconPositionIndex].id).className = "bMAP-mapPointIcon bMAP-mapPointIcon-activeGroup";
			//}
			//
			//else if (bMAP_IconPositions[bMAP_IconPositionIndex].id == mapPointID) thisIconPositionIndex = bMAP_IconPositionIndex;
			
			if (bMAP_IconPositions[bMAP_IconPositionIndex].id == mapPointID) {
				thisIconPositionIndex = bMAP_IconPositionIndex;
				document.getElementById("bMAP-mapPointIcon-"+bMAP_IconPositions[bMAP_IconPositionIndex].id).className = "bMAP-mapPointIcon" + bMAP_IconPositions[bMAP_IconPositionIndex].extraClassName + " bMAP-mapPointIcon-activeIcon";
			}
			else if (bMAP_IconPositions[bMAP_IconPositionIndex].groupid == groupid) {
				//¤document.getElementById("bMAP-mapPointIcon-"+bMAP_IconPositions[bMAP_IconPositionIndex].id).className = "bMAP-mapPointIcon bMAP-mapPointIcon-activeGroup";
				//¤document.getElementById("bMAP-mapPointIcon-"+bMAP_IconPositions[bMAP_IconPositionIndex].id).className += " bMAP-mapPointIcon-activeGroup";
				document.getElementById("bMAP-mapPointIcon-"+bMAP_IconPositions[bMAP_IconPositionIndex].id).className = "bMAP-mapPointIcon" + bMAP_IconPositions[bMAP_IconPositionIndex].extraClassName + " bMAP-mapPointIcon-activeGroup";
			}
			///else {
			///	document.getElementById("bMAP-mapPointIcon-"+bMAP_IconPositions[bMAP_IconPositionIndex].id).className = "bMAP-mapPointIcon bMAP-mapPointIcon-nonActiveGroup";
			///}
			
		}
		
		
		// Info-altruta
		var mapPointNODE = document.getElementById("bMAP-mapPoint-"+mapPointID);
		var bMAP_mapPointLabel = document.createElement("div");
			bMAP_mapPointLabel.setAttribute('id','bMAP-mapPointLabel-'+bMAP_IconPositions[bMAP_IconPositionIndex].id);
			bMAP_mapPointLabel.className = 'bMAP-mapPointLabel';
			bMAP_mapPointLabel.innerHTML = bMAP_IconPositions[thisIconPositionIndex].label;
			//bMAP_mapPointLabel.style.top = mapPointNODE.style.top;
			//bMAP_mapPointLabel.style.left = paresIntmapPointNODE.style.left;
			bMAP_mapPointLabel.style.left = bMAP_Positions["bMAP_MapTileBase"].left + bMAP_IconPositions[thisIconPositionIndex].left + 1 + "px"; // +1 kompenserar för positionen -1 på bMAP_LabelBaseOBJ
			bMAP_mapPointLabel.style.top = bMAP_Positions["bMAP_MapTileBase"].top + bMAP_IconPositions[thisIconPositionIndex].top + 1 + "px"; // +1 kompenserar för positionen -1 på bMAP_LabelBaseOBJ
			
		//mapPointNODE.appendChild(bMAP_mapPointLabel);
		bMAP_LabelBaseOBJ.appendChild(bMAP_mapPointLabel);
	}
}
function bMAP_blurMapIcon(mapPointID, groupid) {
	if(document.getElementById("bMAP-mapPointIcon-"+mapPointID)) {
		
		//document.getElementById("bMAP-mapPoint-"+mapPointID).className = "bMAP-mapPoint";
		
		//¤document.getElementById("bMAP-mapPointIcon-"+mapPointID).className = "bMAP-mapPointIcon";
		//¤var curClassName = String(document.getElementById("bMAP-mapPointIcon-"+mapPointID).className);
		//¤if(curClassName.indexOf(" ") != -1) {
		//¤	document.getElementById("bMAP-mapPointIcon-"+mapPointID).className = curClassName.substring(0, curClassName.lastIndexOf(" "));
		//¤}
		
		for (var bMAP_IconPositionIndex in bMAP_IconPositions) {
			/*
			if (bMAP_IconPositions[bMAP_IconPositionIndex].id != mapPointID && bMAP_IconPositions[bMAP_IconPositionIndex].groupid == groupid) {
				document.getElementById("bMAP-mapPointIcon-"+bMAP_IconPositions[bMAP_IconPositionIndex].id).className = "bMAP-mapPointIcon";
			}
			
			else if (bMAP_IconPositions[bMAP_IconPositionIndex].id == mapPointID) thisIconPositionIndex = bMAP_IconPositionIndex;
			*/
			
			if (bMAP_IconPositions[bMAP_IconPositionIndex].id == mapPointID) {
				thisIconPositionIndex = bMAP_IconPositionIndex;
				document.getElementById("bMAP-mapPointIcon-"+bMAP_IconPositions[bMAP_IconPositionIndex].id).className = "bMAP-mapPointIcon" + bMAP_IconPositions[bMAP_IconPositionIndex].extraClassName;
			}
			///else {
			else if (bMAP_IconPositions[bMAP_IconPositionIndex].groupid == groupid) {
				//¤document.getElementById("bMAP-mapPointIcon-"+bMAP_IconPositions[bMAP_IconPositionIndex].id).className = "bMAP-mapPointIcon";
				//¤var curClassName = String(document.getElementById("bMAP-mapPointIcon-"+bMAP_IconPositions[bMAP_IconPositionIndex].id).className);
				//¤if(curClassName.indexOf(" ") != -1) {
				//¤	document.getElementById("bMAP-mapPointIcon-"+bMAP_IconPositions[bMAP_IconPositionIndex].id).className = curClassName.substring(0, curClassName.lastIndexOf(" "));
				//¤}
				document.getElementById("bMAP-mapPointIcon-"+bMAP_IconPositions[bMAP_IconPositionIndex].id).className = "bMAP-mapPointIcon" + bMAP_IconPositions[bMAP_IconPositionIndex].extraClassName;
			}
		}
		
		/*
		// Info-altruta
		var mapPointNODE = document.getElementById("bMAP-mapPoint-"+mapPointID);
		var bMAP_mapPointLabel = document.getElementById("bMAP-mapPointLabel-"+bMAP_IconPositions[bMAP_IconPositionIndex].id);
		mapPointNODE.removeChild(bMAP_mapPointLabel);
		*/
		if(bMAP_mapPointLabel = document.getElementById("bMAP-mapPointLabel-"+bMAP_IconPositions[bMAP_IconPositionIndex].id)) {
			bMAP_LabelBaseOBJ.removeChild(bMAP_mapPointLabel);
		}
	}
}

function bMAP_focusSingleMapIcon(mapPointID) {
	
	if(document.getElementById("bMAP-mapPointIcon-"+mapPointID)) {
		//¤document.getElementById("bMAP-mapPointIcon-"+mapPointID).className = "bMAP-mapPointIcon bMAP-mapPointIcon-activeIcon";
		document.getElementById("bMAP-mapPointIcon-"+mapPointID).className += " bMAP-mapPointIcon-activeIcon";
	}
}
function bMAP_blurSingleMapIcon(mapPointID) {
	
	if(document.getElementById("bMAP-mapPointIcon-"+mapPointID)) {
		//¤document.getElementById("bMAP-mapPointIcon-"+mapPointID).className = "bMAP-mapPointIcon";
		var curClassName = String(document.getElementById("bMAP-mapPointIcon-"+mapPointID).className);
		if(curClassName.indexOf(" ") != -1) {
			document.getElementById("bMAP-mapPointIcon-"+mapPointID).className = curClassName.substring(0, curClassName.lastIndexOf(" "));
		}
	}
}
//------------------------------------------------------------------------------------------------------------------

function bMAP_removeIcons(condition, axis, threshold) {
	
	for (var bMAP_IconPositionIndex = bMAP_IconPositions.length-1; bMAP_IconPositionIndex >= 0; bMAP_IconPositionIndex--) {
		var removeIcon = false;
		
		if (axis == "x" && condition == "above") {
			removeIcon = (bMAP_IconPositions[bMAP_IconPositionIndex].x >= threshold);
		}
		else if (axis == "x" && condition == "below") {
			removeIcon = (bMAP_IconPositions[bMAP_IconPositionIndex].x < threshold);
		}
		else if (axis == "y" && condition == "above") {
			removeIcon = (bMAP_IconPositions[bMAP_IconPositionIndex].y >= threshold);
		}
		else if (axis == "y" && condition == "below") {
			removeIcon = (bMAP_IconPositions[bMAP_IconPositionIndex].y < threshold);
		}
		
		if (removeIcon) {
			//Raderar ikonen
			bMAP_removeIcon(bMAP_IconPositions[bMAP_IconPositionIndex].id);
			
			//Raderar posten i arrayen
			bMAP_IconPositions.splice(bMAP_IconPositionIndex,1);
		}
	}
}

function bMAP_removeIcon(id) {
	var mapPointNODE = document.getElementById("bMAP-mapPoint-"+id);
	bMAP_IconBaseOBJ.removeChild(mapPointNODE);
}
//------------------------------------------------------------------------------------------------------------------

function bMAP_displayMapInfo() {
	document.getElementById("bMAP-mapInfo").className = "bMAP-mapInfo-Visible";
	
}
function bMAP_hideMapInfo() {
	document.getElementById("bMAP-mapInfo").className = "";
}
//------------------------------------------------------------------------------------------------------------------

