// Copyright New York State Thruway Authority. All rights reserved.
var map;    //contains map div location
var mapdir; //contains map div location for the directions page
var markers; //info from xml file, needs xml parse to use
var mArray = new Array(); //ordered list by distance, holds marker objects for each location
var foundpoints = new Array(); //ordered list by distance, holds location info ie cname, address
var leftNavArray = new Array(); //holds each location found w/links to open infowindows
var bounds; //holds center of map view that will show all found locations/markers
var ezotgAddr; //holds address of destination E-ZPass OTG Location
var companyname;  //company name text
var formattedStartAddr; //Returned from geocoder, starting address correctly formatted
var startAddrPoint; //Returned from geocoder, starting address GLatLng object
var foundLocations=false; //=true locations were found near start addr, =false when we show them closest points
var saveMapCenter;  //Holds center of map when they open info window, so we can move them back
var userZoomedorDragged=false;  // =true when user moves map themselves
/************************** END GLOBAL VARIABLES ********************************/
/************************** START code for directions.html **********************/
function load_mapdir() {
  var keyValuePair;
  mapdir = new GMap2(document.getElementById("map_canvas"));
  mapdir.setCenter(new GLatLng(43.0207, -76.1682), 7);  //Set centerpoint and scale of mapdir
  mapdir.addControl(new GSmallMapControl());  //Add small zoom mapdir control
  mapdir.addControl(new GMapTypeControl());   //Add Hybrid and satellite availability    
  mapdir.addControl(new GScaleControl());     //Add mapdir Scale   
  mapdir.enableDoubleClickZoom();             //Enable a Double click zoom in and out
  mapdir.enableContinuousZoom();
  var mini = new GOverviewMapControl();       //Set up overview control
  mapdir.addControl(mini);                    //Add overview control 
  mini.hide(); 
  
  //check URL params
  if (location.search) {
    var queryString = location.search;
    queryString = queryString.replace(/%20/gi," "); // replace %20 with spaces
    // split the query string
    var fromAddress = queryString.substring(queryString.indexOf("?p1=") +4, queryString.indexOf("&p2="));
    ezotgAddr = queryString.substring(queryString.indexOf("&p2=") +4, queryString.indexOf("&p3=")); // global
    var ezotgPoint = queryString.substring(queryString.indexOf("&p3=") +4, queryString.indexOf("&p4="));
    companyname = queryString.substring(queryString.indexOf("&p4=") +4); //global
    showLoadingBox("true");
	loadDirections(fromAddress, ezotgAddr, ezotgPoint);
  } else {
	//Hide the print link, bc of the error there is nothing to print
	document.getElementById("print").style.visibility="hidden";  
    document.getElementById('dirpanel').innerHTML = "<p>There was a problem calculating directions.</p><p>Code: no query string.</p>";
	document.getElementById('dirpanel').innerHTML += "<p><a href='javascript:window.close();'>Close Window</a></p>"; 
  } // end if location.search
} // end of function
//*********************************************************************************
function loadDirections(fromIn, toIn, pointIn) {
  var userInfo="";
  var directionsPanel = document.getElementById("dirpanel");
  var directions = new GDirections(mapdir, directionsPanel); 
  
  document.getElementById('start').innerHTML = fromIn;
  document.getElementById('end').innerHTML = "<img style='display:inline;' align='left' alt='"+ companyname+"' title='"+ companyname+"' src='" +getLogo(companyname,1)+ "' />&nbsp;" + companyname + "<br />&nbsp;" + toIn;

  GEvent.addListener(directions, "load", function() {
    document.getElementById("dirpanel").innerHTML = "";
  }); 
  GEvent.addListener(directions, "addoverlay", function() {
    showLoadingBox("false");
  }); 
  GEvent.addListener(directions, "error", function() {
	// hopefully we will not have an error
    showLoadingBox("false");
	var reasonText = getReasonTxt(directions.getStatus().code);
	//Hide the print link, bc of the error there is nothing to print
    document.getElementById('dirpanel').innerHTML = "<p><strong>There was a problem calculating directions, please check the starting address and try again.</strong></p>";
    document.getElementById('dirpanel').innerHTML += "<p>"+reasonText+"</p>";	
    document.getElementById('dirpanel').innerHTML += "<p>"+"from: "+fromIn+ " to: " + toIn + " @ " +pointIn+"</p>";	
  });
  
  directions.clear(); //cancels any previous load
  // use @lat/long as it gives us better directions in cases that the map data is wrong
  directions.load("from: "+fromIn+ " to: " + toIn + " @ " +pointIn);
} // end of function
//*********************************************************************************
function hidePrintMap() {
 if (document.getElementById("map_canvas").style.visibility == "visible") {
   document.getElementById('hidemap').innerHTML = "Show Map";
   document.getElementById("map_canvas").style.visibility="hidden";
   document.getElementById("map_canvas").style.width="0";
   document.getElementById("map_canvas").style.height="0";
   document.getElementById("dirpanel").style.width="95%";
  } else {
    document.getElementById("map_canvas").style.visibility="visible";
    document.getElementById("map_canvas").style.width="58%";
    document.getElementById("map_canvas").style.height="400px";
    document.getElementById("dirpanel").style.width="40%";
    document.getElementById('hidemap').innerHTML = "Hide Map";
  }
} // end of function
/************************** END code for directions.html *************************/
//*********************************************************************************
function TWYBrowserIsCompatible() {
  // special function to detect IE5.x because google's function does not flag
  // IE 5.x as incompatible.
  var version=0;
  if (navigator.appVersion.indexOf("MSIE")!=-1) { // check for IE
    var temp=navigator.appVersion.split("MSIE");
    version=parseFloat(temp[1]);
  }
  if (version>=5.0 && version<=5.5) {
	return(false);
  }
  return(true);
}  //end function
//*********************************************************************************
function load() {
  if (TWYBrowserIsCompatible() && GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(new GLatLng(43.0207, -76.1682), 7);  //Set centerpoint and scale of map
	map.addControl(new GLargeMapControl());  //Add zoom map control
    map.addControl(new GMapTypeControl());   //Add Hybrid and satellite availability    
    map.addControl(new GScaleControl());     //Add Map Scale   
    map.enableDoubleClickZoom();             //Enable a Double click zoom in and out
	map.enableContinuousZoom();
	var mini = new GOverviewMapControl();    //Set up overview control
	map.addControl(mini);                    //Add overview control 
	mini.hide();                             //minimize overview
    map.clearOverlays();
	
	GEvent.addListener(map,"infowindowopen", function() {
	  saveMapCenter = map.getCenter();
	});
	GEvent.addListener(map,"infowindowclose", function() {
	  if (userZoomedorDragged == false) {
	    map.panTo(saveMapCenter); // pan back to saved center
	  }
    }); 
	GEvent.addListener(map, "zoomend", function() {
 	  userZoomedorDragged = true;
    }); 
	GEvent.addListener(map, "dragend", function() {
 	  userZoomedorDragged = true;
    }); 
	
  } else {
	document.forms['leftNavDirections'].saddr.disabled = true;
	document.forms['leftNavDirections'].miles.disabled = true;
	document.forms['leftNavDirections'].sbutton.disabled = true;
	document.getElementById("map_canvas").innerHTML = "<p>Having a problem viewing this map-based page?  <a href='http://www.e-zpassny.com/static/onthego/onthegolocations.shtml'>A text version is available on www.e-zpassny.com.</a></p>";
	alert("This section of our website uses the Google Maps API which is not compatible with your browser version.  We recommend that you upgrade to the latest version of your browser or use a newer standards compliant browser.  However, if you are not able to upgrade your browser you can use our 'Text View' page instead.");
  }   // end if broswer compatible 
}	//end function load
//*********************************************************************************
function showLoadingBox(strShowbox) {
  if (strShowbox == "true") {
    document.getElementById("loadingbox").style.visibility="visible";
  } else {
    document.getElementById("loadingbox").style.visibility="hidden";
  } // end if strShowbox
} // end of function
// *****************************************************************************		
function ltrim(strIn) { return strIn.replace(/^\s+/,""); } //regexp
// *****************************************************************************		
function rtrim(strIn) { return strIn.replace(/\s+$/,""); } //regexp
// *****************************************************************************		
function showLocation() {
  var leftNavInfo = "";
  var reasonText = null;
  var address = document.forms['leftNavDirections'].saddr.value;
  var milesFrom = document.forms['leftNavDirections'].miles.value;
  address = rtrim(ltrim(address)); //trim spaces
  
  if (!address) {
	alert("Please enter an Address");
	document.forms['leftNavDirections'].saddr.focus;
  } else {
    showLoadingBox("true");
    var geocoder = new GClientGeocoder();
    geocoder.getLocations(address, function(geoLatLng) {
      if ((geoLatLng) && (geoLatLng.Status.code == 200)) {
	    map.clearOverlays();
	    if (geoLatLng.Placemark.length > 1) {
	      leftNavInfo = "<p><strong>Did You Mean?</strong></p>";
		  for (var i=0; i < geoLatLng.Placemark.length; i++) {  
		    leftNavInfo += (i+1) + ". <a href='javascript: document.forms[&quot;leftNavDirections&quot;].saddr.value=\"" + geoLatLng.Placemark[i].address + "\"; showLocation();'>" + geoLatLng.Placemark[i].address + "</a><br /><br />"; 
		   }
           showLoadingBox("false");
	       document.getElementById("directions").innerHTML = leftNavInfo;		  
	    } else {
          var place = geoLatLng.Placemark[0];
          startAddrPoint = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
		  formattedStartAddr= place.address;
		  findNearestLocations(milesFrom, address);
        } // end if Placemark.length > 1
      } else {
        showLoadingBox("false");
		reasonText = getReasonTxt(geoLatLng.Status.code);
	    alert("We were unable to locate '" +address+ "'.\n\n" +reasonText+"\n\nPlease check and re-enter the address.");
	  } // end if Status.code
    }); //end geocoder function
  } // end if !address
} /* end of function */
/*********************************************************************************/
function findNearestLocations(inMilesFrom, inAddress) {
  var x=0;
  var directionsHTML = "";
  // opera fails on 2nd read of file if in cache already, needs to be unique?
  var xmlFile = "../../xml/netdata/ezpassotg.xml?" + new Date().getTime();
  //GDownloadUrl("ezpassgofix.xml", function(data, responseCode) {
  GDownloadUrl(xmlFile, function(data, responseCode) {
    // responseCode returns 0 in local file mode
    if (responseCode == 200 || responseCode == 0) {										   
	  var xml = GXml.parse(data);
	  markers = xml.getElementsByTagName("otg_location");
      for (var i = 0; i < markers.length; i++) {
	    var lat = markers[i].getElementsByTagName("latitude_y")[0].firstChild.data;
		var lng = markers[i].getElementsByTagName("longitude_x")[0].firstChild.data;
		var live = markers[i].getElementsByTagName("live_ind")[0].firstChild.data;
		var ezotgPoint = new GLatLng(lat,lng);
	    var metersFromStart = startAddrPoint.distanceFrom(ezotgPoint);
		if ((metersFromStart <= (inMilesFrom *1609.344)) && (live.toUpperCase() == "Y")) {
		  x++;  // keep track of how many points we have  
		  if (x == 1) {
		    document.getElementById("directions").innerHTML = "";
		    directionsHTML = "";
			foundpoints = [];  //reset arrays
			mArray = [];
   		  }   
		  var cname = markers[i].getElementsByTagName("company_name")[0].firstChild.data;
		  var address = markers[i].getElementsByTagName("address")[0].firstChild.data;
		  var city = markers[i].getElementsByTagName("city")[0].firstChild.data;
		  var zip = markers[i].getElementsByTagName("zip")[0].firstChild.data;
		  directionsHTML =  address + "<br />" + city + ", NY " + zip + " "; 
		  addToObjArray(metersFromStart, i, directionsHTML, ezotgPoint, cname);
		} // end if within meters
	  } // end for loop
	  if (x > 0) { // check if we found any
        foundLocations = true;
		// sort array of objects asc by the metersFromStart property
		foundpoints.sort(sortBymetersFromStart);  
		buildLeftNavArray();
		markerLoop(0);
	  } else {
        showLoadingBox("false");
	    var answeredOK = confirm("No E-ZPass On-the-Go locations were found within " +inMilesFrom+ " miles of '" +inAddress+ "'\n\nClick OK to show nearest location(s)\nClick Cancel to change your search")
		if (answeredOK) { 
          showLoadingBox("true");
          foundLocations = false;
		  findClosestInstead(); 
		} else{
		  document.forms['leftNavDirections'].saddr.focus;
		}
	  } // end if x > 0
      showLoadingBox("false");
	} else {
      showLoadingBox("false");
	  alert("E-ZPass On-the-Go information is not available at this time.\nCode: "+ responseCode);
	} // end if responseCode
  }); //end of GDownloadURL
} /* end of function */
//*********************************************************************************
function buildLeftNavArray() {
  for (var i=0; i<foundpoints.length; i++) {
	var sideLink1 = "<a href='javascript:openInfoWin(" +i+ ");'>" + foundpoints[i].cname + "</a>";
	var sideLink2 = "<br /><a href='javascript:openInfoWin(" +i+ ");'>Directions</a>";
	leftNavArray[i] = sideLink1+ "<br />"  +foundpoints[i].directionsHTML+ " " +sideLink2+ "<br />";
  } // end for
}  // end of function
//*********************************************************************************
function findClosestInstead() {
  var tmpPoint=null;
  var tmpDistance=null;
  
  // find the ezpotg location that is closest to the starting address
  for (var i=0; i < markers.length; i++) {
    var lat = markers[i].getElementsByTagName("latitude_y")[0].firstChild.data;
	var lng = markers[i].getElementsByTagName("longitude_x")[0].firstChild.data;
	var live = markers[i].getElementsByTagName("live_ind")[0].firstChild.data;
	var ezotgPoint = new GLatLng(lat,lng);
	var metersFromStart = startAddrPoint.distanceFrom(ezotgPoint);
    if (live.toUpperCase() == "Y") {
	  if (i == 0) {
	    tmpPoint = ezotgPoint;
	    tmpDistance = metersFromStart;
	  } else {
	    if (metersFromStart < tmpDistance) {
	      tmpPoint = ezotgPoint;
	      tmpDistance = metersFromStart;
	    }
	  } // end if i==0
	} // end if live
  } // end of for

  var x=0;
  for (var i=0; i < markers.length; i++) {
    lat = markers[i].getElementsByTagName("latitude_y")[0].firstChild.data;
	lng = markers[i].getElementsByTagName("longitude_x")[0].firstChild.data;
	live = markers[i].getElementsByTagName("live_ind")[0].firstChild.data;
	ezotgPoint = new GLatLng(lat,lng);
    if ((tmpPoint.distanceFrom(ezotgPoint) <= (25 * 1609.344)) && (live.toUpperCase() == "Y")) {
	  x++;  // keep track of how many points we have  
	  if (x == 1) {
	    document.getElementById("directions").innerHTML = "";
		directionsHTML = "";
		foundpoints = [];  //reset arrays
		mArray = [];
	  } 
	  var cname = markers[i].getElementsByTagName("company_name")[0].firstChild.data;
	  var address = markers[i].getElementsByTagName("address")[0].firstChild.data;
	  var city = markers[i].getElementsByTagName("city")[0].firstChild.data;
	  var zip = markers[i].getElementsByTagName("zip")[0].firstChild.data;
      directionsHTML = address + "<br />" + city + ", NY " + zip + " ";
	  metersFromStart = startAddrPoint.distanceFrom(ezotgPoint);
	  addToObjArray(metersFromStart, i, directionsHTML, ezotgPoint, cname);
    } //end if .distanceFrom
  } //end for loop 

  if (x > 0) {  //theoretically x should always be > 0
    buildLeftNavArray();
    markerLoop(0);
  } else {
    showLoadingBox("false");
    alert("No E-ZPass On-the-Go locations were found.\nCode: 0");
  }
}  // end of function
//*********************************************************************************
function ezpotgObj(distance, index, html, point, name) { 
  // this is a special object function
  // add properties to the object
  this.metersFromStart = distance;
  this.i = index;
  this.directionsHTML = html;
  this.point = point;
  this.cname = name;
}  // end of function
//*********************************************************************************
function addToObjArray(distance, index, html, point, name) {
  foundpoints[foundpoints.length++] = new ezpotgObj(distance, index, html, point, name);
}  // end of function
//*********************************************************************************
function markerLoop(a) {
  var milesFrom = document.forms['leftNavDirections'].miles.value;
  var nextPrev = "";
  var leftNavInfo = "";
  var startIcon = new GIcon();		
  startIcon.iconSize = new GSize(23,34);
  startIcon.iconAnchor = new GPoint(11, 17);
  startIcon.infoWindowAnchor = new GPoint(9, 2);
  startIcon.image = "../../images/ezpass/otg/start.gif";
  var title =formattedStartAddr+ " [Starting Point]";
  document.getElementById("directions").scrollTop = 0;  //force the left nav to be at the top
  document.getElementById("directions").innerHTML = "";

  showLoadingBox("true");
  var startMarker = new GMarker(startAddrPoint, { icon: startIcon , title: title, clickable: false} );
  map.addOverlay(startMarker);	
  var num = 20 * a;
  var loopnum;
  if (foundpoints.length - num >= 20) {
	loopnum = 20;
  } else {
	loopnum = foundpoints.length -  num;
  }
	
  // build next-previous html for left nav
  if (a == 0) {
    if (foundpoints.length > 20) {  // Next link only
	  nextPrev += "<div style='text-align:center; margin:0; padding-bottom:2px;'><a href='javascript:void(0); map.clearOverlays(); markerLoop(1);'><span class='small'>Next 20</span></a></div><div class='hrbar' style='padding-bottom:5px;margin-bottom:5px;'></div>";		
	}
  } else { // a > 0
    if (foundpoints.length > (num + 20)) {  // Next and Previous links
	  nextPrev += "<div style='text-align:center; margin:0; padding-bottom:2px;'><span class='small'><a href='javascript:void(0); map.clearOverlays(); markerLoop(" + (a - 1) + ");'>Previous 20</a>|<a href='javascript:void(0); map.clearOverlays(); markerLoop(" + (a + 1) + ");'>Next 20</a></span></div><div class='hrbar' style='padding-bottom:5px;margin-bottom:5px;'></div>";	
	} else if (foundpoints.length <= (num + 20)) {  // only a previous link
	    nextPrev += "<div style='text-align:center; margin:0; padding-bottom:2px;'><a href='javascript:void(0); map.clearOverlays(); markerLoop(" + (a - 1) + ");'><span class='small'>Previous 20</span></a></div><div class='hrbar' style='padding-bottom:5px;margin-bottom:5px;'></div>";		
	}
  } // end if a==0
  
  // add markers to map
  // build otg locations html for left nav
  for (var i=0; i< loopnum; i++) {
    createMarkers((num + i), i);
	if (i == 0) {
	  bounds = new GLatLngBounds(foundpoints[i + num].point,foundpoints[i + num].point);
	  if (foundLocations) {
	    leftNavInfo = "<br/><div class='startaddrlnav'><img src='../../images/ezpass/otg/start.gif' width='23' height='34' alt='Starting Location' align='left' style='padding: 5px 10px 0px 5px;' />" +formattedStartAddr+ "</div>"; 
	  } else {
		leftNavInfo = "<br /><div class='nolocationslnav' width='225'><img src='../../images/ezpass/otg/warning.gif' alt=' ' width='28' height='28' align='left' hspace='1'/>No E-ZPass On-the-Go locations were found within " +milesFrom+ " miles of " +document.forms['leftNavDirections'].saddr.value+ ".<br />Here are the closest locations.</div>"; 
	  } // end if foundLocations
	  leftNavInfo += "<p style='padding-left:5px; margin:3px 0 8px 0;'><strong>" + foundpoints.length + " Location(s) Found<br />Displaying Locations " +(num+1)+ " - " + (loopnum+num) + "</strong></p>";
	  leftNavInfo += nextPrev;
	} else {
	  bounds.extend(foundpoints[i].point);  
	}  // end if i==0
	leftNavInfo += "<div style='float:left;'><a href='javascript: openInfoWin(" +(i + num)+ ");'><img src='../../images/ezpass/otg/ez" + (i + 1) + ".gif' height='20' width='20' alt='E-ZPass On-the-Go Location' /></a></div><div style='float:left; margin-left:3px;'>" + leftNavArray[i + num] + "</div><br style='clear:both;'/><br />";
  } //end for loop
  
  leftNavInfo += nextPrev;
  document.getElementById("directions").innerHTML = leftNavInfo; 
  showLoadingBox("false");
  var boundszoom = map.getBoundsZoomLevel(bounds, map.getSize());
  map.setCenter(bounds.getCenter(), boundszoom - 1);
  map.savePosition();	
} //end of function
//*********************************************************************************
function createMarkers(i, a) { //create unique markers 
  var baseIcon = new GIcon();
  baseIcon.iconSize = new GSize(20, 20);
  baseIcon.iconAnchor = new GPoint(10,10);
  baseIcon.infoWindowAnchor = new GPoint(9, 2); 		
  baseIcon.image = "../../images/ezpass/otg/ez" + (a + 1) + ".gif";	
  var title = foundpoints[i].cname + " " + foundpoints[i].directionsHTML.replace(/<br \/>/, " ");
  var markerOptions = { zIndexProcess: zind, icon: baseIcon , title: title};
  var marker = new GMarker(foundpoints[i].point, markerOptions);
  mArray.push(marker); 					
  map.addOverlay(marker);
	
  GEvent.addListener(marker, "click", function() {
     openInfoWin(i);
  }); //end of click listening function
  
  // Needed to create an infowindowopen eventlistener when we went to version 2.140
  GEvent.addListener(marker, "infowindowopen", function() {
     InfoWinListener(i);
  }); //end of click listening function
} //end of function
//*********************************************************************************
function zind(marker,b) { return 60000; }
//*********************************************************************************
function openInfoWin(a) { // creates PopupInfoWindow everytime a point is clicked
  var number = foundpoints[a].i;
  var point = foundpoints[a].point;
  var cname = markers[number].getElementsByTagName("company_name")[0].firstChild.data;
  var address = markers[number].getElementsByTagName("address")[0].firstChild.data;
  var city = markers[number].getElementsByTagName("city")[0].firstChild.data;
  var zip = markers[number].getElementsByTagName("zip")[0].firstChild.data;
  var destAddress = address + "<br /> " + city + ", NY, " + zip;
  ezotgAddr  = address + ", " + city + ", NY, " + zip; //global
  var label1 = "Location";
  var label2 = "Detail Map";
  userZoomedorDragged=false;
  
  var tab1 = "<div class='ezpotgbox' id='tab1'><div class='ezpotgboxtitle'>E-ZPass On-the-Go Location</div><div style='font-size: 80%; padding:5px 5px 0 5px;' ><img src='" +getLogo(cname,1)+ "' alt=' ' style='float:right;' ;/><div style='text-align:left; margin-left:10px; width:220px; padding:0;'><strong>" + cname + "</strong><br />" + destAddress + "</div><hr /><div style='text-align:left; margin-left:10px; padding:0; '><strong>Get Directions</strong><br /><form name='Address' action='javascript:void(0);' onsubmit='javascript:void(0);' style='padding:0; margin:0;'><label for='startaddressinfowin'>Enter Start Address:<br /><small style='color:gray;'>e.g. \"10 main st, albany, ny, 12345\"</small></label><br /><input tabindex='4' type='text' id='startaddressinfowin' name='saddr' size='30' maxlength='255' value='" + formattedStartAddr + "'/><br /><input tabindex='5' type='submit' value='Get Directions' onclick=\'popupDirWin(" +a+ "); map.closeInfoWindow();' /></form></div></div></div><div>";
  
  var tab2 = "<div class='ezpotgbox'><div id='detailmap' style='width:300px; height:220px;'></div></div>";
  
  //open info window
  mArray[a].openInfoWindowTabsHtml([new GInfoWindowTab(label1,tab1), new GInfoWindowTab(label2,tab2)],{selectedTab:0});
  
} //end openInfoWin() function
//*********************************************************************************
// InfoWindow Listener - Needed to create an eventlistener when we went to version 2.140
function InfoWinListener(a) { 
  var point = foundpoints[a].point;
  //create a new map for detail tab, needed to specify size
  var detailmap = new GMap2(document.getElementById("detailmap"), {size: new GSize(parseInt(document.getElementById("detailmap").style.width), parseInt(document.getElementById("detailmap").style.height))});
  detailmap.setCenter(point, 15);
  detailmap.addControl(new GSmallMapControl()); //Add zoom map control
  detailmap.addControl(new GMapTypeControl()); //Add Hybrid and satellite availability    
  detailmap.enableDoubleClickZoom(); //Enable a Double click zoom in and out
  detailmap.enableContinuousZoom(); //Enable continuous zooming from one level to next one
  var detIcon = new GIcon();		
  detIcon.iconSize = new GSize(20, 20);
  detIcon.iconAnchor = new GPoint(10, 10);
  detIcon.infoWindowAnchor = new GPoint(9, 2);
  detIcon.image = "../../images/ezpass/otg/ez.gif";
  var detmarker = new GMarker(point, { icon: detIcon , clickable: false } );
  detailmap.addOverlay(detmarker);	

  // get tab1 size, need to use offsetHeight because height is not defined in style.
  var tab1Height = document.getElementById('tab1').offsetHeight;
  // get detailmap height, height is defined in style
  var detailmapHeight = parseInt(document.getElementById("detailmap").style.height);
  // check sizes and resize detailmap to the size of the first tab, which could change height with word wrapping
  // make sure you compare pixels to pixels
  if (tab1Height > detailmapHeight) {
     document.getElementById("detailmap").style.height = tab1Height + "px";
     // need to make sure map tab is active before checkResize() to work correctly
     map.getInfoWindow().selectTab(1);
     detailmap.checkResize();
     // switch back to first tab after checkResize()
     map.getInfoWindow().selectTab(0);
  // check sizes and resize tab1 to the size of the detailmap if needed
  } else if (tab1Height < detailmapHeight) {
	 document.getElementById("tab1").style.height = detailmapHeight + "px";
  }  // end if
} //end InfoWinListener() function
//*********************************************************************************
function popupDirWin(a) { 
  //if user changes starting location in the info window, need to update this variable
  if (formattedStartAddr != document.forms['Address'].saddr.value) {  
    formattedStartAddr = rtrim(ltrim(document.forms['Address'].saddr.value)); //trim spaces
  }  
  // parameters in URL need to be kept in order, p1, p2, p3, p4
  var url = "directions.html?p1=" +formattedStartAddr+ "&p2=" +ezotgAddr+ "&p3=" +
      foundpoints[a].point.toUrlValue() + "&p4=" +foundpoints[a].cname;
  var w=window.open(url,"Directions","scrollbars=yes,menubar=yes,height=590,width=790,top=0,left=0,resizable=yes,toolbar=no,location=no,status=no");  
  w.focus();  // need to set focus because window can be minimized
} // end of function
//*********************************************************************************
function sortBymetersFromStart(a, b) {
  var x = a.metersFromStart;
  var y = b.metersFromStart;
  return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}  // end of function
//*********************************************************************************
function leftNav_hide() {
  // called from index.html file
  // style needs to be inline in the HTML file for this to work!
  document.getElementById('dir_container').style.display = "none"; 
  document.getElementById('map_canvas').style.width = "97%";
  document.getElementById("hidepanel").style.display = "none";
  document.getElementById("showpanel").style.display = "block";
}  // end of function
//*********************************************************************************
function leftNav_show() {
  // called from index.html file
  // style needs to be inline in the HTML file for this to work!
  document.getElementById('map_canvas').style.width = "72%";	
  document.getElementById('dir_container').style.display = "block";
  document.getElementById("hidepanel").style.display = "block";
  document.getElementById("showpanel").style.display = "none";
}  // end of function
//*********************************************************************************
function getLogo(cnameIn, size) {
  var image1="";
  var image2="";
  switch(cnameIn.toLowerCase()) {
	case '640 pittsford-victor llc':
	  image1 = "../../images/ezpass/otg/store/640pittsford_small.gif";
	  image2 = "../../images/ezpass/otg/store/640pittsford_big.gif";
	  break;
	case 'aaa hudson valley':
	  image1 = "../../images/ezpass/otg/store/aaanorthway_small.gif";
	  image2 = "../../images/ezpass/otg/store/aaanorthway_big.gif";
	  break;
	case 'aaa northway':
	  image1 = "../../images/ezpass/otg/store/aaanorthway_small.gif";
	  image2 = "../../images/ezpass/otg/store/aaanorthway_big.gif";
	  break;
	case 'aaa western & central ny':
	  image1 = "../../images/ezpass/otg/store/aaawestern_small.gif";
	  image2 = "../../images/ezpass/otg/store/aaawestern_big.gif";
	  break;
	case 'albany airport':
	  image1 = "../../images/ezpass/otg/store/albanyairport_small.gif";
	  image2 = "../../images/ezpass/otg/store/albanyairport_big.gif";
	  break;
	case 'catalano motors':
	  image1 = "../../images/ezpass/otg/store/catalano_small.gif";
	  image2 = "../../images/ezpass/otg/store/catalano_big.gif";
	  break;
	case 'canandaigua motors':
	  image1 = "../../images/ezpass/otg/store/canandaigua_small.gif";
	  image2 = "../../images/ezpass/otg/store/canandaigua_big.gif";
	  break;
	case 'capital cities imported cars':
	  image1 = "../../images/ezpass/otg/store/capitalcities_small.gif";
	  image2 = "../../images/ezpass/otg/store/capitalcities_big.gif";
	  break;
	case 'byrne dairy':
	  image1 = "../../images/ezpass/otg/store/byrnedairy_small.gif";
	  image2 = "../../images/ezpass/otg/store/byrnedairy_big.gif";
	  break;
	case 'delta sonic':
	  image1 = "../../images/ezpass/otg/store/deltasonic_small.gif";
	  image2 = "../../images/ezpass/otg/store/deltasonic_big.gif";
	  break;
	case 'dutchess dodge inc':
	  image1 = "../../images/ezpass/otg/store/dutchess_small.gif";
	  image2 = "../../images/ezpass/otg/store/dutchess_big.gif";
	  break;
	case 'express mart':
	  image1 = "../../images/ezpass/otg/store/expressmart_small.gif";
	  image2 = "../../images/ezpass/otg/store/expressmart_big.gif";
	  break;
	case 'e-zpass service center':
	  image1 = "../../images/ezpass/otg/store/ezpasssc_small.gif";
	  image2 = "../../images/ezpass/otg/store/ezpasssc_big.gif";
	  break;
    case 'fastrac':
	  image1 = "../../images/ezpass/otg/store/fastrac_small.gif";
	  image2 = "../../images/ezpass/otg/store/fastrac_big.gif";
	  break;
    case 'friendly honda house':
	  image1 = "../../images/ezpass/otg/store/friendlyhonda_small.gif";
	  image2 = "../../images/ezpass/otg/store/friendlyhonda_big.gif";
	  break;
	case 'holt brothers':
	  image1 = "../../images/ezpass/otg/store/holt_small.gif";
	  image2 = "../../images/ezpass/otg/store/holt_big.gif";
	  break;
	case 'hudson pontiac buick':
	  image1 = "../../images/ezpass/otg/store/hudson_small.gif";
	  image2 = "../../images/ezpass/otg/store/hudson_big.gif";
	  break;
	case 'kk marts':
	  image1 = "../../images/ezpass/otg/store/kk_small.gif";
	  image2 = "../../images/ezpass/otg/store/kk_big.gif";
	  break;
	case 'nice n easy grocery shoppe':
	  image1 = "../../images/ezpass/otg/store/niceneasy_small.gif";
	  image2 = "../../images/ezpass/otg/store/niceneasy_big.gif";
	  break;
	case 'noco':
	  image1 = "../../images/ezpass/otg/store/noco_small.gif";
	  image2 = "../../images/ezpass/otg/store/noco_big.gif";
	  break;
	case 'price chopper':
	  image1 = "../../images/ezpass/otg/store/pricechopper_small.gif";
	  image2 = "../../images/ezpass/otg/store/pricechopper_big.gif";
	  break;
	case 'rana candy store':
	  image1 = "../../images/ezpass/otg/store/rana_small.gif";
	  image2 = "../../images/ezpass/otg/store/rana_big.gif";
	  break;
	case 'sugar creek':
	  image1 = "../../images/ezpass/otg/store/sugarcreek_small.gif";
	  image2 = "../../images/ezpass/otg/store/sugarcreek_big.gif";
	  break;
	case 'genesee chamber of commerce':
	  image1 = "../../images/ezpass/otg/store/geneseecoc_small.gif";
	  image2 = "../../images/ezpass/otg/store/geneseecoc_big.gif";
	  break;
	case 'george thompson agency':
	  image1 = "../../images/ezpass/otg/store/thompson_small.gif";
	  image2 = "../../images/ezpass/otg/store/thompson_big.gif";
	  break;
	case 'new york state thruway authority':
	  image1 = "../../images/ezpass/otg/store/thruway_small.gif";
	  image2 = "../../images/ezpass/otg/store/thruway_big.gif";
	  break;
	case 'village of churchville':
	  image1 = "../../images/ezpass/otg/store/churchville_small.gif";
	  image2 = "../../images/ezpass/otg/store/churchville_bigl.gif";
	  break;
	case 'village of webster':
	  image1 = "../../images/ezpass/otg/store/webster_small.gif";
	  image2 = "../../images/ezpass/otg/store/webster_big.gif";
	  break;
	case 'wegmans':
	  image1 = "../../images/ezpass/otg/store/wegmans_small.gif";
	  image2 = "../../images/ezpass/otg/store/wegmans_big.gif";
	  break;
	case 'wilson farms':
	  image1 = "../../images/ezpass/otg/store/wilsonfarms_small.gif";
	  image2 = "../../images/ezpass/otg/store/wilsonfarms_big.gif";
	  break;
	case 'wilson farms xpress':
	  image1 = "../../images/ezpass/otg/store/wilsonfarmsxpress_small.gif";
	  image2 = "../../images/ezpass/otg/store/wilsonfarmsxpress_big.gif";
	  break;
	case 'yates chamber of commerce':
	  image1 = "../../images/ezpass/otg/store/yatescoc_small.gif";
	  image2 = "../../images/ezpass/otg/store/yatescoc_big.gif";
	  break;
	default:
	  image1 = "../../images/ezpass/otg/store/default_small.gif";
	  image2 = "../../images/ezpass/otg/store/default_big.gif";
	  break;
  }
  return ((size == 1)?image1:image2);
}  // end of function
//*********************************************************************************
function getReasonTxt(codeIn) {
  var txt;
  switch(codeIn) {
	case G_GEO_SUCCESS:
      txt = "Success";  //200
	  break;
	case G_GEO_BAD_REQUEST:
	  txt = "A directions request could not be successfully parsed."; //400
	  break;
	case G_GEO_SERVER_ERROR:
	  txt = "Server error: The geocoding request could not be successfully processed.";  //500 
	  break;
	case G_GEO_MISSING_QUERY:
	  txt = "No query was specified in the input."; //601
	  break;
	case G_GEO_MISSING_ADDRESS:
	  txt = "Missing Address: The address was either missing or had no value."; //601
	  break;
	case G_GEO_UNKNOWN_ADDRESS:
	  txt = "Unknown Address:  No corresponding geographic location could be found for the specified address."; // 602
	  break;
	case G_GEO_UNAVAILABLE_ADDRESS:
	  txt = "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons."; //603
	  break;
	case G_GEO_UNKNOWN_DIRECTIONS:
	  txt = "The GDirections object could not compute directions between the points."; //604
	  break;
	case G_GEO_BAD_KEY:
	  txt = "Bad Key: The API key is either invalid or does not match the domain for which it was given."; //610
	  break;
	case G_GEO_TOO_MANY_QUERIES:
	  txt = "Too Many Queries: The daily geocoding quota for this site has been exceeded."; //620
	  break;
	default:
	  txt = "Code: "+codeIn+".";
	  break;
  }
  return txt;
} // end of function
//*********************************************************************************
// Copyright New York State Thruway Authority. All rights reserved.
