var map
var myPano
var panoClient 
//var latlng = new GLatLng(41.82184, -80.06582);

function fill_lat_long_accomm()
{
    var myaddr = document.getElementById("accommodation_address").value;
    var mycity = document.getElementById("accommodation_city").value;
    var mystate = "PA";
    var myzip = document.getElementById("accommodation_zip").value;
    var myaddress = myaddr + ", " + mycity + ", " + mystate + ", " + myzip;
    alert("address: " + myaddress);
    var geo = new GClientGeocoder();
    var pntlat;
    var pntlon;
    geo.getLatLng(myaddress,function(latlng) 
	{ 
	    if (!latlng) 
	    { 
		alert(myaddress + " not found"); 
		exit;
	    }
	    else
	    {
	      document.getElementById('accommodation_latitude').value = latlng.lat();
          document.getElementById('accommodation_longitude').value = latlng.lng();
 	    }
	});
}

function fill_lat_long_adventure()
{
    var myaddr = document.getElementById("adventure_address").value;
    var mycity = document.getElementById("adventure_city").value;
    var mystate = "PA";
    var myzip = document.getElementById("adventure_zip").value;
    var myaddress = myaddr + ", " + mycity + ", " + mystate + ", " + myzip;
    alert("address: " + myaddress);
    var geo = new GClientGeocoder();
    var pntlat;
    var pntlon;
    geo.getLatLng(myaddress,function(latlng) 
	{ 
	    if (!latlng) 
	    { 
		alert(myaddress + " not found"); 
		exit;
	    }
	    else
	    {
	      document.getElementById('adventure_latitude').value = latlng.lat();
          document.getElementById('adventure_longitude').value = latlng.lng();
 	    }
	});
}

function fill_lat_long_event()
{
    var myaddr = document.getElementById("event_address").value;
    var mycity = document.getElementById("event_city").value;
    var mystate = "PA";
    var myzip = document.getElementById("event_zip").value;
    var myaddress = myaddr + ", " + mycity + ", " + mystate + ", " + myzip;
    alert("address: " + myaddress);
    var geo = new GClientGeocoder();
    var pntlat;
    var pntlon;
    geo.getLatLng(myaddress,function(latlng) 
	{ 
	    if (!latlng) 
	    { 
		alert(myaddress + " not found"); 
		exit;
	    }
	    else
	    {
	      document.getElementById('event_latitude').value = latlng.lat();
          document.getElementById('event_longitude').value = latlng.lng();
 	    }
	});
}

function fill_lat_long_shop()
{
    var myaddr = document.getElementById("shop_address").value;
    var mycity = document.getElementById("shop_city").value;
    var mystate = "PA";
    var myzip = document.getElementById("shop_zip").value;
    var myaddress = myaddr + ", " + mycity + ", " + mystate + ", " + myzip;
    alert("address: " + myaddress);
    var geo = new GClientGeocoder();
    var pntlat;
    var pntlon;
    geo.getLatLng(myaddress,function(latlng) 
	{ 
	    if (!latlng) 
	    { 
		alert(myaddress + " not found"); 
		exit;
	    }
	    else
	    {
	      document.getElementById('shop_latitude').value = latlng.lat();
          document.getElementById('shop_longitude').value = latlng.lng();
 	    }
	});
}
        
function directionsto(strTo)
{
    dirto = document.getElementById('toadd');
    dirto.value = strTo;
}

function directionsfrom(strFrom)
{
    dirfrom = document.getElementById('fromadd');
    dirfrom.value = strFrom; 
}

function createMarkerEvent(lat,lng,html)
{
    html=unescape(html.replace(/\+/gm,' '));
    var point = new GLatLng(lat,lng, true) 
    if(point)
    {
    	var blueIcon = new GIcon(G_DEFAULT_ICON);
        blueIcon.image = "/images/markers/camera.png";
        blueIcon.shadow = "/images/markers/cameraShadow.png"
        blueIcon.iconSize = new GSize(31,20);
        blueIcon.shadowSize = new GSize(41,20);
        blueIcon.iconAnchor = new GPoint(16,20);
        blueIcon.infoWindowAnchor = new GPoint(19,11);
        blueIcon.printImage = "/images/markes/printCamera.gif";
        blueIcon.mozPrintImage = "/images/markers/mozPrintCamera.gif";
        blueIcon.printShadow = "/images/markers/printCameraShadow.gif";      
    	markerOptions = { icon:blueIcon };    	
    	var marker = new GMarker(point,markerOptions);
    	
	   GEvent.addListener(marker, "click", function() 
    	{
	   	   marker.openInfoWindowHtml(html);
	   	   panoClient.getNearestPanorama(point, showPanoData);
         });
            map.addOverlay(marker); 
           // return marker;
     }
}


function createMarker2(lat,lng,html) 
{
    html=unescape(html.replace(/\+/gm,' '));
    var point = new GLatLng(lat,lng, true) 
    if(point)
    {
      	var redIcon = new GIcon(G_DEFAULT_ICON);
        redIcon.image = "/images/markers/suitcase.png";
        redIcon.shadow = "/images/markers/suitcaseShadow.png"
        redIcon.iconSize = new GSize(31,23);
        redIcon.shadowSize = new GSize(43,23);
        redIcon.iconAnchor = new GPoint(16,23);
        redIcon.infoWindowAnchor = new GPoint(19,11);
        redIcon.printImage = "/images/markes/printSuitcase.gif";
        redIcon.mozPrintImage = "/images/markers/mozPrintSuitcase.gif";
        redIcon.printShadow = "/images/markers/printSuitcaseShadow.gif";
        redIcon.imageMap = [20,0,21,1,21,2,22,3,25,4,26,5,27,6,28,7,28,8,28,9,28,10,28,11,28,12,28,13,28,14,28,15,28,16,28,17,28,18,28,19,27,20,26,21,25,22,5,22,4,21,3,20,2,19,1,18,1,17,1,16,1,15,1,14,1,13,1,12,1,11,1,10,1,9,1,8,2,7,2,6,3,5,5,4,8,3,8,2,9,1,10,0];
    	markerOptions = { icon:redIcon }; 
    	var marker = new GMarker(point, markerOptions);
	   GEvent.addListener(marker, "click", function() 
    	{
	   	   marker.openInfoWindowHtml(html);
	   	   panoClient.getNearestPanorama(point, showPanoData);
         });
            map.addOverlay(marker); 
           // return marker;
     }
}
        
function newdirections()
{
	//v3
	// map = new google.maps.Map(document.getElementById("map"), myOptions);

	//v2
	map = new GMap2(document.getElementById("map"));
	strFrom = document.getElementById('fromadd').value;
	strTo = document.getElementById('toadd').value;
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(41.82184, -80.06582), 9);
	//createMarker(strFrom);
	//createMarker(strTo);            
	document.getElementById("directions").innerHTML = "";			
	var gdir=new GDirections(map, document.getElementById("directions"));

         // === Array for decoding the failure codes ===
	var reasons=[];
	reasons[G_GEO_SUCCESS]            = "Success";
	reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
	reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
	reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
	reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
	reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
	reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
	reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
	reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
	reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";

	// === catch Directions errors ===
	GEvent.addListener(gdir, "error", function() 
	{
        	var code = gdir.getStatus().code;
        	var reason="Code "+code;
        	if (reasons[code]) {
        	reason = reasons[code]
        } 
        alert("Failed to obtain directions, "+reason);
	});
          
	gdir.load("from: " + strFrom + " to: " + strTo); 
	
    GEvent.addListener(gdir,"load",function()
    {
        var marker1 = gdir.getMarker(0);
        GEvent.addListener(marker1,"click",function()
        {
         //   marker1.openInfoWindowHTML(strFrom);
        	panoClient.getNearestPanorama(marker1.getPoint(), showPanoData);
        }
		);
		var marker2 = gdir.getMarker(1);
        GEvent.addListener(marker2,"click",function()
        {
          //  marker2.openInfoWindowHTML(strTo);
        	panoClient.getNearestPanorama(marker2.getPoint(), showPanoData);
        }
		);
    }
    );
        
}
        
// this is called when the page loads. 
// it initializes the map, and creates each marker
function initializemap(countyid) 
{

    map = new GMap2(document.getElementById("map"));
	//map = new google.maps.Map(document.getElementById("map"), myOptions);
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
    myPano = new GStreetviewPanorama(document.getElementById("streetview"));
    panoClient = new GStreetviewClient();  
    
	switch(countyid)
	{
	   case "1": //crawford
	       map.setCenter(new GLatLng(41.70686, -80.10594), 9);
	       break;
	   case "2": //erie
	       map.setCenter(new GLatLng(42.11032, -80.10030), 9);
	       break;
	   case "3": //mercer
	       map.setCenter(new GLatLng(41.24654, -80.22679), 9);
	       break;
	   case "4": //venango
	       map.setCenter(new GLatLng(41.623277, -79.677064), 9);
	       break;
	   default: 
         map.setCenter(new GLatLng(41.82184, -80.06582), 9);
	}

} 


function createMarker(address)
{
    var geo = new GClientGeocoder();
    var pnt = geo.getLatLng(address,
	function(latlng) 
	{ 
	    if (!latlng) 
	    { 
		alert(address + " not found"); 
	    } 
	    else 
	    { 
		var marker = new GMarker(latlng); 
		map.addOverlay(marker); 
		GEvent.addListener(marker,"click",
		    function()
		    {
			 marker.openInfoWindowHTML(address);
			 panoClient.getNearestPanorama(point, showPanoData);
		    }
		);
	    } 
	} 
    );
}
 function showPanoData(panoData) 
 {
    switch(panoData.code) 
    {
        case 200:
            myPano.setLocationAndPOV(panoData.location.latlng);
            break;
        case 500:
            alert('Server Error Retrieving Streetview Data.')
            break;
        case 600:
            document.getElementById('streetview').innerHTML = "Streetview data not available";
            break;
        default:
            myPano.setLocationAndPOV(panoData.location.latlng);
        
    }

}