/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace('Geodata.Component.ADF.MeasureTask');

Geodata.Component.ADF.MeasureTask.callbackTarget = "MeasureTask1";
Geodata.Component.ADF.MeasureTask.map = null;
Geodata.Component.ADF.MeasureTask.m_measureCoords = "";
Geodata.Component.ADF.MeasureTask.m_measureLastCoords = "";
Geodata.Component.ADF.MeasureTask.m_measureMouseUpSet = false;
Geodata.Component.ADF.MeasureTask.m_measureGraphicFeature = null;
Geodata.Component.ADF.MeasureTask.m_currentMeasureToolbarTool = "polyline";

Geodata.Component.ADF.MeasureTask.CopyToClipboard = function(textAreaId, toBeCopiedId)
{
    if(!isIE) {
        alert('Copying to clipboard only works in Internet Explorer.');
        return;
    }
    var textArea = document.getElementById(textAreaId);
    textArea.innerHTML = document.getElementById(toBeCopiedId).innerText;
    GDCopiedText = textArea.createTextRange();
    GDCopiedText.execCommand('Copy');
}

// Handler for MeasurePoint clicks
Geodata.Component.ADF.MeasureTask.MapCoordsClick = function(geom, evtArgs) {
    Geodata.Component.ADF.MeasureTask.DoMapCoordsClick(geom, evtArgs);
}
Geodata.Component.ADF.MeasureTask.DoMapCoordsClick = function(geom, evtArgs) {
	var geomString = '';
	var type = '';
    this.removeMeasureGraphic();
    var style = null; 
	if(ESRI.ADF.Geometries.Point.isInstanceOfType(geom)) {
		geomString = geom.toString(':');
		type = 'point';
		style = new ESRI.ADF.Graphics.MarkerSymbol("images/crosshair.png",6,6);
	}
	else if(ESRI.ADF.Geometries.Polyline.isInstanceOfType(geom)) {
		geomString = geom.getPath(0).toString('|',':');
		type = 'polyline';
		style = new ESRI.ADF.Graphics.LineSymbol("black",2);
	}
	else if(ESRI.ADF.Geometries.Polygon.isInstanceOfType(geom)) {
		geomString = geom.getRing(0).toString('|',':');
		type = 'polygon';
		style = new ESRI.ADF.Graphics.FillSymbol("black","black",2);	
		style.set_opacity(0.2);	
	}
	this.m_measureGraphicFeature = $create(ESRI.ADF.Graphics.GraphicFeature,
		        {"id": "MeasurePointIcon","geometry":geom,"symbol":style});
	this.map.addGraphic(this.m_measureGraphicFeature);        
	  	
	this.m_measureLastCoords = this.m_measureCoords;
	this.m_measureCoords = "";
	this.SendMeasureCallback(type, geomString);
}

Geodata.Component.ADF.MeasureTask.SendMeasureCallback = function(type, coords)
{
	var argument = 'ControlID='+this.map.get_id()+'&EventArg='+type+'&coords='+coords+'&VectorMode=measure';	
	var context = this;
    var callbackInvocation = "WebForm_DoCallback('" + this.callbackTarget + "',argument,ESRI.ADF.System.processCallbackResult,context,postBackError,true)";
    if(typeof(m_measureRequestId) != 'undefined') callbackInvocation = callbackInvocation.replace("argument", "(argument + '&MeasureRequestId=' + (++m_measureRequestId))");
    eval(callbackInvocation);
}

Geodata.Component.ADF.MeasureTask.MeasureCoordsMouseUp = function(sender, args) {
    Geodata.Component.ADF.MeasureTask.DoMeasureCoordsMouseUp(sender, args);
}
Geodata.Component.ADF.MeasureTask.DoMeasureCoordsMouseUp = function(sender, args) {
    if (this.m_currentMeasureToolbarTool!="point") {
        var coords = args.coordinate;
        if (coords!=null && !isNaN(coords.get_x()) && !isNaN(coords.get_y())) {
            // ignore null or non-numeric input
            if (this.m_measureCoords.length>0) {
                if (args.button==Sys.UI.MouseButton.rightButton) {
                    var pos = this.m_measureCoords.lastIndexOf("|");
                    this.m_measureCoords =  this.m_measureCoords.substring(0,pos);
                } else if (args.button==Sys.UI.MouseButton.leftButton){  
                    this.m_measureCoords += (this.m_measureCoords.length>0 ? "|" : "") + coords.get_x() + ":" + coords.get_y();
                } else 
                    return;
            	this.SendMeasureCallback(this.m_currentMeasureToolbarTool, this.m_measureCoords);
	        } else {
                this.removeMeasureGraphic();
                if (args.button==Sys.UI.MouseButton.rightButton)
                    this.m_measureCoords = "";
                else if (args.button==Sys.UI.MouseButton.leftButton)
	                this.m_measureCoords = coords.get_x() + ":" + coords.get_y(); 
	        }
	        this.m_measureLastCoords = this.m_measureCoords;
	    }
	}
}

Geodata.Component.ADF.MeasureTask.removeMeasureGraphic = function() {
    if (this.m_measureGraphicFeature!=null) {
        this.map.removeGraphic(this.m_measureGraphicFeature);
        this.m_measureGraphicFeature.dispose();
        this.m_measureGraphicFeature = null;
    }
}

Geodata.Component.ADF.MeasureTask.StopMeasure = function(mapId) {
    var map = Maps[mapId];
    if(map == null) return;
    this.map = map;
    this.removeMeasureGraphic();
    map.cancelGetGeometry();
    map.remove_mouseUp(this.MeasureCoordsMouseUp); 
    this.m_measureMouseUpSet = false; 
}

Geodata.Component.ADF.MeasureTask.StartMeasure = function(mapId, callbackTarget, type, helpText, helpTextId, toolbarId) {
    var map = Maps[mapId];
    this.map = map;
    this.callbackTarget = callbackTarget;
    type = type.toLowerCase();
    this.m_currentMeasureToolbarTool = type;
    var md;
    if (helpTextId!=null)
        md = $get(helpTextId);
    if (md!=null) md.innerHTML = helpText;
	if (type=="point") {
	    if (map!=null) {
		    map.getGeometry(ESRI.ADF.Graphics.ShapeType.Point,this.MapCoordsClick,null,'black',null,'pointer', true);        
	    }    
	} else if (type=="polyline") {
	    if (map!=null) {
		    map.getGeometry(ESRI.ADF.Graphics.ShapeType.Path,this.MapCoordsClick,null,'black','blue','crosshair', true);
            vectortoolbar = toolbarId;
	    }
	} else {
	    if (map!=null) {
		    map.getGeometry(ESRI.ADF.Graphics.ShapeType.Ring,this.MapCoordsClick,null,'black','black','crosshair', true);        
            vectortoolbar = toolbarId;
	    }
	}
	if (!this.m_measureMouseUpSet) {
	    if(map != null) {
	        map.add_mouseUp(this.MeasureCoordsMouseUp);
	        this.m_measureMouseUpSet = true;
	    }
	}
}
