/*************************************************
Copyright : 2008 - GEOSIGNAL - www.geosignal.fr
Projet : TISSEO
Class : 
**************************************************/

var ggDirrecherche = new Class ({
		initialize: function() {
			this.odir = null;
			this.opoly = null;
			this.opolycolor = null;
			this.opacity = null;
			this.weight = null;
    	this.pLinePoints = null;
    	this.dragIcon = null;
    	this.stopPoint = new Array();
		},
	  zoomtrajet: function(omkr1,omkr2) {
	  	if(omkr1 != null && omkr2 != null){
	  		if(this.odir == null) this.initdir();
	  		var apoints = this.getarrangedpoints(omkr1.getLatLng(),omkr2.getLatLng(),this.stopPoint);
	  		this.odir.loadFromWaypoints(apoints,{getPolyline:true,preserveViewport:false});
	  		searchwaiting();
	  	}
	  },
	  zoomtrajetunik: function(omkr1,omkr2) {
	  	if(omkr1 != null && omkr2 != null){
	  		if(this.odir == null) this.initdirunik();
	  		var apoints = this.getarrangedpoints(omkr1.getLatLng(),omkr2.getLatLng(),this.stopPoint);
	  		this.odir.loadFromWaypoints(apoints,{getPolyline:true,preserveViewport:false});
	  		afficherwaiting();
	  	}
	  },
	  simpledraw: function() {
	  	/*var apoints = this.getarrangedpoints(omkr1.getLatLng(),omkr2.getLatLng(),this.stopPoint);
	  	this.odir.loadFromWaypoints(apoints,{getPolyline:true,preserveViewport:false});*/
	  },	  	  
	  initdirunik: function() {
    	this.odir = new GDirections();
    	GEvent.clearListeners(this.odir,"load");
			GEvent.addListener(this.odir, "load", function(){							
				closewaiting();
				if(this.opoly) {
					mapSearch.removeOverlay(this.opoly);					
				}
				this.opoly = this.odir.getPolyline();
				if(this.opolycolor) this.opoly.color = this.opolycolor;
				if(this.opacity) this.opoly.opacity = this.opacity;
				if(this.weight) this.opoly.weight = this.weight;
				//var bounds = this.opoly.getBounds();
				//var newZoom = mapSearch.getBoundsZoomLevel(bounds);
				//mapSearch.setCenter(bounds.getCenter(),newZoom-1);
				mapSearch.addOverlay(this.opoly);
			}.bind(this));
    },     	  
	  initdir: function() {
    	this.odir = new GDirections();
    	GEvent.clearListeners(this.odir,"load");
			GEvent.addListener(this.odir, "load", function(){							
				closewaiting();
				if(this.opoly) {
					mapSearch.removeOverlay(this.opoly);					
				}
				this.opoly = this.odir.getPolyline();
				if(this.opolycolor) this.opoly.color = this.opolycolor;
				if(this.opacity) this.opoly.opacity = this.opacity;
				if(this.weight) this.opoly.weight = this.weight;
				var bounds = this.opoly.getBounds();
				var newZoom = mapSearch.getBoundsZoomLevel(bounds);
				mapSearch.setCenter(bounds.getCenter(),newZoom-1);
				mapSearch.addOverlay(this.opoly);
			}.bind(this));
    },        
    getarrangedpoints: function(vtx0,vtx1,aObjmkr) {
    	var currPoints = new Array();
    	currPoints[0] = vtx0;
    	for(i = 0; i < aObjmkr.length; i++) {
    		if(!aObjmkr[i]) continue;
    		currPoints[i+1] = aObjmkr[i].getLatLng();
    	}    	
    	var distance = -1;
    	for(j = 1; j < currPoints.length; j++) {
    		if(distance < 0) {
    			distance = vtx0.distanceFrom(currPoints[j]);
    			continue;
    		}
    		dtemp = vtx0.distanceFrom(currPoints[j]);
    		if(distance < dtemp) {
    			distance = dtemp;
    		}
    		else {
    			temps = currPoints[j];
    			currPoints[j] = currPoints[j-1];
    			currPoints[j-1] = temps;    			
    		}
    	}
    	currPoints[currPoints.length] = vtx1;
    	return currPoints;
    },
    ajouterstoppoint : function(pt) {
    	var baseIcon2 = new GIcon();
				baseIcon2.iconSize=new GSize(13,13);
				baseIcon2.iconAnchor=new GPoint(4,4);
				baseIcon2.infoWindowAnchor=new GPoint(4,0);
    	var oIcon = (new GIcon(baseIcon2, "images/yellowSquare.png", null, ""));
    	var ostop = new GMarker(pt,{icon:oIcon,draggable:false,bouncy:false});    			    	
    	mapSearch.addOverlay(ostop,{zIndexProcess:10});
    	this.stopPoint.push(ostop);
    }
});