/**
  * Points to the GMap object itself
  */
var map;

/**
  *	A GIcon object that stores the NWN-style map marker
  */
var icon;

/**
  * The minimum, maximum and default zoom levels.
  * 13 = 25%, 14 = 50%, 15 = 100%, 16 = 200%
  */
var minZoom = 13;
var maxZoom = 16;
var defZoom = 15;

/**
  * The default x/y positions
  */
var defX = -0.013260841251231791;
var defY = 0.08407115936279297;

/**
  * The images used for the zoom in/out buttons
  *  in their enabled/disabled states
  */
var zoomInEnabledImg = 		'./imgs/zoomin.gif';
var zoomInDisabledImg = 	'./imgs/zoomin_disabled.gif';
var zoomOutEnabledImg = 	'./imgs/zoomout.gif';
var zoomOutDisabledImg = 	'./imgs/zoomout_disabled.gif';

/**
  * Whether the zoom in/out buttons are enabled or not
  */
var zoomOutEnabled = true;
var zoomInEnabled = true;

/**
  * The right and bottom "margins" used to determine the width/height
  *  to make the map.
  * These need to take into account potential scrollbars and varying
  *  sizes of browser GUI (and the sidebar for the width)
  */
var baseWindowWidthOffset = 30+8+5;
var windowWidthOffset = baseWindowWidthOffset+250;
var windowHeightOffset = 55;

/**
  * The last map position saved by the savePos() function.
  * This is used to prevent re-saving cookies when nothing has moved
  */
var lastSavedPosX = defX;
var lastSavedPosY = defY;
var lastSavedPosZ = defZoom;


/**
  * Despite its name, it returns an array of the width and height of the
  *  browser document area, which can be used to determine the size
  *  to make the map.
  * Based upon a script from:
  *  http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
  */
function alertSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return Array(myWidth,myHeight);
}

function resizePopup(mapHeight, mapWidth) {
	var horizontalPadding = 90; //Actually the padding for the container
	var verticalPadding = 70;
	var verticalMargin = 28; //padding applied to the container
	var horizontalMargin = 10;
	var maxWidth = mapWidth-horizontalPadding-horizontalMargin; //And this is the max width of the container
	var maxHeight = mapHeight-verticalPadding-verticalMargin;
	
	//First we have to work out how large the popup image container needs to be
	var popupimgcont = document.getElementById('popupimgcontainer');
	var img = document.getElementById('popupimg');
	//How big is the image?
	var imgHeight = img.height;
	var imgWidth = img.width;
	//Container widths
	var contWidth = 200;
	var contHeight = 100;
	var autoOverflow = false;
	//Is the image larger than the maximum allowed popup size?
	if (imgWidth > maxWidth) {
		//Too wide
		contWidth = maxWidth;
		if (!autoOverflow)  autoOverflow = true;
	} else {
		contWidth = imgWidth;
	}
	if (imgHeight > maxHeight) {
		//Too tall
		contHeight = maxHeight;
		if (!autoOverflow)  autoOverflow = true;
	} else {
		contHeight = imgHeight;
	}
	if (imgHeight > maxHeight || imgWidth > maxWidth) {
		//Add on the margins
		contWidth += horizontalMargin+10;
		contHeight += verticalMargin+10;
	}
	popupimgcont.style.width = contWidth+'px';
	popupimgcont.style.height = contHeight+'px';
	if (autoOverflow) {
		popupimgcont.style.overflow = 'auto';
	} else {
		popupimgcont.style.overflow = 'hidden';
	}
	
	//Now position the window itself
	var topOffset = 40;
	var leftOffset = 30;
	var popup = document.getElementById('popup');
	var popupTop = 100;
	var popupLeft = 60;
	popupTop = Math.round(mapHeight/2 - (contHeight+verticalMargin)/2) + topOffset;
	popupLeft = Math.round(mapWidth/2 - (contWidth+horizontalMargin)/2) + leftOffset;
	popup.style.left = popupLeft+'px';
	popup.style.top = popupTop+'px';
	
	//Position the shadow
	var shadowXOffset = 5;
	var shadowYOffset = 10;
	var popupShadow = document.getElementById('popupshadow');
	var shadowLeft = popupLeft+shadowXOffset;
	var shadowTop = popupTop+shadowYOffset;
	var shadowWidth = contWidth;
	var shadowHeight = contHeight;
	popupShadow.style.left = shadowLeft+'px';
	popupShadow.style.top = shadowTop+'px';
	popupShadow.style.width = shadowWidth+'px';
	popupShadow.style.height = shadowHeight+'px';
}

/**
  * Resize the map area and sidebar to fill the window again.
  * This is called when the browser window is resized and when the
  *  sidebar is enabled/disabled
  */
function doResize(firstTime) {
	var windowDimensions = alertSize();
	var width = (windowDimensions[0]-windowWidthOffset);
	var height = (windowDimensions[1]-windowHeightOffset);
	document.getElementById('map').style.width = width+"px";
	document.getElementById('map').style.height = height+"px";
	document.getElementById('mapoutside').style.width = (windowDimensions[0]-windowWidthOffset+10)+"px";
	document.getElementById('mapoutside').style.height = (windowDimensions[1]-windowHeightOffset+27)+"px";
	document.getElementById('sidebarcollapse').style.left = (width-7+11)+"px";
	document.getElementById('sidebar').style.height = (height+29)+'px';
	resizePopup(height, width);
	if (firstTime) {
		document.getElementById('thebody').style.visibility = 'visible';
	} else {
		map.checkResize();
	}
}

/**
  * Load the map itself.
  */
function load() {
	if (GBrowserIsCompatible()) {
		window.onresize = function() { doResize(false); }
		document.getElementById('popupimg').onload = function() { doResize(); }
		document.getElementById('popup').onclick = function() { hidePopup(); }
		doResize(true);
		map = new GMap2(document.getElementById("map"));
		
		//Enable continuous zoom
//		map.enableContinuousZoom();
		
		CustomGetTileUrl=function(xy,z){
//			return 'http://www.udcx.com/GMaps/img.php?x='+xy.x+'&y='+xy.y+'&z='+z;
			var x = xy.x;
			var y = xy.y;
			var zoomOffset = 5;
			var CX = 0;
			var CY = 0;
			x -= Math.pow(2, z-1);
			y -= Math.pow(2, z-1);
			z -= zoomOffset;
			switch(z) {
				case 8:
					CX = 2;
					CY = 2;
					break;
				case 9:
					CX = 3;
					CY = 3;
					break;
				case 10:
					CX = 5;
					CY = 5;
					break;
				case 11:
					CX = 9;
					CY = 9;
					break;
			}
			x += CX;
			y += CY;
			return 'http://www.udcx.com/GMaps/imgs/vl/z'+z+'/z10_'+y+'x'+x+'.jpg';
		}
		
		var tilelayers = [new GTileLayer(new GCopyrightCollection("Dark Century: X"),minZoom,maxZoom)];
		tilelayers[0].getTileUrl = CustomGetTileUrl;
		tilelayers[0].getCopyright = function(a,b) {
			return {prefix:"Map: ", copyrightTexts:["Dark Century: X"]};
		}
		var dcxmap = new GMapType(tilelayers, G_SATELLITE_MAP.getProjection(), "DC:X",{textColor:'#ffffff',linkColor:'#5555FF',minResolution:minZoom,maxResolution:maxZoom,errorMessage:"Error loading image<br /><br />Possibly outside of the game world"});
		
		map.addMapType(dcxmap);
		
		//Add keyboard handler
		var keyboardHandler = new GKeyboardHandler(map,window);
		
		map.setCenter(new GLatLng(defX, defY), defZoom, dcxmap);
		map.savePosition();
		
		setTimeout("savePos()", 5000);
		
		checkZoomButtons();
		
		//Create the NWN-style marker
		icon = new GIcon();
		icon.image = "./imgs/marker.png";
		icon.transparent = "./imgs/marker_t.png";
		icon.iconSize = new GSize(10, 10);
		icon.iconAnchor = new GPoint(4, 4);
		icon.infoWindowAnchor = new GPoint(5, 5);
		icon.imageMap=[
					2,5,
					2,6,
						5,2,
						 6,2,
							9,5,
							9,6,
						6,9,
					   5,9
						];
						
		//Create the "hidden area" marker
		haicon = new GIcon();
		haicon.image = "./imgs/hamarker.png";
		haicon.transparent = "./imgs/hamarker_t.png";
		haicon.shadow = "./imgs/hamarker_s.png";
		haicon.iconSize = new GSize(13, 14);
		haicon.iconAnchor = new GPoint(1, 13);
		haicon.infoWindowAnchor = new GPoint(7, 6);
		haicon.imageMap=[8,1,11,1,13,3,13,4,10,7,11,9,3,13,1,13,3,6,5,3,7,4];
		
		buildLandmarkList();
		buildAreaLists();
		
		doDefaults();
		
		addMarkers();
		createHiddenAreasMarkers();
	} else {
		document.getElementById('map').innerHTML = "<h1>Incompatible Browser</h1><p><b>Sorry but your Web browser is not compatible with these maps. Please use a different browser, such as Mozilla Firefox, Opera or Microsoft Internet Explorer (all available for Windows and all free).</b></p>";
	}
}

/**
  * Determine whether each zoom button needs to be enabled or disabled
  * Called every time we zoom in/out and also when the map is first loaded
  */
function checkZoomButtons() {
	var zoomLevel = map.getZoom();
	if (zoomLevel >= maxZoom) {
		//Need to disable the zoom in button
		document.getElementById('zoomin').src = zoomInDisabledImg; //Disable zoom in
		document.getElementById('zoomin').style.borderColor = '#636163';
		zoomInEnabled = false;
	} else {
		//Enable the zoom in button
		document.getElementById('zoomin').src = zoomInEnabledImg; //Disable zoom in
		document.getElementById('zoomin').style.borderColor = '#8f8f89';
		zoomInEnabled = false;
	}
	if (zoomLevel <= minZoom) {
		//Disable the zoom out button
		document.getElementById('zoomout').src = zoomOutDisabledImg;
		document.getElementById('zoomout').style.borderColor = '#636163';
		zoomOutEnabled = false;
	} else {
		//Enable the zoom out button
		document.getElementById('zoomout').src = zoomOutEnabledImg;
		document.getElementById('zoomout').style.borderColor = '#8f8f89';
		zoomOutEnabled = false;
	}
}

function doSetZoom(lvl) {
	if (lvl >= minZoom && lvl <= maxZoom) {
		map.setZoom(lvl);
	}
}
	
/**
  * Zoom in (or at least try to)
  */
function doZoomIn(link) {
	var zoomLevel = map.getZoom();
	if (zoomLevel < maxZoom) {
		//Allowed to zoom in
		map.zoomIn();
		checkZoomButtons();
	}
	link.blur();
}

/**
  * Zoom out (or try to)
  */
function doZoomOut(link) {
	var zoomLevel = map.getZoom();
	if (zoomLevel > minZoom) {
		//Allowed to zoom out
		map.zoomOut();
		checkZoomButtons();
	}
	link.blur();
}

/**
  * Pan northwards
  */
function panUp(button) {
	if (button != undefined)
		button.blur();
	map.panDirection(0,1);
}

/**
  * Pan eastwards
  */
function panRight(button) {
	if (button != undefined)
		button.blur();
	map.panDirection(-1,0);
}

/**
  * Pan southwards
  */
function panDown(button) {
	if (button != undefined)
		button.blur();
	map.panDirection(0,-1);
}

/**
  * Pan leftwards
  */
function panLeft(button) {
	if (button != undefined)
		button.blur();
	map.panDirection(1,0);
}

/**
  * Pan to the original position
  */
function panHome(button) {
	if (button != undefined)
		button.blur();
	map.returnToSavedPosition();
}

/**
  * Create a marker on the map at the specified lat & lng
  */
function createMarker(lat,lng,html,theid) {
	var marker = new GMarker(new GLatLng(lat,lng), icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	map.addOverlay(marker);
	return marker;
}

/**
  * Add all of our markers to the map
  */
function addMarkers() {
	for (var i=0; i<Markers.length; i++) {
		Markers[i].o = createMarker(Markers[i].x, Markers[i].y, Markers[i].h, Markers[i].k);
	}
}

function showMarker(i) {
	Markers[i].o.openInfoWindowHtml(Markers[i].h);
}

function makeMarkerHtml(i) {
	Markers[i].h = '<b>'+Markers[i].n+'</b><br />'+Markers[i].d;
}

function buildLandmarkList() {
	//<li><a href="/#$d['n']" onclick="goTo($d[y],$d[x]);showMarker('$area.$k');return false;">$d['n']</a></li>
	var div = document.getElementById('sidebar_markers');
	var divChildren = div.getElementsByTagName('ul');
	var ul = divChildren[0];
	for (var i=0; i<Markers.length; i++) {
		makeMarkerHtml(i);
		var m = Markers[i];
		var li = document.createElement('li');
			ul.appendChild(li);
		var a = document.createElement('a');
			a.href = '#'+m.n;
			eval("a.onclick = function() { goTo("+m.x+","+m.y+"); showMarker("+i+"); return false; }");
			var txt = document.createTextNode(m.n);
				a.appendChild(txt);
			li.appendChild(a);
	}
}

function buildAreaListCat() {
//<h2>
//  <a href="#Collapse Category" id="a_123" onclick="toggleCat('123'); return false;">
//    <span id="s_123">-</span>
//    Some area
//  </a>
//</h2>
//<ul id="cat_123" class="map_location_list" style="display:block;">
//  <li><a href="#Go to VL - Somewhere" onclick="goTo(0.0,1.1); return false;">Somewhere</a></li>
//</ul>
	var div = document.getElementById('sidebar_cat');
	for (var section in Locations) {
		var l = Locations[section];
		var h2 = document.createElement('h2');
			h2.className = 'map_location_cat';
			var at = document.createElement('a');
				at.href = '#Collapse Category';
				at.id = 'a_'+l.k;
				eval("at.onclick = function() { toggleCat('"+l.k+"'); return false; }");
				var span = document.createElement('span');
					span.id = 's_'+l.k;
					var spantxt = document.createTextNode('-');
					span.appendChild(spantxt);
				at.appendChild(span);
				var attxt = document.createTextNode(section);
				at.appendChild(attxt);
			h2.appendChild(at);
		div.appendChild(h2);
		var ul = document.createElement('ul');
			ul.id = 'cat_'+l.k;
			ul.className = 'map_location_list';
			ul.style.display = 'block';
			for (var ii=0; ii<l.d.length; ii++) {
				var areainfo = l.d[ii];
				var li = document.createElement('li');
					var a = document.createElement('a');
						a.href = '#Go to '+section+' - '+areainfo.n;
						eval("a.onclick = function() { goTo("+areainfo.x+","+areainfo.y+"); return false; }");
						var atxt = document.createTextNode(areainfo.n);
						a.appendChild(atxt);
					li.appendChild(a);
				ul.appendChild(li);
			}
		div.appendChild(ul);
	}
}
function buildAreaListAlpha() {
//<h2 class="map_location_cat">
//  <a href="#Collapse Category" id="a_A" onclick="toggleCat('A'); return false;">
//    <span id="s_A">-</span> A
//  </a>
//</h2>
//<ul id="cat_A" class="map_location_list" style="display:block;">
//  <li>
//    <a href="#Go to VL - Somewhere" onclick="goTo(0.0, 1.0); return false;">somewhere</a>
//  </li>
//</ul>
	var H2s = [];
	var ULs = [];
	var div = document.getElementById('sidebar_alpha');
	for (var section in Locations) {
		var l = Locations[section];
		for (var ii=0; ii<l.d.length; ii++) {
			var areainfo = l.d[ii];
			var letter = areainfo.n.substr(0, 1).toUpperCase();
			if (ULs[letter] == undefined) {
				var h2 = document.createElement('h2');
					var a = document.createElement('a');
						a.href = '#Collapse Category';
						a.id = 'a_'+letter;
						eval("a.onclick = function() { toggleCat('"+letter+"'); return false; }");
						var span = document.createElement('span');
							span.id = 's_'+letter;
							var spantxt = document.createTextNode('-');
							span.appendChild(spantxt);
						a.appendChild(span);
						var atxt = document.createTextNode(letter);
						a.appendChild(atxt);
					h2.appendChild(a);
				H2s[letter] = h2;
				var ul = document.createElement('ul');
					ul.id = 'cat_'+letter;
					ul.className = 'map_location_list';
					ul.style.display = 'block';
				ULs[letter] = ul;
			}
			var li = document.createElement('li');
				var a = document.createElement('a');
					a.href = '#Go to '+section+' - '+areainfo.n;
					eval("a.onclick = function() { goTo("+areainfo.x+","+areainfo.y+"); return false; }");
					var atxt = document.createTextNode(areainfo.n);
					a.appendChild(atxt);
				li.appendChild(a);
			ULs[letter].appendChild(li);
		}
	}
	//Wow this is bad :P
	var Alphabet = Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
	
	for (var x in Alphabet) {
		var let = Alphabet[x];
		if (H2s[let] != undefined) {
			div.appendChild(H2s[let]);
			div.appendChild(ULs[let]);
		}
	}
}
function buildAreaLists() {
	buildAreaListCat();
	buildAreaListAlpha();
}

/**
  * Pan the map to the specified x & y coordinates
  */
function goTo(x,y) {
	map.panTo(new GLatLng(x, y, map.getZoom()));
}

/**
  * Generate and show the URL to this map position
  */
function showMapURL() {
	var centre = map.getCenter();
	var x = centre.lng();
	var y = centre.lat();
	var z = map.getZoom();
	var url = 'http://www.udcx.com/GMaps/?x='+x+'&y='+y+'&z='+z;
//	var url = 'x=>'+x+', y=>'+y;
	prompt('The URL to link to this view of this page is:', url);
}

/**
  * Switch between categorised and alphabetical views of the sidebar
  */
function setSideList(what) {
	var sbcat = document.getElementById('sidebar_cat');
	var sbalpha = document.getElementById('sidebar_alpha');
	var cat = document.getElementById('tabmenu_cat');
	var alpha = document.getElementById('tabmenu_alpha');
	if (what=='cat') {
		setCookie('ugm_sbl', 'cat');
		sbcat.style.display = 'block';
		sbalpha.style.display = 'none';
		cat.src = './imgs/menu/categories_on.png';
		alpha.src = './imgs/menu/alpha_off.png';
	} else {
		setCookie('ugm_sbl', 'alpha');
		sbcat.style.display='none';
		sbalpha.style.display='block';
		cat.src = './imgs/menu/categories_off.png';
		alpha.src = './imgs/menu/alpha_on.png';
	}
}

function setSideType(what) {
	var sbareas = document.getElementById('sidebar_areas');
	var sbmarkers = document.getElementById('sidebar_markers');
	var areas = document.getElementById('tabmenu_areas');
	var landmarks = document.getElementById('tabmenu_landmarks');
	if (what=='areas') {
		setCookie('ugm_sbt', 'areas');
		sbareas.style.display='block';
		sbmarkers.style.display='none';
		areas.src = './imgs/menu/areas_on.png';
		landmarks.src = './imgs/menu/landmarks_off.png';
	} else {
		setCookie('ugm_sbt', 'markers');
		sbareas.style.display='none';
		sbmarkers.style.display='block';
		areas.src = './imgs/menu/areas_off.png';
		landmarks.src = './imgs/menu/landmarks_on.png';
	}
}

/**
  * Expand the specified category
  */
function expandCat(cat) {
	var c = document.getElementById('cat_'+cat);
	var a = document.getElementById('a_'+cat);
	var s = document.getElementById('s_'+cat);
	if (c != undefined && a != undefined && s != undefined) {
		c.style.display = 'block';
		a.href = '#Collapse Category';
		s.innerHTML = '-';
		setCookie('ugm_sc_'+cat, '1');
	} else {
		setCookie('ugm_sc_'+cat, '');
	}
}

/**
  * Collapse the specified category
  */
function collapseCat(cat) {
	var c = document.getElementById('cat_'+cat);
	var a = document.getElementById('a_'+cat);
	var s = document.getElementById('s_'+cat);
	if (c != undefined && a != undefined && s != undefined) {
		c.style.display = 'none';
		a.href = '#Expand Category';
		s.innerHTML = '+';
		setCookie('ugm_sc_'+cat, '0');
	} else {
		setCookie('ugm_sc_'+cat, '');
	}
}

/**
  * Toggle the specified category
  */
function toggleCat(cat) {
	var c = document.getElementById('cat_'+cat);
	if (c.style.display == 'none') {
		expandCat(cat);
	} else {
		collapseCat(cat);
	}
}

/**
  * Expand/collapse all categories
  */
function setAll(what, how) {
	var div = document.getElementById('sidebar_'+what);
	var children = div.getElementsByTagName('ul');
	for (var i = 0; children[i]; i++) {
		var c = children[i];
		if (c.id.substr(0, 4) == 'cat_') {
			if (how == 'expand') {
				//Expand category
				expandCat(c.id.substr(4));
			} else {
				//Collapse category
				collapseCat(c.id.substr(4));
			}
		}
	}
}

/**
  * Set a cookie with the specified name/value that will expire in 5 years' time
  */
function setCookie(cookieName,cookieValue) {
	//Function modified http://www.javascripter.net/faq/settinga.htm
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 157680000000); //5 yrs
	document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString();
}

var sidebarVisible = true;
/**
  * Hide the sidebar
  */
function hideSidebar() {
	var sb = document.getElementById('sidebar');
	var sbc = document.getElementById('sidebarcollapse');
	setCookie('ugm_ssb','0');
	sb.style.display = 'none';
	sb.style.right = '0px';
	sbc.getElementsByTagName('a')[0].href='/#Show sidebar';
	sbc.getElementsByTagName('a')[0].getElementsByTagName('img')[0].src='./imgs/sidebarshow.png';
	windowWidthOffset = baseWindowWidthOffset;
	sidebarVisible = false;
	doResize(false);
}

/**
  * Show the sidebar
  */
function showSidebar() {
	var sb = document.getElementById('sidebar');
	var sbc = document.getElementById('sidebarcollapse');
	setCookie('ugm_ssb','1');
	sb.style.display = 'block';
	sb.style.right = '0px';
	sbc.getElementsByTagName('a')[0].href='/#Hide sidebar';
	sbc.getElementsByTagName('a')[0].getElementsByTagName('img')[0].src='./imgs/sidebarhide.png';
	windowWidthOffset = baseWindowWidthOffset+250;
	sidebarVisible = true;
	doResize(false);
}

var slideDelay = 0;
var slideAmount = 50;
var minSlideAmount = 10;
var slideChange = 1.2;
var sliding = false;
function slideOutSidebarStart() {
	if (sliding) return;
	sliding = true;
	var sb = document.getElementById('sidebar');
	sb.style.display = 'block'; //Ensure it's visible first
	windowWidthOffset += slideAmount;
	slideAmount = 50;
	slideOutSidebarMiddle(0);
}
function slideOutSidebarMiddle(w) {
	var sb = document.getElementById('sidebar');
	var m = document.getElementById('map');
	if (slideAmount > minSlideAmount) {
		slideAmount = Math.round(slideAmount/slideChange);
	}
	sb.style.right = w+'px';
	windowWidthOffset -= slideAmount;
	doResize(false);
	if (w >= (-250+slideAmount)) {
		setTimeout("slideOutSidebarMiddle("+(w-slideAmount)+")", slideDelay);
	} else {
		setTimeout("slideOutSidebarEnd()", slideDelay);
	}
}
function slideOutSidebarEnd() {
	sliding = false;
	hideSidebar();
}

function slideInSidebarStart() {
	if (sliding) return;
	sliding = true;
	var sb = document.getElementById('sidebar');
	sb.style.display = 'block'; //Ensure it's visible first
	windowWidthOffset -= slideAmount;
	slideAmount = 50;
	slideInSidebarMiddle(-250);
}
function slideInSidebarMiddle(w) {
	var sb = document.getElementById('sidebar');
	var m = document.getElementById('map');
	if (slideAmount > minSlideAmount) {
		slideAmount = Math.round(slideAmount/slideChange);
	}
	sb.style.right = w+'px';
	windowWidthOffset += slideAmount;
	doResize(false);
	if (w <= (0-slideAmount)) {
		setTimeout("slideInSidebarMiddle("+(w+slideAmount)+")", slideDelay);
	} else {
		setTimeout("slideInSidebarEnd()", slideDelay);
	}
}
function slideInSidebarEnd() {
	sliding = false;
	showSidebar();
}

/**
  * Toggle the sidebar's visibility
  */
function toggleSidebar() {
	var sb = document.getElementById('sidebar');
	if (sidebarVisible) {
		//Hide sidebar
		slideOutSidebarStart();
//		hideSidebar();
	} else {
		//Show sidebar
		slideInSidebarStart();
//		showSidebar();
	}
}

/**
  * Save the current map position in a cookie
  */
function doSavePos() {
	if (map == undefined) return;
	var centre = map.getCenter();
	var x = centre.lng();
	var y = centre.lat();
	var z = map.getZoom();
	if (x != lastSavedPosX || y != lastSavedPosY || z != lastSavedPosZ) {
		setCookie('ugm_stx', x);
		setCookie('ugm_sty', y);
		setCookie('ugm_stz', z);
		lastSavedPosX = x;
		lastSavedPosY = y;
		lastSavedPosZ = z;
	}
}

/**
  * Call the doSavePos() function every 10 seconds
  */
function savePos() {
	doSavePos();
	setTimeout("savePos()", 10000);
}



/**** Pop-up hidden areas box ****/
/**
  * Create a hidden areas marker on the map at the specified lat & lng
  */
function createHiddenAreasMarkers() {
	for (var i=0; i<HAMarkers.length; i++) {
		var marker = new GMarker(new GLatLng(HAMarkers[i].x,HAMarkers[i].y), haicon);
		eval('GEvent.addListener(marker, "click", function() {showPopup('+i+');});');
		map.addOverlay(marker);
	}
}

function showPopup(i) {
	if (i != undefined) {
		var img = document.getElementById('popupimg');
		img.src = './imgs/progress_bar.gif';
		img.src = HAMarkers[i].imgurl;
	}
	document.getElementById('popup').style.display = 'block';
	document.getElementById('popupshadow').style.display = 'block';
}

function hidePopup() {
	document.getElementById('popup').style.display = 'none';
	document.getElementById('popupshadow').style.display = 'none';
}










function doUnload() {
	doSavePos();
	GUnload();
}

window.onload = function() { load(); }
window.onunload = function() { doUnload(); }