﻿// JScript File

// variables for interactive clicks
var clickCount = 0;
var clickPointX = new Array();
var clickPointY = new Array();
var clickPtScreenX = new Array();
var clickPtScreenY = new Array();

var clickMeasure = new Array();
	// type - 1=Measure; 2=SelectLine ; 3=SelectPolygon
var clickType = 3;

var cmouseX = 0;
var cmouseY = 0;

var selectCount = 0; //Move this to aimsSelect.js

var leftButton =1;
var rightButton = 2;


// Global variables for tracking mouse movement
  
// Geo locations of mouse position represented as an envelope based on pixel tolerance  
var mouseMinX;
var mouseMinY;
var mouseMaxX;
var mouseMaxY;

// Pixel location of mouse position
var mousX;
var mousY;

// Determine if the mouse is moving or not

var motionDetection;
var mouseInMotion=true;

//onMouseMove =  "trackmouse()"    // Put this event on the object to listen to    



function clearMapClicks()
{
	clickCount=0;
	clickPointX.length=0;
	clickPointY.length=0;
	selectCount=0;
	aimsClickPresent=false;
	selectByShape=false;
	aimsBuffer = false;
	
	 isModQRY=false;
	 isModQuery=false;
	 ModImgQRY = null;
	 
	 
	 
	if (document.getElementById('OutfallFrame') != null){
	 document.getElementById('OutfallFrame').src = document.getElementById('OutfallFrame').src;
	 }
	if (document.getElementById('ResultslFrame') != null){	 
	 document.getElementById('ResultslFrame').src = document.getElementById('ResultslFrame').src;
    }	 
	getMapWithCurrentExtent();
	
	    
	//var clickMeasure.length=0;
   //totalMeasure=0;
}


// put a point at click and add to clickCount
function clickAddPoint() {
	var mXloc1 = cmouseX;
	var mYloc1 = cmouseY;
	
	 //alert("2cmouseX " + x1 + "  \n  " + "2cmouseY " + y1);
    var tPoint = getMapXY(mXloc1, mYloc1);
  	var idMins = convertPixelToMap(mXloc1-(pixelTolerance/2), mYloc1+(pixelTolerance/2));
	var idMaxs = convertPixelToMap(mXloc1+(pixelTolerance/2), mYloc1-(pixelTolerance/2));

	clickMinX = idMins[0];
	clickMinY = idMins[1];
	clickMaxX = idMaxs[0];
	clickMaxY = idMaxs[1];	
	var mmapX =idMins[0];
	var mmapY =  idMaxs[1];		
	
	clickPointX[clickCount]=mmapX;
	clickPointY[clickCount]=mmapY;
	clickCount += 1;
	selectCount=0;
	
	getMapWithCurrentExtent();

	//totalMeasure = totalMeasure + currentMeasure;
		//var u = Math.pow(10,numDecimals);
		//if (totalMeasure!=0) totalMeasure = parseInt(totalMeasure*u+0.5)/u;

	//clickMeasure[clickCount]=totalMeasure;
	//legendTemp=legendVisible;
	//legendVisible=false;
	//var theString = writeXML();
	//var theNum = 99;
	//sendToServer(imsURL,theString,theNum);

}


function mapTool (e) {

	aimsClickPresent=true;
	selectByShape=true;
	var theButton= 0;
	// get the button pushed... if right, ignore... let browser do the popup... it will anyway
	if (isNav) {
		theButton = e.which;
	} else {
		theButton =window.event.button;
	}	
	if (theButton==leftButton) {
			getXY(e);
			//var x1 = theX - mleft;
	        //var y1 = theY - mtop;
	       clickPtScreenX[clickCount] = theX;
	       clickPtScreenY[clickCount] = theY;
	        cmouseX = theX - mleft;
	        cmouseY = theY - mtop;
	        //alert("cmouseX " + cmouseX + "  \n  " + "cmouseY " + cmouseY + "\n" + "  theX   " +  theX + "  theY  " +  theY);
	        clickAddPoint();
        }
 	if (theButton==rightButton) {
		alert("The right mouse button has been clicked");
        }       
  } 

function setUpCoordBox()
{
        var content = '        <table border="1" bordercolor="#006600"  bgcolor="#ffffff" style="width:148px">';
        content += '            <tr>';
        content += '                <td style=\'width: \"50%\";height: \"50%\"\'>';
        content += '                    <p><strong>X</strong></p></td>';
        content += '                <td style=\'width: \"50%\";height: \"50%\"\'>';
        content += '                    <p><strong>Y</strong></p></td>';
        content += '            </tr>';
        content += '            <tr>';
        content += '                <td style=\'width: \"50%\";height: \"50%\"\'>';
        content += '                    <p id="lblXpos"  name="lblXpos">eastings</p></td>';
        content += '                <td style=\'width: \"50%\";height: \"50%\"\'>';
        content += '                    <p id="lblYpos" name="lblYpos">northings</p></td>';
        content += '            </tr>';
        content += '        </table>';
        
 
 	// Make the Div to display XY events
       //var theXYDiv = document.createElement('div');
       var theXYDiv = document.getElementById("coordDiv");
       var ancObj = document.getElementById("mapContainer");
       //theXYDiv.setAttribute('style','z-index:250;position:absolute;width:\"148px\";height:\"40px\";top:' + ancObj.offsetTop + 'px; left:' + ancObj.offsetLeft + 'px;');
       //theXYDiv.setAttribute('id', 'xyDiv');
       //theXYDiv.setAttribute('name', 'xyDiv');
        //theXYDiv.innerHTML = content;  
        updateContent('coordDiv', content);
        //showLayer('coordDiv');


}
function trackMouseMoves()
    {
        /*
        var content = '        <table border="1" bordercolor="#006600"  bgcolor="#ffffff" style="width: 148px">';
        content += '            <tr>';
        content += '                <td style=\'width: \"50%\";height: \"50%\"\'>';
        content += '                    <p><strong>X</strong></p></td>';
        content += '                <td style=\'width: \"50%\";height: \"50%\"\'>';
        content += '                    <p><strong>Y</strong></p></td>';
        content += '            </tr>';
        content += '            <tr>';
        content += '                <td style=\'width: \"50%\";height: \"50%\"\'>';
        content += '                    <p id="lblXpos"  name="lblXpos">eastings</p></td>';
        content += '                <td style=\'width: \"50%\";height: \"50%\"\'>';
        content += '                    <p id="lblYpos" name="lblYpos">northings</p></td>';
        content += '            </tr>';
        content += '        </table>';
        */
 
 	// Make the Div to display XY events
       //var theXYDiv = document.createElement('div');
       var theXYDiv = document.getElementById("coordDiv");
       var ancObj = document.getElementById("mapContainer");
       theXYDiv.setAttribute('style','z-index:1;position:absolute;width:\"148px\";height:\"40px\";top:' + ancObj.offsetTop + 'px; left:' + ancObj.offsetLeft + 'px;');
       //theXYDiv.setAttribute('id', 'xyDiv');
       //theXYDiv.setAttribute('name', 'xyDiv');
       //theXYDiv.innerHTML = content;  

        
        // Append XY position box to mapContainer
        var MapCont  = document.getElementById('mapContainer');
        //MapCont.appendChild( theXYDiv);                          // Uncomment this line to see XY location
        //document.appendChild( theXYDiv);
        // Listen for mouse movement and track coordinates with "alertCoord"
        MapCont.onmousemove = alertCoord;
        //MapCont.onmousemove = trackmouse();
        
        
     }
    


  
function alertCoord(e) {
  if( !e ) {
    if( window.event ) {
      //Internet Explorer
      e = window.event;
    } else {
      //total failure, we have no way of referencing the event
      return;
    }
  }
  if( typeof( e.pageX ) == 'number' ) {
    //most browsers
    var xcoord = e.pageX;
    var ycoord = e.pageY;
  } else if( typeof( e.clientX ) == 'number' ) {
    //Internet Explorer and older browsers
    //other browsers provide this, but follow the pageX/Y branch
    var xcoord = e.clientX;
    var ycoord = e.clientY;
    var badOldBrowser = ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) ||
     ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) ||
     ( navigator.vendor == 'KDE' )
    if( !badOldBrowser ) {
      if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //IE 4, 5 & 6 (in non-standards compliant mode)
        xcoord += document.body.scrollLeft;
        ycoord += document.body.scrollTop;
      } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE 6 (in standards compliant mode)
        xcoord += document.documentElement.scrollLeft;
        ycoord += document.documentElement.scrollTop;
      }
    }
  } else {
    //total failure, we have no way of obtaining the mouse coordinates
    return;
  }
  
  mousX = xcoord - mleft;
  mousY = ycoord - mtop;
  
  var pixTolerance = 1;  // Custom pixel tolerance for tracking mouse movement
  var idMins = convertPixelToMap(mousX-(pixTolerance/2), mousY+(pixTolerance/2));
  var idMaxs = convertPixelToMap(mousX+(pixTolerance/2), mousY-(pixTolerance/2));
  
  mouseMinX = idMins[0];
  mouseMinY = idMins[1];
  mouseMaxX = idMaxs[0];
  mouseMaxY = idMaxs[1];
  
  
  // Uncomment these lines to show XY position
  
  // Round the mouse location for the XY panel
   var xPt=Math.round(mouseMinX*100)/100
   var yPt=Math.round(mouseMinY*100)/100	
  
  if (!document.getElementById('lblXpos')) 
  {
  } else {
    document.getElementById('lblXpos').innerHTML=xPt.toFixed(2);
    document.getElementById('lblYpos').innerHTML=yPt.toFixed(2);
  }
  window.status = "X: " + xPt.toFixed(2) + "  Y: " + yPt.toFixed(2); 
  //showMapTip();
  
}



function showMapTip()
{

      if (ActiveLayer == "7") {

        var MapTipServicePath = "http://www.simwright.org/Germantown%5FWebGISdev/framelessv1/parcelMapTip.aspx?" 
        
        var MapTipsvcQStr = "minX=" + mouseMinX + "&minY=" + mouseMinY + "&maxX=" + mouseMaxX + "&maxY=" + mouseMaxY + ""

       if (document.getElementById('mapTipdiv')!=null) { 
       
	        
	   if( document.all ) {
	        var strIFrme = '<IFRAME ID="MapTipFrame"  SRC="' + MapTipServicePath + MapTipsvcQStr + '" NAME="MapTipFrame"  BORDER=0 FRAMEBORDER=no STYLE="position:absolute; left:0%;top:0%; width:115px; height:45px;"';
            	strIFrme += '"></IFRAME>';
		//var theXYDiv = document.getElementById('mapContainer').document.getElementById('mapTipdiv');
		var theXYDiv = document.getElementById('MapTipFrame');
		//var theXYFrame = document.getElementById('MapTipFrame');
		document.getElementById('mapTipdiv').innerHTML = strIFrme;
		document.getElementById('MapTipFrame').style.top =mousY + "px";
	        document.getElementById('MapTipFrame').style.left = mousX + "px"; 
	        
            } else {
            	var strIFrme = '<IFRAME ID="MapTipFrame"  SRC="' + MapTipServicePath + MapTipsvcQStr + '" NAME="MapTipFrame"  BORDER=0 FRAMEBORDER=no STYLE="position:absolute; left:0%;top:0%; width:85px; height:30px;"';
            	strIFrme += '"></IFRAME>';
            	
                var theXYDiv = document.getElementById('mapTipdiv');
                //var theXYFrame = document.getElementById('MapTipFrame');
                theXYDiv.style.top =mousY + "px";
	        theXYDiv.style.left = mousX + "px";  
	        theXYDiv.innerHTML = strIFrme;
            }    

       		//setTimeout(alignthem(),5000);
       } else {
		    var MapCont  = document.getElementById('mapContainer');
		    var theXYDiv = document.createElement('div');
		    theXYDiv.setAttribute('style','position:absolute;width:85px;height:30px;top:50px; left:50px;background-color: rgb(58, 58, 130);');
		    theXYDiv.setAttribute('id', 'mapTipdiv');
		    theXYDiv.setAttribute('name', 'mapTipdiv');
		    theXYDiv.setAttribute('background-color', '#3A3A82'); 
		    theXYDiv.setAttribute('visibility', 'visible');      
		    theXYDiv.innerHTML = strIFrme; 
		    MapCont.appendChild(theXYDiv);   
       } 
    }
}


function alignthem()
{
document.getElementById('MapTipFrame').style.width = document.getElementById('MapTipFrame').contentDocument.getElementById('tblParcelIDs').offsetWidth + "px";
       
              document.getElementById('MapTipFrame').style.height = document.getElementById('MapTipFrame').contentDocument.getElementById('tblParcelIDs').offsetHeight + "px";
}




///////////////////////////////////////////////////////////////////////
/////  Motion detector functions /////////////////////
///////////////////////////////////////////////////////////////////////
//http://friendsofed.infopop.net/2/OpenTopic?a=tpc&s=989094322&f=3183053165&m=7561049721


function trackmouse() {
	//reset the motionDetection interval
	clearInterval(motionDetection);
	
	//trace("Mouse motion detected");
	
	//if the mouse moves mouseInMotion boolean is set to true;
	mouseInMotion = true;

	//set an interval to execute noMotionDetected function after 500 milliseconds(1/2 second)
	//if the mouse moves before 500 milliseonds this timer is reset
	motionDetection = setInterval(noMotionDetected, 500);


}

function noMotionDetected() {
	//reset the motionDetection interval
	clearInterval(motionDetection);
	
	//the mouse hasnt moved for 500 milliseconds so set mouseInMotion boolean to false;
	mouseInMotion = false;
	

	
	//set an interval to execute motionDetector every 50 milliseconds
	motionDetection = setInterval(motionDetector, 50);
}

function motionDetector() {
	
	//if mouseInMotion is false execute
	if (!mouseInMotion){
		//Execute your no motion code here		
		//trace("No mouse motion detected");
		
      // Show MapTip if mouse stops moving
      //showMapTip();
	 
	} else {
	   mouseInMotion = true;
	
	}
	
	document.getElementById('lblYpos').innerHTML=mouseInMotion;
} 

///////////////////////////////////////////////////////////////////////
/////  End of Motion detector functions /////////////////////
///////////////////////////////////////////////////////////////////////    
     



