/*	2008.03.31, AA
	MouseWheel.js, mousewheel eventini handle ediyor. 
*/

var minExt = 0;


var zoomfactor=0.1; //Enter factor (0.05=5%)
 
function zoomhelper(){
if (parseInt(whatcache.style.width)>10&&parseInt(whatcache.style.height)>10){
whatcache.style.width=parseInt(whatcache.style.width)+parseInt(whatcache.style.width)*zoomfactor*prefix
whatcache.style.height=parseInt(whatcache.style.height)+parseInt(whatcache.style.height)*zoomfactor*prefix
}
}

function zoom(originalW, originalH, what, state){
if (!document.all&&!document.getElementById)
return
whatcache=eval("document.images."+what)
prefix=(state=="in")? 1 : -1
if (whatcache.style.width==""||state=="restore"){
whatcache.style.width=originalW+"px"
whatcache.style.height=originalH+"px"
if (state=="restore")
return
}
else{
zoomhelper()
}
zoomhelper();
}

function clearzoom(){
if (window.beginzoom)
clearInterval(beginzoom)
}

function getMidMapProjCoords(){
	var midX = (curMaxX - curMinX) /2;
	var midY = (curMaxY - curMinY) /2; 
	return { x: midX, y: midY };
}


// Px ve Py screen kordinatlaridir. 
function wheelZoomIn(Px, Py, width, height){ 
	var P = imageCoordsToProjectCoords(Px, Py, width, height); 

           //  zoom(width,height,'map','in');


	if (((curMaxX - curMinX) > minExt) && ((curMaxY - curMinY) > minExt)) {  
		// Sol taraf...
		var DeltaX = ((P.x - curMinX)/ZoomRatio);
		curMinX = curMinX + DeltaX;
		
		// Asagi taraf...
		var DeltaY = ((P.y - curMinY)/ZoomRatio);		
		curMinY = curMinY + DeltaY;

		// Sag Taraf
		DeltaX = ((curMaxX - P.x)/ZoomRatio);
		curMaxX = curMaxX - DeltaX;
		
		// Yukari taraf...
		DeltaY = ((curMaxY - P.y )/ZoomRatio);
		curMaxY = curMaxY - DeltaY;
		
		if ((curMaxX - curMinX) < minExt){
			curMaxX = curMinX + minExt;
		}
		
		if ((curMaxY - curMinY) < minExt){
			curMaxY = curMinY + minExt;
		}
	}
	
}

// Px ve Py screen kordinatlaridir. 
function wheelZoomOut(Px, Py, width, height){ 
	var P = imageCoordsToProjectCoords(Px, Py, width, height); 

	//Sol taraf
	var DeltaX = ((P.x - curMinX)/(ZoomRatio-1));
	curMinX = curMinX - DeltaX;
	
	//Sag taraf
	var DeltaY = ((P.y - curMinY)/(ZoomRatio-1));
	curMinY = curMinY - DeltaY;
	
	//Ust taraf
	DeltaX = ((curMaxX - P.x)/(ZoomRatio-1));
	curMaxX = curMaxX + DeltaX;
	//Alt taraf
	DeltaY = ((curMaxY - P.y)/(ZoomRatio-1));
	curMaxY = curMaxY + DeltaY;
	
}

var secs;
var timerID = null;
var timerRunning = false;
var delay = 500;

function InitializeTimer(){
    // Set the length of the timer, in seconds
    secs = 1;
    StopTheClock();
    StartTheTimer();
}

function StopTheClock(){
    if(timerRunning){
        clearTimeout(timerID);
    }
    timerRunning = false;
}

function StartTheTimer(){
    if (secs==0){
        TimerFunction();       
    }
    else{
        secs = secs - 1;
        timerRunning = true;
        timerID = self.setTimeout("StartTheTimer()", delay);
    }
}

var	inTimer = false;

function TimerFunction(){
	inTimer = true;
	getMap(curMinX, curMinY, curMaxX, curMaxY);
	inTimer = false;
	msgGizle();	
}

var MouseWheeled = false;
var wheelEnable  = true;

/*	Buraya gelen Px ve Py, Imaj uzerindeki piksel kordinatlaridir. */
function handle(delta, Px, Py) {
yukleniyorPenceresi('Yükleniyor...');

	if ((wheelEnable) && (!inTimer)){
		wheelEnable = false;
		var width  = map.width;
		var height = map.height;
	
		MouseWheeled = true;
		InitializeTimer();
		
	    // tekerlek, ileri dogru dondu, yani kullanicidan uzaklasti.  
	    if (delta < 0){
	    	wheelZoomOut(Px, Py, width, height );    	    	
	    }
	    // tekerlek geri dogru dondu, kullaniciya yaklasti. 
		else{
			wheelZoomIn(Px, Py, width, height);
		}
		wheelEnable = true;		
	}


}






