if (GBrowserIsCompatible()) {

	// ====== Create the Euclidean Projection for the flat map ======
	// == Constructor ==
	function EuclideanProjection(zoom){
		this.pixelsPerLonDegree=[];
		this.pixelsPerLonRadian=[];
		this.pixelOrigo=[];
		this.tileBounds=[];
		var b=256;
		var c=1;
		for(var d=0;d<zoom;d++){
			var e=b/2;
			this.pixelsPerLonDegree.push(b/360);
			this.pixelsPerLonRadian.push(b/(2*Math.PI));
			this.pixelOrigo.push(new GPoint(e,e));
			this.tileBounds.push(c);
			b*=2;
			c*=2
		}
	};
 
	// == Attach it to the GProjection() class ==
	EuclideanProjection.prototype=new GProjection();
 
	// == A method for converting latitudes and longitudes to pixel coordinates == 
	EuclideanProjection.prototype.fromLatLngToPixel=function(a,b){
		var c=Math.round(this.pixelOrigo[b].x+a.lng()*this.pixelsPerLonDegree[b]);
		var d=Math.round(this.pixelOrigo[b].y+(-2*a.lat())*this.pixelsPerLonDegree[b]);
		return new GPoint(c,d)
	};

	// == a method for converting pixel coordinates to latitudes and longitudes ==
	EuclideanProjection.prototype.fromPixelToLatLng=function(point,zoom,nofix){
		var lat=-0.5*(point.y-this.pixelOrigo[zoom].y)/this.pixelsPerLonDegree[zoom];
		var lng=(point.x-this.pixelOrigo[zoom].x)/this.pixelsPerLonDegree[zoom];
		return new GLatLng(lat,lng,nofix)
	};

	// == a method that checks if the y value is in range, and wraps the x value ==
	EuclideanProjection.prototype.tileCheckRange=function(a,b,c){
		var d=this.tileBounds[b];
		if (a.y<0||a.y>=d) {
			return false;
		}
		if(a.x<0||a.x>=d){
			a.x=a.x%d;
			if(a.x<0){
				a.x+=d;
			}
		}
		return true;
	};

	// == a method that returns the width of the tilespace ==      
	EuclideanProjection.prototype.getWrapWidth=function(zoom) {
		return this.tileBounds[zoom]*256;
	};

	function loadMap() {
		
		var map = new GMap2(document.getElementById("map"));

		stPathRoot = "http://ae.wolfbound.com/worldmap/newtiles/";
		CustomGetTileUrl = function (a, b){
			offtheright = (a.x >= Math.pow(2, b-1) / 2);
			offthetop = (a.y <= -80);
			offthebottom = (a.y >= Math.pow(2, b-1) / 2);
			if (offtheright || offthebottom || offthetop) {
				return stPathRoot + "Zoom_6_0_0.jpg";
			} else {
				return stPathRoot + "Zoom_" + b + "_" + a.y + "_" + a.x + ".jpg";
			}
		};

		var tilelayers = [new GTileLayer(new GCopyrightCollection("Map Data: Saveena & Behin"),2,7)];
		tilelayers[0].getTileUrl = CustomGetTileUrl;
		tilelayers[0].getCopyright = function(a,b) {
			return {prefix:"", copyrightTexts:["Map Data: Saveena & Behin"]};
		};
				
		// == Create the GMapType, copying most things from G_SATELLITE_MAP ==
		var flatprojection = new EuclideanProjection(8);
		var cmapoptions = {minResolution:2, 
					maxResolution:7, 
					errorMessage:_mMapError, 
					textColor:"gold", 
					linkColor:"gold" };
		var custommap = new GMapType(tilelayers, flatprojection, "AE", cmapoptions);


		// == Add the maptype to the map ==
		map.addMapType(custommap);

		
		CustomGetSextant = function (coord, zoom) {
			var scale = Math.pow(2, 6) / Math.pow(2,zoom); 
			var c = Math.round((flatprojection.fromLatLngToPixel(coord, zoom).x) * scale) ;
			var d = Math.round((flatprojection.fromLatLngToPixel(coord, zoom).y) * scale) ; 
			return new GPoint (c, d);
		};
		
		CustomGetCoords = function (sextantpos) {
			return flatprojection.fromPixelToLatLng(sextantpos, 6);
		};
		
		map.addControl(new GOverviewMapControl());
		map.addControl(new GLargeMapControl());

		var silvest = CustomGetCoords(new GPoint(1130, 1330));
		map.setCenter(silvest, 6, custommap);
		//map.setCenter(new GLatLng(75, 235), 4, custommap);		

		var mypointer;
		GEvent.addListener(map, "click", function(overlay, coord){
			//map.clearOverlays();
			map.removeOverlay(mypointer)
			if (coord) {				
				mypointer = new GMarker(coord);
				map.addOverlay(mypointer);
				map.panTo(coord);
				var sextantpos = CustomGetSextant(coord, map.getZoom());

				msg = "X: "+sextantpos.x+" &nbsp;Y: "+sextantpos.y;
				document.getElementById("mypoint").innerHTML = msg;
			}
		});
		
		// The allowed region which the whole map must be within
		var BoundUpperLeft = CustomGetCoords(new GPoint(128, 3968));
		var BoundLowerRight = CustomGetCoords(new GPoint(3968, 128));
		var allowedBounds = new GLatLngBounds(BoundUpperLeft, BoundLowerRight);
		//var allowedBounds = new GLatLngBounds(GLatLng(70, 245), GLatLng(80, 235));
		  
		// If the map position is out of range, move it back
		function checkBounds() {
			// Perform the check and return if OK
			var C = map.getCenter();
			
			if (allowedBounds.contains(C)) {
				return;
			}

			// It`s not OK, so find the nearest allowed point and move there
			var X = C.lng();
			var Y = C.lat();
			
			var AmaxX = allowedBounds.getNorthEast().lng();
			var AmaxY = allowedBounds.getNorthEast().lat();
			var AminX = allowedBounds.getSouthWest().lng();
			var AminY = allowedBounds.getSouthWest().lat();
			
			if (X < AminX) {X = AminX;}
			if (X > AmaxX) {X = AmaxX;}
			if (Y < AminY) {Y = AminY;}
			if (Y > AmaxY) {Y = AmaxY;}
			//alert ("Restricting "+Y+" "+X);
			map.setCenter(new GLatLng(Y,X));
		}

		// Add a move listener to restrict the bounds range
		GEvent.addListener(map, "move", function() {
			checkBounds();
		});

		var lblctr=0;
		// ============= A function to create the TLabels ===========
		function createTLabel(point,html) {
			var label = new TLabel();
			label.id = 'Label_' + lblctr;
			label.anchorLatLng = point;
			label.anchorPoint = 'bottomLeft';
			label.content = '<div class="labelmarker">\n<div class="label">' + html +'</div></div>'
			label.percentOpacity = 70;
			map.addTLabel(label);
			lblctr++;
		}

		GDownloadUrl("labels.xml", function(data, responseCode) {
			var xml = GXml.parse(data);
			var towns = xml.documentElement.getElementsByTagName("town");
			for (var i = 0; i < towns.length; i++) {
				var point = new GPoint(parseFloat(towns[i].getAttribute("x")),
										parseFloat(towns[i].getAttribute("y")));
				var descr = towns[i].getAttribute("descr");
				createTLabel(CustomGetCoords(point), descr);				
				//map.addOverlay(new GMarker(CustomGetCoords(point), {title:descr}));
			}
		});
		
	}

// display a warning if the browser was not compatible
} else {
	alert("Sorry, the Google Maps API is not compatible with this browser");
};

