//client info command to control client behavior for info tool.
function Command(name, interaction)
{
}

Command.prototype.CreateUrl = function()
{
}

Command.prototype.AddParamToUrl = function(param, value)
{
	this.url += "&" + param + "=" + value;
}

Command.prototype.Execute = function()
{
}
Command.prototype.Init = function(name, interaction)
{
	this.name = name;
	if (interaction != null) {
		this.interaction = interaction;
		this.interaction.onComplete = this.eventHandler("Execute");
	}
}

function MapCommand(name, interaction)
{
	if (arguments.length > 0) {
		this.Init(name, interaction);
	}
}
MapCommand.prototype = new Command();
MapCommand.prototype.constructor = MapCommand;
MapCommand.superclass = Command.prototype;
MapCommand.prototype.CreateUrl = function()
{
	var mapImage = this.interaction.element;
	if (!mapImage.mapAlias) mapImage.mapAlias = mapImage.attributes["mapAlias"].value;
	if (!mapImage.exportFormat) mapImage.exportFormat = mapImage.attributes["exportFormat"].value;
	
	this.url = "Map.aspx?Command="+ this.name + 
					"&Width=" + mapImage.width +
					"&Height=" + mapImage.height +
					"&ExportFormat=" + mapImage.exportFormat +
					"&Ran=" + Math.random();
	if (this.interaction.PointsData.NumPoints() > 0) this.AddParamToUrl("Points", this.interaction.PointsData.GetPointsString(mapImage.origin));
	if (mapImage.mapAlias) this.AddParamToUrl("MapAlias", mapImage.mapAlias);
}

MapCommand.prototype.UpdateMap = function()
{
	var mapImage = this.interaction.element;
	
	// Set the source of the image to url to just change the map
	mapImage.style.left = 0;
	mapImage.style.top = 0;
	mapImage.style.clip = 'rect(' + 0 + ' ' +  mapImage.width + ' ' + mapImage.height + ' ' + 0 +')';
	try {
	mapImage.src = this.url;
	} catch(e) { alert("ll"); }
}


MapCommand.prototype.Execute = function()
{
	this.CreateUrl();
	
	this.UpdateMap();
}

//client info command to control client behavior for info tool.
function AreaCommand(name, interaction)
{
	if (arguments.length > 0) {
		this.Init(name, interaction);
	}
}
AreaCommand.prototype = new MapCommand();
AreaCommand.prototype.constructor = AreaCommand;
AreaCommand.superclass = MapCommand.prototype;
AreaCommand.prototype.Execute = function()
{
    mapIsLoading();
	this.CreateUrl();
	this.AddParamToUrl("PixelTolerance", this.pixelTolerance);
	//create an XMLHttp obj to send request to server
	var xmlHttp = CreateXMLHttp();
	xmlHttp.open("GET", this.url, false);
	xmlHttp.send(null);
	
	//get response back
	this.result = xmlHttp.responseText;
	//document.forms[0].submit();
	window.location="MainForm.aspx";

};

//client info command to control client behavior for info tool.
function AreaSenderCommand(name, interaction)
{
	if (arguments.length > 0) {
		this.Init(name, interaction);
	}
}
AreaSenderCommand.prototype = new MapCommand();
AreaSenderCommand.prototype.constructor = AreaSenderCommand;
AreaSenderCommand.superclass = MapCommand.prototype;
AreaSenderCommand.prototype.Execute = function()
{
    mapIsLoading();
	this.CreateUrl();
	this.AddParamToUrl("PixelTolerance", this.pixelTolerance);
	//create an XMLHttp obj to send request to server
	var xmlHttp = CreateXMLHttp();
	xmlHttp.open("GET", this.url, false);
	xmlHttp.send(null);
	this.result = xmlHttp.responseText;
	window.location="SendArea.aspx";
};

//client info command to control client behavior for info tool.
function DistanceCalculationCommand(name, interaction)
{
	if (arguments.length > 0) {
		this.Init(name, interaction);
	}
}
DistanceCalculationCommand.prototype = new MapCommand();
DistanceCalculationCommand.prototype.constructor = DistanceCalculationCommand;
DistanceCalculationCommand.superclass = MapCommand.prototype;
DistanceCalculationCommand.prototype.Execute = function()
{
    mapIsLoading();
	this.CreateUrl();
	this.AddParamToUrl("PixelTolerance", this.pixelTolerance);
	//create an XMLHttp obj to send request to server
	var xmlHttp = CreateXMLHttp();
	xmlHttp.open("GET", this.url, false);
	xmlHttp.send(null);
	//get response back
	this.result = xmlHttp.responseText;
	//mywindow = window.open("DistanceResultForm.aspx","","Height=150 Width=300 menubar=no resizable=no scrollbars=no status=no toolbar=no");
	//document.forms[0].submit();
	window.location="MainForm.aspx";
};


//client info command to control client behavior for info tool.
function PointRequestCommand(name, interaction)
{
	if (arguments.length > 0) {
		this.Init(name, interaction);
	}
}
PointRequestCommand.prototype = new MapCommand();
PointRequestCommand.prototype.constructor = PointRequestCommand;
PointRequestCommand.superclass = MapCommand.prototype;
PointRequestCommand.prototype.Execute = function()
{
    mapIsLoading();
	this.CreateUrl();
	this.AddParamToUrl("PixelTolerance", this.pixelTolerance);
	//create an XMLHttp obj to send request to server
	var xmlHttp = CreateXMLHttp();
	xmlHttp.open("GET", this.url, false);
	xmlHttp.send(null);
	//get response back
	this.result = xmlHttp.responseText;
	window.location="SendPoint.aspx";
	//RefreshMap();
};



function RefreshMap()
{
    var winW = Math.round(document.documentElement.clientWidth * 0.75);
	var winH = Math.round(document.documentElement.clientHeight * 0.65);
	
	var scaleBar = document.getElementById("scalebar");
	if(scaleBar!=null){
	    scaleBar.width = Math.round(191 * (winW / 600));
	}
	var mapImage = document.getElementById("MapControl1_Image");
	if(mapImage!=null){
		if (!mapImage.mapAlias) 
		    mapImage.mapAlias = mapImage.attributes["mapAlias"].value;
			if (!mapImage.exportFormat) mapImage.exportFormat = mapImage.attributes["exportFormat"].value;
				//this.url = "MapController.ashx?Command=GetMap" +
				this.url = "Map.aspx?Command=GetMap" +
				//"&Width=" + mapImage.width +
				//"&Height=" + mapImage.height +
				"&Width=" + winW +
				"&Height=" + winH +
				"&ExportFormat=" + mapImage.exportFormat +
				"&Ran=" + Math.random();
				if (mapImage.mapAlias) 
				    this.url += "&MapAlias=" + mapImage.mapAlias;
				document.getElementById("loader").style.visibility="visible"; 
			    mapImage.src = this.url;
	}
};