/*	2008.03.31, AA
	MouseWheel.js, mousewheel eventini handle ediyor. 
*/

var minExt = 0;

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); 

	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;	
}

var MouseWheeled = false;
var wheelEnable  = true;

/*	Buraya gelen Px ve Py, Imaj uzerindeki piksel kordinatlaridir. */
function handle(delta, Px, Py) {
	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;		
	}
}






