﻿
arcgisIdentifyTool = null;
var scrnpnt = null;
var identifyPosition = new Object();

ESRI.ADF.UI.MapIdentifyTool = function(element) {
	ESRI.ADF.UI.MapIdentifyTool.initializeBase(this);
	this.selectedIndex = 0;
	this.selectedValue = "";
	this._identifyIcon = null;
	this._waitIcon = null;
	this._processing = false;
	this._onIdentify = Function.createDelegate(this,function(geom) {
		// don't continue if previous call is not finished
		if (!this._processing) {
			var map = $find("Map1");
			this._processing = true;
			// remove callout if it already exists
			if (this.identifyMapTips) this.identifyMapTips.dispose();
			if(this.identifyMapCallout) {
				map.removeGraphic(this.identifyMapCallout);
				this.identifyMapCallout.dispose();
			}
			// put the ajax activity indicator at cursor click and make visible
			var mappnt = new ESRI.ADF.Geometries.Point(geom.get_x(), geom.get_y());
			scrnpnt = this._map.toScreenPoint(mappnt);
			var wicon = $find(this.get_id()+'_waiticon');
			if(wicon==null) {
				this.waitIcon = $create(ESRI.ADF.Graphics.GraphicFeature,
				{"id":this.get_id()+'_waiticon',"geometry":geom,"symbol":new ESRI.ADF.Graphics.MarkerSymbol(this._waitIcon,8,8)
				});
				this.waitIcon.get_symbol().set_imageFormat("gif");
				this.waitIcon.get_symbol().set_width(16);
				this.waitIcon.get_symbol().set_height(16);
			}
			else
				this.waitIcon.set_geometry(geom);
			map.addGraphic(this.waitIcon);
			// make callback for identify
			var argument = "mode=identify&coords=" + geom.get_x() + ":" + geom.get_y();
	        this.doCallback(argument,this);
		}
	});
	arcgisIdentifyTool = this;
};
ESRI.ADF.UI.MapIdentifyTool.prototype = {
	startIdentify : function() {
	    arcgisIdentifyTool = this;
		this._processing = false;
		this._map.getGeometry(ESRI.ADF.Graphics.ShapeType.Point,this._onIdentify,null,'black','gray','pointer',true);
	},
	processCallbackResult : function(action,params) {
	    if(action=="mappoint") {
			this.identifyLocation(this._map.get_id(), params[0], params[1], params[2]); 
		}
		else if(action=='error') {
			if(this.waitIcon) { map.removeGraphic(this.waitIcon); }
			alert("Error: " + params[0]);
			Sys.Debug.trace ('Error: '+params[0])
		}
	},
	doCallback : function(argument, context) {
		/// <summary>
		/// Performs a callback or partial postback depending on it's postback mode
		/// </summary>
		/// <param name="argument" type="String">The argument parsed back to the server control</param>
		/// <param name="context" type="string">The context of this callback</param>
		/// <returns />
		ESRI.ADF.System._doCallback(this._callbackFunctionString, this.get_uniqueID(), this.get_id(), argument, context);
	},
	get_uniqueID : function() {
		/// <value name="uniqueID" type="String">Gets or sets the unique ID used to identify the control serverside</value>
		return this._uniqueID;
	},
	set_uniqueID : function(value) {
		this._uniqueID = value;
	},
	get_callbackFunctionString : function() {
		/// <value name="callbackFunctionString" type="String">Gets or sets the callback function string used to call back to the serverside control.</value>
		/// <remarks>
		/// Executing the callbackfunctionstring will either generate a partial postback or a callback to the server, depending on the current AJAX mode.
		/// To perform a call to the servercontrol, use the <see cref="doCallback"/> method of this instance.
		/// </remarks>
		return this._callbackFunctionString;
	},
	set_callbackFunctionString : function(value) {
		this._callbackFunctionString = value;
	},
	get_map : function() {
		return this._map;
	},
	set_map : function(value) {
		this._map = value;
	} ,
    _getTitle : function(index){
	    if (this.identifyAttributes == null || this.identifyAttributes.length < (index + 1))
			return "";
		var title =  this.identifyAttributes[index]["title"];
		return title;
	},
	identifyLocation : function (mapID, pointX, pointY, pointAttributes) {
	    this.identifyMapClientID = mapID;
		var newPt = new ESRI.ADF.Geometries.Point(pointX,pointY);
		var identifyMapPoint = newPt;
		this.identifyAttributes = pointAttributes;
		this.selectedIndex = 0;
		this.selectedValue = "";
		this.identifyComboBox = "<b>" + "There is no information available for this location." + "</b>";
		if (this.identifyAttributes.length==0) { this.identifyComboBox = "No Features Found"; }
		else if (this.identifyAttributes.length == 1) { // One layer
			this.identifyComboBox = this._getTitle(0); 
			this._lastExpanded = true;
		} 
		if (this.identifyComboBox.length==0) this.identifyComboBox = "<b>" + "No Features found at this location." + "</b>";
		
		this.identifyMapTips = new ESRI.ADF.UI.IdentifyMapTips(this);
		this.identifyMapTips.set_animate(true);
		this.identifyMapTips.set_hoverTemplate(this.identifyComboBox);
		this.identifyMapTips.set_maxheight(350);	
		this.identifyMapTips.set_width(275);
		this.identifyMapTips.initialize();
		var imcallout = $find(mapID+'_identifyicon');
		if (imcallout!=null) {
			this._map.removeGraphic(imcallout);
			imcallout.dispose();
			imcallout = null; 
		}
		this.identifyMapCallout = $create(ESRI.ADF.Graphics.GraphicFeature,
			{"id":mapID+'_identifyicon',"geometry":identifyMapPoint,"symbol":new ESRI.ADF.Graphics.MarkerSymbol(this._identifyIcon,12,24),
			"mapTips":this.identifyMapTips,
			"attributes":pointAttributes[0]});

		this.identifyMapCallout.get_symbol().set_imageFormat("png32");
		if(this.waitIcon) { this._map.removeGraphic(this.waitIcon); }	    
		this._map.addGraphic(this.identifyMapCallout);
		var callout = this.identifyMapTips.get_callout();
		// populate, position, and show the inital display
        this.identifyMapTips._setContent(this.identifyMapCallout.get_attributes());
		this.identifyMapTips.setPosition(this.identifyMapCallout.get_geometry());   //This is where the position gets set for the data box
		this.identifyMapTips.get_callout().show();
		
		if(this.identifyAttributes.length>0) { 
		    var layerinfo = $get(this.identifyMapTips._maptipLayerInfoID);
		    if(layerinfo) { layerinfo.style.display=''; }
		    this.identifyMapTips.adjustHeight();
		    ESRI.ADF.UI.IdentifyMapTips.callBaseMethod(this.identifyMapTips, 'expand');
		}
		else {
		    var layerinfo = $get(this.identifyMapTips._maptipLayerInfoID);
		    if(layerinfo) { layerinfo.style.display='none'; }
		    this.identifyMapTips.adjustHeight();
		    ESRI.ADF.UI.IdentifyMapTips.callBaseMethod(this.identifyMapTips, 'collapse');
		}
		
		if(!this.onMapZoom) {
			this._onMapZoom = Function.createDelegate(this, this.clearOutCallout );
			this._map.add_zoomStart(this._onMapZoom);
		}
		this._processing = false;
	},
    get_identifyIcon : function() {
		return this._identifyIcon;
	},
	set_identifyIcon : function(value) {
		this._identifyIcon = value;
	},
	get_waitIcon : function() {
		return this._waitIcon;
	},
	set_waitIcon : function(value) {
		this._waitIcon = value;
	},
	clearOutCallout : function() { 
		if (this.identifyMapTips) this.identifyMapTips.dispose();
		if(this.identifyMapCallout) {
			this._map.removeGraphic(this.identifyMapCallout);
			this.identifyMapCallout.dispose();
		}
	},
	 //get the page height
	getPageHeight : function() {
		var height = window.innerHeight;
		if (height == null) {
			if (document.documentElement && document.documentElement.clientHeight)
				height = document.documentElement.clientHeight;
			else
				height = document.body.clientHeight;
		}
		return height;	
	}
};

ESRI.ADF.UI.MapIdentifyTool.registerClass('ESRI.ADF.UI.MapIdentifyTool', Sys.Component);

ESRI.ADF.UI.IdentifyMapTips = function(idTool) {
    ESRI.ADF.UI.IdentifyMapTips.initializeBase(this);
	this._onMouseGfxOverHandler = Function.createDelegate(this,function() { }); 
	this._onZoomStartHandler = Function.createDelegate(this, function() { if(arcgisIdentifyTool.identifyMapTips.get_callout()) { arcgisIdentifyTool.identifyMapTips.get_callout().hide(); } window.clearTimeout(arcgisIdentifyTool.identifyMapTips._showDelayedTimer); });
	this.set_maxheight(300);
	this.set_autoHide(false);
	this.set_expandOnClick(false);
	this._idTool = idTool;
	var elmID = this._idTool.get_id();
	this._maptipExpandID = "expand_"+elmID;
	this._maptipCloseID = "close_"+elmID;
	this._maptipLayerInfoID = "layerinfo_"+elmID;
	this._closeUrl = (isIE && ieVersion<7) ? "images/dismiss_solid.png" : "images/dismiss.png";
};

ESRI.ADF.UI.IdentifyMapTips.prototype = {
	initialize : function() {
	    ESRI.ADF.UI.IdentifyMapTips.callBaseMethod(this, 'initialize');
	},
	createTemplate : function() {
	    //overrides base.createTemplate
		var template = '<div style="position: relative; z-index: 12000;"><table cellpadding="0" cellspacing="0" style="position: relative;  z-index=12000; width:400px; background-color:White;border: solid 1px #F6B537;">';
		template +='<tr style="padding:2;margin:0"><td style="background-color:#F6B537;border-bottom: 1px solid #F6B537;"><table cellpadding="0" cellspacing="0" style="width: 100%"><tr>';
		template += '<td style="color: White;" id="'+ this.get_maptipTitleID() +'" >{@title}</td>';
		template +='<td align="right"><img id="' + this._maptipCloseID + '" src="' + this._closeUrl + '" alt="Close" onmousedown="closeIdentifyPanel(this)" style="cursor: pointer" /></td></tr></table></td></tr>';
		template +='<tr id="identify_content_tr" ><td style="padding:0; background-color: #F6B537"><div id="'+this.get_maptipContentID()+'" style="overflow:auto; overflow-x: auto; margin: 1px 3px 5px 3px; background-color: White; color: Black">{@content}</div>';
		template +='<div id="'+this._maptipLayerInfoID+'" style="background-color: #F6B537; padding: 0px 0px 3px 6px; font-size: xx-small; color: White;"><span style="cursor: pointer;" onClick="PrintMap();"><u>Print Map</u></span></div>';
		template +='</td></tr></table>';
		template +='</div>';
		return template;
	},
    _setContent : function(attributes){
        var contents;
		if (attributes != null) 
			contents = attributes["contents"];
		else
			contents = ""; 
		this.get_callout().setContent({"title":this._idTool.identifyComboBox,"content":contents});
	},
	updateContent : function() {
	    /// <summary>Sets the content of the maptip</summary>
		/// <param name="func" type="Object">Function to execute</param>
		var attributes = null;
		if (this._currentElement && this._currentElement.get_attributes()) {
			attributes = this._currentElement.get_attributes();
		} else {
			attributes = this._idTool.identifyAttributes[this._idTool.selectedIndex];
		}
		this._setContent(attributes);
		this._setPngForIE6(this._maptipCloseID, "images/dismiss.png");
		this.adjustHeight();
	},
	_setPngForIE6 : function (imgId, newSource) {
		if (Sys.Browser.agent === Sys.Browser.InternetExplorer && Sys.Browser.version<7) {
			var imgElement = $get(imgId);
            ESRI.ADF.System.setIEPngTransparency(imgElement, newSource,true);
		}
	}
};
ESRI.ADF.UI.IdentifyMapTips.registerClass('ESRI.ADF.UI.IdentifyMapTips', ESRI.ADF.UI.MapTips);

function closeIdentifyPanel() {
    if (Sys.Browser.agent == Sys.Browser.Firefox && Sys.Browser.version >= 3)
	    window.setTimeout("arcgisIdentifyTool.clearOutCallout();", 500); // delay closing panel in Firefox 3 or later to avoid firing mousedown on map
    else
	    arcgisIdentifyTool.clearOutCallout();
};