var cardinalIcon = new GIcon();
var halfcardinalIcon = new GIcon();
cardinalIcon.iconSize = new GSize(57, 64);
halfcardinalIcon.iconSize = new GSize(43, 48);
cardinalIcon.iconAnchor = new GPoint(29, 64);
halfcardinalIcon.iconAnchor = new GPoint(22, 48);
cardinalIcon.infoWindowAnchor = new GPoint(29, 0);
halfcardinalIcon.infoWindowAnchor = new GPoint(22, 0);

function createCompass(direction, lng, baseIcon) {
	var compassIcon = new GIcon(baseIcon);
	compassIcon.image = "/markers/marker" + direction + ".png";
	markerOptions = { icon:compassIcon };
	var marker = new GMarker(new GLatLng(0, lng + compassOffset), markerOptions);
	return marker;
}

var baseIcon = new GIcon();
baseIcon.iconSize = new GSize(34, 40);
baseIcon.iconAnchor = new GPoint(17, 40);
baseIcon.infoWindowAnchor = new GPoint(17, 2);

function createMarker(number, lat, lng) {
	var numberedIcon = new GIcon(baseIcon);
	numberedIcon.image = "/markers/marker" + number + ".png";
	markerOptions = { icon:numberedIcon };
	var marker = new GMarker(new GLatLng(lat, lng + compassOffset), markerOptions);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(
		'<p align="center"><a href="' + number + '.html">Click to go to<br><b>Block&nbsp;' + number + '</a>'
		);
	});
	return marker;
}

compassMarkers = new Array(
	createCompass( "W", -90, cardinalIcon),
	createCompass( "N", 0, cardinalIcon),
	createCompass( "E", 90, cardinalIcon),
	createCompass( "S", 180, cardinalIcon),
	createCompass( "NW", -45, halfcardinalIcon),
	createCompass( "NE", 45, halfcardinalIcon),
	createCompass( "SE", 135, halfcardinalIcon),
	createCompass( "SW", -135, halfcardinalIcon)
)

var centreLat=0.0;
var centreLon=0.0;
var initialZoom=4
var map;

function customGetTileURL(a,b) {
	var c=Math.pow(2,b);
	var d=a.x;
	var e=a.y;
	var f="t";
	for(var g=0;g<b;g++){
		c=c/2;
		if (e<c) {
			if(d<c){f+="q"}
			else{f+="r";d-=c}
		} else {
			if(d<c){f+="t";e-=c}
			else{f+="s";d-=c;e-=c}
		}
	}
	return blockNumber+"-tiles/"+f+".jpg"
}

function getWindowHeight() {
	if (window.self&&self.innerHeight) {
		return self.innerHeight;
	}
	if (document.documentElement&&document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	}
	return 0;
}

var latBound = 20.13;
function enforceBounds() {
	var b = map.getBounds();
	var north = b.getNorthEast().lat();
	var south = b.getSouthWest().lat();
	var bottomGap = map.fromLatLngToDivPixel(new GLatLng(south, 0)).y - map.fromLatLngToDivPixel(new 
	GLatLng(-latBound, 0)).y;
	var topGap = map.fromLatLngToDivPixel(new GLatLng(latBound, 0)).y - map.fromLatLngToDivPixel(new GLatLng(north, 
	0)).y;


	if (topGap < 0 && bottomGap > 0) {
		if (bottomGap > -topGap) {
			map.panBy(new GSize(0, -topGap));
		} else {
			map.panBy(new GSize(0, bottomGap));
		}
	} else if (topGap > 0 && bottomGap < 0) {
		if (topGap > -bottomGap) {
			map.panBy(new GSize(0, bottomGap));
		} else {
			map.panBy(new GSize(0, -topGap));
		}
	} else {
		var top = north;
		var bottom = south;
		if (bottom < -latBound) { bottom = -latBound; }
		if (top > latBound) { top = latBound; }
		for (var i = 0; i < compassMarkers.length; i++) {
			compassMarkers[i].setLatLng(new GLatLng(bottom, compassMarkers[i].getLatLng().lng()));
		}
	}
}

function resizeMapDiv() {
	//Do not call any map methods here as the resize is called before the map is created.
	var d=document.getElementById("map_canvas");
	var offsetTop=0;
	for (var elem=d; elem!=null; elem=elem.offsetParent) {
		offsetTop+=elem.offsetTop;
	}
	var height=getWindowHeight()-offsetTop-16;
	if (height>=0) {
		d.style.height=height+"px";
	}
	if (map) {
		map.checkResize();
		enforceBounds();
	}
}

function initialize() {
	if (GBrowserIsCompatible()) {
		resizeMapDiv();
		var copyright = new GCopyright(1,
		new GLatLngBounds(new GLatLng(-90, -180),
		new GLatLng(90, 180)),
		0,
		"<a href=\"http://birchmans.com.au\">Birchmans Estate</a>");
		var copyrightCollection = new GCopyrightCollection("&copy;");
		copyrightCollection.addCopyright(copyright);

		var pic_tileLayers = [ new GTileLayer(copyrightCollection , 0, 17)];
		pic_tileLayers[0].getTileUrl = customGetTileURL;
		pic_tileLayers[0].isPng = function() { return false; };
		pic_tileLayers[0].getOpacity = function() { return 1.0; };
		var pic_customMap = new GMapType(pic_tileLayers, new GMercatorProjection(7), "Pic",
			{maxResolution:6, minResolution:4, errorMessage:" "});

		map = new GMap2(document.getElementById("map_canvas"),{mapTypes:[pic_customMap]});
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();

		var kh = new GKeyboardHandler(map);
		var targetSource = GKeyboardHandler;

		GEvent.addListener(map, "moveend", function() {
			enforceBounds();
			});

		map.setCenter(new GLatLng(centreLat, compassOffset), initialZoom, pic_customMap);

		for (var i = 0; i < compassMarkers.length; i++) {
			map.addOverlay(compassMarkers[i]);
		}

		for (var i = 0; i < blockNeighbours.length; i++) {
			map.addOverlay(createMarker(blockNeighbours[i][0], blockNeighbours[i][1], blockNeighbours[i][2]));
		}

		var mapTitle = new GScreenOverlay("/markers/title" + blockNumber + ".png",
		new GScreenPoint(1, 1, 'fraction', 'fraction'),
		new GScreenPoint(1, 1, 'fraction', 'fraction'),
		new GScreenSize(218, 32));
		map.addOverlay(mapTitle);

		var mapLogo = new GScreenOverlay("/MapLogo.png",
		new GScreenPoint(0.5, 1, 'fraction', 'fraction'),
		new GScreenPoint(0.5, 1, 'fraction', 'fraction'),
		new GScreenSize(433, 38));
		map.addOverlay(mapLogo);
	}
}
