
/*
   Title: TripVectorLayer

   The TripVectorLayer class is the presented layer of a route above the map.[{BR}]
   With this class, you can set the route style.[{BR}]
   
   In order to show a route on the map, you should add to the route vector layer the <Route> object and attach the
   route vector layer to the map.[{BR}]
   You can also use the default route vector layer of the map (you can get the object by the map's tripVectorLayer property), and just
   use the map's <showRoute> method.

   [{HR style=~color:#E6E6E6~}]
*/

TripVectorLayer.prototype = new EventListener();

/***********************************************************************************************/

/*
   Function: TripVectorLayer

   The TripVectorLayer constructor.
   Creates a new route vector layer instance.

   @Desc TripVectorLayer()
*/
//function TripVectorLayer(_parentMap, _name) {
function TripVectorLayer(_name, _swfPath) {
  
  // Inherited from EventListener.
  this.EventListener = EventListener;
  this.EventListener();
   
  var t = this
  var route = null
  var parentMap = null;
	

  t.type ="tripVectorLayer"
  var style          = new LineStyle(6,"17F750",60) 
  var routeVisible      = false
  var animationVisible  = true
  var maneuversVisible  = true
  var routeSpatialArr   = null;
  var isEditable		= true;

  var maneuversList	      = new Array();
  var maneuversCount      = null
  var tripItemsList       = null

  var name    = _name
  var swfPath = _swfPath

  var movie_path = "/:ExternalDynamicLayers/"+name

  var firstPoint = null;
  var lastPoint  = null;

  var tripPoints = null;
  var id;
  t.videoPointsList = new Array();

/////////////////////////////////////////////////////////////////////////////
t.toString=function() {
  return "tripVectorLayer"  
} 
/////////////////////////////////////////////////////////////////////////////
t.getName=function() {
  return name
} 
/////////////////////////////////////////////////////////////////////////////
t.setName=function(_name) {
  name = _name
} 
 /////////////////////////////////////////////////////////////////////////////
t.setId=function(id) {
  t.runFlashFunction(movie_path,"setId",id)
} 
/////////////////////////////////////////////////////////////////////////////
t.getPath=function() {
  return swfPath
} 
/////////////////////////////////////////////////////////////////////////////
t.setPath=function(_swfPath) {
  swfPath = _swfPath
} 
/////////////////////////////////////////////////////////////////////////////
t.setParentMap=function(_parentMap) {
	parentMap = _parentMap
}
/////////////////////////////////////////////////////////////////////////////
t.getParentMap=function(_swfPath) {
	return parentMap;
}
/////////////////////////////////////////////////////
var getFlash = function() {
	return parentMap.getFlash();
}
/////////////////////////////////////////////////////
t.onFlashLoaded = function() {
	var flash = getFlash();
	flash.doCommandsQueue(movie_path);
	for (var i=0;i<flashCommands.length;i++) {
		flash.runFunction.apply(this,flashCommands[i]);
	}
	isFlashLoaded = true;
	if (tripPoints!=null) {
		t.setTripPoints(tripPoints)
		t.triggerEvent(TripVectorLayer.EVENT_LAYER_FLASH_LOADED,t)
	}
	
}
/////////////////////////////////////////////////////
var flashCommands = new Array();
var isFlashLoaded = false;
t.runFlashFunction = function() {
	if (parentMap!=null) {
		var flash = getFlash();
		flash.runFunction.apply(this,arguments);
		
	} else {
		flashCommands.push(arguments)
	}
}
/////////////////////////////////////////////////////
t.parserArgs = function(evt,args) {
  return new Array(args);
}

/*
 *
 **/
t.blur=function() {
  t.runFlashFunction(movie_path,"blur")
} 
/*
 *
 **/
t.unBlur=function() {
  t.runFlashFunction(movie_path,"unblur")
} 

/*
 *
 **/
var toMercator = function(lat, long) {
   var horzPos = long*100000
   var d = Math.min(0.99999999, Math.max(-0.99999999, Math.sin((lat)*Math.PI/180)))
   var vertPos = (0.5*Math.log((1+d)/(1-d))*180/Math.PI) * -100000  
   return {horzPos : horzPos, vertPos: vertPos}
}
/*
 *
 **/
var zoomPowerToLevel = function(zoomPower) {
	 var ZL=0;
	 if (zoomPower==1) return 0;
	 if (zoomPower<1) {
	   var Z=1;
	   while (zoomPower-0<Z) {
		Z=Z/2;
		ZL=ZL-1;
	   }
	 } else {
	   var Z=1;
	   while (zoomPower-0>Z) {
		 Z=Z*2;
		 ZL=ZL+1;
	   }
	 }
	 return ZL;	 
}
/*
 *
 **/
var fitMapByTrip = function (tripPoints) {
	   var PosX = tripPoints[0]-0
	   var PosY = tripPoints[1]-0
	   var minX=PosX; var minY=PosY; var maxX=PosX; var maxY=PosY;
	   for(var i=2; i<tripPoints.length;i=i+2) {
			var PosX = tripPoints[i]-0;
			var PosY = tripPoints[i+1]-0;
			if (PosX<minX) minX=PosX
			  else if (PosX>maxX) maxX=PosX
			if (PosY<minY) minY=PosY
			   else if (PosY>maxY) maxY=PosY
	   }

	   if (minX!=null) {
		   var deltaX = Math.abs(maxX-minX);
		   var deltaY = Math.abs(maxY-minY);
		   var zoomPower;

		   if ((deltaX==0) || (deltaY==0)) {
			   if ((deltaX==0) && (deltaY==0)) {
				   zoomPower = 1;
			   } else {
			       if (deltaX==0) zoomPower = parentMap.height/deltaY 
				     else zoomPower = parentMap.width/deltaX
			   }
		   } else {
			   zoomPower  = Math.min(parentMap.width/deltaX,parentMap.height/deltaY) 
		   }
		   if (zoomPower>=1) zoomPower=1
		   var zoomLevel = zoomPowerToLevel(zoomPower);

		   parentMap.setCenterPosition(new Coordinate((minX+maxX)/2,(minY+maxY)/2),zoomLevel)
	   }
}

var ItemData = function (x, y, type, time, text, url,thumbUrl ) {
	this.x = x
	this.y = y
	this.type = type
	this.time = time
	if (text!=null) {
		if (text.toString()=="[object Object]") text=""
	}
	this.text = text
	this.url = url
	this.thumbUrl  = thumbUrl 
	//alert(thumbUrl );
}

/*
 *
 */
t.setTripPoints = function (_tripPoints) {
	//alert(_tripPoints);
   tripPoints = _tripPoints
   //dmp(tripPoints,7);
   if (isFlashLoaded) {
	   var itemsList, item;
	   var mercatorPoint;

	   var arrPoints	   = new Array();
	   var trackPointsList = new Array();
	   tripItemsList   = new Array();
	   
	   firstPoint = null;
	   lastPoint  = null;
	   t.videoPointsList = new Array();
		
	   for (var i=0;i<tripPoints.length;i++) {
			if ((!isNaN(tripPoints[i].x-0)) && (!isNaN(tripPoints[i].y-0))) {
				 mercatorPoint = toMercator(tripPoints[i].y, tripPoints[i].x)
				 if (i==0) firstPoint = mercatorPoint
				  else if (i==tripPoints.length-1) lastPoint = mercatorPoint

				 trackPointsList.push(mercatorPoint.horzPos, mercatorPoint.vertPos);
				 t.videoPointsList.push(mercatorPoint.horzPos, mercatorPoint.vertPos, tripPoints[i].localTime);

				 if (tripPoints[i].item) {
					   if (!tripPoints[i].item.length) tripPoints[i].item = new Array(tripPoints[i].item);
					   itemsList = tripPoints[i].item
					   for (var j=0;j<itemsList.length;j++) {
						  item = itemsList[j]
						  text = ""
						  if (item.text!=null) {
							  if (item.text[0]!=null) text = item.text[0].Text
						  }
						  tripItemsList.push(new ItemData(mercatorPoint.horzPos, mercatorPoint.vertPos, item.type, tripPoints[i].localTime, text, item.url , item.thumbUrl ))
					   }
				 }
			}
	   }
	   if (trackPointsList.length>0) {
		   trackPoints   = trackPointsList.join(",")
		   t.videoPointsList = t.videoPointsList.join(",")
		   t.runFlashFunction(movie_path,"setAllTripPoints",t.videoPointsList)
		 //  flashPlayer.runFunction("","Init",videoPointsList.join(","))
		   maneuversList.push(new Maneuver("","","","","","",arrPoints,trackPoints))
		   maneuversCount = maneuversList.length


		   t.initSpatial(maneuversCount-1)

		   t.doSpatial()

		   initFlash();

		   parentMap.addListener(Map.EVENT_MAP_MODIFIED,onMapModified,parentMap);
		   fitMapByTrip(trackPointsList)
		   //return;
	   }
	   t.triggerEvent(TripVectorLayer.EVENT_LAYER_FLASH_LOADED,t)
   }
}
/////////////////////////////////////////////////////////////////////////////
t.getItemsStr_old = function() {
	var tripStr = ""
	var item;
	for (var i=0;i<tripItemsList.length;i++) {
		item = tripItemsList[i]
		tripStr += item.x+"^"+item.y+"^"+item.type+"^"+item.time+"^"+item.text+"^"+item.url+"|"
	}
	if (tripStr!="") tripStr=tripStr.substring(0,tripStr.length)	
	return tripStr;
}
/////////////////////////////////////////////////////////////////////////////
t.getItemsStr = function() {
	var tripStr = ""
	var item;
	var img_url;
	var tmb_url = "";
	
	//alert(1);
	//dmp(tripItemsList,7);
	for (var i=0;i<tripItemsList.length;i++) {
		item = tripItemsList[i]
		// tumbs expansion 
		//alert(item.thumbUrl);
		img_url = item.url;
		
		tmb_url = item.thumbUrl;
		
		if (tmb_url == null)tmb_url = item.url;
		
		//img_url = tmb_url;
		
		//tripStr += item.x+"^"+item.y+"^"+item.type+"^"+item.time+"^"+item.text+"^"+item.url+"^"+img_url+"|"
		tripStr += item.x+"^"+item.y+"^"+item.type+"^"+item.time+"^"+item.text+"^"+img_url+"^"+tmb_url+"|"
	}
	//dmp(tripStr);
	if (tripStr!="") tripStr=tripStr.substring(0,tripStr.length)	
	return tripStr;
}
/////////////////////////////////////////////////////////////////////////////
t.getItemsStr_thumbs = function() {
	var tripStr = ""
	var item;
	var img_url;
	var tmb_url = "";
	
	//alert(1);
	//dmp(tripItemsList,7);
	for (var i=0;i<tripItemsList.length;i++) {
		item = tripItemsList[i]
		// tumbs expansion 
		//alert(item.thumbUrl);
		img_url = item.url;
		
		tmb_url = item.thumbUrl;
		
		if (tmb_url == null)tmb_url = item.url;
		
		//img_url = tmb_url;
		
		//tripStr += item.x+"^"+item.y+"^"+item.type+"^"+item.time+"^"+item.text+"^"+item.url+"^"+img_url+"|"
		tripStr += item.x+"^"+item.y+"^"+item.type+"^"+item.time+"^"+item.text+"^"+img_url+"^"+tmb_url+"|"
	}
	//dmp(tripStr);
	if (tripStr!="") tripStr=tripStr.substring(0,tripStr.length)	
	return tripStr;
}
/////////////////////////////////////////////////////////////////////////////
t.getItems = function() {
	return tripItemsList;
}
/////////////////////////////////////////////////////////////////////////////
var initFlash = function () {
 // if (routeId=LastRouteId) {
	  routeVisible = true
	  //return;

	  tripStr = t.getItemsStr();
	  //dmp(tripStr);
	  if (lastPoint==null) lastPoint = firstPoint
      firstLastPointStr = firstPoint.horzPos+"^"+firstPoint.vertPos+"^"+lastPoint.horzPos+"^"+lastPoint.vertPos
	  //dmp(firstLastPointStr + " " + movie_path);
	  t.runFlashFunction(movie_path,"InitRoute",objectId,tripStr, firstLastPointStr)


	 //  parentMap.getFlash().runFunction(movie_path,"InitRoute",objectId,tripStr, firstLastPointStr)
	 //  t.runFlashFunction(movie_path,"SetLineStyle",style.width,style.color,style.alpha)
	 //  alert(tripStr);
	 //  flashImagesStrip.runFunction("","Init",objectId,tripStr)

	 // var flash = holdFlashObjectsArr[0].getFlash()
	 /* var flash = parentMap.getFlash();
	  var j;
	  var locationStr;
	  var locationsString = "1^"+maneuversList[0].location.getMapX() +"^"+ maneuversList[0].location.getMapY() +"|"
	  for (i=1;i<maneuversList.length-1;i++) {
		  if (maneuversList[i].type.toLowerCase()=="middle") locationStr = "M"
		    else locationStr=""
		  locationsString += locationStr+"^"+maneuversList[i].location.getMapX() +"^"+ maneuversList[i].location.getMapY() +"|"
	  }
	  locationsString += "1^"+maneuversList[maneuversList.length-1].location.getMapX() +"^"+ maneuversList[maneuversList.length-1].location.getMapY()+"|"
	  flash.runFunction(movie_path,"InitRoute",objectId, locationsString)*/
//  }
}
/////////////////////////////////////////////////////////////////////////////
t.initFlash_flickr = function (str) {
	//alert(movie_path);
 // if (routeId=LastRouteId) {
	  routeVisible = true
	  //return;

	  tripStr = str ;//t.getItemsStr();
	  //dmp(tripStr);
	  /*
	  if (lastPoint==null) lastPoint = firstPoint
      firstLastPointStr = firstPoint.horzPos+"^"+firstPoint.vertPos+"^"+lastPoint.horzPos+"^"+lastPoint.vertPos
	  */
	  //firstLastPointStr = null
	  firstLastPointStr = ""
	  //dmp(firstLastPointStr + " " + movie_path);
	  t.runFlashFunction(movie_path,"InitRoute","flickr2",tripStr, firstLastPointStr)
	  //t.runFlashFunction(movie_path,"InitRoute","flickr",tripStr, "firstLastPointStr")


	 //  parentMap.getFlash().runFunction(movie_path,"InitRoute",objectId,tripStr, firstLastPointStr)
	 //  t.runFlashFunction(movie_path,"SetLineStyle",style.width,style.color,style.alpha)
	 //  alert(tripStr);
	 //  flashImagesStrip.runFunction("","Init",objectId,tripStr)

	 // var flash = holdFlashObjectsArr[0].getFlash()
	 /* var flash = parentMap.getFlash();
	  var j;
	  var locationStr;
	  var locationsString = "1^"+maneuversList[0].location.getMapX() +"^"+ maneuversList[0].location.getMapY() +"|"
	  for (i=1;i<maneuversList.length-1;i++) {
		  if (maneuversList[i].type.toLowerCase()=="middle") locationStr = "M"
		    else locationStr=""
		  locationsString += locationStr+"^"+maneuversList[i].location.getMapX() +"^"+ maneuversList[i].location.getMapY() +"|"
	  }
	  locationsString += "1^"+maneuversList[maneuversList.length-1].location.getMapX() +"^"+ maneuversList[maneuversList.length-1].location.getMapY()+"|"
	  flash.runFunction(movie_path,"InitRoute",objectId, locationsString)*/
//  }
}
/////////////////////////////////////////////////////////////////////////////
var onMapModified = function() {
  drawRoute();
}
/////////////////////////////////////////////////////////////////////////////
var drawRoute = function () {
	if (routeVisible) {
	

		//var flash = holdFlashObjectsArr[0].getFlash()
		var flash = parentMap.getFlash();
		var cvp = parentMap.getClientViewPosition(true)
		var zoomLevel = parentMap.getZoomLevel()

		var zoomPower = Math.pow(2,zoomLevel)
		deltaX = 400/zoomPower
		deltaY = 200/zoomPower
		var mapBound = new TBound(cvp.topLeft.getMapX()-deltaX,cvp.topLeft.getMapY()-deltaY,cvp.bottomRight.getMapX()+deltaX,cvp.bottomRight.getMapY()+deltaY)
		//if (maneuversVisible) {

		   flashLocationsListString = getRouteLocationsByMapPosition(mapBound,zoomPower)

		   //} else {
		 //  flashLocationsListString = ""
		//}

		flashCoordinatesListString = getRouteCoordinatesByMapPosition(mapBound,zoomLevel)

		t.runFlashFunction(movie_path,"SetRoute",flashLocationsListString,flashCoordinatesListString)
		//dmp(flashLocationsListString);
		//dmp(flashCoordinatesListString);

		//flash.runFunction(movie_path,"SetRoute",flashLocationsListString,flashCoordinatesListString)
	}
}
/////////////////////////////////////////////////////////////////////////////
var getRouteLocationsByMapPosition = function (mapBound,zoomPower) {
  var locationsNumbersList = new Array()
  for (i=0;i<tripItemsList.length;i++) {
	
	 
	 if (isCoordinateInMap(mapBound,tripItemsList[i].x,tripItemsList[i].y,10)) {
		// Do Cluster
		showLocation = true
		/*maneuverX = maneuversList[i].location.getMapX()
		maneuverY = maneuversList[i].location.getMapY()
		
		for(S=0; S<locationsNumbersList.length; S++) {
			X = maneuversList[locationsNumbersList[S]].location.getMapX()
			Y = maneuversList[locationsNumbersList[S]].location.getMapY()

			z = 12/zoomPower
			if ((X-z<maneuverX) && (Y-z<maneuverY) && (X+z>maneuverX) && (Y+z>maneuverY)) {
				 showLocation=false																																																				
			}
		}*/
		if (showLocation) locationsNumbersList.push(i)
		/////////////////////
	 }
  }

  return locationsNumbersList.join("^")
}
/////////////////////////////////////////////////////////////////////////////
var getRouteCoordinatesByMapPosition = function (mapBound,zoomLevel) {
  
  if (zoomLevel>=-6) {
   z = 0
  } else z = 1
 // z = 1
//z = 0
  var segments = routeSpatialArr[z].segmentsList
  
  var flashCoordinatesList = new Array()
  var coordinate1,coordinate2,bound,coordinatesList;
  addDelimiter = false
// alert("AsdA")
  for (i=0;i<segments.length;i++) {
	  if (segments[i]!=null) {
			  bound      = segments[i].bound
			  coordinatesList = segments[i].coordinatesList

			  if (segments[i].coordinatesList.length>0) {
				//  alert(i)
				  if (isRectanglesOverlappingMap(mapBound,bound.x1,bound.y1,bound.x2,bound.y2)) {
					  isFirstSegment = true
					  coordinateX1 = coordinatesList[0]
					  coordinateY1 = coordinatesList[1]

					  for (j=2;j<coordinatesList.length;j=j+2) {
						  coordinateX2 = coordinatesList[j]
						  coordinateY2 = coordinatesList[j+1]

						  if (isRectanglesOverlappingMap(mapBound,Math.min(coordinateX1,coordinateX2),Math.min(coordinateY1,coordinateY2),Math.max(coordinateX1,coordinateX2),Math.max(coordinateY1,coordinateY2))) {
							 if (addDelimiter) {
								flashCoordinatesList.push("|")  
								addDelimiter = false
							 } 
							 if (isFirstSegment) {
								flashCoordinatesList.push(coordinateX1)
								flashCoordinatesList.push(coordinateY1)
								isFirstSegment = false
							 }
				
							 flashCoordinatesList.push(coordinateX2)
							 flashCoordinatesList.push(coordinateY2)	

						  } else {
							 addDelimiter=true
						  }
						  coordinateX1 = coordinateX2
						  coordinateY1 = coordinateY2

					  }
				  }
			  }
	  }
  }	
  resultString = flashCoordinatesList.join("^")
  resultString = resultString.replace(/\^\|\^/g,"|");

  if (resultString.substring(0,2)=="|^") resultString=resultString.substring(2,resultString.length)
  return resultString;
}
/////////////////////////////////////////////////////////////////////////////
var TBound = function(_x1,_y1,_x2,_y2) {
   this.x1 = Math.min(_x1,_x2)
   this.y1 = Math.min(_y1,_y2)
   this.x2 = Math.max(_x1,_x2)
   this.y2 = Math.max(_y1,_y2)	
}
/////////////////////////////////////////////////////////////////////////////
var TSegment = function(_x1,_y1,_x2,_y2) {
  this.coordinatesList = new Array()
  this.bound = new TBound(_x1,_y1,_x2,_y2)
}
/////////////////////////////////////////////////////////////////////////////
var TSpatial = function(_distance) {
   this.segmentsList = new Array()
   this.distance = _distance
   this.x = 0
   this.y = 0
}
/////////////////////////////////////////////////////////////////////////////
var pntToSegmentDistance = function(Px, Py, x1, y1, x2, y2) {
  if ((x1==x2) && (y1==y2)){
		return Math.round(Math.sqrt(((x1-Px)*(x1-Px))+((y1-Py)*(y1-Py))));
   } else {
     Dx    = x2 - x1;
     Dy    = y2 - y1;
     Ratio = ((Px - x1) * Dx + (Py - y1) * Dy) / (Dx * Dx + Dy * Dy);
	 if (Ratio<0) { 
       return Math.round(Math.sqrt(((x1-Px)*(x1-Px))+((y1-Py)*(y1-Py))));
	 } else {
	    if (Ratio>1) {
			 return Math.round(Math.sqrt(((x2-Px)*(x2-Px))+((y2-Py)*(y2-Py))));
	    } else 	{
			x2=(1 - Ratio) * x1 + Ratio * x2
			y2=(1 - Ratio) * y1 + Ratio * y2
			return Math.round(Math.sqrt(((x2-Px)*(x2-Px))+((y2-Py)*(y2-Py))));
		}
     }
  }
}
/////////////////////////////////////////////////
function checkMinMaxSegment(z,i,pntX,pntY) {
	var b = routeSpatialArr[z].segmentsList[i].bound
	if (pntX<b.x1) b.x1 = pntX
	   else if (pntX>b.x2) b.x2 = pntX
	if (pntY<b.y1) b.y1 = pntY
	   else if (pntY>b.y2) b.y2 = pntY
}
/////////////////////////////////////////////////
function isRectanglesOverlappingMap(mapBound,minX,minY,maxX,maxY) {
	if (mapBound.x1>maxX) return false;
    if (mapBound.x2<minX) return false;
    if (mapBound.y1>maxY) return false;
    if (mapBound.y2<minY) return false;
	return true;
}
/////////////////////////////////////////////////
function isCoordinateInMap(mapBound,X,Y,delta) {
    if ((mapBound.x1-delta<=X) && (mapBound.y1-delta<=Y) && (mapBound.x2+delta>=X) && (mapBound.y2+delta>=Y)) return true
     else return false
}
/////////////////////////////////////////////////
t.initSpatial = function (segmentsCount) {
  routeSpatialArr = new Array();
  routeSpatialArr.push(new TSpatial(-1))
  routeSpatialArr.push(new TSpatial(100))
  // routeSpatialArr.push(new TSpatial(400))
  // routeSpatialArr.push(new TSpatial(200))
  for (z=0; z<routeSpatialArr.length; z++) {
	 routeSpatialArr[z].segmentsList = new Array(segmentsCount)
  }
}
/////////////////////////////////////////////////
t.doSpatial = function () {

  var j,z,x,y,x1,y1,x2,y2;
  var rsArr = routeSpatialArr
  
  var i;
  for (i=0; i<maneuversCount; i++) {
	if (i<maneuversCount) {
		  coordinatesList = maneuversList[i].coordinatesString.split(",")
		  if (coordinatesList!="") {
				 // alert(i+" - " + coordinatesList);
				  ///////////////////////////////////////////////////////////////
				  // add first coordinate segment
				  firstSegmentCoordinateX = coordinatesList[0]-0 // First coordinate X of current (i) segment.
				  firstSegmentCoordinateY = coordinatesList[1]-0 // First coordinate Y of current (i) segment.
				  for (z=0; z<rsArr.length; z++) { // Loop over number of route spatial.
					 rsArr[z].x = firstSegmentCoordinateX
					 rsArr[z].y = firstSegmentCoordinateY
					 rsArr[z].segmentsList[i] = new TSegment(firstSegmentCoordinateX,firstSegmentCoordinateY,firstSegmentCoordinateX,firstSegmentCoordinateY)
					 rsArr[z].segmentsList[i].coordinatesList.push(firstSegmentCoordinateX,firstSegmentCoordinateY)
				  }
				  ///////////////////////////////////////////////////////////////

				  ///////////////////////////////////////////////////////////////
				  x1 = coordinatesList[2]-0
				  y1 = coordinatesList[3]-0

				  for (j=4; j<coordinatesList.length-2; j=j+2) {
					 x2 = coordinatesList[j]-0
					 y2 = coordinatesList[j+1]-0
					 for (z=0; z<rsArr.length; z++) {
						if (pntToSegmentDistance(x1,y1,rsArr[z].x,rsArr[z].y,x2,y2)>rsArr[z].distance) {
							 rsArr[z].segmentsList[i].coordinatesList.push(x1,y1)
							 checkMinMaxSegment(z,i,x1,y1)
							 rsArr[z].x = x1
							 rsArr[z].y = y1
						} 
					 }
					 x1 = x2
					 y1 = y2
				  }
				  ///////////////////////////////////////////////////////////////
				 
				  ///////////////////////////////////////////////////////////////
				  // add last coordinate segment
				  lastSegmentCoordinateX = coordinatesList[coordinatesList.length-2]-0 // First coordinate of current segment.
				  lastSegmentCoordinateY = coordinatesList[coordinatesList.length-1]-0 // First coordinate of current segment.
				  for (z=0; z<rsArr.length; z++) { // Loop over number of route spatial.
					 rsArr[z].x = lastSegmentCoordinateX
					 rsArr[z].y = lastSegmentCoordinateY
					 rsArr[z].segmentsList[i].coordinatesList.push(lastSegmentCoordinateX,lastSegmentCoordinateY)
					 checkMinMaxSegment(z,i,lastSegmentCoordinateX,lastSegmentCoordinateY)
				  }
				  ///////////////////////////////////////////////////////////////
		  }
	 }
  }
  ////////////////////////////////////////////
  /*LastSegment = 0
  for (i=1; i<maneuversCount; i++) {
	if (i<maneuversCount-1) {
		
 		 for (z=0; z<rsArr.length; z++) { // Loop over number of route spatial.
		    var spatialArr = rsArr[z]
			if (rsArr[z].segmentsList[i].coordinatesList.length<=4) { // Less than 3 coordinates 
		        x2 = spatialArr.segmentsList[i].coordinatesList[0]
				y2 = spatialArr.segmentsList[i].coordinatesList[1]
		        x1 = spatialArr.segmentsList[LastSegment].coordinatesList[0]
				y1 = spatialArr.segmentsList[LastSegment].coordinatesList[1]
		        x3 = spatialArr.segmentsList[i].coordinatesList[2]
				y3 = spatialArr.segmentsList[i].coordinatesList[3]
				if (pntToSegmentDistance(x2,y2,x1,y1,x3,y3)<spatialArr.distance) {
				   alert(spatialArr.segmentsList[i].coordinatesList.length);
				   spatialArr.segmentsList[i].coordinatesList.length = 0
				   i++
					//  alert("asdasd");
				} else LastSegment=i-1
			} else LastSegment=i-1
		 } 
	}
  }*/
  ////////////////////////////////////////////
 
}
/////////////////////////////////////////////////////
/*
   Function: setStyle

   Sets the route line style.

   @Desc setStyle(<LineStyle> lineStyle)

   Parameters:

      lineStyle   - The new route line style.

   See Also:

		<getStyle>
*/
t.setStyle = function(lineStyle) {
   style = lineStyle
   //var flash = holdFlashObjectsArr[0].getFlash()
 //  var flash = parentMap.getFlash();
   
   t.runFlashFunction(movie_path,"SetLineStyle",style.width,style.color,style.alpha)




   //flash.runFunction(movie_path,"SetLineStyle",style.width,style.color,style.alpha)
}

t.setBgStyle = function(lineStyle) {
   style = lineStyle
   t.runFlashFunction(movie_path,"SetBgLineStyle",lineStyle.width,lineStyle.color,lineStyle.alpha)
}




/////////////////////////////////////////////////////
/*
   Function: getStyle

   Returns the route line style.

   @Desc <LineStyle> getStyle()

   See Also:

		<setStyle>
*/
t.getStyle = function() {
   return style;
}
/////////////////////////////////////////////////////
/*
   Function: hide

   Hides the route from the map.

   @Desc hide()

   See Also:

		<show>
*/
t.hide = function() {
   //routeVisible = false
   //var flash = holdFlashObjectsArr[0].getFlash()
   var flash = parentMap.getFlash();
   flash.runFunction(movie_path,"HideRoute")
}
/////////////////////////////////////////////////////
t.flickrMode = function() {
   //routeVisible = false
   //var flash = holdFlashObjectsArr[0].getFlash()
   var flash = parentMap.getFlash();
   flash.runFunction(movie_path,"flickrMode")
}
/////////////////////////////////////////////////////
/*
   Function: hide

   Shows the route.

   @Desc show()

   See Also:

		<show>
*/
t.show = function() {
//return;
   if (!routeVisible) {
	   routeVisible = true
	   drawRoute()
	  // var flash = holdFlashObjectsArr[0].getFlash()
	   var flash = parentMap.getFlash();
	   flash.runFunction(movie_path,"ShowRoute")
   }
}
/////////////////////////////////////////////////////////////////////////////
/*
   Function: hideAnimation

   Hides the route animation.

   @Desc show()

   See Also:

		<showAnimation>
*/
t.hideAnimation = function() {
   showHideAnimation(false)
}
/////////////////////////////////////////////////////
/*
   Function: showAnimation

   Shows the route animation.

   @Desc showAnimation()

   See Also:

		<hideAnimation>
*/
t.showAnimation = function() {
   if (!animationVisible) showHideAnimation(true)
}
/////////////////////////////////////////////////////////////////////////////
var showHideAnimation = function(isShow) {
    animationVisible = isShow
	//if (routeVisible) {
	  // var flash = holdFlashObjectsArr[0].getFlash()
	   var flash = parentMap.getFlash();
	   flash.runFunction(movie_path,"showHideAnimation",isShow)
	//}
}
/////////////////////////////////////////////////////////////////////////////
/*
   Function: hideAnimation

   Hides the route maneuvers.

   @Desc hideAnimation()

   See Also:

		<showManeuvers>
*/
t.hideManeuvers = function() {
   showHideManeuvers(false)
}
/////////////////////////////////////////////////////
/*
   Function: showManeuvers

   Shows the route maneuvers.

   @Desc showManeuvers()

   See Also:

		<hideAnimation>
*/
t.showManeuvers = function() {
   if (!maneuversVisible) showHideManeuvers(true)
}
/////////////////////////////////////////////////////////////////////////////
var showHideManeuvers = function(isShow) {
    maneuversVisible = isShow
	//if (routeVisible) {
	  // var flash = holdFlashObjectsArr[0].getFlash()
	   var flash = parentMap.getFlash();
	   flash.runFunction(movie_path,"showHideManeuvers",isShow)
	//}
}
/////////////////////////////////////////////////////
// set if route can be editable or not
// the user can drag the route to a new point to add a new location
/*
   Function: setEditable

   Sets the route as editable.[{BR}]
   The user can drag the route to a new point to add a new location.

   @Desc setEditable()

   See Also:

		<getEditable>
*/
t.setEditable = function(_isEditable) {
   isEditable = _isEditable
   var flash = parentMap.getFlash();
   flash.runFunction(movie_path,"SetEditable",isEditable)
}
/////////////////////////////////////////////////////
/*
   Function: getEditable

   Returns true if the route is editable, otherwize, returns false.[{BR}]

   @Desc <Boolean> getEditable()

   See Also:

		<setEditable>
*/
t.getEditable = function() {
   return isEditable;
}
/////////////////////////////////////////////////////
t.onRouteFlashLoaded = function() {
	//var flash = holdFlashObjectsArr[0].getFlash()
	var flash = parentMap.getFlash();
	flash.doCommandsQueue(movie_path);   
}
/////////////////////////////////////////////////////////////////////////////
// Kill the route object and remove from the memory
/*
   Function: clearRoute

   Clears the route from the map.[{BR}]
   This method also kills the route object.

   @Desc clearRoute()
*/
t.clearRoute = function() {
   if (routeSpatialArr!=null) {
	  // var flash = holdFlashObjectsArr[0].getFlash()
	   var flash = parentMap.getFlash();
	   flash.runFunction(movie_path,"clearRoute")
	   routeSpatialArr.length = 0
	   parentMap.removeListener(Map.EVENT_MAP_MODIFIED,onMapModified,parentMap);
	   routeVisible    = false
	   routeSpatialArr = null
	   route = null
   }
}
/////////////////////////////////////////////////////////////////////////////
}

/// Group: Map Events ////////////////////////////////////
  /*
	Variable: Map.EVENT_MAP_INITIALIZE 
    This event dispatches after the map has succsesfully initialized.[{BR}]
	The event occurs only once during the application run time.

    Arguments:
      sender     - <Map> The sender object.
  */ 
  TripVectorLayer.EVENT_LAYER_FLASH_LOADED     = "onlayerflashloaded"
//////////////////////////////////////////////////
 
function escape_relive_session(session_id){

	tmp_sid = session_id;
	tmp_sid = tmp_sid.replace(/\+/g, "%2B");
	tmp_sid = tmp_sid.replace(/\//g, "%2F");
	tmp_sid = tmp_sid.replace(/=/g, "%3D");
		
	return tmp_sid;
}