
var TFGlobalCID = "";
var TFFirstPass = true;

function TFInRect(x, y, x1, y1, x2, y2) 
{
	return ((x >= x1) && (x <= x2) && (y >= y1) && (y <= y2))
}


function TFNav(elmnt, event)
{
	var x = event.offsetX?(event.offsetX):event.layerX;
	var y = event.offsetY?(event.offsetY):event.layerY;
	var sCmd = "";
	
	if (TFInRect(x, y, 20, 0, 36, 16)) {
		sCmd = "PanUp";
	}
	else if (TFInRect(x, y, 0, 20, 16, 36)) { 
		sCmd = "PanLeft";
	}
	else if (TFInRect(x, y, 40, 20, 56, 36)) { 
		sCmd = "PanRight";
	}
	else if (TFInRect(x, y, 20, 40, 36, 56)) { 
		sCmd = "PanDown";
	}
	else if (TFInRect(x, y, 20, 20, 36, 36)) { 
		sCmd = "Center";		
	}	
	
	if (sCmd != "")	{
		zoomCmd.DoZoom("SliderZoom", 0, sCmd);
	} 
}

function TFShowProgress()
{
	var progImage = document.getElementById("TFMapProgress"); 
	progImage.style.visibility = "visible";
	setTimeout('TFHideProgress();', 5000);  //this will hide the progress image even if things fail to return
}

function TFHideProgress()
{
	var progImage = document.getElementById("TFMapProgress"); 
	progImage.style.visibility = "hidden";
}

function TFMapError()
{
	TFHideProgress();
}

function TFGetZoom()
{
	if (TFFirstPass) {
		TFFirstPass=false;
		return;
	}
	
	var url = "MapController.ashx?Command=SliderZoom&Cmd=GetZoom&XRan=" + Math.random();
	var mapImage = document.getElementById("MapControl_Image"); 
	if (mapImage.mapAlias) {
		url += "&MapAlias=" + mapImage.mapAlias;
	}

	//TFShowProgress();
	try 
	{
		var xmlHttp = CreateXMLHttp();	
		xmlHttp.open("GET", url, false);	
		xmlHttp.send(null);		
		var result = xmlHttp.responseText; 
		//alert(result);
		//if we do not get a zoom value back something happened, like the session timed out
		//then redirect back to the address page to start up the session again.  
		//might want to put a message here letting the user know?
		if (result == '') {  
			//location = 'address.aspx?rnd=' + (Math.random());
		}
		else {
			tfZoomSlider.f_setValue(result);
		}
		TFHideProgress();
	} 
	catch(e) 
	{ 
		TFHideProgress();
	}	
}

function TFZoomTo(lZoom)
{	
	zoomCmd.DoZoom("SliderZoom", lZoom, "Zoom");
}


//CustomCommand definition, base class for ThemeCommand and LegendCommand
function CustomCommand(mapImageID)
{
}
CustomCommand.prototype.CreateUrl = function()
{
	this.url = "MapController.ashx?XRan=" + Math.random();
				
	//if (this.mapImage.mapAlias) this.AddParamToUrl("MapAlias", this.mapImage.mapAlias);
	var mapImage = document.getElementById("MapControl_Image"); 
	//if (mapImage.mapAlias) {
	//	url += "&MapAlias=" + mapImage.mapAlias;
	//}
	
}
CustomCommand.prototype.AddParamToUrl = function(param, value)
{
	this.url += "&" + param + "=" + value;	
}

//Width, Height, ExportFormat, Command are predefined names at server side to get request values
//So a couple of functions defined to hard-code these key names
CustomCommand.prototype.AddMapSizeToUrl = function(width, height)
{
	this.url += "&Width=" + width;
	this.url += "&Height=" + height;	
}

CustomCommand.prototype.AddMapExportFormatToUrl = function(format)
{
	this.url +=  "&ExportFormat=" + format;	
}

CustomCommand.prototype.AddSrvCommandNameToUrl = function(cmdName)
{
	
	this.url += "&Command=" + cmdName;	
}

CustomCommand.prototype.Init = function(mapImageID)
{
	this.mapImageID = mapImageID;
	this.mapImage = document.getElementById(mapImageID);
}

//ZoomCommand inherits CustomCommand
function ZoomCommand(mapImageID)
{
	if (arguments.length > 0) {
		this.Init(mapImageID);
	}
}
ZoomCommand.prototype = new CustomCommand();
ZoomCommand.prototype.constructor = ZoomCommand;
ZoomCommand.superclass = CustomCommand.prototype;
ZoomCommand.prototype.Init = function(mapImageID)
{
	ZoomCommand.superclass.Init.call(this, mapImageID);
}
ZoomCommand.prototype.DoZoom = function(srvCmdName, lZoom, sSubCmd)
{
	this.CreateUrl();
	if (this.mapImage.mapAlias) {
		this.url += "&MapAlias=" + this.mapImage.mapAlias;
	}
	else {
		this.url += "&MapAlias=TFIMap";
	}
	
	this.AddMapSizeToUrl(this.mapImage.width, this.mapImage.height);
	this.AddMapExportFormatToUrl("Gif");
	this.AddParamToUrl("Zoom", lZoom);
	this.AddParamToUrl("Cmd", sSubCmd);
	this.AddParamToUrl("CID", TFGlobalCID);
	this.AddSrvCommandNameToUrl(srvCmdName);		
	try 
	{
		TFShowProgress();
overrideMapInfoImage(this.url);
	//	this.mapImage.src = this.url;
	} 
	catch(e) 
	{ 
		TFHideProgress();
	}					
}

//define themeCmd and legendCmd, which can be used throughout the application.
var zoomCmd = new ZoomCommand("MapControl_Image");
