var map;

var icon1 = new GIcon();
icon1.image = "http://www.google.com/mapfiles/markerA.png";
icon1.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon1.iconSize = new GSize(20, 34);
icon1.shadowSize = new GSize(37, 34);
icon1.iconAnchor = new GPoint(10, 34);

var icon2 = new GIcon();
icon2.image = "http://www.google.com/mapfiles/markerB.png";
icon2.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon2.iconSize = new GSize(20, 34);
icon2.shadowSize = new GSize(37, 34);
icon2.iconAnchor = new GPoint(10, 34);


var geocoder, location1, location2, gDir, resultaddress1, resultaddress2;


	function initialize() {
		geocoder1 = new GClientGeocoder();
		geocoder2 = new GClientGeocoder();
		gDir = new GDirections();
		GEvent.addListener(gDir, "load", function() {
			drivingDistanceMiles = Math.round(gDir.getDistance().meters / 1609.344);
			drivingDistanceKilometers = Math.round(gDir.getDistance().meters / 1000);
	
			var poly = gDir.getPolyline();
			map.addOverlay(poly); 
			geocoder1.getLocations(resultpoint1, function(addresses1) {
				resultaddress1 = addresses1.Placemark[0].address;
				geocoder2.getLocations(resultpoint2, function(addresses2) {
				resultaddress2 = addresses2.Placemark[0].address;
				write_html();});
				});
			
			var latlngbounds = new GLatLngBounds();
			latlngbounds.extend(resultpoint1);
			latlngbounds.extend(resultpoint2);
			map.setCenter(latlngbounds.getCenter(), map.getBoundsZoomLevel(latlngbounds)-1);
												  });
	}
	
	function write_html()
	{
		//document.getElementById('results').innerHTML = '<strong>From:</strong> ' + resultaddress1 + '<br /><strong>To:</strong> ' + resultaddress2;
		document.getElementById('results').innerHTML = '<strong>From:</strong> ' + document.getElementById('from_input').value + '<br /><strong>To:</strong> ' + resultaddress2;
		document.getElementById('results2').innerHTML = '<strong>Driving Distance:</strong> ' + drivingDistanceMiles + ' miles (or ' + drivingDistanceKilometers + ' kilometers)';
		
		document.getElementById('egg_timer').style.display = 'none';

		//get_prices_flightlink(document.price_cfg.pax.value);
		calculate_prices(document.price_cfg.pax.value, drivingDistanceMiles);
	}
	
	function get_results(to_value)
	{
		document.getElementById('egg_timer').style.display = 'block';
	
		var localSearch1 = new GlocalSearch();
		var localSearch2 = new GlocalSearch();
		
		localSearch1.setSearchCompleteCallback(null, 
			function() {
				
				if (localSearch1.results[0])
				{		
					result1Lat = localSearch1.results[0].lat;
					result1Lng = localSearch1.results[0].lng;
					resultpoint1 = new GLatLng(result1Lat,result1Lng);
					localSearch2.execute(to_value + ", UK");
				}else{
					alert("Postcode1 not found!");
				}
			});	
		
		localSearch2.setSearchCompleteCallback(null, 
			function() {
				
				if (localSearch2.results[0])
				{		
					result2Lat = localSearch2.results[0].lat;
					result2Lng = localSearch2.results[0].lng;
					resultpoint2 = new GLatLng(result2Lat,result2Lng);
					output_results(to_value);
				}else{
					alert("Postcode2 not found!");
				}
			});	
		
		//localSearch1.execute(document.postcodes.address1.value + ", UK");
		localSearch1.execute(document.getElementById('from_input').value + ", UK");
	}
		
	function output_results(to_value)
	{
		map.clearOverlays();
		map.addOverlay(new GMarker(resultpoint1,icon1));
		map.addOverlay(new GMarker(resultpoint2,icon2));
		gDir.load('from: ' + document.getElementById('from_input').value + ' to: ' + to_value, {getPolyline:true});
	}
	
	function placeMarkerAtPoint(point)
	{
		var marker = new GMarker(point,icon);
		map.addOverlay(marker);
	}
	
	function setCenterToPoint(point)
	{
		map.setCenter(point, 17);
	}
	
	function showPointLatLng(point)
	{
		alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
	}
	
	function mapLoad() {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));
			
			
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(54.622978,-2.592773), 5, G_HYBRID_MAP);
		}
	}
	
	function addLoadEvent(func) {
	  var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
		window.onload = func;
	  } else {
		window.onload = function() {
		  oldonload();
		  func();
		}
	  }
	}
	
	function addUnLoadEvent(func) {
		var oldonunload = window.onunload;
		if (typeof window.onunload != 'function') {
		  window.onunload = func;
		} else {
		  window.onunload = function() {
			oldonunload();
			func();
		  }
		}
	}
	
	addLoadEvent(initialize);
	addLoadEvent(mapLoad);
	
	addUnLoadEvent(GUnload);

