/*********************************************************************************
 Copyright (c) 2002-2003 Armin Burger
 
 Permission is hereby granted, free of charge, to any person obtaining 
 a copy of this software and associated documentation files (the "Software"), 
 to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 and/or sell copies of the Software, and to permit persons to whom the Software 
 is furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included 
 in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR OR 
 COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
  USES THE JAVASCRIPT LIBRARIES JSGRAPHICS FROM WALTER ZORN
  SEE FILE /JAVASCRIPT/WZ_JSGRAPHICS.JS FOR DETAILS OF COPYRIGHT
  
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
**********************************************************************************/

// -------------------------
//  FUNCTIONS FOR MEASURING
// -------------------------

// ADAPT LINE COLOR
var lineColor = '#E51B23';

// ADAPT LINE WIDTH
var lineWidth = 2;

// ADAPT FACTOR FOR DISTANCE/AREA CALCULATION
// eg. 1000 for km, 1 for m
var calcFact = 1; //  1000

var numSize = 0;

// Mouse click X/Y
var clickXprev;      // Previous clicks
var clickYprev;
var clickCount = 0;  // Counter for clicks; reset when clearing lines


// For area measure
var xc = new Array;
var yc = new Array;
var nc = 0;
var area = 0;

//
// MAIN FUNCTION, DRAWS SYMBOL POINTS BETWEEN MOUSECLICKS
//
function measureDrawSymbols(e, clickX, clickY, dblClick) {
             
    // Reset everything when last measure ended with double click
    if (clickCount == 0)
    {
        jg.clear();
        document.measureForm.sumLen.value = '';
        document.measureForm.segLen.value = '';
    }
    // Don't go outside map
    outerloop:
    if ((clickX < mapW) && (clickY < mapH)) {

        var x_geo = minx_geo + ((clickX/mapW)  * xdelta_geo);
        var y_geo = maxy_geo - ((clickY/mapH) * ydelta_geo);
        
        // First point for start click
        if (clickCount < 1) {
            drawLineSegment(clickX, clickY, clickX, clickY, ""); 
            clickCount++;
    
        // Fill distance between clicks with symbol points
        } else {
            // Diff X/Y between current new and previous click
            var x_delta = clickXprev - clickX;
            var y_delta = clickYprev - clickY;

            // SINGLE CLICK
            if (dblClick != 1) { 

                // Segment length in Pixel
                var segLenPix = Math.sqrt((Math.pow(x_delta, 2)) + (Math.pow(y_delta, 2)));
                
                // Segment length in  map coordinates,  write values to input boxes
                var segLenGEO_0 = ((segLenPix/mapW) * xdelta_geo) / calcFact ;
                var cntSegLen = Math.round(segLenGEO_0).toString().length;
                numSize = Math.max(0, (4 - cntSegLen));
                var segLenGEO = roundN(segLenGEO_0, numSize); 
                //var segLenGEO = segLenGEO_0;

                if (document.measureForm.sumLen.value != '') {
                    sumLenGEO = parseInt(document.measureForm.sumLen.value) + segLenGEO;
                } else {
                    sumLenGEO = segLenGEO;
                }

                document.measureForm.sumLen.value = sumLenGEO;
                document.measureForm.segLen.value = segLenGEO;

                // BPU : chgt unité
                if (sumLenGEO > 1000) {
                    calcFact2 = 1000;
                    calcUnit2 = " km.";
                }
                else {
                    calcFact2 = 1;
                    calcUnit2 = " m.";
                }
                
                // USE wz_jsgraphics.js TO DRAW LINE
                drawLineSegment(clickXprev, clickYprev, clickX, clickY, roundN(sumLenGEO/calcFact2,2)+calcUnit2);

            
            // DOUBLE CLICK => CALCULATE AREA
            } else if (dblClick) {

                counter = 0;

                // calculate polygon area
                xc[xc.length] = xc[0] - xc[(xc.length)-1]; 
                yc[yc.length] = yc[0] - yc[(yc.length)-1]; 

                area = 0;
                for(var k=0; k<nc; k++) {
                   var j = (k+1);
                   area = area + ( xc[k] * yc[j] );
                   area = area - ( xc[j] * yc[k] );
                }
                area = area / 2;
                area = Math.abs(roundN (area / (calcFact * calcFact), 1)) ;
                
                // BPU : chgt unité
                if (area > 1000000) {
                    calcFact2 = 1000000;
                    calcUnit2 = " km˛.";
                }
                else {
                    calcFact2 = 1;
                    calcUnit2 = " m˛.";
                }
                
                //alert('Surface approximative : ' + roundN(area/calcFact2,2) + calcUnit2);

                // Change input text box to 'Area'
                //parent.bottomFrame.document.getElementById("mSegTxt").innerHTML = parent.bottomFrame.document.getElementById("mAreaTxt").getAttribute('value');
                //parent.bottomFrame.document.measureForm.segLen.value = area;
                

                // Reset variables and delete measure points
                nc = 0;
                area = 0;
                xc = new Array;
                yc = new Array;
                clickCount = 0;
                
                // remove measure line
                //jg.clear();
                
                break outerloop;
            }
        }
        
       
        
        // Set previous click to current, increase counter         
        clickXprev = clickX;
        clickYprev = clickY;
        clickCount = 2;
        
        // Add x/y clicks to array for area measurement
        xc[nc] = x_geo;
          yc[nc] = y_geo;
        nc++; 
    }    
}


//
// DRAW LINE USING JSGRAPHICS
// modif BPU : ajout d'une bulle contenant un texte
function drawLineSegment(xfrom, yfrom, xto, yto, strTexte) {
    jg.setColor(lineColor);
    jg.setStroke(lineWidth); 
    jg.drawLine(xfrom, yfrom, xto, yto); 
    jg.fillEllipse(xto, yto, 7, 7)
    
    if (strTexte!="") {
        xM = (xfrom+xto)/2;
        yM = (yfrom+yto)/2; 
        jg.setColor(lineColor);
        jg.setStroke(2);
        jg.fillRect(xM-25, yM-6, 50, 12); 
        //jg.fillEllipse(xM-10,yM-1,55,13);
        //jg.setColor("#F7941D");
        //jg.drawEllipse(xM-10,yM-2,55,13);
        jg.setColor("#FFFFFF");
        jg.setFont("arial","9px",Font.BOLD);
        //jg.drawString(strTexte,xM,yM);
        jg.drawStringRect(strTexte, xM-25, yM-6, 50, "center"); 
    }
    jg.paint();
}


//
// Remove all measure settings
//
function resetMeasure() {

    // remove lines
    jg.clear();
    
    // Reset form fields 
    document.measureForm.sumLen.value = '';
    document.measureForm.segLen.value = '';
    document.getElementById("mSegTxt").innerHTML = document.getElementById("mSegTxt").getAttribute('value');
    
    // Reset Click counter
    clickCount = 0;
}


//
// Round to a specified decimal
//
function roundN(numin, rf) {
    return ( Math.round(numin * Math.pow(10, rf)) / Math.pow(10, rf) );
} 