ExportTool = function(floatingPanel, map, mainPanel, loadingPanel, completedPanel, saveButton, previewLink, previewOptionPanel)
{						  	
	this.floatingPanel = floatingPanel;
    this.map = map;
	this.mainPanel = mainPanel;
	this.loadingPanel = loadingPanel;
	this.completedPanel = completedPanel;
	this.previewOptionPanel = previewOptionPanel;
	this.saveButton = saveButton;	this.previewLink = previewLink;
	this.floatingPanel.add_close(Function.createDelegate(this, this.closePanel));	
	this.saveButton.onclick = Function.createDelegate(this, this.saveFile); 	
	this.preview = null;
	this.previewBorder = null;
	this.previewStrokePixels = 500;
	this.open = false;
	this.userModified = false;
	this.enablePreviewOption = true;
	
	this.mainHelpString = "Image Sizes (for export formats other than PDF) assume 96 dpi.  A PDF export has a static layout, therefore the image size is not configurable.  <br/><br/>For Image Exports, PNG is strongly recommended for image quality but JPG will frequently result in a smaller file size.";
}
											   	
ExportTool.prototype = 
{	
	show : function()
	{	
		this.mainPanel.style.display = "block";
		this.loadingPanel.style.display = "none";
		this.completedPanel.style.display = "none";		  	
	
		this.map.add_extentChanged(Function.createDelegate(this, this.showPreviewArea));
		
		floatingPanelManager.closeAll();
		this.floatingPanel.open();
		this.floatingPanel.get_helpPanel().innerHTML = this.mainHelpString;		
		this.open = true;
		
		this.setFormat();	
	},
	
	closePanel : function()
	{
		this.map.remove_extentChanged(Function.createDelegate(this, this.showPreviewArea));
		if(this.preview != null)
		{
			this.map.graphicsLayers["tools"].removeItem(this.preview);
			this.map.graphicsLayers["tools"].removeItem(this.previewBorder);
			this.map.graphicsLayers["tools"].removeItem(this.previewText);
			this.preview = null;
			this.previewBorder = null;
			this.previewText = null;
		}					  		
		this.open = false;		
	},
	
	saveFile: function()
	{
		window.location.href = exporter.previewFilePath + "&download=true";
		this.closePanel();
		return false;
	},
	
	setFormat: function()
	{
		var list = $get("ddlExportFormat");
		if(list.selectedIndex < 3){
			$get("exportImageOptions").style.display = "block";
			this.enablePreviewOption = true;
		}else{
			$get("exportImageOptions").style.display = "none";
			this.enablePreviewOption = false;
		}
		
		this.showPreviewArea();
	},
	
	showPreviewArea : function()
	{	
		if(this.open)
		{	
		    if(!this.userModified)
			{
			    var width1 = this.map.get_width() / 96;
			    var width1Floored = Math.floor(width1);

			    var height1 = this.map.get_height() / 96;
			    var height1Floored =  Math.floor(height1);
			    var height1ByAspectRatio = width1Floored * this.map.get_aspectRatio();
			    
			    $get("tbExportWidth").value = width1Floored;
			    $get("tbExportHeight").value = height1Floored;
			}
			
			var width = 742;
			var height = 742;
			
			if($get("ddlExportFormat").value != "pdf"){
				width = parseInt($get("tbExportWidth").value) * 96;
				height = parseInt($get("tbExportHeight").value) * 96;
			}
			
			var unitX = this.map.get_mapUnitsPerPixelX() * width;	
			var unitY = this.map.get_mapUnitsPerPixelY() * height;
			
			var center = this.map.get_extent().get_centerPoint();
			
			var xMin = center.x - (unitX / 2);
			var xMax = center.x + (unitX / 2);
			var yMin = center.y - (unitY / 2);
			var yMax = center.y + (unitY / 2);
			
			//-----------------------------------------------
			//adjustment calculations for "window" envelope
			//-----------------------------------------------
			
			var ieVMLAdjustHackValue = 0;
			
			if (isIE) {
				var dpi = screen.deviceXDPI;
			    if (dpi == 96) {
			        ieVMLAdjustHackValue = -83;
			    } else {
			        //120dpi
			        ieVMLAdjustHackValue = -166;
			    }
		    }
						
			var adjustUnitsX = this.map.get_mapUnitsPerPixelX() * (this.previewStrokePixels/2 - ieVMLAdjustHackValue);
			var adjustUnitsY = this.map.get_mapUnitsPerPixelY() * (this.previewStrokePixels/2 - ieVMLAdjustHackValue);
			
			var adjusted_xMin = xMin - adjustUnitsX;
			var adjusted_xMax = xMax + adjustUnitsX;
			var adjusted_yMin = yMin - adjustUnitsY;
			var adjusted_yMax = yMax + adjustUnitsY;
			//-----------------------------------------------
			
			if(this.preview == null)
			{
				this.preview = new MapsDirect.Vector.Graphic("envelope");
				this.previewBorder = new MapsDirect.Vector.Graphic("envelope");
				
				this.previewBorder.style = new MapsDirect.Vector.Style("shortdash", "black", 2, 0.7, "black", 0.0);
				this.preview.style = new MapsDirect.Vector.Style("solid", "#666666", this.previewStrokePixels, 0.5, "white", 0);
				this.map.graphicsLayers["tools"].addItem(this.preview, false);
				this.map.graphicsLayers["tools"].addItem(this.previewBorder, false);
				
				this.previewText = new MapsDirect.Vector.Graphic("text");
				this.previewText.textString = "Export\nArea";
				this.previewText.style = new MapsDirect.Vector.Style("shortdash", "black", 2, 0.5, "black", 0.1);
				this.previewText.style.textSize = "24pt";														 				
				this.map.graphicsLayers["tools"].addItem(this.previewText, false);				
			}
			
			if(this.getScaleToFit() == "no") {
				this.preview.geometry.putCoords({x:adjusted_xMin, y:adjusted_yMax}, {x:adjusted_xMax, y:adjusted_yMin});
				this.previewBorder.geometry.putCoords({x:xMin, y:yMax}, {x:xMax, y:yMin});
				//this.preview.geometry.putCoords({x:xMin-this.previewStrokePixels/2, y:yMax+this.previewStrokePixels/2}, {x:xMax+this.previewStrokePixels/2, y:yMin-this.previewStrokePixels/2});
				
				var x = center.x - (this.map.get_mapUnitsPerPixelX() * 100); // rough calculation to position the text
				var y = center.y + (this.map.get_mapUnitsPerPixelY() * 25);;
				this.previewText.geometry.defineGeometry({x:x,y:y}, 1);
				
				this.map.graphicsLayers["tools"].draw(this.preview);
				this.map.graphicsLayers["tools"].draw(this.previewBorder);
				this.map.graphicsLayers["tools"].draw(this.previewText);
			}else{
				this.map.graphicsLayers["tools"].erase(this.preview);
				this.map.graphicsLayers["tools"].erase(this.previewBorder);
				this.map.graphicsLayers["tools"].erase(this.previewText);
			}
		}
	},
		
	exportToFile: function()
	{
		this.map.graphicsLayers["tools"].removeItem(this.preview, false);
		this.map.graphicsLayers["tools"].removeItem(this.previewBorder, false);
	    this.map.graphicsLayers["tools"].removeItem(this.previewText, false);
				
		this.mainPanel.style.display = "none";
		this.loadingPanel.style.display = "block";
		this.completedPanel.style.display = "none";
		
		var format = $get("ddlExportFormat").value;
		var width = $get("tbExportWidth").value;
		var height = $get("tbExportHeight").value;
		var scaleToFit = this.getScaleToFit();
		this.map.exportToFile(format, width, height, scaleToFit, exportComplete);
	},
	
	getScaleToFit : function()
	{
	    var i;
	    var group=null;
	    
	    for (i=0; i<document.forms.length; i++) {
	        if (group==null) {
	            var group = document.forms[i].rbExportScaleToFit;
	        }
	    }
	    
	    if (group != null) {
		    for(var i = 0; i < group.length; i++) {
			    if (group[i].checked) {
				    return group[i].value;
			    }
		    }
		} else {
		    //error
		}
	}
}

function exportComplete(file) {
	exporter.mainPanel.style.display = "none";
	exporter.loadingPanel.style.display = "none";
	exporter.completedPanel.style.display = "block";
	
	if (exporter.enablePreviewOption) {
	    exporter.previewOptionPanel.style.display="";
	} else {
	    exporter.previewOptionPanel.style.display="none";
	}
	
	exporter.file = file;
	
	// remove all bookmarks (#) from the url
	var path = location.href.replace(/(#.*)?$/, "");
	// remove all parmeter from the url
	path = path.replace(/(\?.*)?$/, "");
	exporter.previewFilePath = path + "?getexport=" + exporter.file;
	exporter.previewLink.href = exporter.previewFilePath;
};
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();