
var clickCount = 0;


function zoomButton(zoomType) {
	
	//get zoom factor
	var xHalf = Math.abs(left-right)/2;
	var yHalf = Math.abs(bottom-top)/2;
	saveLastExtent();
	if (zoomType == 1) {
		// zoom in
		left = left + (xHalf/2);
		right = right - (xHalf/2);
		bottom = bottom + (yHalf/2);
		top = top - (yHalf/2);
		
	}
	else {
		// zoom out
		left = left - (xHalf/2);
		right = right + (xHalf/2);
		bottom = bottom - (yHalf/2);
		top = top + (yHalf/2);
		
	}
		controlla_fattore();
}


function zoomin_box(prX,prY,secX,secY) {

        getMapXY(prX,prY); // return mapX & mapY in gaussBoaga coords
		var tempX = mapX;
		var tempY = mapY;
		getMapXY(secX,secY);
		saveLastExtent();
		left = Math.min(tempX,mapX);
		right = Math.max(tempX,mapX);
		bottom = Math.min(tempY,mapY);
		top = Math.max(tempY,mapY);
	    //sendMapXML();
		controlla_fattore();

}

function zoomout_box(prX,prY,secX,secY) {
    getMapXY(prX,prY);
	var tempX = mapX;
	var tempY = mapY;
			
	getMapXY(secX,secY);
    if((tempX==mapX)||(tempY==mapY)){
	  	saveLastExtent();
	  	left = startLeft;
	  	right = startRight;
	  	top = startTop;
	  	bottom = startBottom;
	}else{
    	xMedio = (tempX+mapX)/2;
		yMedio = (tempY+mapY)/2;
	
		var zWidth = Math.abs(tempX-mapX);
		var zHeight = Math.abs(tempY-mapY);
		var xRatio = larghezza / zWidth;
		var yRatio = altezza / zHeight;
	
		var xAdd = xRatio * Math.abs(left-right) / 2;
		var yAdd = yRatio * Math.abs(bottom-top) / 2;
		saveLastExtent();
		var leftT = left - xAdd;
		var rightT = right + xAdd;
		var topT = top + yAdd;
		var bottomT = bottom - yAdd;
	
		left =  xMedio - (Math.abs(leftT - rightT)/2);
		right = xMedio + (Math.abs(leftT - rightT)/2);
		top  =  yMedio + (Math.abs(topT - bottomT)/2);
		bottom  = yMedio - (Math.abs(topT - bottomT)/2);
	}
    //sendMapXML();
	controlla_fattore();
}	

function fullExtent() {
    //set initial extent
	saveLastExtent();
	left = startLeft;
	right = startRight;
	bottom = startBottom;
	top = startTop;
	LastTop=top;
	LastLeft=left;
	LastRight=right;
	LastBottom=bottom;
	controlla_fattore();
}
/*
// save the last extent 
function saveLastExtent() {
	lastLeft = left;
	lastRight = right;
	lastTop = top;
	lastBottom = bottom;
}

function zoomLast(){
    lastLeft1 = left;
	lastRight1 = right;
	lastTop1 = top;
	lastBottom1 = bottom;

    left=lastLeft;
	right=lastRight;
	top=lastTop;
	bottom=lastBottom;
	
	lastLeft = lastLeft1;
	lastRight = lastRight1;
	lastTop = lastTop1;
	lastBottom = lastBottom1;
	
    controlla_fattore();
	
}
*/

// save the last extent 
function saveLastExtent() {
 
  if(counterLastExtent < numberLastExtent){
    lastLeft[counterLastExtent] = left;
	lastRight[counterLastExtent] = right;
	lastTop[counterLastExtent] = top;
	lastBottom[counterLastExtent] = bottom;
	counterLastExtent += 1;
  }else{
    //counterLastExtent  = numberLastExtent-1;
    for(var ii=0;ii<numberLastExtent-1;ii++){
	   lastLeft[ii] = lastLeft[ii+1];
	   lastRight[ii] = lastRight[ii+1];
	   lastTop[ii] = lastTop[ii+1];
	   lastBottom[ii] = lastBottom[ii+1];
	}//end-for
	lastLeft[numberLastExtent-1] = left;
	lastRight[numberLastExtent-1] = right;
	lastTop[numberLastExtent-1] = top;
	lastBottom[numberLastExtent-1] = bottom;
	
  }//end-if

}

function zoomLast(){
    if (counterLastExtent > 0){
	
	    counterLastExtent -=1 ;
        left = lastLeft[counterLastExtent];
	    right = lastRight[counterLastExtent];
	    top = lastTop[counterLastExtent];
	    bottom = lastBottom[counterLastExtent];
		
	}else{
	    //alert('fine pila');
	}
	  //Contatore = 0;
	  
    controlla_fattore();
	//Contatore += 1;
}

function panButton(panType) {

    //Calc pan factor
	saveLastExtent();
    var panFactor = .5;
	xDistance = Math.abs(left-right);
	yDistance = Math.abs(bottom-top);
	panX = xDistance * panFactor;
	panY = yDistance * panFactor;
	switch(panType) {

	  case 1:
		//west
		left = left - panX;
		right = left + xDistance;
		break
	  case 2:
		// north
		top = top + panY;
		bottom = top - yDistance;
		break
	  case 3:
		// east
		right = right + panX;
		left = right - xDistance;
		break
	  case 4:
		// south
		bottom = bottom - panY;
		top = bottom + yDistance;
		break
	}
	
	//create the map based on the new extents.
	//sendMapXML();
	controlla_fattore();
	
}

function ov_pan(mgX,mgY) {

	var xHalf = Math.abs(ovLeft-ovRight)/2; 
	var yHalf = Math.abs(ovBottom-ovTop)/2; 
    saveLastExtent();
	left = mgX - (xHalf); 
	right = mgX + (xHalf); 
	bottom = mgY - (yHalf); 
	top = mgY + (yHalf);
	//sendMapXML();
	controlla_fattore();

}

function pan_move(a,b,c,d) {
//function pan_move(pX,pY,sX,sY) {
        getMapXY(a,b);
		pX = mapX;
		pY = mapY;
		getMapXY(c,d);
		sX = mapX;
		sY = mapY;
        var supX = right - sX;
        var supY = top - sY;
        var sotX = sX - left;
        var sotY = sY - bottom;
        saveLastExtent();
		left = pX - sotX; 
		right = pX + supX; 
		bottom = pY - sotY; 
		top = pY + supY;
		//sendMapXML();
		controlla_fattore();
}


//************************** Funzioni di selezione  ****************************************


function rectCircleAndPan(a,b,c,d){
     if (useRett)    arrayrett(a,b,c,d);
	 if (useZoomIn)  zoomin_box(a,b,c,d);
	 if (useZoomOut) zoomout_box(a,b,c,d);
	 if (usePan)     pan_move(a,b,c,d);
	  
}


//funzione utilizzata nel caso di una funzionalitą con un click solo
function point(valX,valY) {
	var pixelTolerance=1;
	if (parent.toolFrame.document.form_scala.text_scala.value < 100000) pixelTolerance=1;
	if (parent.toolFrame.document.form_scala.text_scala.value < 50000) pixelTolerance=1;
	if (parent.toolFrame.document.form_scala.text_scala.value < 20000) pixelTolerance=1;
	if (parent.toolFrame.document.form_scala.text_scala.value < 10000) pixelTolerance=1;
	//if(usePoint) pointSelection(valX-pixelTolerance,valY+pixelTolerance,valX+pixelTolerance,valY-pixelTolerance);
	if(usePoint)
		{
		if(objIdStrada!=""){
	      		pointSelectionKm(valX-pixelTolerance,valY+pixelTolerance,valX+pixelTolerance,valY-pixelTolerance);
				usePoint=false;
		  }
		  else if (objIdStradaUrb!=""){
			  pixelTolerance=3;
			  pointSelectionUrb(valX-pixelTolerance,valY+pixelTolerance,valX+pixelTolerance,valY-pixelTolerance);
			  pixelTolerance=1;
			  usePoint=false;
		  }
		  else{
				pointSelection(valX-pixelTolerance,valY+pixelTolerance,valX+pixelTolerance,valY-pixelTolerance);
		}
	}

	if(useMultipla) multipla(valX-pixelTolerance,valY+pixelTolerance,valX+pixelTolerance,valY-pixelTolerance);
}









