// JavaScript Document
//<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript">

window.onload=function(){
	if (document.getElementsByClassName == undefined) {
		document.getElementsByClassName = function(className)
		{
			var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
			var allElements = document.getElementsByTagName("*");
			var results = [];
	
			var element;
			for (var i = 0; (element = allElements[i]) != null; i++) {
				var elementClass = element.className;
				if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
					results.push(element);
			}
	
			return results;
		}
	}
}

var map;
var geocoderv3;
geocoderv3 = new google.maps.Geocoder();
function codeAddress(strLocation, strLatDivID, strLngDivID, strValidationScript) {
//	alert('Step 3');
  var northEast = new google.maps.LatLng(51.80861475198521,0.68939208984375);
	var southWest = new google.maps.LatLng(51.222367645529715,-0.65643310546875);
  var bounds = new google.maps.LatLngBounds(southWest,northEast);
	
	geocoderv3.geocode( { 'address': strLocation, 'bounds': bounds, 'region' : 'uk'}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			var strNewLatLon = results[0].geometry.location+'';
			arrNewLatLon = strNewLatLon.replace(" ", "").replace("(", "").replace(")", "").split(",");
			document.getElementById(strLatDivID).value=arrNewLatLon[0];
			document.getElementById(strLngDivID).value=arrNewLatLon[1];
		} else {
			document.getElementById(strLatDivID).value='';
			document.getElementById(strLngDivID).value='';
		}
		if (strValidationScript=='Sales' || strValidationScript=='Lettings') {
			submitSearch(strValidationScript);
		} else if (strValidationScript=='editDestinationAddress') {
//			alert('Step 4');
			editDestinationAddress();
		}
		
	});
	
}


codeAddress2 = function(strLocation, strLatDivID, strLngDivID, strValidationScript) {
  var northEast = new google.maps.LatLng(51.74063616409767,0.4669189453125);
	var southWest = new google.maps.LatLng(51.23784668914442,-0.5712890625);
  var bounds = new google.maps.LatLngBounds(southWest,northEast);

	geocoderv3.geocode( { 'address': strLocation, 'bounds': bounds, 'region' : 'uk'}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			var strNewLatLon = results[0].geometry.location+'';
			arrNewLatLon = strNewLatLon.replace(" ", "").replace("(", "").replace(")", "").split(",");
			document.getElementById(strLatDivID).value=arrNewLatLon[0];
			document.getElementById(strLngDivID).value=arrNewLatLon[1];
		} else {
			document.getElementById(strLatDivID).value='';
			document.getElementById(strLngDivID).value='';
		}
		if (strValidationScript=='Sales' || strValidationScript=='Lettings') {
			submitSearch(strValidationScript);
		}
		
	});
	
}



var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map; 

function initializev3() {
	directionsDisplay = new google.maps.DirectionsRenderer();
//	var chicago = new google.maps.LatLng(41.850033, -87.6500523);
	var myOptions = {
		zoom:7,
		mapTypeId:google.maps.MapTypeId.ROADMAP
//		center:chicago
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	directionsDisplay.setMap(map);
}

function calcRoute(intTotalDest) {
	var start = document.getElementById('address_0').value;
	var end = document.getElementById('address_'+intTotalDest).value;

	arrWayPoints = [];
	if (intTotalDest>1) {
		for (variable=1;variable<intTotalDest;variable=variable+1) {
			arrWayPoints.push ({location:document.getElementById('address_'+variable).value});
		}
	}
	
	var request = {
		origin:start,
		destination:end,
		waypoints:arrWayPoints,
		travelMode:google.maps.DirectionsTravelMode.DRIVING
	};
	directionsDisplay = new google.maps.DirectionsRenderer();
	directionsService.route(request, function(result, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(result);
			computeTotalDistance(directionsDisplay.directions);
		} else {
			//alert(status);
		}
	});
}

function computeTotalDistance(result) {
	var total = 0;
	var myroute = result.routes[0];
	for (i = 0; i < myroute.legs.length; i++) {
		total += myroute.legs[i].distance.value;
	}
	//convert to miles
	total = total / 1609.344
	if (document.getElementById("current_route_milage")) {
		document.getElementById("current_route_milage").value = Math.ceil(total) + " miles";
		updatebookingmileageprice(Math.ceil(total));
	}
}

var last_infowindow;

function createMarkerv3(options) {
	
	// Create a lettered icon for this point using our icon class
	if (options.icon == 'this_house') {
		var propertyIcon = 'beachflag.png';
	} else if (options.icon == 'house') {
		var propertyIcon = 'beachflag.png';
	} else if (options.icon == 'this_flat') {
		var propertyIcon = 'beachflag.png';
	} else if (options.icon == 'flat') {
		var propertyIcon = 'beachflag.png';
	} else if (options.icon == 'this_multi') {
		var propertyIcon = 'beachflag.png';
	} else if (options.icon == 'multi') {
		var propertyIcon = 'beachflag.png';
	} else if (options.icon == 'default') {
		var propertyIcon = 'beachflag.png';
	} else {
		var propertyIcon = 'beachflag.png';
	}
	
	if(options.animation="drop") { 
		options.animation = google.maps.Animation.DROP;
	} else {
		options.animation = NULL
	}

	var myLatLng = new google.maps.LatLng(options.lat, options.lon);
	//var contentString = strMarkerContent;

	var infowindow = new google.maps.InfoWindow({
		content: options.content
	});
	
	var marker = new google.maps.Marker({
		position: myLatLng,
		map: map,
		title: options.title,
		animation: options.animation
	});

	function selectMarker() {
		if( last_infowindow	) { last_infowindow.close() };
		infowindow.open(map,marker);
		last_infowindow = infowindow;
		if ( typeof ( options.marker_select ) == 'function' ) {
			options.marker_select();
		}
	}
	
	google.maps.event.addListener(marker, 'click', selectMarker );
	
	if ( options.link != null ) {
		objLink = document.getElementById( options.link );
		if ( typeof ( objLink ) == 'object' ) {
			if( document.addEventListener ) {
				objLink.addEventListener( 'click' , selectMarker , false);
			} else if ( document.attachEvent ) {
				objLink.attachEvent('onclick', selectMarker );
			}
			
		}
		
	}
	
}

function initializemapv3(strCurrentLat, strCurrentLng, intLoadIcons, intZoomLevel) {
	var latlng = new google.maps.LatLng(strCurrentLat, strCurrentLng);
	var myOptions = {
			zoom: intZoomLevel,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	if (intLoadIcons==1) {
		if(typeof loadmarkers == 'function') {
			loadmarkers();
		}
	}
	
}

function loadmapv3(options) {
	
	if(!options.scrollwheel) { options.scrollwheel = false; }
	
	if(!options.mapdiv) { options.mapdiv = "map_canvas"; }
	
	if(!options.zoom) { options.zoom = 10; }
	
	switch(options.maptype) {
		case "roadmap":
			options.mapTypeId = google.maps.MapTypeId.ROADMAP;
			break;
		case "hybrid":
			options.mapTypeId = google.maps.MapTypeId.HYBRID;
			break;
		case "satellite":
			options.mapTypeId = google.maps.MapTypeId.SATELLITE;
			break;
		case "terrain":
			options.mapTypeId = google.maps.MapTypeId.TERRAIN;
			break;									  
		default:
			options.mapTypeId = google.maps.MapTypeId.ROADMAP;
			break;	
	}
	
	options.center = new google.maps.LatLng(options.lat, options.lon);
		
	map = new google.maps.Map(document.getElementById(options.mapdiv), options);

	if (options.callback) {
		
		if(typeof options.callback == 'function') {
			options.callback();
		}
	}
	
}

