

function szukaniePrzyTrasieManager(map, searchManagerHandle){
	var MO = {};
	var directions;
	var normalProj;	
	var radius = 10;
	var normalProj;
	var searchAreaHighlight;
	var pLinePoints;

	
	var init = function(){
			directions = new GDirections(map, null);
			GEvent.addListener(directions, "load", directionsOnLoad);
			GEvent.addListener(directions, "error", handleGoogleErrors);
			normalProj = G_NORMAL_MAP.getProjection();
			GEvent.addListener(map, "zoomend", markSearchArea);	
	};
	
	MO.search = function(param){
			if (typeof(param.radius)!='undefined') { radius =  param.radius; }
			//directions.load("from: "+param.locFrom+" to: "+param.locTo);
			var wayPoints = [];
			wayPoints.push(param.locFrom);
			
			if(param.locThrough){
				wayPoints.push(param.locThrough);	
			}
			wayPoints.push(param.locTo);
			
			directions.loadFromWaypoints(wayPoints, { "locale": 'pl_PL' });
	};
	
	MO.clear = function(){
		if (searchAreaHighlight) {
			map.removeOverlay(searchAreaHighlight);
		}		
		directions.clear();
		pLinePoints = [];
	};
	var handleGoogleErrors = function(){
		var status = directions.getStatus();
		Util.alert({ text : "Nie można okreslić punktu startowego lub końcowego.<br/>Proszę spróbować dopisać województwo punktu startowego i docelowego.",  title : "uwaga"});
	};
	
	var directionsOnLoad = function(){
		var status = directions.getStatus();
		var bounds = directions.getBounds();
		var distance = directions.getDistance();
		
		//alert(distance);
		map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));	
		
		pLinePoints = getPolylinePoints(directions.getPolyline());			

		var threshhold = radius * 1000 * 0.1; 
		var ptsReduced = [];
		var crdToSend=[];
		var lineString = '';
		var lastPoint = new GLatLng(0,0);
		var added = 0;
		//wybieram przerzedzone punkty 
		for (n = 0 ; n < pLinePoints.length ; n++) {
			var p = pLinePoints[n];
			if (p.distanceFrom(lastPoint) > threshhold) {
				ptsReduced.push(p);
				lineString += p.lat().toFixed(6) + ',' + p.lng().toFixed(6) + 'x';
				lastPoint = p;
				added++;
			}
		}	
		if(added<300){
			searchManagerHandle.search({
				searchKryt_przyTrasie_pkty : lineString,
				searchKryt_przyTrasie_promien : radius
			}, {});		
		} else {
			Util.alert({ text : "Wybrana trasa jest zbyt długa.<br>Proszę spróbować bardziej sprecyzować odcinek trasy na którym planowany jest nocleg.",  title : "uwaga"});
			
		}
		markSearchArea();
		//alert(lineString);
			
	};
	
	
	//zwraca tablice z punktami z trasy
	var getPolylinePoints = function(p) {
		var pLinePoints = Array();
		for (var n = 0 ; n < p.getVertexCount() ; n++ ) {
			pLinePoints.push(p.getVertex(n));
		}
		return pLinePoints;
	}
	
	
	
	
	var markSearchArea = function() {
		var zoomToWidthTransTable = {
			 7 : 30 * (radius / 10),
			 8 : 56 * (radius / 10),
			 9 : 112 * (radius / 10),
			 10 : 224 * (radius / 10),
			 11 : 448 * (radius / 10),
			 12 : 896 * (radius / 10),
			 13 : 1792 * (radius / 10),
			 14 : 3584 * (radius / 10)
		};
	
		
		if(!getPolylinePoints){
			return;
		}
		if (searchAreaHighlight) {
			map.removeOverlay(searchAreaHighlight);
		}	
	 
	 
		var center = map.getCenter();
		var zoom = map.getZoom();

		if (pLinePoints && pLinePoints.length) {

			for (n = 0 ; n < pLinePoints.length ; n++) {
				var p = pLinePoints[n];
			}
			var centerPx = normalProj.fromLatLngToPixel(center, zoom);
			var nextPxLatLon =  normalProj.fromPixelToLatLng(new GPoint(centerPx.x+1,centerPx.y), zoom);
			var dPx = nextPxLatLon.distanceFrom(center)/ 1000; //  /1000;
			var pxWidth = typeof(zoomToWidthTransTable[zoom]) != 'undefined' ?  zoomToWidthTransTable[zoom] : Math.ceil(radius / dPx) * 2;

			//GPolyline(points,  color?,  weight?,  opacity?)
			searchAreaHighlight = new GPolyline(pLinePoints,'#FFFF00',pxWidth, 0.30);
			map.addOverlay(searchAreaHighlight);
		}	else {
			return;
			var circlePoints = drawCircle(center,radius,'KM');
			//	GPolygon(points,  strokeColor?,  strokeWeight?,  strokeOpacity?,  fillColor?,  fillOpacity?)
			searchAreaHighlight = new GPolygon(circlePoints, '#00C000', 2 , 0.5 , '#00C000', 0.10);	
			map.addOverlay(searchAreaHighlight);
		}


	};	
	
	
	var drawCircle = function(center,circleRadius,circleUnits){
		
		var circlePoints = Array();
		with (Math) {
			if (circleUnits == 'KM') {
				var rLat = (circleRadius/6378.8) * (180/PI);
			}
			else { //miles
				var rLat = (circleRadius/3963.189) * (180/PI);
			}
			var rLng = rLat/cos(center.lat() * (PI/180));

			for (var a = 0 ; a < 361 ; a+=1 ) {
				var aRad = a*(PI/180);
				var x = center.lng() + (rLng * cos(aRad));
				var y = center.lat() + (rLat * sin(aRad));
				var point = new GLatLng(parseFloat(y),parseFloat(x));
				circlePoints.push(point);
			}
		}
		return circlePoints;
	};
	
	
	init();
	return MO;
}
