//============================================================================================
// Loads all of the images needed for the map navigation buttons
function tocItem (name)
{
	this.on = new Image();
	this.on.src = "../images/" + name + "on.gif"
	this.off = new Image();
	this.off.src = "../images/" + name + "off.gif"
}

function toc_new (name)
{
	tocItem[name] = new tocItem(name);
}

function img_act (imgName)
{
	if (document.images) document[imgName].src = tocItem[imgName].on.src;
}

function img_inact (imgName)
{
	if (document.images) document [imgName].src = tocItem[imgName].off.src;
}

if (document.images != null) load_toc ('toc', 21);

function load_toc (name, num)
{
	for (i = 1; i <= num; i++) toc_new(name + i);
}

//============================================================================================
// Define a function to get the map object
function getMap()	//Get MGMap Object
{
   	if (navigator.appName == "Netscape")
		return parent.MAPFRAME.document.map;
	else
		return parent.MAPFRAME.map;
}

//============================================================================================
// Defines what the map navigation buttons do
// Zoom in
function zoomin()
{
	var theMap = getMap();
	if (!theMap.isBusy())
		theMap.zoomInMode();
	//else alert('The map is busy...Please try again in a few seconds.');
}

// Zoom out
function zoomout()
{
	var theMap = getMap();
	if (!theMap.isBusy())
		theMap.zoomOutMode();
	//else alert('The map is busy...Please try again in a few seconds.');
}

// Zoom to the previous windows extent
function zoomprevious()
{
	var theMap = getMap();
	if (!theMap.isBusy())
		theMap.zoomPrevious();
	//else alert('The map is busy...Please try again in a few seconds.');
}

// Zoom to the county's extent
function zoomcounty()
{
	var theMap = getMap();
	if (!theMap.isBusy())
		theMap.zoomOut();
	//else alert('The map is busy...Please try again in a few seconds.');
}

// Pan
function pan()
{
	var theMap = getMap();
	if (!theMap.isBusy())
		theMap.panMode();
	//else alert('The map is busy...Please try again in a few seconds.');
}

// Measure
function measure()
{
	var theMap = getMap();
	if (!theMap.isBusy())
		theMap.viewDistance('FT');
	//else alert('The map is busy...Please try again in a few seconds.');
}

// Print the map
function print()
{
	var theMap = getMap();
	if (!theMap.isBusy())
	{
		// Set parameters for map
		var theSetup = theMap.getPageSetup();
		theSetup.setIncludeLegend(false);
		theSetup.setUsePlotScale(false);
		theSetup.setInclude("mg_url", false);
		var theAttr = theSetup.getTitleTextAttr();
		theAttr.setFontName("Verdana");
//		theAttr.setColor(4);   //black
		theAttr.setHeight(8.0, "");
		theAttr.setHorizAlign(1);
		theAttr.setVertAlign(1);
		if (theMap.getScale() <= 20000)
		{
			var theTitle = 'Flood Control District of Maricopa County (FCDMC) - Unofficial Document' +
						'\nThis document cannot be used for floodplain determinations. Current studies, erosion setbacks and other factors may also affect' +
						'\nthe floodplain status of the property. The information shown for pending floodplains are the best technical information available' +
						'\nat this time to determine the 100-year floodplain and are subject to change. To get an official floodplain determination, call the' +
						'\n"Contact" specified on the web page.' +
//						'\n' +
						'\nFCDMC does not guarantee the positional accuracy of the parcel lines. The parcel lines are for illustration purposes only and ' +
						'\nand are not intended to be used as a survey product.'
//							'\nThe assessors parcel boundary lines are not intended to be used as a survey product.'
		}
		else
		{
			var theTitle = 'Flood Control District of Maricopa County (FCDMC) - Unofficial Document' +
						'\nThis document cannot be used for floodplain determinations. Current studies, erosion setbacks and other factors may also affect' +
						'\nthe floodplain status of the property. The information shown for pending floodplains are the best technical information available' +
						'\nat this time to determine the 100-year floodplain and are subject to change. To get an official floodplain determination, call the' +
						'\n"Contact" specified on the web page.'
		}
		theSetup.setTitle(theTitle);
//		alert('Under properties, choose landscape for the print orientation.');
		theMap.printDlg();
	}
	else alert('The map is busy...Please try again in a few seconds.');
}

// Copy map to the clipboard
function copyclipboard()
{
	var theMap = getMap();
	if (!theMap.isBusy())
		theMap.copyMap();
	//else alert('The map is busy...Please try again in a few seconds.');
}

//============================================================================================
// Functions that open another window
// Opens window for all of the quick zoom to/search by methods
function searchbyWinopen()
{
	window.open('searchbyindex.cfm','searchbyWin','toolbar=0,location=0,directories=0,status=0,menubar=0,dependent=no,scrollbars=0,resizable=0,height=335,width=500');
}

// Opens window for the help information
function helpWinopen()
{
	window.open('helpwin.cfm','helpWin','toolbar=0,location=0,directories=0,status=0,menubar=0,dependent=no,scrollbars=1,resizable=1,height=500,width=500');
}

//============================================================================================
// Zoom functions called from address, street intersection and township/range windows
//  Zooms to the extents passed into this function by zoom to address
function zoomToExtents(MinX, MinY, MaxX, MaxY, zoomScale)
{
	var theMap = getMap();
	var centerY = (MinX + MaxX) / 2.0;
	var centerX = (MinY + MaxY) / 2.0;
	theMap.setAutoRefresh(true);  
	var result = theMap.zoomScale(centerX,centerY, zoomScale);
}

//  Zooms to the extents passed into this function by zoom to intersection and township/range
function zoomToLocation(lat, lon, zoomScale)
{
	var theMap = getMap();
	theMap.setAutoRefresh(true);  
	var result = theMap.zoomScale(lat, lon, zoomScale);
}

// Turns on the parcel group and/or layer and then passes the extents to zoomToExtents
function zoomToParcel(MinX, MinY, MaxX, MaxY, zoomScale)
{
	var theMap = getMap();
	
	//prevent theMap from entering a busy state
	theMap.SetAutoRefresh(false)
	
	//check to see if the parcel group and boundary layer are on
	var mapGroup = theMap.GetMapLayerGroup("Land")
	var mapLayer = theMap.getMapLayer("assessorparcel");
	//turn on the parcel layer
	var mapLayerOff = false
	if (!mapLayer.IsVisible())
	{
		var mapLayerOff = true
		mapLayer.SetVisibility(true)
	}
	//turn on the parcel group
	var mapGroupOff = false
	if (!mapGroup.GetVisibility())
	{
		var mapGroupOff = true
		mapGroup.SetVisibility(true)
		//mapLayer.setrebuild(true)
	}
	//if the layer group or layer was off, then the legend and map need to be refreshed
	if ((mapLayerOff) || (mapGroupOff))
	{
		theMap.RedrawLegend()
	}
//	theMap.setAutoRefresh(true);
    zoomToExtents(MinX, MinY, MaxX, MaxY, zoomScale)
}

/*
//  Zoom to and highlight the entered parcel
function highlightParcel(KEY, MinX, MinY, MaxX, MaxY, zoomScale)
{
	var theMap = getMap();
	
	//clear any selected features
	var theMapSelection = theMap.GetSelection()
	theMapSelection.Clear()
	
	//select the entered parcel
	var mapLayer = theMap.getMapLayer("parcel");
	var theParcel = mapLayer.getMapObject(KEY);
	
	
	var theSelection = theMap.GetSelection().getMapObjectsEx(null);
	alert("Selected features: " + theSelection.size());
	alert(KEY);
	
	//prevent theMap from entering a busy state
	//that would interfere with the selection events
	theMap.SetAutoRefresh(false)
	
	//add the mapfeature to the selection
	theMapSelection.addObject(theParcel, false);
	theMap.ZoomSelected()
	
	//zoom to a buffer
	zoomToExtents(MinX, MinY, MaxX, MaxY, zoomScale);

//taken care of in the zoomToExtents function	
	//refresh the map
//	theMap.SetAutoRefresh(true)
//	theMap.Refresh()

}
*/

//============================================================================================
// Help window text for each of the buttons on the map
/*Removed for a single help window for all of the buttons
function helpWin(value)
{
	if(value == "1")
		parent.HELPWIN.location.href="helpZoomin.cfm";
	if(value == "2")
		parent.HELPWIN.location.href="helpZoomout.cfm";
	if(value == "3")
		parent.HELPWIN.location.href="helpZoomcounty.cfm";
	if(value == "4")
		parent.HELPWIN.location.href="helpPan.cfm";
	if(value == "5")
		parent.HELPWIN.location.href="helpPrint.cfm";
	if(value == "9")
		parent.HELPWIN.location.href="helpMeasure.cfm";
	if(value == "10")
		parent.HELPWIN.location.href="helpStop.cfm";
	if(value == "11")
		parent.HELPWIN.location.href="helpZoomprevious.cfm";
	if(value == "12")
		parent.HELPWIN.location.href="helpAddress.cfm";
	if(value == "13")
		parent.HELPWIN.location.href="helpIntersection.cfm";
	if(value == "14")
		parent.HELPWIN.location.href="helpTwnshprng.cfm";
}
*/

//============================================================================================
// Upon loading the map for Netscape users, check viewer plug-in version
function init()
{
	// For Netscape browsers, check for plug-in
	if (navigator.appName == "Netscape")
	{
		// If the Plug-In is installed, check the version by returning
		// the API version
		var version = getMap().getApiVersion();
		//If the API/Plug-In version is previous to 5.0,
		//display the message
		if (version < "5.0")
		{
			updateDownloadMsg();
			return;
		}
	}
}

// Called from function INIT
function updateDownloadMsg()
{
	// Display dialog box.
	msg = "You do not have the latest version of the " +
	"Autodesk MapGuide Viewer. This web page is " +
	"currently using version 5.0. Click OK to update " +
	"or Cancel to continue."
	// If user clicks OK, load download page from Autodesk web site
	if (confirm(msg))
	{
		top.window.location =
		"http://www.autodesk.com/prods/mapguide/ver5/download/index.htm";
	}
	else
	{
		msg="The Autodesk Mapguide Viewer was not updated. Some " +
		"functionality of this web site may be limited or " +
		"possibly not work at all. If you experience this " +
		"situation, reload this web page and click OK when " +
		"prompted to update the viewer."
		alert(msg);
		//history.go(-1);
	}
}
