var mapID;
var calloutID;
var calloutMapX;
var calloutMapY;
var toolbarID;
var identifyToolID;
var customIdentifyGroupID;
var selectedTool;

function showOwnerInfo(own){
var purl = 'http://www.greenecountymo.org/assessor/search_name.php'
var pTF = confirm("You are being redirected to the Greene County Assessor's web page. Do you wish to continue?");
    if (pTF==true)
     {
      window.open(purl);
     }
    else
     {
     }

}

function showResultInfo(pin){
var purl = 'http://www.greenecountymo.org/assessor/search_parcel.php'
var pTF = confirm("You are being redirected to the Greene County Assessor's web page. Do you wish to continue?");
    if (pTF==true)
     {
      window.open(purl);
     }
    else
     {
     }

}


function showPropInfo(pid){
  spid = pid.toString();
  if (spid.length == 12){
    spid = spid.substring(2,12)
  }
  if (spid.length == 10){
    //var url = 'http://www.greenecountyassessor.org/Datalets.asp?mnu=PSearch&submnu=Profile&pin=' + spid + '&tp=1&cp=1';
    var url = 'http://www.greenecountymo.org/assessor/search_parcel.php';

    var TF = confirm("You are being redirected to the Greene County Assessor's web page. Do you wish to continue?");
    if (TF==true)
     {
      window.open(url);
     }
    else
     {
     }
   
  }
  else{
    alert("PIN has the wrong length.  Can't open property information page");
  }
}

function calloutStartUp()
{
    //Set global variables
    mapID = 'Map1';
    calloutID = 'MySimpleCallout';
    toolbarID = 'Toolbar1';
    identifyToolID = 'CustomIdentify';
    customIdentifyGroupID = 'CustomIdentifyGraphicsGroup';
    selectedTool = null;
        
    var map = $find(mapID);
    map.add_extentChanged(mapExtentChange);
    map.add_extentChanging(mapExtentChange);
    
     // Add an event that fires whenever the map sends a request to the server.
    map.add_onServerRequest(mapServerRequest);
    
    // Get a reference to the client-side Web ADF toolbar object
    var toolbar = $find(toolbarID);
    // Add an event handler that fires whenever a tool on the toolbar is selected.
    toolbar.add_onToolSelected(toolbarToolSelected);
}

function displayCallout (featureXCoOrd, featureYCoOrd, callOutHtml) {
        var map = $find(mapID);
        calloutMapX = featureXCoOrd;
        calloutMapY = featureYCoOrd;
        if(!map) {return; }
        
        var myCallout = $find(calloutID);
        if (myCallout == null){
          var myCallout = $create(ESRI.ADF.UI.Callout, { "id": calloutID, "parent":document.body,"animate":true, "autoHide":false }, null);
          var template = String.format('<div style="background-color:#fff;border: solid 1px #ddd;padding: 0px; margin:0;"><div style="background:#eee; margin:0; height: 22px; width:260px"><table style="height:12px;width:250px"><tr align="right"><td><span style="cursor: pointer;" onclick="$find(\'{0}\').hide();">X</span></td></tr></table></div><div style="padding:0;">{{@content}}</div></div>', calloutID);
          myCallout.set_template(template);
          myCallout.set_calloutBuffer(0);
        }
        myCallout.setContent({"content":callOutHtml});
        positionCallout(myCallout);
        myCallout.show();
    }

function mapExtentChange(sender,eventArgs){
    positionCallout($find(calloutID));
}

function positionCallout (callout)
{
    if(callout == null){ return;}
    
    var mapBounds = Sys.UI.DomElement.getBounds($get(mapID));
    var screenPoint = map.toScreenPoint(new ESRI.ADF.Geometries.Point(calloutMapX, calloutMapY));
    callout.setPosition(screenPoint.offsetX + mapBounds.x, screenPoint.offsetY + mapBounds.y);
        
    var mapBounds = Sys.UI.DomElement.getBounds($get(mapID));
    var mapCenterX = mapBounds.x + (mapBounds.width / 2);
    var mapCenterY = mapBounds.y + (mapBounds.height / 2);
    var calloutPoint = callout.getPosition();
    if(calloutPoint[0] > mapCenterX && calloutPoint[1] > mapCenterY){
        callout.set_anchorPoint(ESRI.ADF.UI.AnchorPoint.BottomRight);
    }
    else if(calloutPoint[0] > mapCenterX && calloutPoint[1] < mapCenterY){
        callout.set_anchorPoint(ESRI.ADF.UI.AnchorPoint.TopRight);
    }
    else if(calloutPoint[0] < mapCenterX && calloutPoint[1] > mapCenterY){
        callout.set_anchorPoint(ESRI.ADF.UI.AnchorPoint.BottomLeft);
    }
    else if(calloutPoint[0] < mapCenterX && calloutPoint[1] < mapCenterY){
        callout.set_anchorPoint(ESRI.ADF.UI.AnchorPoint.TopLeft);
    }
    callout.checkPosition();
}

// Fires when a tool on Toolbar1 is selected
function toolbarToolSelected(toolbarObject, activeToolInfo)
{
    // Store a reference to the seleceted tool
    selectedTool = activeToolInfo.tool;        
}

// Fires when the map sends a request to the server
function mapServerRequest(mapObject, inputArgument)
{
    // Check whether the custom tool is the currently selected tool
    if (selectedTool != null && selectedTool.name == identifyToolID)
    {

        var pnt = mapObject.graphicsEditor._geometry;
        if (pnt == null) { return; }
        
        var screenPnt = mapObject.toScreenPoint(pnt);
        var graphicPoint = mapObject.toMapPoint(screenPnt.offsetX + 8, screenPnt.offsetY + 8)
        //var graphicPoint = new ESRI.ADF.Geometries.Point(pnt.get_x() + 8, pnt.get_y() + 8, pnt.get_spatialReference());
        
        var defaultSymbol = null;
        defaultSymbol = new ESRI.ADF.Graphics.MarkerSymbol('images/callbackActivityIndicator2.gif',16,16,'cross');
	    defaultSymbol.set_height(16);
	    defaultSymbol.set_width(16);
	    		    
        // Try retrieving the graphicFeatureGroup (i.e. layer) with the ID corresponding to the current 
        // geometry type.  If the group is not found, create it.
        var graphicFeatureGroup = $find(customIdentifyGroupID);
	    if(!graphicFeatureGroup) 
	    {   
            // Create the graphic feature group with the ID, symbols, and mapTips.  Also, wire the
            // onMouseOverGraphics and onMouseOutGraphics to the group's mouseOver and mouseOut events    
	        graphicFeatureGroup = $create(ESRI.ADF.Graphics.GraphicFeatureGroup,{"id":customIdentifyGroupID,
	            "symbol":defaultSymbol});
    	                  
            // Add the new graphic feature group to the map
            map.addGraphic(graphicFeatureGroup);
	    }
        //Clear any existing activity indicators
        graphicFeatureGroup.clear();
                
        // Create the graphic feature with a unique ID made up of the customIdentifyGroupID and the graphicCount; 
        // the passed-in geometry; and the attributes object that specifies the valueInTextbox attribute
	    var graphicFeature = $create(ESRI.ADF.Graphics.GraphicFeature, {"id":"customID", "geometry":graphicPoint});
	        
	   // Add the graphic to the group
	   
        graphicFeatureGroup.add(graphicFeature);
    }
}
