// requires /js/GeographicalSearch.js and /js/GeographicalSearch_AreaTypes.js

// The following extents are approximations based on the LGA map.
// Other maps have slightly different extents but the approximation is close enough.
var MapLatitudeExtentNorth=-9.22;
var MapLatitudeExtentSouth=-29.18;
var MapLongitudeExtentWest=137.50;
var MapLongitudeExtentEast=154.05;

var SearchPageName="SummaryInfo";
var Subdirectory=SearchPageName+"/";

var HighlightImage;

registerInitFunction(function() {
	initGeographicalSearch();
	setAreaTypeID(getInitialAreaType());
	initZoom();
	initHighlight();
	initTextualSearch();
});

function initHighlight() {
	var p_areaID=getQueryStringValue("AreaID");
	if (p_areaID) {
		areaMouseOverEnabled=false;
		try {
			areaHighlight(p_areaID);
			setMapMessage(p_areaID);
		} catch (ex) {}
		setTimeout("areaMouseOverEnabled=true",1000);
	}
}

function initTextualSearch() {
	if (location.search && location.search.indexOf("SearchQuery")!=-1) {
		var searchQueryInputElement=document.getElementById("SearchQuery");
		searchQueryInputElement.select();
	}
}

function setAreaTypeID(areaTypeID) {
	areaTypeID=setAreaTypeIDBase(areaTypeID);
	if (areaTypeID==null) return;
	for (i=0; i<AreaTypeIDs.length; i++) {
		var listAllAreasLinkAreaTypeID=AreaTypeIDs[i];
		var listAllAreasLinkElement=document.getElementById("ListAllAreasLink-"+listAllAreasLinkAreaTypeID);
		listAllAreasLinkElement.style.display=(areaTypeID==listAllAreasLinkAreaTypeID ? "inline" : "none");
	}
}

function areaTypeButtonClick(inputElement) {
	var areaTypeID=inputElement.value;
	if (navigator.userAgent.indexOf("Opera")!=-1) {
		// stupid opera doesn't handle setting the useMap property more than once, so can't change the map type dynamically.
		var newURL=location.href;
		var queryStringPos=newURL.indexOf("?");
		if (queryStringPos!=-1) newURL=newURL.substring(0,queryStringPos);
		newURL+="?AreaTypeID="+areaTypeID;
		location.replace(newURL);
		return;
	}
	setAreaTypeID(areaTypeID);
}

function areaClick() {
	if (WasMoving) return false;
	document.location=Subdirectory+this.id+getSummaryInformationPageExtension();
}

function areaHighlight(areaID) {
	var imageID=HighlightImagePrefix+areaID;
	setTimeout("hideAllHighlights('"+imageID+"')",0);
	var image=document.getElementById(imageID);
	if (!image) {
		image=document.createElement("img");
		image.id=imageID;
		image.className="AreaHighlight";
		image.src=getHighlightImagePath(areaID);
		image.width=MapImage.originalWidth;
		image.height=MapImage.originalHeight;
		initImage(image);
		image.width=image.originalWidth*CurrentZoom*ZoomFactor;
		image.height=image.originalHeight*CurrentZoom*ZoomFactor;
		MapWindow.insertBefore(image,OverlayImage);
	}
	HighlightImage=image;
	// delay displaying image to help prevent placeholder image in firefox.
	setTimeout('HighlightImage.style.visibility="visible"',0);
}

function areaUnhighlight(areaID) {
	setTimeout("hideAllHighlights()",0);
}

function hideAllHighlights(imageID) {
	// imageID may be null
	var images=MapWindow.getElementsByTagName("img");
	var image;
	for (i=0; i<images.length; i++) {
		image=images[i];
		if (image.className=="AreaHighlight" && image.id!=imageID) image.style.visibility="hidden";
	}
}

function getSummaryInformationPageExtension() {
	var sampleJSPPageURLLink=document.getElementById("SampleJSPPageURL");
	if (!sampleJSPPageURLLink) return ".jsp";
	var sampleJSPPageURL=sampleJSPPageURLLink.href;
	return sampleJSPPageURL.substring(sampleJSPPageURL.lastIndexOf('.'));
}