var Scale = 1050;
var Ebenen = [];
var MapDataLoaded = false;
var ImageMapLoaded = false;
var Redrawn = false;
var EbenenLoaded = false;
var ShowInfoBox = true;
var OpenedWindow = "none";
var SuchTreffer = [];
var ImageQueue = 0;
var TabOrder = 0;
var PrevTab;
var FSize = 2;
var Massstab = 0;
var X_Koord;
var Y_Koord;
var Zoom = false;
var Tool = "";

var KreisDB; //wird zur Laufzeit aus Datenbank geladen
var OrtDB; //wird zur Laufzeit aus Datenbank geladen

function gemeindeGeo(G, Datei, Ordner, Kachel_x, Kachel_y, Kachel_h, Kachel_gk, V_x, V_y, G_x, G_y){
	this.G = G; //wird zur Laufzeit aus Datenbank geladen
	this.Datei = Datei;
	this.Ordner = Ordner;
	this.Kachel_x = Kachel_x;
	this.Kachel_y = Kachel_y;
	this.Kachel_h = Kachel_h;
	this.Kachel_gk = Kachel_gk;
	this.V_x = V_x;
	this.V_y = V_y;
	this.G_x = G_x;
	this.G_y = G_y;
	this.K_x = 4403835; //linker oberer Eckpunkt der großen Übersichtskarte
	this.K_y = 5650473;
	this.K_xmax = 4452197; //rechter Eckpunkt der großen Übersichtskarte
	this.LB_x = 4408951; //linker oberer Eckpunkt des Luftbilds
	this.LB_y = 5643604;
	this.Overflow = 0;
	this.Anpassen = false;
}
var GemeindeGeo = new gemeindeGeo();

/*				*/
/*Festlegung der Skalierungsmaße*/
/*				*/
function scales(a, b, c, dx, dy, e, f1, f2, g){
	this.a = a;
	this.b = b;
	this.c = c;
	this.dx = dx;
	this.dy = dy;
	this.e = e;
	this.f1 = f1;
	this.f2 = f2;
	this.g = g;
}

function fonts(klein, mittel, standard, gross, sehrgross, h3){
	this.klein = klein;
	this.mittel = mittel;
	this.standard = standard;
	this.gross = gross;
	this.sehrgross = sehrgross;
	this.h3 = h3;
}

var Masse = {
	m600: new scales(66, 440, 0, 323, 401, 0, 15, 280, 74),
	m768: new scales(66, 570, 0, 418, 520, 30, 20, 300, 79),
	m900: new scales(66, 660, 0, 483, 600, 50, 30, 320, 85),
	m1050: new scales(66, 770, 0, 563, 700, 70, 40, 340, 90)
};

var Schrift = {
	m600: new fonts("8.5pt", "9pt", "10pt", "11pt", "12pt", "12.5pt"),
	m768: new fonts("9pt", "10pt", "11pt", "12pt", "13pt", "13pt"),
	m900: new fonts("10pt", "10.5pt", "11.5pt", "12pt", "13pt", "13.5pt"),
	m1050: new fonts("10pt", "11pt", "12pt", "13pt", "14pt", "14pt")
};

/*								  */
/*Allgemeine Zuweisungen, startet bei Laden der Dokumentenstruktur*/
/*								  */
$(document).ready(function(){
	//Laden der Vektorinformationen für die Kreiskarte

	$.ajax({
		type: "GET",
		url: "IlmMap.svg",
		dataType: "text",
		success: function(d){
			var e = parseXML(d);
			MapDataLoaded = parseSVG(e); }		
	});
	
	//Zuweisen der ImageMap beim ersten Überfahren der Karte
	
	$("#Karte").mouseover(function(){
		if(MapDataLoaded && !ImageMapLoaded){ 
			ImageMapLoaded = createImageMap("#Karte");
		};
		if(Redrawn && ImageMapLoaded){
			placeMasks();
			Redrawn = false;
		}
	});

	//Beim Erststart: Gemeindedaten aus Datenbank laden, Zur gewünschten Ansicht gehen
	
	ajaxSQL("SELECT GemeindeID, GemeindeName, VG, Z1, Z2, Z3, Z4, Z5, Z6, Z7, Z8, Z9, Z10, Z11 FROM Kreis JOIN KreisDaten ON Kreis.DatenID = KreisDaten.DatenID ORDER BY GemeindeName",
                    function(obj){
                    	loadListKreis(obj);
			var initialHash = $.history.getCurrent();
			parseHistoryHash(initialHash);
	});

	//Beim Nutzen der Navigationsfunktionen des Browsers
	
	$(window).history(function(e, hash){
		parseHistoryHash(hash);
	});

	//Neuaufbau beim Ändern der Fenstergröße
	
	$(window).wresize(function(){
		Redrawn = redraw(Ebenen.length);
	});
	
	//Tab-Reihenfolge definieren und Scrollen per Pfeiltaste im blauen Fenster
	
	$(document).keydown(function(e){
		if(e.which==9){
			if(PrevTab){ PrevTab.blur(); };
			if(TabOrder<0){
				return false;
			};
			var tap	= Math.floor(TabOrder / 256);
			var tao = TabOrder % 256;
			switch(tap){
				case 0:
					if(Ebenen.length==1){ TabOrder = 256; break; };
					$(".button_side").eq(tao).focus();
					PrevTab = $(".button_side").eq(tao);
					TabOrder++;
					while($(".button_side").eq(TabOrder).css("display")=="none"){
						TabOrder++;
					};
					if(TabOrder >= $(".button_side").length){ TabOrder = 256; };
					break;
				case 1:
					if( $("#Liste").position().left < -200 ){
						$(".pop_right").hide();
						$("#M_Liste").show();
						$("#B_Liste").children("img").attr("src", $("#B_Liste").children("img").data("bild_aktiv"));
						showList();
					};
					$(".list_item").eq(tao).children("a").focus();
					PrevTab = $(".list_item").eq(tao).children("a");
					TabOrder++;
					tao++;
					if(tao == $("#Liste_navi").children("li").length){ TabOrder = 512; };
					break;
				case 2:
					if(tao==0){
						$(".pop_right").hide();
						$("#Liste").css("left", "-1000px");
						$("#Liste_fuss").hide();
						$("#M_Suche").show();
						$("#B_Suche").children("img").attr("src", $("#B_Suche").children("img").data("bild_aktiv"));
						$("#B_Liste").children("img").attr("src", $("#B_Liste").children("img").data("bild_inaktiv"));
						$("#Begriff").focus();
						PrevTab = $("#Begriff");
						TabOrder++;
					} else if(tao==1){
						$("#Anfrage").focus();
						PrevTab = $("#Anfrage");
						TabOrder = 768;
					};
					break;
				case 3:
					if(tao==0){
						$(".pop_right").hide();
						$("#M_Font").show();
						$("#B_Font").children("img").attr("src", $("#B_Font").children("img").data("bild_aktiv"));
						$("#B_Suche").children("img").attr("src", $("#B_Suche").children("img").data("bild_inaktiv"));
					};
					$(".button_aaa").eq(tao).focus();
					PrevTab = $(".button_aaa").eq(tao);
					TabOrder++;
					if(TabOrder==770){ TabOrder = 1024; };
					break;
				case 4:
					if(tao==0){
						$(".pop_right").hide();
						$("#B_Font").children("img").attr("src", $("#B_Font").children("img").data("bild_inaktiv"));
					}
					$(".fussLink").eq(tao).focus();
					PrevTab = $(".fussLink").eq(tao);
					TabOrder++;
					tao++;
					if(tao == $(".fussLink").length){ TabOrder = 0; };
					break;
				case 5: //im blauen Fenster
					$("#Blue_window_Head a").blur();
					$("#Blue_window_Content").find("a").eq(tao).focus();
					tao++;
					if(tao > $("#Blue_window_Content").find("a").length){
						$("#Blue_window_Head a").focus();
						TabOrder = 1280;
					} else {
						TabOrder++;
					};
					break;
				case 6: //Bildergalerie
					$("#Blue_window_Head a").blur();
					var pic = $("#Blue_window_Content").find("img").eq(tao);
					var prevpic = $("#Blue_window_Content").find("img").eq(tao-1);
					var prevcontainer = prevpic.parent();
					prevcontainer.css({ height: 80, width: 80 });
					var w = Math.ceil( prevpic.width() / prevpic.height() * prevcontainer.height() );
					var h = Math.ceil( prevpic.height() / prevpic.width() * prevcontainer.width() );
					if (w < h) {
						prevpic.addClass("thumb").css({ height: 'auto', width: prevcontainer.width(), marginTop: -(h-prevcontainer.height())/2 });
					} else {
						prevpic.addClass("thumb").css({ width: 'auto', height: prevcontainer.height(), marginLeft: -(w-prevcontainer.width())/2 });
					};
					tao++;
					if(tao > $("#Blue_window_Content").find("img").length){
						$(".thumb").parent().show();
						$("#Blue_window_Content").css("height","auto");
						adjustWindow();
						$("#Blue_window_Head a").focus();
						TabOrder = 1536;
					} else {
						pic.removeClass("thumb").css({ height: 'auto', width: 650, margin: 0 })
						 .parent().show().css({ height: pic.height(), width: pic.width() });
						$(".thumb").parent().hide();
						adjustWindow();
						TabOrder++;
					};
					break;
			};		
			return false;
		} else if(e.which==38 || e.which==40){
			if( $("#Blue_window").length != 0 ){
				$("#Blue_window_Content").focus();
			} else {
				return false;
			};
		};
	});
	
	//Zuweisen der Wechsel-Bilder für Schaltflächen
	
	$.each($(".button img"), function(){
		var str = $(this).attr("src");
		$(this).data("bild_inaktiv", str);
		str = str.replace(/.png/i, "_a.png");
		$(this).data("bild_aktiv", str);
	});

	//Funktionsschaltfläche zum Verstecken/Zeigen der Infobox

	$("#Fenster_Info_hide").click(function(){
		if(ShowInfoBox){
			$("#Fenster_Info").hide();
			$("#Infotext").hide();
			ShowInfoBox = false;
		} else {
			$("#Fenster_Info").show();
			$("#Infotext").show();
			ShowInfoBox = true;
		};
		$("#Fenster_Info_arrow").toggleClass("arrow_l");
		$("#Fenster_Info_arrow").toggleClass("arrow_r");
		Redrawn = redraw(Ebenen.length);
	});
	$("#Fenster_Info_hide").hover(function(){
		$("#Fenster_Info_hide").css("background","#ff3427");
			},function(){
		$("#Fenster_Info_hide").css("background","#d0d070");
	});

	//Infotext für Schaltflächen an linker Seite

	$(".button_side").hover(function(){
		$(this).prev().show();
		    },function(){
		$(this).prev().hide();
	}).focus(function(){
		$(this).prev().show();
	}).blur(function(){ 
		$(this).prev().hide(); 
	});
	
	//Infotext für Wappen und Schaltflächen in Menüleiste
	
	$(".button_unter").hover(function(){
		$("#ToolTip").text($(this).children("img").attr("alt")).show().css( { left: $(this).position().left, top: 70 } );
	 	    },function(){
	 	$("#ToolTip").empty().hide();
	}).focus(function(){
		$("#ToolTip").text($(this).children("img").attr("alt")).show().css( { left: $(this).position().left, top: 70 } );
	}).blur(function(){ 
		$("#ToolTip").empty().hide();
	});
	
	
	$("#Wappen").hover(function(){
		$("#ToolTip").text("Zurück zum Kreis").show().css( { left: 135, top: 82 } );
		    }, function(){
		$("#ToolTip").empty().hide();
	});

	//Aufschiebungen im Hauptmenü - generell

	$(".button").mouseover(function(){
		$(".pop_right").hide();
		$(this).next().show();
		$.each($(".button img"), function(){
			$(this).attr("src", $(this).data("bild_inaktiv"));
		});
		var bt = $(this).children("img");
		bt.attr("src", bt.data("bild_aktiv"));
	});
	$("#Fenster, #Wappen").mouseover(function(){
		$(".pop_right").hide();
		$("#Liste").css("left", "-1000px");
		$("#Liste_fuss").hide();
		$.each($(".button img"), function(){
			$(this).attr("src", $(this).data("bild_inaktiv"));
		});
	});

	//Aufschiebungen im Hauptmenü - Navigationsliste

	$("#B_Liste").mouseover(showList);
	
	$("#B_Suche, #B_Font, #B_Karte").mouseover(function(){
		$("#Liste").css("left", "-1000px");
		$("#Liste_fuss").hide();
	});
	
	//Aufschiebungen im Hauptmenü - Suche
	
	$("#Anfrage").click( windowSearch );
	
	//Aufscheibungen im Hauptmenü - Karte
	
	$("#Legende").click(function(){
		var query = "SELECT TypID, TypName FROM Typen";
		ajaxSQL(query, windowLegend);
	});
	$("#ZoomIn").click(function(){
		Tool = "Plus";
		$(".luftbild").css("cursor","url(./pic/symbols/zoom_in.cur), e-resize");
	});
	$("#ZoomOut").click(function(){
		Tool = "Minus";
		$(".luftbild").css("cursor","url(./pic/symbols/zoom_out.cur), n-resize");
	});
	
	//Aufschiebungen im Hauptmenü - Schriftgröße

	$("#Font_hoch").click(function(){
		if(FSize<4){
			FSize++;
			Redrawn = redraw(Ebenen.length, "fontonly");
		};
		return false;
	});
	$("#Font_runter").click(function(){
		if(FSize>0){
			FSize--;
			Redrawn = redraw(Ebenen.length, "fontonly");
		};
		return false;
	});

	//Maussensitive Kreiskarte
	
	$("#Kreiskarte area").mouseover(function(){
		var maske_x = $("#Karte").position().left + $(this).data("X_min") + 1;
		var maske_y = $("#Karte").position().top + $(this).data("Y_min") + 1;
		$(".karte_maske").hide();
		$("#Karte_" + $(this).attr("id")).show().css("behavior","url(iepngfix.htc)");
		
		var i = 0;
		while(KreisDB[i].GemeindeID != $(this).attr("id")){
			i++;
		};
		$("#Infotext").empty();
		if(KreisDB[i].VG == 1){
			$("#Infotext").append("<h3>Verwaltungsgemeinschaft</h3><br />");
		};
		$("#Infotext").append("<h3>" + KreisDB[i].GemeindeName + "</h3><br /><br />");
		$("#Infotext").append(KreisDB[i].Z1 + "<br />");
		$("#Infotext").append(KreisDB[i].Z2 + "<br />");
		$("#Infotext").append(KreisDB[i].Z3 + "<br />");
		$("#Infotext").append(KreisDB[i].Z4 + "<br />");
		if(KreisDB[i].Z5!=""){ $("#Infotext").append(KreisDB[i].Z5 + "<br />"); }
		if(KreisDB[i].Z6!=""){ $("#Infotext").append(KreisDB[i].Z6 + "<br />"); }
		if(KreisDB[i].Z7!=""){ $("#Infotext").append(KreisDB[i].Z7 + "<br />"); }
		if(KreisDB[i].Z8!=""){ $("#Infotext").append(KreisDB[i].Z8 + "<br />"); }
		if(KreisDB[i].Z9!=""){ $("#Infotext").append(KreisDB[i].Z9 + "<br />"); }
		if(KreisDB[i].Z10!=""){ $("#Infotext").append(KreisDB[i].Z10 + "<br />"); }
		if(KreisDB[i].Z11!=""){ $("#Infotext").append(KreisDB[i].Z11 + "<br />"); }
	});
	$(".k_maske").mouseout(function(){
		$(".karte_maske").hide();
	});
	$(".k_maske").click(function(){
		followLink(this);
		return false;
	});
	
	//große Buttons auf der linken Seite
	
	$(".button_side").click(function(){
		if(Zoom && $(this).attr("id")=="B_Zurueck"){
			Zoom = false;
			var top = Ebenen.length - 1;
			parseHistoryHash(Ebenen[top] + "&" + top + "&reload");	
		} else {
			followLink(this);	
		};
		return false;
	});

	//Wappen führt zu Kreisansicht

	$("#Wappen").click(function(){
		goToKreis();
	});
	
	//Imagefilm aufrufen
	
	/*$("#Imagefilm").click(function(){
		windowFilm("Ilm-Kreis Imagefilm");
	});*/
});

/*                                       */
/*Daten asynchron aus der Datenbank laden*/
/*                                       */
function ajaxSQL(queryStr, callback, param) {
	$.ajax({
		type: "POST",
		url: "sql/dbquery.php",
		data: {querystr: queryStr},
		dataType: "json",
		success: function(d){
			if(!param){
				callback.call(this, d);
			}else{
				callback.call(this, d, param);
			}
		},
		error: function(a,b,c){
			alert(b + ": Fehler beim Zugriff auf Datenbank");
		}
        });
}

/*						   			   */
/*Eine Ganzzahl in eine Hexadezimale Zahl umwandeln mit vorgesetzten Nullen*/
/*						  			   */
function intToHex(num) {
    var numVals  = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
    var result = "";
    var work = num;
    while(work > 0) {
      result = numVals[work % 16] + result;
      work = Math.floor(work / 16);
    };
    while(result.length != 8){
      result = "0" + result;
    };
    return result;
}

/*															   */		
/*Zur gewünschten Ansicht wechseln, beim Benutzen der History-Funktion des Browsers oder beim Aufrufen einer bestimmten URL*/
/*															   */
function parseHistoryHash(hash){
	TabOrder = 0;
	EbenenLoaded = false;
	ListLoaded = false;
	
	Ebenen[0] = "IlmKreis";
	if(hash == "" || hash == "IlmKreis") {
		goToKreis();
	} else {
		var pHash = hash.split("&");
		if( pHash[0] != Ebenen[Ebenen.length-1] || pHash[2]=="reload"){
			showLoading();
			pHash[1] = parseInt(pHash[1]);
			if( pHash[1] > 1 ){
				Ebenen = $.grep(Ebenen, function(n,i){ return i==0; });
				Ebenen[pHash[1]] = pHash[0];
				EbenenLoaded = fillEbenen(pHash[1]);
			} else if( pHash[1] == 1 ){
			        Ebenen = $.grep(Ebenen, function(n,i){ return i<=1; });
				Ebenen[1] = pHash[0];
				goToGemeinde(pHash[0]);
				EbenenLoaded = true;
			};
		};
		switch(pHash[2]){
			case "reload":
			case "none": 
				$("#Blue_window").remove();
				break;
			case "info": 
				windowInfo();
				break;
			case "gallery":
				var query = "SELECT OrtsName, BildURL FROM Orte JOIN OrtsBilder ON Orte.BilderID=OrtsBilder.BilderID WHERE OrtsID='" + Ebenen[Ebenen.length-1] + "'";
				ajaxSQL(query, windowGallery);
				break;
			case "links":
				var query = "SELECT OrtsName, LinkURL, LinkBeschreibung FROM Orte JOIN OrtsLinks ON Orte.LinkID=OrtsLinks.LinkID WHERE OrtsID='" + Ebenen[Ebenen.length-1] + "'";
				ajaxSQL(query, windowLinks);
				break;
		};
	};
}

/*				     */
/*rekursives Füllen des Arrays Ebenen*/
/*				     */
function fillEbenen(bisHier){
	if( !Ebenen[bisHier-1] ){
		ajaxSQL("SELECT OberID FROM Orte WHERE OrtsID = '" + Ebenen[bisHier] + "'", function(obj){
			Ebenen[bisHier-1] = obj.data[0].OberID;
			fillEbenen(bisHier-1);
		});
	} else {
		goToOrt(Ebenen[Ebenen.length-1]);
	}
	return true;
}

/*			   		       	             */
/*Navigationsliste erstellen und Daten den Gemeinden zuweisen*/
/*			  		                     */
function loadListKreis(obj){
	KreisDB = obj.data;
	$.each(KreisDB, function(){
		if(this.VG == 1){
			$("#Liste_navi").append("<li id='" + this.GemeindeID + "' class='list_item list_ebene1 list_town'><a href='#" + this.GemeindeID + "&1&none'>VG " + this.GemeindeName + "</a>");
		}
		else{
			$("#Liste_navi").append("<li id='" + this.GemeindeID + "' class='list_item list_ebene1 list_town'><a href='#" + this.GemeindeID + "&1&none'>" + this.GemeindeName + "</a>");		
		}
	});
	
	$(".list_ebene1").click(function(){
	  	parseHistoryHash($(this).attr("id") + "&1&none");
	});
}

/*				        */
/*rekursives Füllen der Navigationsliste*/
/*					*/
function refreshList(){
	$(".list_ebene2, .list_ebene3, .list_ebene4").remove();
	query = "SELECT OrtsID, OrtsName, Typ FROM Orte WHERE OberID='" + Ebenen[1] + "' ORDER BY OrtsName DESC";
	ajaxSQL(query, loadListOrt, 1);
	return true;
}

/*		       */
/*Listenebene erstellen*/
/*		       */
function loadListOrt(obj, ebene){
	ebene++;
	
        $.each(obj.data, function(){
		$("#Liste_navi").children("#" + Ebenen[ebene-1]).after("<li id='" + this.OrtsID + "' class='list_item list_ebene" + ebene + "'><a href='#" + this.OrtsID + "&" + ebene + "&none'>" + this.OrtsName + "</a>");
		if( this.Typ=="Ortschaft" || this.Typ=="Ortsteil" ){
			$("#Liste_navi").children("#" + this.OrtsID).addClass("list_town");
		}else{
			$("#Liste_navi").children("#" + this.OrtsID).addClass("list_landmark");
		};
	});
	if( ebene<=(Ebenen.length-1) ){
		query = "SELECT OrtsID, OrtsName, Typ FROM Orte WHERE OberID='" + Ebenen[ebene] + "' ORDER BY OrtsName DESC";
		ajaxSQL(query, loadListOrt, ebene);
	} else if( ebene>(Ebenen.length-1) ){
		$(".list_aktiv").removeClass("list_aktiv");
		$("#Liste_navi").children("#" + Ebenen[Ebenen.length-1]).addClass("list_aktiv");
		
		$(".list_ebene2").click(function(){
			parseHistoryHash($(this).attr("id") + "&2&none");
		});
		$(".list_ebene3").click(function(){
			parseHistoryHash($(this).attr("id") + "&3&none");
		});
		$(".list_ebene4").click(function(){
			parseHistoryHash($(this).attr("id") + "&4&none");
		});
	};
}

/*			   */
/*Navigationsliste anzeigen*/
/*			   */
function showList(){
	$("#Liste").css({
		"height":"auto",
		"overflow":"hidden"});
	$("#Liste").css("left", ( $("#M_Liste").position().left - 67) + "px");
	$("#Liste").css("top", $("#Kopf_Menu").height() + "px");
	var l_height = $("#Liste").height();
	var f_height = $("#Fenster").height();
	if( l_height > 0.8 * f_height ){
		$("#Liste").height(0.8 * f_height).css("overflow","scroll");
		var w = $("#Liste").width()  + 43;
		var t = $("#Liste").position().top + $("#Liste").height() - 17;
		var l = $("#Liste").position().left;
		$("#Liste_fuss").show().css({
			"width": w + "px",
			"top": t + "px",
			"left": l + "px" });
	};
	$("#M_Liste").width($("#Liste").width() - 7);
}

/*				   */
/*Links führen parseHistoryHash aus*/
/*				   */
function followLink(verweis){
	var goToHash = $(verweis).attr("href");
	goToHash = goToHash.substring( goToHash.lastIndexOf("#")+1 );
	parseHistoryHash(goToHash);
}

/*		          */
/*Blaues Fenster erstellen*/
/*		          */
function createWindow(titel){
	$("#Blue_window").remove();
	$("body").append("<div id='Blue_window'></div>");
	$("#Blue_window").append("<div id='Blue_window_Head'><h3 class='schrift_weiss'>" + titel + "</h3></div>");
	$("#Blue_window_Head").append("<a href='#'><img src='./pic/symbols/Close.gif' /></a>");
	$("#Blue_window_Head").append("<div class='handle'>&nbsp;<div>");
	$("#Blue_window").corner({
		tl: { radius: 18 },
		tr: { radius: 18 },
		bl: { radius: 18 },
		br: { radius: 18 },
		antiAlias: true,
		autoPad: false 
	});
	$("#Blue_window_Head").corner({
			tl: { radius: 18 },
			tr: { radius: 18 },
			bl: false,
			br: false,
			antiAlias: true,
			autoPad: true
	});
	$("#Blue_window").css({
		position: "absolute",
		left: "250px",
		top: "150px"
	});
	
	if(titel!="Suchergebnisse"){
		var e = Ebenen.length - 1;
		$.history.add(Ebenen[e] + "&" + e + "&" + OpenedWindow);
	};
	
	$("#Blue_window_Head").bind("dragstart", function( event ){
		return $(event.target).is(".handle");
	}).bind("drag", function( event ){
		$("#Blue_window").css({
			top: event.offsetY,
			left: event.offsetX
		});
        });
        
        $("#Blue_window_Head a").click(function(){
        	OpenedWindow = "none"
        	if( $("#Blue_window_Head h3").html() != "Suchergebnisse" ){
        		var e = Ebenen.length - 1;
			$.history.add(Ebenen[e] + "&" + e + "&" + OpenedWindow);
		};
		$("#Blue_window").remove();
		TabOrder = 0;
		return false;
        }).focus(function(){
        	$(this).children("img").attr("src", "./pic/symbols/Close_a.gif");
        }).blur(function(){
        	$(this).children("img").attr("src", "./pic/symbols/Close.gif");
        });
        $("#Blue_window_Head a").focus();       
        $("#Blue_window_Head a img").hover(function(){
        	$(this).attr("src", "./pic/symbols/Close_a.gif");
        },function(){
        	$(this).attr("src", "./pic/symbols/Close.gif");
        });
        
        TabOrder = 1280;
}

/*		                    */
/*Größe des blauen Fensters anpassen*/
/*		                    */
function adjustWindow(){
	if( $("#Blue_window_Content").height() > 530 ){
		$("#Blue_window_Content").height(530);
	};
	$("#Blue_window").height( $("#Blue_window_Content").height()+30 );
}

/*				   */
/*Fenster "Wissenswertes" erstellen*/
/*				   */
function windowInfo(){
        OpenedWindow = "info";
	var text = $("#BlindText").html();
	if(!text){
		var query = "SELECT OrtsName, Text FROM Orte JOIN OrtsTexte ON Orte.OrtsID = OrtsTexte.TextID WHERE OrtsID='" + Ebenen[Ebenen.length-1] + "'";
		ajaxSQL(query, function(obj){
			var titel = obj.data[0].OrtsName + " - Wissenswertes";
			createWindow(titel);
			$("#Blue_window").append("<div id='Blue_window_Content'>" + obj.data[0].Text + "<br />&nbsp;</div>");
			adjustWindow();
		});
	} else {
		var titel = $("#Momentan").text() + " - Wissenswertes";
		createWindow(titel);
		$("#Blue_window").append("<div id='Blue_window_Content'>" + text + "<br />&nbsp;</div>");
		adjustWindow();
	};
	
	$(".intern").click(function(){
		followLink(this);
		return false;
	});
}

/*			           */
/*Fenster "Bildergalerie" erstellen*/
/*			           */
function windowGallery(obj){
	OpenedWindow = "gallery";
	var titel = obj.data[0].OrtsName + " - Bildergalerie";
	createWindow(titel);
	TabOrder = 1536;
	$("#Blue_window").append("<div id='Blue_window_Content'><ul class='gallery'></ul></div>");
	$.each(obj.data, function(i){
		$(".gallery").append("<li><img class='thumb' id='thumb" + i + "' src='./pic/galerie/" + this.BildURL + "' alt='" + this.OrtsName + "' title='" + this.OrtsName + "'></li>");
		$("#thumb" + i).load(function(){
			var container = $(this).parent();
			var w = Math.ceil( $(this).width() / $(this).height() * container.height() );
			var h = Math.ceil( $(this).height() / $(this).width() * container.width() );
			if (w < h) {
				$(this).css({ height: 'auto', width: container.width(), marginTop: -(h-container.height())/2 });
			} else {
				$(this).css({ width: 'auto', height: container.height(), marginLeft: -(w-container.width())/2 });
			}
		}).click(function(){
		        var container = $(this).parent();
			if($(this).hasClass("thumb")){
				$(this).removeClass("thumb").css({ height: 'auto', width: 650, margin: 0 });
				container.css({ height: $(this).height(), width: $(this).width() });
				$(".thumb").parent().hide();
				adjustWindow();
			} else {
			        container.css({ height: 80, width: 80 });
				var w = Math.ceil( $(this).width() / $(this).height() * container.height() );
				var h = Math.ceil( $(this).height() / $(this).width() * container.width() );
				if (w < h) {
					$(this).addClass("thumb").css({ height: 'auto', width: container.width(), marginTop: -(h-container.height())/2 });
				} else {
					$(this).addClass("thumb").css({ width: 'auto', height: container.height(), marginLeft: -(w-container.width())/2 });
				}
				$(".thumb").parent().show();
				$("#Blue_window_Content").css("height","auto");
				adjustWindow();
			};
		});
	});
	adjustWindow();
}

/*					  */
/*Fenster "Weiterführende Links" erstellen*/
/*					  */
function windowLinks(obj){
	OpenedWindow = "links";
	var titel = obj.data[0].OrtsName + " - Weiterführende Links";
	createWindow(titel);
	$("#Blue_window").append("<div id='Blue_window_Content'><ul class='linkList'></ul></div>");
	$.each(obj.data, function(i){
		$(".linkList").append("<li><a href='" + this.LinkURL + "' target='_blank'>" + this.LinkBeschreibung + "</a></li>");
	});
	adjustWindow();
}

/*			     */
/*Fenster "Legende" erstellen*/
/*			     */
function windowFilm(titel){
	createWindow(titel);
	$("#Blue_window").append("<div id='Blue_window_Content'></div>");
}

/*			     */
/*Fenster "Legende" erstellen*/
/*			     */
function windowLegend(obj){
	createWindow("Legende");
	$("#Blue_window").append("<div id='Blue_window_Content'><table id='LegTabelle'></table></div>");
	var gefundeneTypen = 0;
	$.each(obj.data, function(){
		var typid = this.TypID;
		var gefunden = false;
		for(i=0; i<$(".punkt").length; i++){
			var src = $(".punkt").eq(i).children("img").attr("src");
			src = src.match(/_\w+\.png/);
			src = src[0].replace(/_(\w+)\.png/, "$1");
			if(src==typid){
				gefunden = true;
				gefundeneTypen++;
				break;
			};
		};
		if(gefunden){
			if(gefundeneTypen%2!=0){
				$("#LegTabelle").append("<tr><td><img alt='" + this.TypName + "' src='./pic/symbols/Punkt_" + typid + ".png' /></td><td class='leg_b'>" + this.TypName + "</td></tr>");
			} else {
				$("#LegTabelle tr:last").append("<td><img alt='" + this.TypName + "' src='./pic/symbols/Punkt_" + typid + ".png' /></td><td class='leg_b'>" + this.TypName + "</td>");
			};
		};
	});
	adjustWindow();
}		

/*				    */
/*Fenster "Suchergebnisse" erstellen*/
/*				    */
function windowSearch(){
	var searchStr = $("#Begriff").val();
	if (searchStr=="" || searchStr=="Suchbegriff"){
		return false;
	};
	searchStr = searchStr.replace(/#/g, "");

	createWindow("Suchergebnisse");
	$("#Blue_window").append("<div id='Blue_window_Content'><ul class='suche1'>Beste Treffer:<br /></ul><ul class='suche2'>Weitere Treffer:<br /></ul></div>");
	
	SuchTreffer = [];
	
	var searchArr = searchStr.split(" ");
	searchArr = $.grep(searchArr, function(n,i){ return (n.length>3) });
	
	var query = "SELECT OrtsID, OrtsName, Ebene FROM Orte WHERE ";
	$.each(searchArr, function(i){
		var kl = this.toLowerCase();
		var gr = this.charAt(0).toUpperCase() + this.slice(1);
		query = query + "(Ortsname LIKE '%" + kl + "%' OR Ortsname LIKE '%" + gr + "%')"
		if( i!=(searchArr.length-1) ){
			query = query + " AND ";
		};
	});
	ajaxSQL(query, function(obj){
		searchResult1(obj);
		query = "SELECT OrtsID, OrtsName, Ebene FROM Orte WHERE ";
		$.each(searchArr, function(i){
			var kl = this.toLowerCase();
			var gr = this.charAt(0).toUpperCase() + this.slice(1);
			query = query + "Ortsname LIKE '%" + kl + "%' OR Ortsname LIKE '%" + gr + "%'"
			if( i!=(searchArr.length-1) ){
				query = query + " OR ";
			};
		});
		ajaxSQL(query, function(obj2){
			searchResult2(obj2);
			query = "SELECT OrtsID, OrtsName, Ebene, Text FROM Orte JOIN OrtsTexte ON Orte.TextID=OrtsTexte.TextID WHERE ";
			$.each(searchArr, function(i){
				var kl = this.toLowerCase();
				var gr = this.charAt(0).toUpperCase() + this.slice(1);
				query = query + "Text LIKE '%" + kl + "%' OR Text LIKE '%" + gr + "%'"
				if( i!=(searchArr.length-1) ){
					query = query + " OR ";
				};
			});
			ajaxSQL(query, function(obj3){
				searchResult3(obj3);
				adjustWindow();
			});
		});
	});
	return false;
}

function searchResult1(obj) {
	if(obj.data.length == 0){ 
		$(".suche1").append("...keine genauen Übereinstimmungen gefunden");
		return false; 
	};
	$.each(obj.data, function(){
		$(".suche1").append("<li><a href='#"+ this.OrtsID + "&" + this.Ebene + "&none'>" + this.OrtsName + "</a></li>");
		SuchTreffer.push(this.OrtsID);
	});
	$(".suche1 li a").click(function(){
		followLink(this);
		return false;
	});
}

function searchResult2(obj) {
	if(obj.data.length == 0){ return false; };
	$.each(obj.data, function(){
		var extest = false;
		for(var j=0; j<SuchTreffer.length; j++){
			if( this.OrtsID == SuchTreffer[j] ){
				extest = true;
			};
		};
		if(!extest){
			$(".suche2").append("<li><a href='#"+ this.OrtsID + "&" + this.Ebene + "&none'>" + this.OrtsName + "</a></li>");
			SuchTreffer.push(this.OrtsID);
		};
	});
}

function searchResult3(obj) {
	if(obj.data.length == 0 && $(".suche2 li").length == 0){
		$(".suche2").append("...keine weiteren Übereinstimmungen gefunden");
		return false; 
	};
	$.each(obj.data, function(){
		var extest = false;
		for(var j=0; j<SuchTreffer.length; j++){
			if( this.OrtsID == SuchTreffer[j] ){
				extest = true;
			};
		};
		if(!extest){
			$(".suche2").append("<li><a href='#"+ this.OrtsID + "&" + this.Ebene + "&none'>" + this.OrtsName + "</a></li>");
			SuchTreffer.push(this.OrtsID);
		};
	});
	$(".suche2 li a").click(function(){
		followLink(this);
		return false;
	});
}



/*				  				  */
/*Infobox rechts mit Text befüllen und Titel in #Momentan einfügen*/
/*				  				  */
function fillInfoBox(ansicht) {
	$("#Infotext").empty();
	switch(ansicht){
		case 1: //Kreisansicht
			$("#Infotext").append("<h3>Ilm-Kreis</h3><br /><br />");
			$("#Infotext").append("Bundesland: Thüringen<br />");
			$("#Infotext").append("Verwaltungssitz: Arnstadt<br />");
			$("#Infotext").append("Fläche: 843,3 km²<br />");
			$("#Infotext").append("Einwohner: 114.445<br />");
			$("#Infotext").append("Bevölkerungsdichte:136 Einwohner je km²<br />");
			$("#Infotext").append("Kfz-Kennzeichen: IK<br />");
			$("#Momentan").children("h1").text("Ilm-Kreis");
			break;
			
		case 2: //Gemeindeansicht
			var i = 0;
			while(KreisDB[i].GemeindeID != Ebenen[ansicht-1]){
				i++;
			};
			if(KreisDB[i].VG == 1){
				$("#Infotext").append("<h3>Verwaltungsgemeinschaft</h3><br />");
			};
			$("#Infotext").append("<h3>" + KreisDB[i].GemeindeName + "</h3><br /><br />");
			$("#Infotext").append(KreisDB[i].Z1 + "<br />");
			$("#Infotext").append(KreisDB[i].Z2 + "<br />");
			$("#Infotext").append(KreisDB[i].Z3 + "<br />");
			$("#Infotext").append(KreisDB[i].Z4 + "<br />");
			if(KreisDB[i].Z5!=""){ $("#Infotext").append(KreisDB[i].Z5 + "<br />"); }
			if(KreisDB[i].Z6!=""){ $("#Infotext").append(KreisDB[i].Z6 + "<br />"); }
			if(KreisDB[i].Z7!=""){ $("#Infotext").append(KreisDB[i].Z7 + "<br />"); }
			if(KreisDB[i].Z8!=""){ $("#Infotext").append(KreisDB[i].Z8 + "<br />"); }
			if(KreisDB[i].Z9!=""){ $("#Infotext").append(KreisDB[i].Z9 + "<br />"); }
			if(KreisDB[i].Z10!=""){ $("#Infotext").append(KreisDB[i].Z10 + "<br />"); }
			if(KreisDB[i].Z11!=""){ $("#Infotext").append(KreisDB[i].Z11 + "<br />"); }
			$("#Momentan").children("h1").text(KreisDB[i].GemeindeName);
			break;
			
		case 3: //Ortsansicht
			if(Zoom){ break; };
			if(OrtDB.Z1){
				$("#Infotext").append("<h3>" + OrtDB.OrtsName + "</h3><br /><br />");
				$("#Infotext").append(OrtDB.Z1 + "<br />");
				if(OrtDB.Z2){ $("#Infotext").append(OrtDB.Z2 + "<br />"); }
				if(OrtDB.Z3){ $("#Infotext").append(OrtDB.Z3 + "<br />"); }
				if(OrtDB.Z4){ $("#Infotext").append(OrtDB.Z4 + "<br />"); }
				if(OrtDB.Z5){ $("#Infotext").append(OrtDB.Z5 + "<br />"); }
				if(OrtDB.Z6){ $("#Infotext").append(OrtDB.Z6 + "<br />"); }
				if(OrtDB.Z7){ $("#Infotext").append(OrtDB.Z7 + "<br />"); }
				if(OrtDB.Z8){ $("#Infotext").append(OrtDB.Z8 + "<br />"); }
				if(OrtDB.Z9){ $("#Infotext").append(OrtDB.Z9 + "<br />"); }
				if(OrtDB.Z10){ $("#Infotext").append(OrtDB.Z10 + "<br />"); }
				if(OrtDB.Z11){ $("#Infotext").append(OrtDB.Z11 + "<br />"); }
			} else {
				$("#Infotext").append("<div id='NoDataAvailable'></div>");
			};
			$("#Momentan").children("h1").text(OrtDB.OrtsName);
			break;
	};
}

/*			   		   */
/*Kartendaten der Gemeindeansicht berechnen*/
/*			  		   */
function loadGemeindeGeo(obj) {
	fillInfoBox(2);
	Massstab = 16000;
	GemeindeGeo.G = obj.data;
	GemeindeGeo.G[0].Xmin = parseInt(GemeindeGeo.G[0].Xmin);
	GemeindeGeo.G[0].Xmax = parseInt(GemeindeGeo.G[0].Xmax);
	GemeindeGeo.G[0].Ymin = parseInt(GemeindeGeo.G[0].Ymin);
	GemeindeGeo.G[0].Ymax = parseInt(GemeindeGeo.G[0].Ymax);
	GemeindeGeo.G_x = Math.round(GemeindeGeo.G[0].Xmin + ((GemeindeGeo.G[0].Xmax - GemeindeGeo.G[0].Xmin) / 2));
	GemeindeGeo.G_y = Math.round(GemeindeGeo.G[0].Ymin + ((GemeindeGeo.G[0].Ymax - GemeindeGeo.G[0].Ymin) / 2));
	GemeindeGeo.Kachel_gk = 2845;
	GemeindeGeo.Datei = Math.floor( (GemeindeGeo.G_x - GemeindeGeo.K_x) / 2845 );
	GemeindeGeo.Ordner = Math.floor( (GemeindeGeo.K_y - GemeindeGeo.G_y) / 2845 );
	GemeindeGeo.V_x = (GemeindeGeo.G_x - GemeindeGeo.K_x) % 2845;
	GemeindeGeo.V_y = (GemeindeGeo.K_y - GemeindeGeo.G_y) % 2845;
	Redrawn = redraw(Ebenen.length);
}

/*			   	       */
/*Kartendaten der Ortsansicht berechnen*/
/*			  	       */
function loadOrtsGeo(obj) {
	if(!obj){  
	 //bei Zoom
	 	OrtDB = {Anpassen:1, Pfeil:0};
	 	Zoom = true;
		GemeindeGeo.G_x = X_Koord;
		GemeindeGeo.G_y = Y_Koord;
	} else {   
	//bei normaler Navigation
		OrtDB = obj.data[0];
		fillInfoBox(3);
		GemeindeGeo.G_x = parseInt(OrtDB.X);
		GemeindeGeo.G_y = parseInt(OrtDB.Y);
		Massstab = parseInt(OrtDB.Massstab);
	};
	var em;
	var fo;
	if(Massstab<6000){
	        GemeindeGeo.Kachel_gk = 169.3;
	        em = 2500;
	        fo = 0;
	}else{
		GemeindeGeo.Kachel_gk = 406.5;
	        em = 6000;
	        fo = 4096;
	};
	GemeindeGeo.Datei = Math.floor( (GemeindeGeo.G_x - GemeindeGeo.LB_x) / GemeindeGeo.Kachel_gk );
	GemeindeGeo.Ordner = fo + Math.floor( (GemeindeGeo.LB_y - GemeindeGeo.G_y) / GemeindeGeo.Kachel_gk );
	GemeindeGeo.V_x = (GemeindeGeo.G_x - GemeindeGeo.LB_x) % GemeindeGeo.Kachel_gk;
	GemeindeGeo.V_y = (GemeindeGeo.LB_y - GemeindeGeo.G_y) % GemeindeGeo.Kachel_gk;
	GemeindeGeo.Kachel_h = Math.round( em * 256 / Massstab );
	Redrawn = redraw(Ebenen.length);
}

/*					*/
/*Neuaufbau der Seite gemäß Fenstergröße*/
/*					*/
function redraw(ansicht, spezial) {
	var p_top = null;
	var p_left = null;
	var scaleObj;
	var fontObj;
	
	if(spezial != "fontonly"){
		$(".pointer").remove();
		$(".pop_right").hide();
		$(".karte_maske").hide();
	};
        	
	//Erkennung der Fenstergröße
	var ScaleAlt = Scale;
	var wh = $(window).height();
	if(wh >= 860){ Scale = 1050; }
	else if(wh >= 750){ Scale = 900; }
	else if(wh >= 650){ Scale = 768; }
	else { Scale = 600; };
	
	switch(Scale) {
		case 600: scaleObj = Masse.m600; fontObj = Schrift.m600; break;				
		case 768: scaleObj = Masse.m768; fontObj = Schrift.m768; break;		
		case 900: scaleObj = Masse.m900; fontObj = Schrift.m900; break;
		case 1050: scaleObj = Masse.m1050; fontObj = Schrift.m1050; break;
	};
	
	//Skalierung gemäß Fenstergröße
	
	switch(FSize) {
		case 0: $("style").replaceWith("<style type='text/css'>div{font-size:" + fontObj.klein + ";} h3{font-size:" + fontObj.h3 + ";}</style>"); break;
		case 1: $("style").replaceWith("<style type='text/css'>div{font-size:" + fontObj.mittel + ";} h3{font-size:" + fontObj.h3 + ";}</style>"); break;
		case 2: $("style").replaceWith("<style type='text/css'>div{font-size:" + fontObj.standard + ";} h3{font-size:" + fontObj.h3 + ";}</style>"); break;
		case 3: $("style").replaceWith("<style type='text/css'>div{font-size:" + fontObj.gross + ";} h3{font-size:" + fontObj.h3 + ";}</style>"); break;
		case 4: $("style").replaceWith("<style type='text/css'>div{font-size:" + fontObj.sehrgross + ";} h3{font-size:" + fontObj.h3 + ";}</style>"); break;
	};			
	if(spezial == "fontonly"){ return false; };
	
	if(Scale != ScaleAlt) {
		var ScaleReg = new RegExp(ScaleAlt);
		$("#Fenster").each(function(){
			var src = $(this).css("background-image");
			src = src.replace(ScaleReg, Scale);
			$(this).css("background-image", src);
		});
		$(".karte_maske, #Karte").each(function(){
			src = $(this).attr("src");
			src = src.replace(ScaleReg, Scale);
			$(this).attr("src", src);
		});
		ImageMapLoaded = false;
	};
	$("#Fenster").height(scaleObj.b);
	
        //Positionierung der Infobox
	
	$("#Fenster_Info_hide").show();
	if(ShowInfoBox){
		$("#Fenster_Info").show();
		$("#Infotext").show();
	};
	if( $("#NoDataAvailable").length!=0 ){
		$("#Fenster_Info_hide").hide();
		$("#Fenster_Info").hide();
		$("#Infotext").hide();
	};	
	if( $("#Fenster_Info").css("display")!="none" ){
		switch(ansicht) {
			case 1: //Kreisansicht
				$("#Fenster_Info").fadeTo("fast",0.33);
				break;
			case 2: //Gemeindeansicht
			case 3: //Ortsansicht
			case 4: //Ortsansicht
			case 5: //Ortsansicht
				$("#Fenster_Info").fadeTo("fast",0.90);
				break;
		};
		p_top = scaleObj.e;
		$("#Fenster_Info_hide").height( $("#Fenster").height() - (2 * p_top));
		$("#Fenster_Info").height( $("#Fenster").height() - (2 * p_top) ).width( scaleObj.f2 );
		$("#Fenster_Info").css("margin-top", p_top+"px");
		$("#Fenster_Info_hide").css("margin-top", p_top+"px");
		p_top = p_top + scaleObj.f1;
		$("#Infotext").css({
			"width" : scaleObj.f2 + "px",
			"top" : p_top +"px",
			"left" : $("#Fenster_Info").position().left + "px"
		});
		p_top = ($("#Fenster_Info_hide").height() / 2) - 7;
		$("#Fenster_Info_arrow").css("top", p_top+"px");
	};
	
	//Ausblenden des Kartenbuttons in Kreisansicht
	
	if(ansicht==1){
		$("#B_Karte").hide();
		$("#X_Karte").show();
	} else {
		$("#B_Karte").show();
		$("#X_Karte").hide();
	};

	//Positionierung der Karte

	if(ansicht==1){
	
	    //Kreisansicht
		p_top = ($("#Fenster").height() / 2) - (scaleObj.dy / 2);
		if( $("#Fenster_Info").css("display")=="none" ){
			p_left = ($("#Fenster").width() / 2) - (scaleObj.dx / 2);
		}else{
			p_left = (($("#Fenster").width() - $("#Fenster_Info").width()) / 2) - (scaleObj.dx / 2);
		};
		$("#Karte").width(scaleObj.dx);
		$("#Karte").height(scaleObj.dy);
		$("#Karte").css("top", p_top+"px");
		$("#Karte").css("left", p_left+"px");
		if(spezial == "fade"){
			$("#Karte").hide();
			$("#Karte").fadeIn(1500);
		};
		
	} else if(!Zoom && ansicht==2){
	
	    //Gemeindeansicht
		ImageQueue = 0;
		$(".luftbild").remove();

		var g_w = GemeindeGeo.G[0].Xmax - GemeindeGeo.G[0].Xmin;
		var g_h = GemeindeGeo.G[0].Ymax - GemeindeGeo.G[0].Ymin;
		if(g_w <= g_h*1.35){
			var kachel_h = Math.round(scaleObj.dy * GemeindeGeo.Kachel_gk / g_h);
		}else{
			var kachel_h = Math.round(scaleObj.dy * 1.35 * GemeindeGeo.Kachel_gk / g_w);
		};
		var fensterWidth = $("#Fenster").width();
		if( $("#Fenster_Info").css("display")!="none" ){
			fensterWidth = fensterWidth - scaleObj.f2;
		};
		var vx = Math.round(GemeindeGeo.V_x * kachel_h / GemeindeGeo.Kachel_gk);
		var vy = Math.round(GemeindeGeo.V_y * kachel_h / GemeindeGeo.Kachel_gk);
		var khor = 1 + Math.round( (($("#Fenster").width() / kachel_h) + 2) / 2 );
		var kvert = Math.round( (( scaleObj.b / kachel_h ) + 2) / 2 );

		GemeindeGeo.Overflow = 0;
		var khor2 = Math.round( (( fensterWidth / kachel_h ) + 2) / 2 );
		var overflow = GemeindeGeo.K_x - (GemeindeGeo.G_x - (khor * GemeindeGeo.Kachel_gk));
		var overflow2 = GemeindeGeo.K_x - (GemeindeGeo.G_x - (khor2 * GemeindeGeo.Kachel_gk));
		if(overflow2>0){ 
		   //Kartenüberstand an Westseite
			khor = khor + Math.ceil(overflow / GemeindeGeo.Kachel_gk);
			vx = 0;
			GemeindeGeo.Overflow = Math.round(overflow * kachel_h / GemeindeGeo.Kachel_gk);
			fensterWidth = $("#Fenster").width();
		};
		khor2 = Math.round( (( $("#Fenster").width() / kachel_h ) + 2) / 2 );
		overflow = GemeindeGeo.K_xmax - (GemeindeGeo.G_x + (khor * GemeindeGeo.Kachel_gk));
		overflow2 = GemeindeGeo.K_xmax - (GemeindeGeo.G_x + (khor2 * GemeindeGeo.Kachel_gk));
		if(overflow2<0){ 
		   //Kartenüberstand an Ostseite
			khor = khor - Math.floor(overflow / GemeindeGeo.Kachel_gk);
			GemeindeGeo.Overflow = Math.round(overflow2 * kachel_h / GemeindeGeo.Kachel_gk);
		};

		GemeindeGeo.Kachel_x = (Math.round( fensterWidth / 2)) - vx - GemeindeGeo.Overflow;
		GemeindeGeo.Kachel_y = (Math.round( scaleObj.b / 2) ) - vy;

		for(var m=0; m<=khor; m++) {
			var xk = GemeindeGeo.Kachel_x + (m * kachel_h);
			if(xk<=$("#Fenster").width()){
				var m2 = m + khor;
				var d = GemeindeGeo.Datei + m;
				d = intToHex(d);
				for(var n=0; n<=kvert; n++) {
					var yk = GemeindeGeo.Kachel_y + (n * kachel_h);
					if(yk<=scaleObj.b){
						var n2 = n + kvert
						var o = GemeindeGeo.Ordner + n;
						o = intToHex(o);
						$("#Fenster").prepend("<img class='luftbild' id='K" + m2 + "_" + n2 + "' alt='' style='left:" + xk + "px; top:"+ yk +"px; position:absolute;' src='./pic/maps/roads/R" + o + "/C" + d + ".PNG' />");
						ImageQueue++;
						$("#K" + m2 + "_" + n2).css({ height: kachel_h, width: kachel_h });
					};
					yk = GemeindeGeo.Kachel_y - (n * kachel_h);
					if(n!=0 && yk>-kachel_h){
						n2 = -n + kvert
						o = GemeindeGeo.Ordner - n;
						o = intToHex(o);
						$("#Fenster").prepend("<img class='luftbild' id='K" + m2 + "_" + n2 + "' alt='' style='left:" + xk + "px; top:"+ yk +"px; position:absolute;' src='./pic/maps/roads/R" + o + "/C" + d + ".PNG' />");
						ImageQueue++;
						$("#K" + m2 + "_" + n2).css({ height: kachel_h, width: kachel_h });
					};
				};
			};
			var xk = GemeindeGeo.Kachel_x - (m * kachel_h);
			if(m!=0 && xk>-kachel_h){
				var m2 = -m + khor;
				var d = GemeindeGeo.Datei - m;
				d = intToHex(d);			            
				for(var n=0; n<=kvert; n++) {
					var yk = GemeindeGeo.Kachel_y + (n * kachel_h);
					if(yk<=scaleObj.b){
						var n2 = n + kvert
						var o = GemeindeGeo.Ordner + n;
						o = intToHex(o);
						$("#Fenster").prepend("<img class='luftbild' id='K" + m2 + "_" + n2 + "' alt='' style='left:" + xk + "px; top:"+ yk +"px; position:absolute;' src='./pic/maps/roads/R" + o + "/C" + d + ".PNG' />");
						ImageQueue++;
						$("#K" + m2 + "_" + n2).css({ height: kachel_h, width: kachel_h });
					};
					yk = GemeindeGeo.Kachel_y - (n * kachel_h);
					if(n!=0 && yk>-kachel_h){
						n2 = -n + kvert
						o = GemeindeGeo.Ordner - n;
						o = intToHex(o);
						$("#Fenster").prepend("<img class='luftbild' id='K" + m2 + "_" + n2 + "' alt='' style='left:" + xk + "px; top:"+ yk +"px; position:absolute;' src='./pic/maps/roads/R" + o + "/C" + d + ".PNG' />");
						ImageQueue++;
						$("#K" + m2 + "_" + n2).css({ height: kachel_h, width: kachel_h });
					};
				};
			};
		};

		var halbeBreite = fensterWidth / 2;
		var halbeHoehe = scaleObj.b / 2;
		var rechteGrenze = Math.floor( GemeindeGeo.G_x + ((halbeBreite - 80) * GemeindeGeo.Kachel_gk / kachel_h) );
		var linkeGrenze = Math.ceil( GemeindeGeo.G_x - ((halbeBreite) * GemeindeGeo.Kachel_gk / kachel_h) );
		var obereGrenze = Math.floor( GemeindeGeo.G_y + ((halbeHoehe - 30) * GemeindeGeo.Kachel_gk / kachel_h) );
		var untereGrenze = Math.ceil( GemeindeGeo.G_y - ((halbeHoehe - 30) * GemeindeGeo.Kachel_gk / kachel_h) );
		var query = "SELECT OrtsID, OrtsName, Ebene, Typ, X, Y, Xmin, Xmax, Ymin, Ymax, GemeindeID, GemeindeName, VG FROM Orte JOIN Kreis ON Orte.OberGemeinde=Kreis.GemeindeID " + 
			"WHERE X<" + rechteGrenze + " AND X>" + linkeGrenze + " AND Y<" + obereGrenze + " AND Y>" + untereGrenze + " AND Ebene=2 ORDER BY X DESC";
		ajaxSQL(query, setzPunkte);

		if(Tool=="Plus"){
			$(".luftbild").css("cursor","url(./pic/symbols/zoom_in.cur), e-resize");
		} else if(Tool=="Minus"){
			$(".luftbild").css("cursor","url(./pic/symbols/zoom_out.cur), n-resize");
		};
		
	} else {
	
	     //Ortsansicht
		ImageQueue = 0;	
		$(".luftbild").remove();

		if(OrtDB.Anpassen==1 && Scale!=1050){
			var kachel_h = Math.round(GemeindeGeo.Kachel_h * scaleObj.b / Masse.m1050.b);
		} else {
			var kachel_h = GemeindeGeo.Kachel_h;
		};
		var vx = Math.round(GemeindeGeo.V_x * kachel_h / GemeindeGeo.Kachel_gk);
		var vy = Math.round(GemeindeGeo.V_y * kachel_h / GemeindeGeo.Kachel_gk);
		var khor = 1 + Math.round( (($("#Fenster").width() / kachel_h) + 2) / 2 );
		var kvert = Math.round( (( scaleObj.b / kachel_h ) + 2) / 2 );
		var fensterWidth = $("#Fenster").width();
		if($("#Fenster_Info").css("display")!="none"){
			fensterWidth = fensterWidth - scaleObj.f2;
		};
		GemeindeGeo.Kachel_x = (Math.round( fensterWidth / 2)) - vx;
		GemeindeGeo.Kachel_y = (Math.round( scaleObj.b / 2) ) - vy;

		for(var m=0; m<=khor; m++) {
			var xk = GemeindeGeo.Kachel_x + (m * kachel_h);
			if(xk<=$("#Fenster").width()){
				var m2 = m + khor;
				var d = GemeindeGeo.Datei + m;
				d = intToHex(d);
				for(var n=0; n<=kvert; n++) {
					var yk = GemeindeGeo.Kachel_y + (n * kachel_h);
					if(yk<=scaleObj.b){
						var n2 = n + kvert;
						var o = GemeindeGeo.Ordner + n;
						o = intToHex(o);
						$("#Fenster").prepend("<img class='luftbild' id='K" + m2 + "_" + n2 + "' name='K" + m2 + "_" + n2 + "' alt='' style='left:" + xk + "px; top:"+ yk +"px; position:absolute;' src='./pic/maps/sky/R" + o + "/C" + d + ".JPG' />");
						ImageQueue++;
						$("#K" + m2 + "_" + n2).css({ height: kachel_h, width: kachel_h });
					};
					yk = GemeindeGeo.Kachel_y - (n * kachel_h);
					if(n!=0 && yk>-kachel_h){
						n2 = -n + kvert;
						o = GemeindeGeo.Ordner - n;
						o = intToHex(o);
						$("#Fenster").prepend("<img class='luftbild' id='K" + m2 + "_" + n2 + "' name='K" + m2 + "_" + n2 + "' alt='' style='left:" + xk + "px; top:"+ yk +"px; position:absolute;' src='./pic/maps/sky/R" + o + "/C" + d + ".JPG' />");
						ImageQueue++;
						$("#K" + m2 + "_" + n2).css({ height: kachel_h, width: kachel_h });
					};
				};
			};
			var xk = GemeindeGeo.Kachel_x - (m * kachel_h);
			if(m!=0 && xk>-kachel_h){
				var m2 = -m + khor;
				var d = GemeindeGeo.Datei - m;
				d = intToHex(d);
				for(var n=0; n<=kvert; n++) {
					var yk = GemeindeGeo.Kachel_y + (n * kachel_h);
					if(yk<=scaleObj.b){
						var n2 = n + kvert;
						var o = GemeindeGeo.Ordner + n;						
						o = intToHex(o);
						$("#Fenster").prepend("<img class='luftbild' id='K" + m2 + "_" + n2 + "' name='K" + m2 + "_" + n2 + "' alt='' style='left:" + xk + "px; top:"+ yk +"px; position:absolute;' src='./pic/maps/sky/R" + o + "/C" + d + ".JPG' />");
						ImageQueue++;
						$("#K" + m2 + "_" + n2).css({ height: kachel_h, width: kachel_h });
					};
					yk = GemeindeGeo.Kachel_y - (n * kachel_h);
					if(n!=0 && yk>-kachel_h){
						n2 = -n + kvert;
						o = GemeindeGeo.Ordner - n;
						o = intToHex(o);
						$("#Fenster").prepend("<img class='luftbild' id='K" + m2 + "_" + n2 + "' name='K" + m2 + "_" + n2 + "' alt='' style='left:" + xk + "px; top:"+ yk +"px; position:absolute;' src='./pic/maps/sky/R" + o + "/C" + d + ".JPG' />");
						ImageQueue++;
						$("#K" + m2 + "_" + n2).css({ height: kachel_h, width: kachel_h });
					};
				};
			};			
		};

		if(OrtDB.Pfeil==1){
			$("#Fenster").append("<div class='pointer' style='left:" + (GemeindeGeo.Kachel_x + vx) + "px; top:" + (GemeindeGeo.Kachel_y + vy - 65) + "px; position:absolute;'><img src='./pic/symbols/Pointer.png'/></div>");
			$(".pointer img").css("behavior","url(iepngfix.htc)");
		};

		var halbeBreite = fensterWidth / 2;
		var halbeHoehe = scaleObj.b / 2;
		var rechteGrenze = Math.floor( GemeindeGeo.G_x + ((halbeBreite - 80) * GemeindeGeo.Kachel_gk / kachel_h) );
		var linkeGrenze = Math.ceil( GemeindeGeo.G_x - ((halbeBreite - 140) * GemeindeGeo.Kachel_gk / kachel_h) );
		var obereGrenze = Math.floor( GemeindeGeo.G_y + ((halbeHoehe - 30) * GemeindeGeo.Kachel_gk / kachel_h) );
		var untereGrenze = Math.ceil( GemeindeGeo.G_y - ((halbeHoehe - 30) * GemeindeGeo.Kachel_gk / kachel_h) );
		var zusatzbedingung = "";
		if(!Zoom){ zusatzbedingung = " AND NOT OrtsID='" + Ebenen[Ebenen.length - 1] + "' AND Ebene>" + (Ebenen.length-2); };
		var query = "SELECT OrtsID, OrtsName, Ebene, Typ, X, Y FROM Orte WHERE X<" + rechteGrenze + " AND X>" + linkeGrenze + " AND Y<" + obereGrenze + " AND Y>" + untereGrenze + zusatzbedingung + " ORDER BY X DESC";
		ajaxSQL(query, setzPunkte);

		if(Tool=="Plus"){
			$(".luftbild").css("cursor","url(./pic/symbols/zoom_in.cur), e-resize");
		} else if(Tool=="Minus"){
			$(".luftbild").css("cursor","url(./pic/symbols/zoom_out.cur), n-resize");
		};
	};
	
	$(".luftbild").mousedown(function(){
		if(Tool!="Plus" && Tool!="Minus"){
			return false;
		};
	});
	
	$(".luftbild").click(function(e){
		if(Tool=="Plus"){
			var xpos = e.pageX;
			var ypos = e.pageY - Masse.m600.a;
			X_Koord = convertGKtoPX(xpos, "x");
			Y_Koord = convertGKtoPX(ypos, "y");
			Massstab = Math.ceil(Massstab * 2 / 3);
			loadOrtsGeo();
		} else if(Tool=="Minus"){
			if(Massstab==16000){
				goToKreis(); 
			} else {
				var xpos = e.pageX;
				var ypos = e.pageY - Masse.m600.a;
				X_Koord = convertGKtoPX(xpos, "x");
				Y_Koord = convertGKtoPX(ypos, "y");
				Massstab = Math.ceil(Massstab * 3 / 2);
				if(Massstab>=16000){
					parseHistoryHash(Ebenen[1] + "&" + 1 + "&reload");
				} else {
					loadOrtsGeo();
				};
			};
		};
	});
	
	$(".luftbild").load(function(){
		if(ImageQueue > 0){ 
			ImageQueue--;
			if(ImageQueue == 0){
				$("#Loading").hide();$("body").css("cursor","auto");
			};
		};
	});
	
	$(".luftbild").error(function(){
		var id = $(this).attr("name");
		var src = $(this).attr("src");
		if( src.search(/JPG/gi) == -1 ){
			src = src.match(/R\w+\/C\w+\.PNG/);
			document.images[id].src = "./pic/maps/roads/tilePNG.php?src=" + src;
		} else {
			src = src.match(/R\w+\/C\w+\.JPG/);
			document.images[id].src = "./pic/maps/sky/tileJPG.php?src=" + src;
		}		
	});
	
	return true;
}

/*				 */
/*Setzen der Punkte auf der Karte*/
/*				 */
function setzPunkte(obj){
	var neuPunkt;
	var neuInfo;
	var vorhanden = false;
	var Zfass = [];
	var Zobj = {Ober:"", Name:"", X:0, Y:0, Typ:"", Ebene:1, Klasse:""};

	$(".punkt, .punkt_info, .punkt_zoom").remove();
	if(Ebenen.length < 2){ return false };
	
	//Setzen der Punkte
	var inhalt = 0;
	$.each(obj.data, function(){
		var x_pos = convertGKtoPX(this.X, "x") - 22;
		var y_pos = convertGKtoPX(this.Y, "y") - 22;
		if(Ebenen.length==2 && this.GemeindeID!=Ebenen[1] && !Zoom){
			var x_mitt = Math.round(parseInt(this.Xmin) + ((this.Xmax - this.Xmin) / 2));
			var y_mitt = Math.round(parseInt(this.Ymin) + ((this.Ymax - this.Ymin) / 2));
			x_mitt = convertGKtoPX(x_mitt, "x");
			y_mitt = convertGKtoPX(y_mitt, "y");
			vorhanden = false;
			for(var g=0; g<Zfass.length; g++){
				if(this.GemeindeID==Zfass[g].Ober){
					if( Math.abs(x_pos-x_mitt)+Math.abs(y_pos-y_mitt) < Math.abs(Zfass[g].X-x_mitt)+Math.abs(Zfass[g].Y-y_mitt) ){
						Zfass[g].X = x_pos;
						Zfass[g].Y = y_pos;
					};
					vorhanden = true;
					break;
				};
			};
			if(!vorhanden){
				var titel = "";
				if(this.VG=="1"){ titel = "VG "; };
				titel = titel + this.GemeindeName;
				Zobj = {Ober: this.GemeindeID,
					Name: titel,
					X: x_pos,
					Y: y_pos,
					Typ: "Ortsteil",
					Ebene: 1,
					Klasse: ""};
				Zfass.push(Zobj);
			};
		} else if(Ebenen.length>2 || Zoom){
			vorhanden = false;
			for(var g=0; g<Zfass.length; g++){
				var abstand_x = x_pos - Zfass[g].X;
				var abstand_y = y_pos - Zfass[g].Y;
				var abstand_r = Math.round(Math.sqrt(Math.pow(abstand_x,2)+Math.pow(abstand_y,2)));
				if(abstand_r<40 && Massstab>3500){
					Zfass[g].X = Math.round(Zfass[g].X + (abstand_x / 2));
					Zfass[g].Y = Math.round(Zfass[g].Y + (abstand_y / 2));
					Zfass[g].Typ = "Ortsteil";
					Zfass[g].Name = "Ortsteil vergrößern";
					Zfass[g].Klasse = "_zoom";
					vorhanden = true;
					break;
				};
			};
			if(!vorhanden && this.OrtsID!=Ebenen[Ebenen.length-1]){
				Zobj = {Ober: this.OrtsID,
					Name: this.OrtsName,
					X: x_pos,
					Y: y_pos,
					Typ: this.Typ,
					Ebene: this.Ebene,
					Klasse: ""};
				Zfass.push(Zobj);
			};
		} else {
			Zobj = {Ober: this.OrtsID,
				Name: this.OrtsName,
				X: x_pos,
				Y: y_pos,
				Typ: this.Typ,
				Ebene: this.Ebene,
				Klasse: ""};
			Zfass.push(Zobj);
		};
	});
	$.each(Zfass, function(){
		var xInfo_pos = this.X + 24;
		neuInfo = "<div class='punkt_info' style='left:" + xInfo_pos + "px; top:" + this.Y + "px; position:absolute;'><h3>"+ this.Name +"</h3></div>";
		neuPunkt = "<div class='punkt" + this.Klasse + "' id='" + this.Ober + "' style='left:" + this.X + "px; top:" + this.Y + "px; position:absolute;'><img src='./pic/symbols/Punkt_" + this.Typ + ".png'/></div>";
		$("#Fenster").append(neuInfo);
		$("#Fenster").append(neuPunkt);
		if(this.Klasse==""){
			$(".punkt:last").fadeIn(1500).data("Ebene", this.Ebene).children("img").css("behavior","url(iepngfix.htc)");
		} else {
			$(".punkt_zoom:last").fadeIn(1500).children("img").css("behavior","url(iepngfix.htc)");
		};
	});

	//Infotext an Punkte binden
	$(".punkt, .punkt_zoom").mouseover(function(){
		$(this).prev().show();
	});
	$(".punkt, .punkt_zoom").mouseout(function(){
		$(this).prev().hide();
	});

	//Wechsel in Ortsansicht beim Klicken der Punkte
	$(".punkt").click(function(){
		$(".punkt, .punkt_zoom").hide();
	        var go = $(this).attr("id");
	        var eb = $(this).data("Ebene");
	    	parseHistoryHash(go + "&" + eb + "&none");
	});
	$(".punkt_zoom").click(function(){
		var x_pos = $(this).position().left + 22;
		var y_pos = $(this).position().top + 22;
		X_Koord = convertGKtoPX(x_pos, "x");
		Y_Koord = convertGKtoPX(y_pos, "y");
		Massstab = 3500;
		$(".punkt, .punkt_zoom").hide();
		loadOrtsGeo();
	});
}

/*							  		  */
/*Berechnet die Pixelposition einer Gauß-Krüger-Koordinate und andersherum*/
/*							  		  */
function convertGKtoPX(wert, ausrichtung){
	var kachel_h = $(".luftbild:first").width();
	if(wert>10000){ 
	 //Gauß-Krüger zu Pixel
	    switch(ausrichtung){
		case "x":
			var result = Math.round( GemeindeGeo.Kachel_x - ((GemeindeGeo.G_x - GemeindeGeo.V_x - wert) / GemeindeGeo.Kachel_gk * kachel_h) );
			return result;
		case "y":
			var result = Math.round( GemeindeGeo.Kachel_y - ((wert - GemeindeGeo.G_y - GemeindeGeo.V_y) / GemeindeGeo.Kachel_gk * kachel_h) );
			return result;
	    };
	} else { 
	 //Pixel zu Gauß-Krüger
	    switch(ausrichtung){
		case "x":
			var result = Math.round( GemeindeGeo.G_x - GemeindeGeo.V_x - ((GemeindeGeo.Kachel_x - wert) * GemeindeGeo.Kachel_gk / kachel_h) );
			return result;
		case "y":
			var result = Math.round( ((GemeindeGeo.Kachel_y - wert) * GemeindeGeo.Kachel_gk / kachel_h) + GemeindeGeo.G_y + GemeindeGeo.V_y );
			return result;
	    };
	};
}

/*									     */
/*Platzieren der roten Gemeindemarkierungen und Zuweisung der Transparenz(IE)*/
/*									     */
function placeMasks(){
	$(".karte_maske").each(function(){
		var thisUsesMap = $(this).attr("usemap") + " area";
		var maske_x = $("#Karte").position().left + $(thisUsesMap).data("X_min") + 1;
		var maske_y = $("#Karte").position().top + $(thisUsesMap).data("Y_min") + 1;
		$(this).show().css({
			left: maske_x + "px",
			top: maske_y + "px",
			behavior: "url(iepngfix.htc)"
		});
	});
}

/*						      */
/*Positionierung der Schaltflächen an der linken Seite*/
/*						      */
function placeLeftButtons(obj){
	var scaleObj;
	switch(Scale) {
		case 600: scaleObj = Masse.m600; break;				
		case 768: scaleObj = Masse.m768; break;		
		case 900: scaleObj = Masse.m900; break;
		case 1050: scaleObj = Masse.m1050; break;
	};

	var top = Ebenen.length - 2;
	$("#B_Zurueck").show().attr("href", "#" + Ebenen[top] + "&" + top + "&none").children("img").css("behavior","url(iepngfix.htc)");
	var top = Ebenen.length - 1;
	if(obj.data[0].TextID!=""){ $("#B_Info").show().attr("href", "#" + Ebenen[top] + "&" + top + "&info").children("img").css("behavior","url(iepngfix.htc)"); };
	if(obj.data[0].BilderID!=""){ $("#B_Fotos").show().attr("href", "#" + Ebenen[top] + "&" + top + "&gallery").children("img").css("behavior","url(iepngfix.htc)"); };
	if(obj.data[0].LinkID!=""){ $("#B_Hyper").show().attr("href", "#" + Ebenen[top] + "&" + top + "&links").children("img").css("behavior","url(iepngfix.htc)"); };
	
	var h = $("#B_Zurueck").position().top;
	$(".button_side:gt(0)").each( function(){
		if( $(this).css("display")!="none" ){
			h = h + scaleObj.g;
			$(this).css("top", h+"px");
			$(this).prev().css("top", h+"px");
		};
	});
}

/*			*/
/*Ladeanzeige darstellen*/
/*			*/
function showLoading() {
	$("body").css("cursor","wait");
        var hh = ($("#Fenster").height() / 2) - 100;
        if(hh<0){ hh = 120; };
        var ww = ($("#Fenster").width() / 2) - 200;
        if($("#Fenster_Info").css("display")!="none"){
		ww = ww - ($("#Fenster_Info").width() / 2);
	}; 
	$("#Loading").show().css({
		"top": hh + "px",
		"left": ww + "px"
	});
	
	window.setTimeout(function(){ 
		$("#Loading").hide();
		$("body").css("cursor","auto");
	}, 20000);
}

/*			 */
/*Aufbau der Kreisansicht*/
/*			 */
function goToKreis() {
	Ebenen = $.grep(Ebenen, function(n,i){
		return i==0;
	});

	OpenedWindow = "none"
	Zoom = false;	
	Tool = "";
	$.history.add("IlmKreis");
	
	$("#Loading").hide();$("body").css("cursor","auto");
	ImageQueue = 0;
	$(".punkt, .punkt_zoom, .punkt_info").remove();
	$(".list_ebene2, .list_ebene3, .list_ebene4").remove();
	$(".list_aktiv").removeClass("list_aktiv");
	$(".luftbild").remove();
	$("#Liste").css("left", "-1000px");
	$("#Liste_fuss").hide();
	$(".button_side").hide();
	$("#Imagefilm").show();
	$("#Karte").show();
	$("#BlindText").empty();

	fillInfoBox(1);
	Redrawn = redraw(1, "fade");
}

/*			    */
/*Aufbau der Gemeindeansicht*/
/*			    */
function goToGemeinde(gem) {
	OpenedWindow = "none"
	Zoom = false;
	Tool = "";
	$.history.add(gem + "&1&" + OpenedWindow);
	
	$("#Liste").css("left", "-1000px");
	$("#Liste_fuss").hide();
	$("#Karte").hide();
	$(".karte_maske").hide();
	$(".button_side").hide();
	$("#Imagefilm").hide();
	$("#B_Zurueck").show().attr("href", "#IlmKreis").children("img").css("behavior","url(iepngfix.htc)");
	
	var query = "SELECT Xmin, Xmax, Ymin, Ymax FROM Kreis WHERE GemeindeID='" + gem + "'";
	ajaxSQL(query, loadGemeindeGeo);
	
	$("#BlindText").empty();

	ListLoaded = refreshList();
}

/*			*/
/*Aufbau der Ortsansicht*/
/*			*/
function goToOrt(ort) {
	OpenedWindow = "none"
	Zoom = false;
	Tool = "";
	$.history.add(ort + "&" + (Ebenen.length-1) + "&" + OpenedWindow);
	
	$("#Liste").css("left", "-1000px");
	$("#Liste_fuss").hide();
	$("#Karte").hide();
	$(".karte_maske").hide();
	$(".button_side").hide();
	$("#Imagefilm").hide();
	ajaxSQL("SELECT TextID, BilderID, LinkID FROM Orte WHERE OrtsID='" + ort + "'", placeLeftButtons);
	
	var query = "SELECT OrtsName, X, Y, Massstab, Pfeil, Anpassen, Z1, Z2, Z3, Z4, Z5, Z6, Z7, Z8, Z9, Z10, Z11 FROM Orte JOIN OrtsDaten ON Orte.DatenID=OrtsDaten.DatenID WHERE OrtsID='" + ort + "'";
	ajaxSQL(query, loadOrtsGeo);
	
	var query = "SELECT Text FROM OrtsTexte WHERE TextID='" + ort + "'";
	ajaxSQL(query, function(obj){
		if(obj.data.length>0){
			$("#BlindText").html(obj.data[0].Text);
		} else {
			$("#BlindText").empty();
		};
	});
	
	ListLoaded = refreshList();
}
