// JavaScript Document



var trailState;

// Direction variables
var INCREMENT = 1;
var DECREMENT = 2;

// Orientation variables
var FRONT = 1; 
var LEFT = 2;
var RIGHT = 3;

// Vertical Orientation variables
var STRAIGHT = 4;
var UP = 5;

var FORWARD = 0;
var BACKWARD = 1;

// Trail indecies
var LNG_I = 0;
var LAT_I = 1;
var DIR_I = 2;
var FRONT_I = 3;
var BACK_I = 4;
var VER_I = 5;
var MARK_I = 6;

// Comment modes
var DESCRIPTION_MODE = 0;
var REVIEW_MODE = 1;

var DESCRIPTION_START = "%%__%%";
var KEYWORD_SEPERATOR = "__%%%%__";

var currMarker;
var icon;
var shortcuts = new Array();
var commentMode = DESCRIPTION_MODE;
var firstLoad = true;
var colors = new Array("#0000FF", "#850407", "#317602", "#4F0505", "#B63475", "#B63475", "#B63475", "#0000FF");

var showHints = true;

function myinit(){
	initState();
	
	firstPoint = new GPoint(trailState.route.getTrailPoint(0).lat, trailState.route.getTrailPoint(0).lng)
    map.centerAndZoom(firstPoint, 2);
	
	createPointer(firstPoint);
	initTrails();
	generalUpdate();
	firstLoad = false;
}

function initTrails(){
	var icon = new GIcon();	
	icon.image = "http://www.viewacity.com/images/mm_20_red_small.png";
	icon.shadow = "";
	icon.iconSize = new GSize(6, 10);
	icon.shadowSize = new GSize(11, 10);
	icon.iconAnchor = new GPoint(3, 10);
	icon.infoWindowAnchor = new GPoint(5, 1);
	
	var startIcon = new GIcon();	
	startIcon.image = "http://www.viewacity.com/images/mm_20_green.png";
	startIcon.shadow = "";
	startIcon.iconSize = new GSize(12, 20);
	startIcon.shadowSize = new GSize(11, 15);
	startIcon.iconAnchor = new GPoint(6, 20);
	startIcon.infoWindowAnchor = new GPoint(5, 1);

	var j;
	var i;
	
	
	
	for(j = 0; j < trails.length; j++){
		var trail = trails[j].trail;
		var trailId = trails[j].trailId;
		var points = [];
		for(i = 0; i < trail.length; i++){
			point = new GPoint(trail[i].lat, trail[i].lng)
			if(trail[i].isShortcut == true){
				var shortcut;
				if(i == 0){
					shortCut = new GMarker(point, startIcon);
				}else{
					shortCut = new GMarker(point, icon);
				}
				GEvent.addListener(shortCut, "click", new Function('jumpToLocation(' + i + ', \''+ trailId + '\');'));
				shortcuts.push(new Array(trailId, i, shortCut));
				//map.addOverlay(shortCut);
			}
			points.push(point);
		}
		map.addOverlay(new GPolyline(points, colors[j]));
	}
	renderMapShortcuts(shortcuts)
}

function renderMapShortcuts(){
	var newShortcutList = new Array();
	for(i = 0; i < shortcuts.length; i++){
		if(shortcuts[i][1] == 0){
			map.addOverlay(shortcuts[i][2]);
		}else if (mainTrail == shortcuts[i][0]){
			map.addOverlay(shortcuts[i][2]);
		}else{
			newShortcutList.push(shortcuts[i]);
		}
	}
	shortcuts = newShortcutList;
}

function changeResolution(highRes){
	trailState.highResolution = highRes;
	updateVirtualPanel();
}

function switchHints(show, src){
	if(show == false){
		if(confirm("By unchecking the \"Side Hints\" you won't be able to view the left and right hint boxes as you\nmove forward/backward in the trail. Do you want to proceed?") == false){
			src.checked = true;
			return;
		}
	}
	showHints = show;
	updateVirtualPanel();
}
	

function moveForward(trailId, point, orientation, direction, vOrientation){
	move(FORWARD, trailId, point, orientation, direction, vOrientation);
}

function moveBackward(trailId, point, orientation, direction, vOrientation){
	move(BACKWARD, trailId, point, orientation, direction, vOrientation);
}

function jumpToLocation(loc, trailId){
	moveToLocation(trailId, loc, FRONT, trailState.direction, STRAIGHT);
}

function moveToLocation(trailId, point, orientation, direction, vOrientation){
	if(setTrail(trailId, point, orientation, direction) == false){
		trailState.direction = direction;
		trailState.currentPoint = point;
		trailState.orientation = orientation;
		trailState.vOrientation = vOrientation;
	}	
	moveToCurrentPoint();
	generalUpdate();
}

function setTrail(trailId, point, orientation, direction, vOrientation){
	if(trailId != trailState.route.trailId){
		trailState.route = getRouteObject(trailId);
		mainTrail = trailId;
		if(window.changeTrailListener != null){
			changeTrailListener(trailId, point);
		}
		trailState.direction = direction;
		trailState.currentPoint = point;
		trailState.orientation = orientation;
		trailState.vOrientation = STRAIGHT;
		renderMapShortcuts();
		return true;
	}
	return false;
}

function move(action, trailId, point, orientation, direction, vOrientation){
	setCurrentLocation(trailId, point, orientation, direction, vOrientation);
	if(action == BACKWARD && trailState.direction == DECREMENT){
		action = FORWARD;
	}
	else if(action == FORWARD && trailState.direction == DECREMENT){
		action = BACKWARD;
	}
	if(action == FORWARD){
		if(trailState.currentPoint < trailState.route.trail.length - 1){
			trailState.currentPoint += 1;
		}
	}else{
		if(trailState.currentPoint > 0){
			trailState.currentPoint -= 1;
		}
	}
	
	generalUpdate();
}

function updateVirtualPanel(){
	var file = getFileForRoute(trailState.route, trailState.currentPoint, trailState.orientation, trailState.direction, trailState.vOrientation);
	var image = file + "_sm.jpg";
	var highResImage = file + ".jpg";
	var descriptionFile = file + ".htm";
		
	width = document.getElementById("virtualPhoto").clientWidth;
	height = Math.ceil((width / 1024) * 768);
	
	if(trailState.orientation == FRONT){
		var increment = 1;
		if(trailState.direction == DECREMENT){
			increment = -1;
		}
		var preloadedPoint = trailState.route.getTrailPoint(trailState.currentPoint+increment);
		if(preloadedPoint != null){
			var preLoadedImage = getFileForRoute(trailState.route, trailState.currentPoint+increment, trailState.orientation, trailState.direction, trailState.vOrientation);
			if(trailState.highResolution){
				document.getElementById("preloadedimage").src = preLoadedImage + ".jpg";
			}else{
				document.getElementById("preloadedimage").src = preLoadedImage + "_sm.jpg";
			}
		}
		
		
	}
	trailState.currentImage = highResImage;
	trailState.currentDescriptionFile = descriptionFile;
	
	
	
	if(trailState.highResolution){
		document.getElementById("virtualPhoto").innerHTML = "<img id=\"virtualimage\" border=\"1\" width=\""+ width +"\" height=\""+height+"\" src=" + highResImage + ">";
	}else{
		document.getElementById("virtualPhoto").innerHTML = "<img id=\"virtualimage\" border=\"1\" width=\""+ width +"\" height=\""+height+"\" src=" + image + ">";
	}
	
	//updateHints(trailState.route, trailState.currentPoint, trailState.orientation, trailState.direction);
		
	//updateDescription(trailState.currentDescriptionFile);
	if(firstLoad){
		var timer = new Timer();
		timer.setTimeout("updateDescription", 100, trailState.route, trailState.currentPoint, trailState.orientation, trailState.direction, trailState.vOrientation);
	}else{
		updateDescription(trailState.route, trailState.currentPoint, trailState.orientation, trailState.direction, trailState.vOrientation);
	}
	
	
	if(trailState.vOrientation != UP && window.updateAds){
		//var timer = new Timer();
		//timer.setTimeout("updateAds", 10, file);
		//updateAds(file);
	}
}

function updateHints(html, route, point, orientation, direction){
	var width = document.getElementById("virtualPhoto").clientWidth;
	var rightHint = document.getElementById("rightHint");
	var leftHint = document.getElementById("leftHint");
	
	leftHint.style.display="none";
	rightHint.style.display="none";
	
	if(orientation == FRONT && showHints){
		//var tPoint = route.getTrailPoint(point);
		var left = "";
		var right = "";
		//var url = "sideDescriptions.php?sequence="+tPoint.sequence+"&direction="+direction+"&trailId="+route.trailId;
		//var html = loadHtmlDoc(url);
		html = trim(html);
	
		if(html != ""){
			//html = html.substring(DESCRIPTION_START.length);
			var sides = html.split(";");
			
			for(i = 0; i < sides.length; i++){
				var values = sides[i].split(":");
				if(values[0] == "LEFT"){
					left = values[1];
				}else if(values[0] == "RIGHT"){
					right = values[1];
				}
			}
		}
		var x = getX(document.getElementById("virtualimage"));
		var y = getY(document.getElementById("virtualimage"));
		
		if(left != ""){
			leftHint.style.left = (x + 10) + "px";
			leftHint.style.top = (y + 10) + "px"; 
			leftHint.style.display="block";
			if(left.length > 41){
				left = left.substring(0, 42) + "...";
			}
			document.getElementById("leftHintText").innerHTML = "<img align=\"left\" src=\"http://www.viewacity.com/images/leftarrow.gif\">&nbsp;" + left;
		}
		if(right != ""){
			rightHint.style.left = (x + width - (150 + 10)) + "px";
			rightHint.style.top = (y + 10) + "px"; 
			rightHint.style.display="block";
			if(right.length > 41){
				right = right.substring(0, 42) + "...";
			}
			document.getElementById("rightHintText").innerHTML = "<img align=\"right\" src=\"http://www.viewacity.com/images/rightarrow.gif\">&nbsp;" + right;
		}
	}
}

function getFile(trailId, point, orientation, direction, vOrientation){
	var route = getRouteObject(trailId);
	return getFileForRoute(route, point, orientation, direction, vOrientation);
}

function getFileForRoute(route, point, orientation, direction, vOrientation){
	var tPoint = route.getTrailPoint(point);
	var dir = tPoint.dir;
	var file = "";
	
	if(orientation == FRONT){
		file="front";
		if(direction == DECREMENT){
			file="back";
		}
	}else if(orientation == LEFT){
		file="left";
		if(direction == DECREMENT){
			file="right";
		}
	}else if(orientation == RIGHT){
		file="right";
		if(direction == DECREMENT){
			file="left";
		}
	}
	if(vOrientation == UP){
		file = file + "_up";
	}
	return "photos/" + dir + "/" + file;
}

//function updateDescription(descFile){
//	var html = loadHtmlDoc(descFile);
//	
//	if(html.substring(0, DESCRIPTION_START.length) == DESCRIPTION_START){
//		html = html.substring(DESCRIPTION_START.length + 1);
//	}else{
//		html = "";
//	}
//	document.getElementById("description").innerHTML = html;
//}

function updateDescription(route, point, orientation, direction, vOrientation){
	var tPoint = route.getTrailPoint(point);
	var url = "";
	var hints = "";
	if(commentMode == DESCRIPTION_MODE){
		url = "description.php?sequence="+tPoint.sequence+"&orientation="+orientation+"&direction="+direction+"&vOrientation="+vOrientation+"&trailId="+route.trailId;
	}else{
		url = "userReview.php?sequence="+tPoint.sequence+"&orientation="+orientation+"&direction="+direction+"&vOrientation="+vOrientation+"&trailId="+route.trailId;
	}
	var keywords = "";
	var html = loadHtmlDoc(url);
	html = trim(html);
	

	if(html.substring(0, DESCRIPTION_START.length) == DESCRIPTION_START){
		html = html.substring(DESCRIPTION_START.length);
		if(commentMode == DESCRIPTION_MODE){
			var values = html.split(DESCRIPTION_START);
			hints = values[0].substring(11);
			html = values[1].substring(13);
		}
			
                if(html != ""){
		    var keywordPos = html.indexOf(KEYWORD_SEPERATOR);
		    if(keywordPos > -1){
			keywords = html.substring(0, keywordPos);
			html = html.substring(keywordPos + KEYWORD_SEPERATOR.length);
                    }
		}
	}else{
		html = "";
	}
	
	document.getElementById("descriptionText").innerHTML = html;
	document.getElementById("keywordsText").innerHTML = keywords;

	updateHints(hints, route, point, orientation, direction);
	
}

function switchCommentMode(newMode){
	if(newMode == commentMode)return;
	
	if(newMode == DESCRIPTION_MODE){
		document.getElementById("descriptionLink").className = "selectedComment";
		document.getElementById("reviewLink").className = "unselectedComment";
		document.getElementById("commentUpdateLink").href = "javascript:editDescription();";
		document.getElementById("commentUpdateLink").innerHTML = "Edit";
		document.getElementById("commentEnlarge").style.display="block";
		commentMode = DESCRIPTION_MODE;
	}else{
		document.getElementById("descriptionLink").className = "unselectedComment";
		document.getElementById("reviewLink").className = "selectedComment";
		document.getElementById("commentUpdateLink").href = "javascript:addReview();";
		document.getElementById("commentUpdateLink").innerHTML = "Add Review";
		document.getElementById("commentEnlarge").style.display="none";
		commentMode = REVIEW_MODE;
	}
	updateDescription(trailState.route, trailState.currentPoint, trailState.orientation, trailState.direction, trailState.vOrientation);
}

function enlarge(includeImage){
	if(trailState.currentImage != ""){
		var tPoint = trailState.route.getTrailPoint(trailState.currentPoint);
		var url = "enlargeImage.php?";
		if(includeImage){
			url = url + "file=" + escape(trailState.currentImage) + "&";
		}
		url = url + "sequence="+tPoint.sequence+"&orientation="+trailState.orientation+ 
					"&direction="+trailState.direction+"&vOrientation="+trailState.vOrientation+"&trailId="+trailState.route.trailId;
		window.open(url, "_blank", "width=930,resizable=yes,scrollbars=yes");
	}
}

function setCurrentLocation(trailId, point, orientation, direction, vOrientation){
	if(setTrail(trailId, point, orientation, direction, vOrientation) == false){
		trailState.direction = direction;
		trailState.currentPoint = point;
		trailState.orientation = orientation;
		trailState.vOrientation = vOrientation;
	}
}
	

function resetToFront(trailId, point, orientation, direction, vOrientation){
	setCurrentLocation(trailId, point, FRONT, direction, vOrientation);
	generalUpdate();
}

function turnLeft(trailId, point, orientation, direction, vOrientation){
	setCurrentLocation(trailId, point, LEFT, direction, vOrientation);
	generalUpdate();
}

function turnRight(trailId, point, orientation, direction, vOrientation){
	setCurrentLocation(trailId, point, RIGHT, direction, vOrientation);
	generalUpdate();
}	

function lookUp(trailId, point, orientation, direction, vOrientation){
	setCurrentLocation(trailId, point, orientation, direction, UP);
	generalUpdate();
}

function lookStraight(trailId, point, orientation, direction, vOrientation){
	setCurrentLocation(trailId, point, orientation, direction, STRAIGHT);
	generalUpdate();
}

function turnAround(trailId, point, orientation, direction, vOrientation){
	
	if(trailState.direction == INCREMENT){
		setCurrentLocation(trailId, point, orientation, DECREMENT, vOrientation);
	}else{
		setCurrentLocation(trailId, point, orientation, INCREMENT, vOrientation);
	}
	generalUpdate();
}

function generalUpdate(){
	moveToCurrentPoint();
	updateVirtualPanel();
	updateNavPanel();
	
	if(window.generalUpdateListener){	
		generalUpdateListener();
	}
}

function updateNavPanel(){
	var tPoint = trailState.route.getTrailPoint(trailState.currentPoint);
	
	if(trailState.vOrientation == UP){
		resetNavigationPanel();
		document.getElementById("navigationTable").style.backgroundImage="url(http://www.viewacity.com/images/streetUp.gif)"; 
		
		setCorner(document.getElementById("cell8"), "<span style=\"color:#FFFFFF;font-weight:bold\">Back</span>");
		document.getElementById("cell8_href").href = createHrefForCurrentLocation("lookStraight");
		return;
	}
	if(trailState.orientation == FRONT){
		resetCorner(document.getElementById("cell1"));
		resetCorner(document.getElementById("cell3"));
		resetCorner(document.getElementById("cell7"));
		resetCorner(document.getElementById("cell9"));
		
		if(document.getElementById("navigationTable").style.backgroundImage!="url(http://www.viewacity.com/images/streetFront.gif)"){ 
			document.getElementById("navigationTable").style.backgroundImage="url(http://www.viewacity.com/images/streetFront.gif)"; 
		}
		
		setCorner(document.getElementById("cell2"), getDirectionImage(tPoint.frontDir, tPoint.backDir, trailState.direction));
		document.getElementById("cell2_href").href = createHrefForCurrentLocation("moveForward");
		
		setCorner(document.getElementById("cell4"),createNavigationArrow("http://www.viewacity.com/images/left_arrow.gif"));
		document.getElementById("cell4_href").href = createHrefForCurrentLocation("turnLeft");
		
		setCorner(document.getElementById("cell6"), createNavigationArrow("http://www.viewacity.com/images/right_arrow.gif"));
		document.getElementById("cell6_href").href = createHrefForCurrentLocation("turnRight");
		
		setCorner(document.getElementById("cell8"), getDirectionImage(tPoint.backDir, tPoint.frontDir, trailState.direction));
		document.getElementById("cell8_href").href  = createHrefForCurrentLocation("moveBackward");
		
		setCorner(document.getElementById("cell5"), createNavigationArrow("http://www.viewacity.com/images/turn_around.gif"));
		document.getElementById("cell5_href").href = createHrefForCurrentLocation("turnAround");
	}
	if(trailState.orientation == LEFT){
		resetCorner(document.getElementById("cell2"));
		resetCorner(document.getElementById("cell4"));
		resetCorner(document.getElementById("cell5"));
		resetCorner(document.getElementById("cell6"));
		resetCorner(document.getElementById("cell7"));
		resetCorner(document.getElementById("cell9"));
		
		if(document.getElementById("navigationTable").style.backgroundImage!="url(http://www.viewacity.com/images/streetSideLeft.gif)"){
			document.getElementById("navigationTable").style.backgroundImage="url(http://www.viewacity.com/images/streetSideLeft.gif)"; 
			setCorner(document.getElementById("cell1"), createNavigationArrow("http://www.viewacity.com/images/side_left_arrow.gif"));
			setCorner(document.getElementById("cell3"), createNavigationArrow("http://www.viewacity.com/images/side_right_arrow.gif"));
			setCorner(document.getElementById("cell8"), "<span style=\"font-weight:bold\">Back</span>");			
		}
		document.getElementById("cell1_href").href = createHrefForCurrentLocation("moveBackward");
		document.getElementById("cell8_href").href = createHrefForCurrentLocation("resetToFront");	
		document.getElementById("cell3_href").href = createHrefForCurrentLocation("moveForward");
		
		var absoluteOrientation = UP_L;
		if(trailState.direction == DECREMENT){
			absoluteOrientation = UP_R;
		}
		if(tPoint.vOrientation == absoluteOrientation || tPoint.vOrientation == UP_L_R){
			setCorner(document.getElementById("cell2"), "<span style=\"font-weight:bold\">Up</span>");
			document.getElementById("cell2_href").href = createHrefForCurrentLocation("lookUp");
		}
	}
	if(trailState.orientation == RIGHT){
		resetCorner(document.getElementById("cell2"));
		resetCorner(document.getElementById("cell4"));
		resetCorner(document.getElementById("cell5"));
		resetCorner(document.getElementById("cell6"));
		resetCorner(document.getElementById("cell7"));
		resetCorner(document.getElementById("cell9"));
		
		if(document.getElementById("navigationTable").style.backgroundImage != "url(http://www.viewacity.com/images/streetSideRight.gif)"){
			document.getElementById("navigationTable").style.backgroundImage="url(http://www.viewacity.com/images/streetSideRight.gif)"; 
			setCorner(document.getElementById("cell1"), createNavigationArrow("http://www.viewacity.com/images/side_left_arrow.gif"));
			setCorner(document.getElementById("cell8"), "<span style=\"font-weight:bold\">Back</span>");
			setCorner(document.getElementById("cell3"), createNavigationArrow("http://www.viewacity.com/images/side_right_arrow.gif"));
		
		}
		document.getElementById("cell1_href").href = createHrefForCurrentLocation("moveForward");
		document.getElementById("cell3_href").href = createHrefForCurrentLocation("moveBackward");
		document.getElementById("cell8_href").href = createHrefForCurrentLocation("resetToFront");		
		
		var absoluteOrientation = UP_R;
		if(trailState.direction == DECREMENT){
			absoluteOrientation = UP_L;
		}
		if(tPoint.vOrientation == absoluteOrientation || tPoint.vOrientation == UP_L_R){
			setCorner(document.getElementById("cell2"), "<span style=\"font-weight:bold;\">Up</span>");
			document.getElementById("cell2_href").href = createHrefForCurrentLocation("lookUp");
		}
	}
}

function createHrefForCurrentLocation(functionName){
	return "javascript:"+functionName + "('" + trailState.route.trailId + "'," +  trailState.currentPoint + "," + trailState.orientation + ","  +
							trailState.direction + "," + trailState.vOrientation + ");";
}

function activateArrow(image){
	var url = image.src;
	var pos = url.lastIndexOf('.');
	image.src = url.substring(0, pos) + "_a" + url.substring(pos);
}		

function deactivateArrow(image){
	var url = image.src;
	var pos = url.lastIndexOf('_a.');
	image.src = url.substring(0, pos) + url.substring(pos + 2);
}

function createNavigationArrow(src){
	return "<img src=" + src + " onMouseOut='deactivateArrow(this);' onMouseOver='activateArrow(this);' >";
}

function getDirectionImage(current, inverse, direction){
	if(direction == DECREMENT){
		current = 5 - inverse;
	}
	if(current == F_L){
		return createNavigationArrow("http://www.viewacity.com/images/front_left_arrow.gif");
	}
	if(current == F){
		return createNavigationArrow("http://www.viewacity.com/images/front_arrow.gif");
	}
	if(current == F_R){
		return createNavigationArrow("http://www.viewacity.com/images/front_right_arrow.gif");
	}
	if(current == B_L){
		return createNavigationArrow("http://www.viewacity.com/images/back_left_arrow.gif");
	}
	if(current == B){
		return createNavigationArrow("http://www.viewacity.com/images/back_arrow.gif");
	}
	if(current == B_R){
		return createNavigationArrow("http://www.viewacity.com/images/back_right_arrow.gif");
	}
}

function setCorner(corner, html){
	if(corner.htmlTempValue == null || corner.htmlTempValue.toLowerCase() != html.toLowerCase()){
		var hrefhtml = "<a id=\""+corner.id + "_href\" href=\"void(0);\" >" + html + "</a>";
		corner.innerHTML = hrefhtml;
		corner.htmlTempValue = html;
		corner.style.cursor = "Hand";
	}
}
	
function resetCorner(corner){
	corner.innerHTML = "&nbsp;";
	corner.onclick = "";
	corner.htmlTempValue = "";
	corner.style.cursor="default";
}

function resetNavigationPanel(){
	var i;
	for(i = 1; i < 10; i++){
		resetCorner(document.getElementById("cell" + i));
	}
}

function trim(str){
   return str.replace(/^\s*|\s*$/g,"");
}

	

function initState(){
	trailState = new Object();
	trailState.direction = startDirection;
	trailState.orientation = startOrientation;
	trailState.vOrientation = STRAIGHT;
	trailState.currentPoint = startPoint;
	trailState.route = getRouteObject(mainTrail);
	trailState.currentImage = "";
	trailState.currentDescriptionFile = "";
	trailState.highResolution = false;
}

function getRouteObject(trailId){
	var i;
	for(i = 0; i < trails.length; i++){
		if(trails[i].trailId == trailId){
			return trails[i];
		}
	}
}

function createPointer(location){
	if(!icon){
	  icon = new GIcon();
	  icon.image = "http://www.viewacity.com/images/man.png";
	  icon.shadow = "";
	  icon.iconSize = new GSize(24, 38);
	  icon.shadowSize = new GSize(24, 38);
	  icon.iconAnchor = new GPoint(12, 38);
	  icon.infoWindowAnchor = new GPoint(5, 1);
	  currMarker = new GMarker(location, icon);
	  map.addOverlay(currMarker);
	  moveToCurrentPoint();
	}
}

function moveToCurrentPoint(){
	var trailPoint = trailState.route.getTrailPoint(trailState.currentPoint);
	var p = new GPoint(trailPoint.lat, trailPoint.lng);
	map.removeOverlay(currMarker);
	currMarker = new GMarker(p, icon);
	//currMarker.redraw(true);
	map.addOverlay(currMarker);
	map.recenterOrPanToLatLng(p);
}


function loadHtmlDoc(url, async, func, request){
	if(async){
		request.onreadystatechange = func;
		request.open('POST', url, true);
		request.send("");
		return;
	}
	var syncRequest = GXmlHttp.create();
	syncRequest.open('POST', url, false);
	syncRequest.send("");
	//request.send(null);
	return syncRequest.responseText;
}

function encodeURL(sStr) {
    return escape(sStr).
             replace(/\+/g, '%2B').
             replace(/\"/g,'%22').
             replace(/\'/g, '%27').
             replace(/\//g,'%2F');
}


function Route(name, trailId, trail, description, startPoint, startOrientation, startDirection){
	this.name = name;
	this.trailId = trailId;
	this.trail = trail;
	this.description = description;
	this.startPoint = startPoint;
	this.startOrientation = startOrientation;
	this.startDirection = startDirection;
}
Route.prototype.getTrailPoint = function(index){
	if(index >= this.trail.length || index < 0){
		return null;
	}
	return this.trail[index];
}

function TrailPoint(lng, lat, trailId, sequence, frontDir, backDir, vOrientation, shortCut){
	this.lng = lng;
	this.lat = lat;
	this.dir = trailId+'/'+sequence;
	this.sequence = sequence;
	this.frontDir = frontDir;
	this.backDir= backDir;
	this.vOrientation = vOrientation;
	this.isShortcut = shortCut;
}
	

