
/*
	2008.01.08, AA - Creation
	zooming.js, haritanin pan, zoom... gibi islevlerini yerine getiren js.    
*/


// Project Extensions
var projMinX = 0;
var projMinY = 0;
var projMaxX = 0;
var projMaxY = 0;

// current Extensions
var curMinX = 0;
var curMinY = 0;
var curMaxX = 0;
var curMaxY = 0;

var ZoomRatio = 6;
var PanRatio  = 3;

var doZoomIn = 0;
var doZoomOut = 0;

//var minExt = 10;  // yapilabilecek minimum Zoomu gosterir. 

function InitializeZoomFactors(){
	/* initialize fonksiyonu, GetCapabilities istegi yapildiktan sonra cagirilmali. 
	Burada, extension degerlerinin doldugundan ve dogrulugundan emin olunmali. 
	t0 aninda extension ve BBox degerleri birbirine esittir. */
	
	curMinX = projMinX;
	curMinY = projMinY;
	curMaxX = projMaxX;
	curMaxY = projMaxY;	
}

function getImageCoords(event){
	//ClearMeasure();
	if (!event) /* For IE. */
    	//event = window.event;
		ie ? window.event : e;
	// burada her klikte info getirilmesin diye, tum isleyis bir boolean ifade ile kontrol edilebilir. 
	var elem = document.getElementById("map");
	var zxcpar = 0;
	var zxclft = elem.offsetLeft;
	var zxctop = elem.offsetTop;
	while(elem.offsetParent!= null){
		zxcpar = elem.offsetParent;
		zxclft += zxcpar.offsetLeft;
		zxctop += zxcpar.offsetTop;
		elem = zxcpar;
	}

	//var Px = event.clientX - zxclft - 1; // Bu -1 sanirim tablonun borderlari ile alakali...
	//var Py = event.clientY - zxctop - 1;
	var Px = event.clientX;
	var Py = event.clientY;
	//alert('getImageCoords fonksiyonundan dönen :' + Px+' , '+Py);
	return { x : Px, y : Py };
}

/* LastPoint degerinin icini doldurur.
   Haritanin, serverdan gelen imajin boyu ile orantili oldugunu varsayarsak, p noktasi icin sadece 
   LowerLeft kosesinden uygulanacak bir benzerlik ise yarayacaktir.   
*/

function imageCoordsToProjectCoords(pX, pY, mapWidth, mapHeight){

	//alert(pX + ' , ' + pY + ' , ' + mapWidth + ' , ' + mapHeight);
	
	var ServerMapDistance; // in real meters.
	var delta;
	var LastProjPointX;
	var LastProjPointY;
	var LowerLeft;
	/*pX = pX + 3;
	pY = pY + 30;*/
	if (Number(pX) > Number(mapWidth)/2){ 
		// upperrighttan benzerlik		
		ServerMapDistance = curMaxX - curMinX;	
		delta = (Number(mapWidth) - Number(pX)) * ServerMapDistance / Number(mapWidth);
		LastProjPointX = curMaxX - delta;  
	}
	else{ 
		// lowerleftten benzerlik	
		ServerMapDistance = curMaxX - curMinX;	
		// gercek noktanin LLX den X duzleminde uzakligi = pX*ServerMapWidth / width dir.  
		delta = Number(pX) * ServerMapDistance / Number(mapWidth);
		LastProjPointX = curMinX + delta;
	}
	if (Number(pY > Number(mapHeight)/2)){
		// lowerleftten benzerlik	
		ServerMapDistance = curMaxY - curMinY;
		delta = (Number(mapHeight) - Number(pY)) * ServerMapDistance / Number(mapHeight);
		LastProjPointY = curMinY + delta; 	
	}
	else{
		// upperrighttan benzerlik
		ServerMapDistance = curMaxY - curMinY;
		delta = (Number(pY)) * ServerMapDistance / Number(mapHeight);
		LastProjPointY = curMaxY - delta; 
	}
	
	return {x : LastProjPointX, y : LastProjPointY}; 			
}	

function zoomToExts(){
	//alert(eventFlag);
	if(isZoomRectEvent!=1) {
	document.getElementById("map").style.cursor = "default";
	}
	
	chooseEvent('zoomToExts');
	
	document.getElementById('styled_popup_info').style.display = "none";
	document.getElementById('styled_popup_measure').style.display = "none";
	InitializeZoomFactors();
	//alert(projMinX);
	

	// INEGOL BELEDIYESI ICIN BUSATIRI DISABLE EDIP ALTTAKI KODLARI EKLEDIK 28-03-2011 Ozgur KUVVET
	//getMap( projMinX, projMinY, projMaxX, projMaxY );



	// INEGOL BELEDIYESI ICIN KODLAR ACILISTA HARITAYI KUCULTEREK EKRANA SIGDIRMAK ICIN EKLENDI Ozgur KUVVET 28-03-2011

	var DeltaX = ((curMaxX - curMinX)/(ZoomRatio-2));
	var DeltaY = ((curMaxY - curMinY)/(ZoomRatio-2));	
	
	curMinX = curMinX - DeltaX;
	curMinY = curMinY - DeltaY - 500;
	curMaxX = curMaxX + DeltaX;
	curMaxY = curMaxY + DeltaY - 500;
	getMap(curMinX, curMinY, curMaxX, curMaxY);
	//-------------------------------------------------------------------------------------------



	document.getElementById("map").style.cursor = "default";
	//document.getElementById('btnZoomToExt').style.backgroundColor = "black";
}

function doPan(panStart, panEnd){

	var pStart = imageCoordsToProjectCoords( panStart.x, panStart.y, document.getElementById("map").width, document.getElementById("map").height );	
	var pEnd   = imageCoordsToProjectCoords( panEnd.x, panEnd.y, document.getElementById("map").width, document.getElementById("map").height );

	
	var deltaX = pEnd.x - pStart.x;
	var deltaY = pEnd.y - pStart.y;
    
	if ((deltaX != 0) || (deltaY != 0)){
		curMinX = curMinX - deltaX;
		curMaxX = curMaxX - deltaX;
		curMinY = curMinY - deltaY;
		curMaxY = curMaxY - deltaY;
		
		getMap( curMinX, curMinY, curMaxX, curMaxY );
			
	}


	document.getElementById('buttons').style.display = "block";
	document.getElementById('topBannerContainer').style.display = "block";
	//alert(eventFlag);
	if(eventFlag==1) { document.getElementById("map").style.cursor = "crosshair"; }
	if(eventFlag==4) { document.getElementById('styled_popup_sorgu').style.display = "block"; }
	if(eventFlag==2) { 
	document.getElementById('styled_popup_info').style.display = "block"; 
	document.getElementById('btnInfo').style.backgroundColor="red"; 
	document.getElementById("map").style.cursor = "help";
	}
	if(eventFlag==3) { 
	document.getElementById('styled_popup_measure').style.display = "block"; 
	document.getElementById('btnMeasure').style.backgroundColor="red";
	document.getElementById("map").style.cursor = "url('images/pen.cur'),default";
	}
	if(eventPanFlag==1) { 
	chooseEvent('btnPan');
	}


}

function PanRight(){
	var Delta = ((curMaxX - curMinX)/PanRatio);
	  
	curMinX = curMinX + Delta;
	curMaxX = curMaxX + Delta;
		
	getMap(curMinX, curMinY, curMaxX, curMaxY);	
}

function PanLeft(){
	var Delta = ((curMaxX - curMinX)/PanRatio);

	curMinX = curMinX - Delta;
	curMaxX = curMaxX - Delta;
		
	getMap(curMinX, curMinY, curMaxX, curMaxY);
}

function PanDown(){
	var Delta = ((curMaxY - curMinY)/PanRatio);

	curMinY = curMinY - Delta;
	curMaxY = curMaxY - Delta;
		
	getMap(curMinX, curMinY, curMaxX, curMaxY);
}

function PanUp(){
	var Delta = ((curMaxY - curMinY)/PanRatio);
	
	curMinY = curMinY + Delta;
	curMaxY = curMaxY + Delta;
		
	getMap(curMinX, curMinY, curMaxX, curMaxY);
}

function ZoomIn(){
	//alert(eventFlag);
	chooseEvent('btnZoomIn');
	
	click2 = 0;
	
	document.getElementById('styled_popup_info').style.display = "none";
	document.getElementById('styled_popup_measure').style.display = "none";
	
	// ZOOM IN BUTONUNA TIKLADIKTAN SONRA SOL TIK YAPILIRSA doZoomIn DEGERI 1 DONECEK VE ZOOM ISLEMI YAPILACAK Ozgur KUVVET 08-02-2011
	if(doZoomIn==1 && eventPanFlag!=1) {	
	
	if (((curMaxX - curMinX) > minExt) && ((curMaxY - curMinY) > minExt)) {  
		var DeltaX = ((curMaxX - curMinX)/ZoomRatio);
		var DeltaY = ((curMaxY - curMinY)/ZoomRatio);
 
		curMinX = curMinX + DeltaX;
		curMinY = curMinY + DeltaY;
		
		curMaxX = curMaxX - DeltaX;
		curMaxY = curMaxY - DeltaY;
		
		if ((curMaxX - curMinX) < minExt){
			curMaxX = curMinX + minExt;
		}
		
		if ((curMaxY - curMinY) < minExt){
			curMaxY = curMinY + minExt;
		}
		getMap(curMinX, curMinY, curMaxX, curMaxY);
		doZoomIn = 0;
		chooseEvent('btnZoomIn');
	
	} 
  }
}

function ZoomOut(){

	chooseEvent('btnZoomOut');

	click2 = 0;

	document.getElementById('styled_popup_info').style.display = "none";
	document.getElementById('styled_popup_measure').style.display = "none";
	
	// ZOOM OUT BUTONUNA TIKLADIKTAN SONRA SOL TIK YAPILIRSA doZoomOut DEGERI 1 DONECEK VE ZOOM OUT ISLEMI YAPILACAK Ozgur KUVVET 08-02-2011
	if(doZoomOut==1 && eventPanFlag!=1) {	
	
	var DeltaX = ((curMaxX - curMinX)/(ZoomRatio-2));
	var DeltaY = ((curMaxY - curMinY)/(ZoomRatio-2));	
	
	curMinX = curMinX - DeltaX;
	curMinY = curMinY - DeltaY;
	curMaxX = curMaxX + DeltaX;
	curMaxY = curMaxY + DeltaY;
	getMap(curMinX, curMinY, curMaxX, curMaxY);
	doZoomOut = 0;
	chooseEvent('btnZoomOut');
	
	}
}
 
function btnZoomInRectClick(srcElement){

  chooseEvent('btnZoomInRect');

  click2 = 0;
  
  //if (isZoomRectEvent == 0) {  
     //window.event.srcElement.style.borderStyle = "inset";
     isZoomRectEvent = 1

    //btnMeasure.style.borderStyle = "outset";
     document.getElementById("styled_popup_measure").style.display = "none";
     document.getElementById("styled_popup_info").style.display="none"; 

 /*} else {
	  
	  ClearMeasure();
	  document.getElementById("map").style.cursor = "default";
     //window.event.srcElement.style.borderStyle = "outset";
	 document.getElementById("btnZoomInRect").style.backgroundColor = "black";
     isZoomRectEvent = 0;
    }*/
}

function btnZoomOutRectClick(){ 
  ZoomOut();
  getMap(curMinX, curMinY, curMaxX, curMaxY);
}





