
//  -----------------------------------------------------------------
//    Notes:	The purpose of this Javascript File is to control the record and send user clicks to framework
//              all global variables must be declared at bottom of originating map page....
//    Global Variables Delcared:         
//           NONE
//
//    Arguments:   
//    Returns:	N/A
//  		E:\WebSite_Default\ArcIMSFrameWorkSource\JavaScript\ArcIMSFrameWork_UserClicks.js
//   -----------------------------------------------------------------
//    Orginization:	City of Asheville IT Services
//    Original Code: 	Dave Michelson 1/31/2006
//    Last Update:	Dave Michelson 1/31/2006
//


//*******************************************************
//Map Interaction Functions  
//*******************************************************

// perform appropriate action with mapTool

  function mDown(e) {
    var theButton= 0;
    mButtonDown = true;

    // 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 == 1){ 
      if (continueSelect){

        //a Rectangle
        if(aShapeType == 3){
          clickCount += 1;
          if (isNav) {
            mX[clickCount] = e.pageX-vSpc;
            mY[clickCount] = e.pageY-hSpc;
          }else{
            mX[clickCount] = event.clientX-vSpc;
            mY[clickCount] = event.clientY-hSpc;
          }
        }
      }
    }
  }
    
  function mUp(e) {

    var xEvent;
    var yEvent;
    
    //Set up event stuff for browsers
    if (isNav) {
      xEvent = e.pageX;
      yEvent = e.pageY;
    }else{
      xEvent = event.clientX;
      yEvent = event.clientY;
    }

    var theButton= 0;
    mButtonDown = false;
    
    // 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 == 1){

      if (aShapeType == 5){
        clickCount += 1;
        mButtonDown = false;

        var myMapImage = getImageXY(e);

        mX[clickCount] = myMapImage.X;
        mY[clickCount] = myMapImage.Y;
        
        MakeShape(clickCount,aShapeType)
        continueSelect = false;
      }

      if (continueSelect){

        //a Rectangle
        if(aShapeType == 3){

          clickCount += 1;
          mButtonDown = false;

          mX[clickCount] = xEvent - vSpc;
          mY[clickCount] = yEvent - hSpc;

          if(clickCount == 1){
            MakeShape(clickCount,aShapeType)
            continueSelect = false;
          }
	}
      }
    }    
  }
  

  function mSetShapeType(aType){
    switch(aType){
      case 1: //line
        aShapeType = aType
        clickMin = 0;
        break;
      case 2: //poly
        aShapeType = aType
        clickMin = 1;
        break;
      case 3: //rectangle
        aShapeType = aType
        clickMin = 1;
        break;
      case 4: //Point
        aShapeType = aType
        clickMin = 0;
        break;
      case 5: //Point - pan
        aShapeType = aType
        clickMin = 1;
        break;
    }
  }

  function mClear(){

    clickCount = -1;
    continueSelect = true;
    mDistance = 0;

    var mX = Array();
    var mY = Array();

    var allX = Array();
    var allY = Array();

    var myMapX = new Array();
    var myMapY = new Array();

    StartX = 0;
    StartY = 0;
    EndX = 0;
    EndY = 0;
    WidthX = 0;
    HeightY = 0;

    jg.clear(); 
    jg2.clear();
    jg.paint();
    jg2.paint();

    resetElement('Measure_Distance','SPAN');
  }
    
  function mClick(e){

    if (continueSelect){
      clickCount += 1;
      if (isNav) {
        mX[clickCount] = e.pageX-vSpc;
        mY[clickCount] = e.pageY-hSpc;
      }else{
        mX[clickCount] = event.clientX-vSpc;
        mY[clickCount] = event.clientY-hSpc;
      }

      //all shapes Other Than a Rectangle and point/pan - point
      if(aShapeType != 3 && aShapeType != 4 && aShapeType != 5){
        if (clickCount > 0){
          MakeShape(clickCount,1);
        }
      }
      //all shapes Other Than a Rectangle and point/pan - point
      //a Point
      if(aShapeType == 4 ){
        if(clickCount == 0){
          MakeShape(clickCount,aShapeType);
          continueSelect = false;
        }
      }
      //a Point
    }else{
      var theTool = getCurrentTool(window.mProcessor.document.frmXMLRequest);
      if(theTool=="Measure"){
        mClear();
        mClick(e);
      }      
    }
  }

  function mDClick(e){
    if (continueSelect){
      var theTool = getCurrentTool(window.mProcessor.document.frmXMLRequest);
      clickCount += 1;
      if (isNav) {
        mX[clickCount] = e.pageX-vSpc;
        mY[clickCount] = e.pageY-hSpc;
      }else{
        mX[clickCount] = event.clientX-vSpc;
        mY[clickCount] = event.clientY-hSpc;
      }    
      if (clickCount > clickMin){
        MakeShape(clickCount,aShapeType);
        if(theTool!="Measure"){
          mConvertToMap(clickCount);
        }
      }
      continueSelect = false;
    }
  }

  function MakeShape(cCount,aType){
    var i;
    if (continueSelect){
      var theTool = getCurrentTool(window.mProcessor.document.frmXMLRequest);
      switch (aType){
        case 1: //line          
          if(theTool=="Measure"){
            getLength(mX[cCount-1],mX[cCount],mY[cCount-1],mY[cCount],'FEET','FEET','Measure_Distance')  
          }
          jg.setColor("#ff0000"); // red
          jg.setStroke(2);
          jg.setOpacity(.5);
          jg.drawLine(mX[cCount-1],mY[cCount-1],mX[cCount],mY[cCount])
          jg.paint(); 
          break;
        case 2: //poly
          var allX = Array(cCount);
          var allY = Array(cCount);
          for (i = 0; i <= cCount-1; i++){
            allX[i] =  mX[i];
            allY[i] =  mY[i];
          }
            jg.setColor("#ff0000"); // red
            jg.setOpacity(.5);
            jg.drawPolygon(allX,allY)
            jg.fillPolygon(allX,allY)
            jg.setStroke(2);
            jg.paint();

            jg2.setColor("#0000ff"); // blue
            jg2.setOpacity(.5);
            jg2.setStroke(2);
            jg2.drawPolygon(allX,allY)
            jg2.paint();

          if(theTool=="Measure"){
            getArea(cCount,allX,allY,'Measure_Area')
            getPerimiter(cCount,allX,allY,'Measure_Perimiter')

            //draw last line
            jg.setColor("#000000"); // black
            jg.setStroke(2);
            jg.setOpacity(1.0);
            jg.drawLine(mX[cCount-(cCount-0)],mY[cCount-(cCount-0)],mX[cCount-1],mY[cCount-1])
            jg.paint(); 

            resetElement('Measure_Segment','SPAN');
            getLength(mX[cCount-(cCount-0)],mX[cCount-1],mY[cCount-(cCount-0)],mY[cCount-1],'FEET','FEET','Measure_Segment')
                     
           }

          break;

        case 3: //Rectangle		
          //use global rectangle varrabiles instead of click
          if(StartX == 0){
            //box too small for zooming use click
            StartX = mX[cCount];
            StartY = mY[cCount];
	    mConvertToMap(cCount+1);
          }else{
            //box size fine use zoom box
            mX[cCount-1] = StartX;
            mY[cCount-1] = StartY;
            mX[cCount] = StartX + WidthX;
            mY[cCount] = StartY + HeightY;
            EndX = StartX + WidthX;
            EndY = StartY + HeightY;
            jg.setColor("#ff0000"); // red
            jg.setOpacity(.5);
            jg.drawRect(StartX ,StartY ,WidthX ,HeightY );
            jg.fillRect(StartX ,StartY ,WidthX ,HeightY );
            jg.setStroke(2);
            jg.paint();
            mConvertToMap(cCount+1);
          }
          jg.paint();
          break;

        case 4: //Point does not actually draw a point just records use click
          if (isNav){
            iX = mX[cCount];
            iY = mY[cCount];
          }else{
            iX = mX[cCount];
            iY = mY[cCount];
          }
          jg.setColor("#ff0000"); // red
          jg.setOpacity(.5);
          jg.setStroke(2);
          jg.drawLine(mX[cCount],mY[cCount],mX[cCount]+.0001,mY[cCount]+.0001);
          jg.paint();
          mConvertToMap(cCount+1);
          break;

        case 5: //Point does not actually draw a point just records use click
          if (isNav){
            iX = mX[cCount];
            iY = mY[cCount];
          }else{
            iX = mX[cCount];
            iY = mY[cCount];
          }
          mConvertToMap(cCount+1);
          break;
      }
    }
  }

  function mMove(e){ 

    var xEvent;
    var yEvent;

    //Set up event stuff for browsers
    if (isNav) {
      xEvent = e.pageX;
      yEvent = e.pageY;
    }else{
      xEvent = event.clientX;
      yEvent = event.clientY;
    }
    //set up color for shape with mouse movement
    if (continueSelect){
      if (mX[clickCount]){
        jg2.clear();
        jg2.setColor("#0000ff"); // blue
        jg2.setOpacity(.5);
        jg2.setStroke(2);
      }else{
        jg2.clear();
        jg2.setColor('#FFFFFF'); // blue
        jg2.setOpacity(.5);
        jg2.setStroke(0);
      }

      if (aShapeType != 3){
        if (aShapeType == 5){
          if(mButtonDown){

            var myMapImage = getImageXY(e);

            //unfourt. must use global start to get start correct start point for each new pan
            if (StartX  == 0){
              clickCount += 1;
              StartX = myMapImage.X;
              StartY = myMapImage.Y;
              mX[clickCount] = StartX;
              mY[clickCount] = StartY;
            }

            //set up move distance
            var xMove = myMapImage.X - StartX;
            var yMove = myMapImage.Y - StartY;

            //move the map image with cursor drag
            moveIt('map_img', xMove , yMove );

            }
          }else{
            //line or polgon draw with mouse move
            jg2.drawLine(mX[clickCount],mY[clickCount],(xEvent-vSpc),(yEvent-hSpc));
            var theTool = getCurrentTool(window.mProcessor.document.frmXMLRequest);
            if(theTool=="Measure"){
              resetElement('Measure_Segment','SPAN');
              getLength(mX[clickCount],(xEvent-vSpc),mY[clickCount],(yEvent-hSpc),'FEET','FEET','Measure_Segment')
            }
          }
        }else{
          //rectangle drag....
          if(mButtonDown){
            //LOWER RIGHT QUADRANT
            if ((xEvent-vSpc)-mX[clickCount] > 0 && (yEvent-hSpc)-mY[clickCount] > 0){
              StartX = mX[clickCount];
              StartY = mY[clickCount];
              WidthX =(xEvent-vSpc)-mX[clickCount];
              HeightY =(yEvent-hSpc)-mY[clickCount];
            }
            //UPPER RIGHT QUADRANT				
            if ((xEvent-vSpc)-mX[clickCount] > 0 && (yEvent-hSpc)-mY[clickCount] < 0){
              StartX = mX[clickCount];
              StartY = (yEvent-hSpc);
              WidthX = (xEvent-vSpc)-mX[clickCount];
              HeightY =mY[clickCount]-(yEvent-hSpc);
            }
            //UPPER LEFT QUADRANT
            if ((xEvent-vSpc)-mX[clickCount] < 0 && (yEvent-hSpc)-mY[clickCount] < 0){
              StartX = (xEvent-vSpc);
              StartY = (yEvent-hSpc);
              WidthX = mX[clickCount]-(xEvent-vSpc);
              HeightY = mY[clickCount]-(yEvent-hSpc);			
            }
            //LOWER LEFT QUADRANT
            if ((xEvent-vSpc)-mX[clickCount] < 0 && (yEvent-hSpc)-mY[clickCount] > 0){
              StartX = (xEvent-vSpc);
              StartY = mY[clickCount];
              WidthX = mX[clickCount]-(xEvent-vSpc);
              HeightY = (yEvent-hSpc)-mY[clickCount];
            }
          //draw rectangle with drag
          jg2.drawRect(StartX ,StartY ,WidthX ,HeightY );

          }
        }
    jg2.paint();
    }
  }

  function mConvertToMap(cCount){
    var i;

    var myMapX = new Array();
    var myMapY = new Array();
    
    //convert all users clicks from image to map coordinates
    for (i = 0; i <= clickCount; i++){
      var MapDim = getMapXY(mX[i],mY[i]);
      myMapX[i] = MapDim.X;
      myMapY[i] = MapDim.Y;
    }

   //add array to processor
   window.mProcessor.document.frmXMLRequest.XML_USERCLICKX.value=myMapX.join(',');
   window.mProcessor.document.frmXMLRequest.XML_USERCLICKY.value=myMapY.join(',');
   window.mProcessor.document.frmXMLRequest.XML_TOOLACTION.value='GO';
   window.mProcessor.document.frmXMLRequest.submit();
   showSplash('Splash');
   MenuHide('mToolBar');
   resetTOC();
  }

  function ClickReset(){
    window.mProcessor.document.frmXMLRequest.XML_USERCLICKX.value='';
    window.mProcessor.document.frmXMLRequest.XML_USERCLICKY.value='';
  }