﻿
//pointIdentifyTool = null;
var scrnpnt = null;
var identifyPosition = new Object();

PointLayerIdentifyTool = function(element) {
    PointLayerIdentifyTool.initializeBase(this);
	this.selectedIndex = 0;
	this.selectedValue = "";
	this._lastExpanded = false;
	this._addToResultsLink = "";
	this._layerInfo = "";
	this._dropDownList = "";
	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
			var dropdown = $get("dropdown_" + this.get_id());
			if (dropdown!=null) dropdown.style.display = "none";
			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();
	        if (document.documentElement.dir=="rtl") argument += "&dir=rtl";
	        this.doCallback(argument, this);
}
	});
	//arcgisIdentifyTool = this;
};


PointLayerIdentifyTool.prototype = {
	startIdentify : function() {
		//arcgisIdentifyTool = this;
		this._processing = false;
		//tell the ESRI Map control to go into "getGeometry" mode
		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]);
		    this._processing = false;
		    if (this.waitIcon) { this._map.removeGraphic(this.waitIcon); }	    
		}
		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;
	} ,
	_getLayerName : function(index){
		if (this.identifyAttributes == null || this.identifyAttributes.length < (index + 1))
			return "";
		return this.identifyAttributes[index]["layer"];
	},
	_getLayerInfo : function(index){
  		if (this.identifyAttributes == null || this.identifyAttributes.length < (index + 1))
			return "";
		var mresource = this.identifyAttributes[index]["resource"];
		return ((mresource.length>0) ? (mresource +  " &gt; ") : "") + this._getLayerName(index); 
	},
	_getTitle : function(index){
		if (this.identifyAttributes == null || this.identifyAttributes.length < (index + 1))
			return "";
		var layerName = this._getLayerName(index);
		var title =  this.identifyAttributes[index]["title"];
		if (title==null || title.length==0)
			titleString = "<b>" + layerName + "</b>"; //if title is empty, use layer name     
		else
			titleString = title + "&nbsp; (" + layerName + ")";
		return titleString;
	},
	setToggleImage : function () {
		var toggleId = "expand_" + this.get_id();
		var toggleImage = $get(toggleId); 
		var maptip = this.identifyMapTips;
		if (maptip.get_isExpanded()) {
			toggleImage.src = "images/collapse.gif";
			toggleImage.alt = "Collapse";
		} else {
			toggleImage.src = "images/expand.gif";
			toggleImage.alt = "Expand";
		}
	},
	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;
	},
	 //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;	
		
	}
};
PointLayerIdentifyTool.registerClass('PointLayerIdentifyTool', Sys.Component);


function identifyExpandDetails(idToolId) {
	var idTool = $find(idToolId); 
	idTool.identifyComboBoxPopulate(idTool.selectedIndex); 
	idTool.setToggleImage();
	idTool.identifyMapTips.expand();
	idTool.identifyMapTips.updateContent();
};

function expandCollapseIdentify(idToolId) {
	hideIdentifyDropdown(idToolId);
	var idTool = $find(idToolId);
	var maptip = idTool.identifyMapTips;
	if (maptip.get_isExpanded()) {
		maptip.collapse();
		idTool._lastExpanded = false;
	} else {
		identifyExpandDetails(idToolId);
		maptip.expand();
		idTool._lastExpanded = true;
	}
};

function closeIdentifyPanel() {
	var dropdown = $get("dropdown_" + arcgisIdentifyTool.get_id());
	if (dropdown) dropdown.style.display = "none";
	arcgisIdentifyTool.clearOutCallout();
};

function toggleIdentifyDropdown(visibility) {
	var dropdown = $get("identifyComboBox");
	var idtitle = $get("identifyTitleText");
	if (dropdown!=null && idtitle!=null) { 
		if (visibility=="visible") {
			dropdown.style.display = "";
			idtitle.style.display = "none";
		} else {
			dropdown.style.display = "none";
			idtitle.style.display = "";
		}
	}
};

function hideIdentifyDropdown(id) {
	var dropdown = $get("dropdown_" + id);
	var textbox = $get("identifyTitleText");
	var button = $get("identifyDropdownButton"); 
	dropdown.style.display = "none";
	if (button!=null) button.style.visibility = "hidden"; 
	if (textbox!=null) textbox.style.borderColor = "rgb(238, 238, 238)";
	return false;
};

function showIdentifyDropdown(id) {
	var textbox = $get("identifyTitleText");
	var dropdown = $get("dropdown_" + id);
	var rect = GetElementRectangle(textbox); 
	var leftpos = (Sys.Browser.agent === Sys.Browser.InternetExplorer) ? rect.left-2 : rect.left;
	var toppos = (Sys.Browser.agent === Sys.Browser.InternetExplorer) ? rect.bottom-2 : rect.bottom;  
	dropdown.style.left = leftpos + "px";
	dropdown.style.top = toppos + "px";
	dropdown.style.height = "auto";
	if (dropdown.style.display==="none") 
		dropdown.style.display = "";
	else
		dropdown.style.display="none";
	var bounds = Sys.UI.DomElement.getBounds(dropdown);
	var ddLength = bounds.height;
	var pHeight = arcgisIdentifyTool.getPageHeight();
	if (pHeight < ddLength + toppos) {
	    var ddHeight = pHeight - toppos;
	    if (ddHeight < 160) {
	        ddHeight = ddLength > toppos ? toppos : ddLength;
	        toppos -=ddHeight;
            dropdown.style.top = toppos + "px";
	    } 
	    dropdown.style.height = ddHeight + "px";
	}
	return false;
};

function showIdentifyDropdownButton() {
	var textbox = $get("identifyTitleText");
	textbox.style.borderColor = "rgb(77, 77, 77)";
	var button = $get("identifyDropdownButton");
	button.style.visibility = "visible";
	return false;
};

function selectIdentifyDropdownItem(idToolId, index,layer) {
	var idTool = $find(idToolId);
	var idText = $get("identifyTitleTextHolder"); 
	idTool.selectedIndex = index;
	idTool.selectedValue = layer;
	 var titleText = layer;
	if (titleText.length==0) titleText = idTool._getLayerName(index);
	if (idText!=null) idText.innerHTML = "&nbsp;" + titleText;
	identifyExpandDetails(idToolId);
	hideIdentifyDropdown(idToolId);
	return false; 
};


function AddToTaskResults(index) {
	var link = $get(arcgisIdentifyTool.identifyMapTips._maptipAddLinkID);
	if (link!=null) {
		link.innerHTML = ""; 
		arcgisIdentifyTool.identifyMapTips.adjustHeight();
	}
	arcgisIdentifyTool._addedToResults[index] = true; 
	var argument = "mode=addresults&index=" + index;
	arcgisIdentifyTool.doCallback(argument, arcgisIdentifyTool);
	
};