// SW: Folgende Einträge nicht editierbar

var actPos= new oCoord(0,0);
var zoomerPos= new oCoord(0,0);

var leftMousebuttonDown= false;

var minLeft= 0;
var maxLeft= 0;
var minTop= 0;
var maxTop= 0;

var sizeActualMap= new oCoord(0,0);
var sizeActualMapPiece= new oCoord(0,0);
var sizeActualZoomer= new oCoord(0,0);

var imagePrefix= "";
var imagePostfix= "";

var sizeHighliteRegion= new oCoord(0,0);

var sizeDisplayMapareaPx= new oCoord(0,0); // Größe in Bildschirmpixel
var sizeDisplayMapareaRef= new oCoord(0,0); // Größe im Referenzkoordinatensystem

function overviewMouseEvent(event) {

	if(leftMousebuttonDown) {

		if(displayConfiguration.initMapInOverview) {
			displayConfiguration.overviewMapVisible= true;
			viewArrows('visible');
			initMap(aZoomOrder[zoomOrderActive.zoomQueue][zoomOrderActive.mapNumber], zoomOrderActive.zoomQueue);
			displayConfiguration.initMapInOverview= false;
		}

		var Coord= getMouseposition(event);
		mouseOverviewPosToRefMapPos(Coord);
	
		updateMarker();
		drawMap();
	}
} 

function mapMouseEvent(event) {
		leftMousebuttonDown= false;

		var Coord= getMouseposition(event);
		actPos= mousePosToRefMapPos(Coord);
		
		zoomerPos= new oCoord(actPos.x, actPos.y);
		
		updateMarker();
		drawMap();
} 

function getPositionStyle(posCoord) {

	var Coord= new oCoord(posCoord.x, posCoord.y);
	// Coord in refMap
	var left= actPos.x-sizeDisplayMapareaRef.x/2;
	var right= actPos.x+sizeDisplayMapareaRef.x/2;
	var top= actPos.y-sizeDisplayMapareaRef.y/2;
	var bottom= actPos.y+sizeDisplayMapareaRef.y/2;
	
	var style;
	
	// Debugger.debug("getPositionStyle(posCoord): left:"+left+" top:"+top+" x:"+Coord.x+" y:"+Coord.y);
	
	if(Coord.x>=left+sizeActualZoomer.x/2 && Coord.x <= right-sizeActualZoomer.x/2 &&
	   Coord.y>=top+sizeActualZoomer.y/2 && Coord.y <= bottom-sizeActualZoomer.y/2) {
	 	
	 	Coord.x-= left+sizeActualZoomer.x/2;
	 	Coord.y-= top+sizeActualZoomer.y/2;
	 	
	 	Coord= refMapPosToActMapPos(Coord);
	 	
		style= "left:"+Coord.x+"px; top:"+Coord.y+"px; visibility:visible;";
	} else {
		// Debugger.debug("hidden debug: left:"+left+" top:"+top+" x:"+Coord.x+" y:"+Coord.y);
		style= "visibility:hidden;";
	}
	
	return style;
}

function mouseOverviewPosToRefMapPos(Coord) {
		if(Coord.x>minLeft && Coord.x<maxLeft) {
			Coord.x-= minLeft;
		} else if(Coord.x>=maxLeft) {
			Coord.x= maxLeft-minLeft;
			leftMousebuttonDown= false;
		} else {
			Coord.x= 0;
			leftMousebuttonDown= false;
		}
		
		if(Coord.y>minTop && Coord.y<maxTop) {
			Coord.y-= minTop;
		} else if(Coord.y>=maxTop) {
			Coord.y= maxTop-minTop;
			leftMousebuttonDown= false;
		} else {
			Coord.y= 0;
			leftMousebuttonDown= false;
		}
		
		Coord.x+= sizeHighliteRegion.x/2;
		Coord.y+= sizeHighliteRegion.y/2;
		
		Coord= overviewPosToRefMapPos(Coord);

		actPos.x= Coord.x;
		actPos.y= Coord.y;
}

function mousePosToRefMapPos(Coord) {
		if(Coord.x>posAbsMap.x && Coord.x<posAbsMap.x+sizeDisplayMapareaPx.x) {
			Coord.x-= posAbsMap.x;
		} else if(Coord.x>=posAbsMap.x+sizeDisplayMapareaPx.x) {
			Coord.x= posAbsMap.x+sizeDisplayMapareaPx.x;
			leftMousebuttonDown= false;
		} else {
			Coord.x= 0;
			leftMousebuttonDown= false;
		}
		
		if(Coord.y>posAbsMap.y && Coord.y<posAbsMap.y+sizeDisplayMapareaPx.y) {
			Coord.y-= posAbsMap.y;
		} else if(Coord.y>=posAbsMap.y+sizeDisplayMapareaPx.y) {
			Coord.y= posAbsMap.y+sizeDisplayMapareaPx.y;
			leftMousebuttonDown= false;
		} else {
			Coord.y= 0;
			leftMousebuttonDown= false;
		}

		Coord.x-= sizeDisplayMapareaPx.x/2;
		Coord.y-= sizeDisplayMapareaPx.y/2;
		
		Coord= actMapPosToRefMapPos(Coord);

		if(Coord.x+actPos.x<0) {
			Coord.x= 0;
		} else if(Coord.x+actPos.x>aMaps[refMap].width-sizeDisplayMapareaRef.x/2) {
			Coord.x= parseInt(aMaps[refMap].width-sizeDisplayMapareaRef.x/2);
		} else {
		 	Coord.x+= actPos.x;
		}
	
		if(Coord.y+actPos.y<0) {
			Coord.y= 0;
		} else if(Coord.y+actPos.y>aMaps[refMap].height-sizeDisplayMapareaRef.y/2) {
			Coord.y= parseInt(aMaps[refMap].height-sizeDisplayMapareaRef.y/2);
		} else {
			Coord.y+= actPos.y;
		}

		return Coord;
}

function refMapPosToOverviewPos(Coord) {
	return new oCoord( parseInt(Coord.x*sizeOverviewMap.x/aMaps[refMap].width), parseInt(Coord.y*sizeOverviewMap.y/aMaps[refMap].height) );
}

function overviewPosToRefMapPos(Coord) {
	return new oCoord( parseInt(Coord.x*aMaps[refMap].width/sizeOverviewMap.x), parseInt(Coord.y*aMaps[refMap].height/sizeOverviewMap.y) );
}

function refMapPosToActMapPos(Coord) {
	return new oCoord( parseInt(Coord.x*sizeActualMap.x/aMaps[refMap].width), parseInt(Coord.y*sizeActualMap.y/aMaps[refMap].height) );
}

function actMapPosToRefMapPos(Coord) {
	// Debugger.debug('actMapPosToRefMapPos. Coord: '+Coord.x+'/'+Coord.y+' refMap.width:'+aMaps[refMap].width+' sA: '+sizeActualMap.x+' Res. '+parseInt(Coord.x*aMaps[refMap].width/sizeActualMap.x));
	return new oCoord( parseInt(Coord.x*aMaps[refMap].width/sizeActualMap.x), parseInt(Coord.y*aMaps[refMap].height/sizeActualMap.y) );
}


function updateMarker() {
		
	overviewCoord= refMapPosToOverviewPos(actPos);
	overviewCoord.x-= sizeHighliteRegion.x/2;
	overviewCoord.y-= sizeHighliteRegion.y/2;

	if(displayConfiguration.overviewMapVisible) {
		document.getElementById("marker").style.top=  overviewCoord.y+"px";
		document.getElementById("marker").style.left= overviewCoord.x+"px";
	}		
}


function oImageInfos() {

	var refMapSizeHighliteRegion= overviewPosToRefMapPos(sizeHighliteRegion);
	var x= (actPos.x-refMapSizeHighliteRegion.x/2)*sizeActualMap.x/aMaps[refMap].width;
	var y= (actPos.y-refMapSizeHighliteRegion.y/2)*sizeActualMap.y/aMaps[refMap].height;
	
	var markerDiffX= 0;
	var markerDiffY= 0;
	
	if(x<0) {
//		markerDiffX= x*aMaps[refMap].width/sizeActualMap.x;
		markerDiffX= x;
		x= 0;
	}
	
	if(y<0) {
//		markerDiffY= y*aMaps[refMap].height/sizeActualMap.y;
		markerDiffY= y;
		y= 0;
	}
	
	this.pieceX= parseInt(x/sizeActualMapPiece.x);
	this.pieceY= parseInt(y/sizeActualMapPiece.y);
	
	this.offsetX= parseInt(x%sizeActualMapPiece.x);
	this.offsetY= parseInt(y%sizeActualMapPiece.y);

	Debugger.debug('pX:'+this.pieceX+' pY:'+this.pieceY+' oX:'+this.offsetX+' oY:'+this.offsetY);
	
	this.markerDiffX= markerDiffX;
	this.markerDiffY= markerDiffY;
}


function drawMap() {
	var ii= new oImageInfos();

	if(displayConfiguration.positionInPixelVisible) {
		document.getElementById("displayPosition").firstChild.nodeValue= actPos.x+"px - "+actPos.y+"px";
	}
	
	for(var y=1; y<=numYMapPieces; y++) {
		for(var x=1; x<=numXMapPieces; x++) {
			for(var z=1; z<=numZLayers; z++) {

				var MapImg= new Image();
				var mapId= "map"+x+y+z;
				
				MapImg.src= imagePrefix+"-"+(ii.pieceY*1+y-1+aMaps[actualMap].imageYStartNum)+"-"+(ii.pieceX*1+x-1+aMaps[actualMap].imageXStartNum)+imagePostfix;
						
				// var img= createNode("img",
				//					 "src", MapImg.src);
				
				mapElement= document.getElementById(mapId);
				
				var clipLeft= (x==1?ii.offsetX:0);
				var clipTop= (y==1?ii.offsetY:0);
				var clipRight= (x==numXMapPieces?ii.offsetX:sizeActualMapPiece.x);
				var clipBottom= (y==numYMapPieces?ii.offsetY:sizeActualMapPiece.y);
	
				with(mapElement) {
					// replaceChild(img, mapElement.firstChild);
					style.clip= "rect("+clipTop+"px "+clipRight+"px "+clipBottom+"px "+clipLeft+"px)";
					innerHTML= "<img src='"+MapImg.src+"' />";
					style.left= ((x-1)*sizeActualMapPiece.x-ii.offsetX)+"px";
					style.top= ((y-1)*sizeActualMapPiece.y-ii.offsetY)+"px";
					// style.border= "solid black 1px";
					// style.zIndex= 0;
				}
			}
		}
	}
	
	zoomerPos.x+=ii.markerDiffX;
	zoomerPos.y+=ii.markerDiffY;
	
	setAttribute(document.getElementById("zoomer"), "style", "position:absolute;z-index:100;visibility:visible;"+getPositionStyle(zoomerPos));
}

function finder(Coord) {

	if(displayConfiguration.smoothScrolling) {
		oSmoothScrolling.init(actPos, Coord);
	} else {
		actPos= new oCoord(Coord.x, Coord.y);
		zoomerPos= new oCoord(actPos.x, actPos.y);
		updateMarker();
		drawMap();
	}
}


function displayCoordinates(event) {

	if(displayConfiguration.positionInGkVisible) {
		var mouseCoord= getMouseposition(event);
		var Coords= aMaps[actualMap].getPositionInGk(refMapPosToActMapPos(mousePosToRefMapPos(mouseCoord) ));
	
		document.getElementById("displayCoordinates").innerHTML= "R: "+Coords.x.getGkCoord()+"<br /> H: "+Coords.y.getGkCoord();
	}
}

function zoom() {
	var zoomIn= arguments.length>0?arguments[0]:true;
	var zoomLoop= arguments.length>1?arguments[1]:true;
	
	if(zoomIn) {
		if(zoomOrderActive.mapNumber++>=aZoomOrder[zoomOrderActive.zoomQueue].length-1) {
			if(zoomLoop) zoomOrderActive.mapNumber= 0;
			else zoomOrderActive.mapNumber= aZoomOrder[zoomOrderActive.zoomQueue].length-1;
		}
	} else {
		if(zoomOrderActive.mapNumber--<=0) {
			if(zoomLoop) zoomOrderActive.mapNumber= aZoomOrder[zoomOrderActive.zoomQueue].length-1;
			else zoomOrderActive.mapNumber= 0;
		}
	}
	
	initMap(aZoomOrder[zoomOrderActive.zoomQueue][zoomOrderActive.mapNumber], zoomOrderActive.zoomQueue);
	updateMarker();
	drawMap();
	
}

function initMap(mapName, zoomQueue) {

	actualMap= mapName;
	initBrowsertype();
	
	sizeActualMap.x= aMaps[mapName].width;
	sizeActualMap.y= aMaps[mapName].height;
	sizeActualMapPiece.x= aMaps[mapName].widthPiece;
	sizeActualMapPiece.y= aMaps[mapName].heightPiece;

	imagePrefix= aMaps[mapName].imagePrefix;
	imagePostfix= aMaps[mapName].imagePostfix;

	sizeDisplayMapareaPx.x= (numXMapPieces-1)*sizeActualMapPiece.x;
	sizeDisplayMapareaPx.y= (numYMapPieces-1)*sizeActualMapPiece.y;
	sizeDisplayMapareaRef= actMapPosToRefMapPos(sizeDisplayMapareaPx);

	sizeHighliteRegion.x= parseInt(sizeOverviewMap.x*sizeDisplayMapareaPx.x/sizeActualMap.x);
	sizeHighliteRegion.y= parseInt(sizeOverviewMap.y*sizeDisplayMapareaPx.y/sizeActualMap.y);

	sizeActualZoomer= actMapPosToRefMapPos(sizeZoomer);
	
	minLeft= posAbsOverviewMap.x+sizeHighliteRegion.x/2;
	maxLeft= posAbsOverviewMap.x-sizeHighliteRegion.x/2+sizeOverviewMap.x;
	minTop= posAbsOverviewMap.y+sizeHighliteRegion.y/2;
	maxTop= posAbsOverviewMap.y-sizeHighliteRegion.y/2+sizeOverviewMap.y;

//	if(actPos.x<0) {
//		actPos.x= 0;
//	} else if(actPos.x>aMaps[refMap].width-sizeDisplayMapareaRef.x/2) {
//		actPos.x= parseInt(aMaps[refMap].width-sizeDisplayMapareaRef.x/2);
//	}

//	if(actPos.y<0) {
//		actPos.y= 0;
//	} else if(actPos.y>aMaps[refMap].height-sizeDisplayMapareaRef.y/2) {
//		actPos.y= parseInt(aMaps[refMap].height-sizeDisplayMapareaRef.y/2);
//	}

	for(var zoomMap in aZoomOrder[zoomQueue]) {
		if(aZoomOrder[zoomQueue][zoomMap]==mapName) {
			zoomOrderActive.mapNumber= zoomMap;
			zoomOrderActive.zoomQueue= zoomQueue;
		}
	}

	zoomerPos.x= actPos.x;
	zoomerPos.y= actPos.y;

	var nodeMapArea= document.getElementById("mapArea");
	nodeMapArea.style.left= posAbsMap.x+"px";
	nodeMapArea.style.top= posAbsMap.y+"px";
	nodeMapArea.style.width= sizeDisplayMapareaPx.x;
	nodeMapArea.style.height= sizeDisplayMapareaPx.y;
	
	while(nodeMapArea.hasChildNodes()) nodeMapArea.removeChild(nodeMapArea.firstChild);

	for(var y=1; y<=numYMapPieces; y++) {
		for(var x=1; x<=numXMapPieces; x++) {
			for(var z=1; z<=numZLayers; z++) {
			
				var div= createNode("div", 
									"id", "map"+x+y+z,
									"style", "position:absolute;top:"+( (y-1)*sizeActualMapPiece.y )+"px; left:"+( (x-1)*sizeActualMapPiece.x )+"px;z-index:"+z,
									"onclick", "mapMouseEvent(event);",
									"onmousemove", "displayCoordinates(event);"
									);
									
				var img= createNode("img","onclick", "mapMouseEvent(event);");
				
				div.appendChild(img);
				nodeMapArea.appendChild(div);
			}
		}
	}
	
	 var zoomer= createNode("div",
							"id", "zoomer",
							"style", "position:absolute; visibility:visible;",
							"onclick", "zoom(true, true); rotMapSel(); mapMouseEvent(event); "
						);

	var zoomerImg= createNode(	"img",
								"src", imgZoomer);
	zoomer.appendChild(zoomerImg);
	nodeMapArea.appendChild(zoomer);
	
	if(displayConfiguration.overviewMapVisible) {
		with(document.getElementById("marker").style) {
			width= sizeHighliteRegion.x+"px";
			height= sizeHighliteRegion.y+"px";
			visibility= "visible";
		}
	
		with(document.getElementById("overviewMap").style) {
			left= posAbsOverviewMap.x+"px";
			top= posAbsOverviewMap.y+"px";
		}
	}
	
	if(displayConfiguration.measureVisible) {
		with(document) {
			getElementById("measure").style.visibility= 'visible';
			getElementById("measure").style.width= aMaps[mapName].measureWidth+"px";
			var tn= createTextNode(aMaps[mapName].measureText);
			getElementById("measureText").replaceChild(tn, getElementById("measureText").firstChild);
		}
	} else {
		with(document) {
			getElementById("measure").style.visibility= 'hidden';
		}	
	}
}

function displayImageInMapArea(html) {
	var nodeMapArea= document.getElementById("mapArea");
	nodeMapArea.style.left= posAbsMap.x+"px";
	nodeMapArea.style.top= posAbsMap.y+"px";
	nodeMapArea.style.width= sizeDisplayMapareaPx.x;
	nodeMapArea.style.height= sizeDisplayMapareaPx.y;

//	while(nodeMapArea.hasChildNodes()) {
//		nodeMapArea.removeChild(nodeMapArea.firstChild);
//	}
	
//	var img= createNode("img","src", imgSrc);
//	nodeMapArea.appendChild(img);
	if(displayConfiguration.measureVisible) {
		document.getElementById("measure").style.visibility= 'hidden';
	}
	if(displayConfiguration.overviewMapVisible) {
		document.getElementById("marker").style.visibility= 'hidden';
	}

	displayConfiguration.initMapInOverview= true;
	nodeMapArea.innerHTML= html;
}

var Debugger= {

	level: "nodebug",
	
	debug: function(text) {

		if(this.level=="debug") {
			document.getElementById("debug").appendChild(document.createTextNode("DEBUG: "+text));
			document.getElementById("debug").appendChild(document.createElement("br"));
		}
	},

	alertAttributes: function(obj) {
		var sOut= "";
		for(attribute in obj) {
			sOut+=attribute+":="+obj[attribute]+"  ,";
		}
		alert(sOut);
	}
};

function createNode(element) {
	var elementNode= document.createElement(element);
	for(var i=1; i<arguments.length; i=i+2) {
		setAttribute(elementNode, arguments[i], arguments[i+1]);
	}
	
	return elementNode;
}


// SW: wandelt einen Namen eines Stylesheet Eintrages in einen Dom/JS benutzbaren Namen um 
function changeCssNameJsConform(styleName) {
	var jsName= "";
	var parts= trim(styleName).split("-");
	for(var num in parts) {
		var part= parts[num];
		jsName+= num>0&&part.length>1?part.substr(0,1).toUpperCase()+part.substr(1,part.length-1):parts[num];
	}
	
	return jsName;
}

// SW: schneidet alle Leerzeichen vom Beginn einer Zeichenkette ab
function trim(text) {
	var pos;
	for(pos= 0; pos<text.length && text.charAt(pos)==" "; pos++);
	return text.substr(pos,text.length-pos);
}
