var houseIcon;
var flatIcon;
var myPano = null;
var map = null;
var geocoder = new GClientGeocoder();
var localSearch = new GlocalSearch();

function createMarker(point, index, markercontent, icontype) {
	// Create a lettered icon for this point using our icon class
	if (icontype == 'this_house') {
		var propertyIcon = new GIcon(ThishouseIcon);
	} else if (icontype == 'house') {
		var propertyIcon = new GIcon(houseIcon);
	} else if (icontype == 'this_flat') {
		var propertyIcon = new GIcon(ThisflatIcon);
	} else if (icontype == 'flat') {
		var propertyIcon = new GIcon(flatIcon);
	} else if (icontype == 'this_multi') {
		var propertyIcon = new GIcon(ThismultiIcon);
	} else if (icontype == 'multi') {
		var propertyIcon = new GIcon(multiIcon);
	} else {
		var propertyIcon = new GIcon(ThishouseIcon);
	}
 
	// Set up our GMarkerOptions object
	markerOptions = { icon:propertyIcon };
	var marker = new GMarker(point, markerOptions);
 
 	if (markercontent) {
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(markercontent);
		});
	}
	return marker;
} 

function initializemap(strCurrentLat, strCurrentLng, intLoadIcons, intZoomLevel) {
	if (GBrowserIsCompatible()) {
		if (!intZoomLevel) {
			intZoomLevel = 12;
		}
		if (document.getElementById("pano")) {
			myPano = new GStreetviewPanorama(document.getElementById("pano"));
			var latlng = new GLatLng(strCurrentLat, strCurrentLng);
			myPano.setLocationAndPOV(latlng);
		}
		if (document.getElementById("map_canvas")) {
			map = new GMap2(document.getElementById("map_canvas"));
			map.setCenter(new GLatLng(strCurrentLat, strCurrentLng), intZoomLevel);
			map.setUIToDefault();
			map.enableContinuousZoom();
			map.disableScrollWheelZoom();
//			map.addMapType(G_SATELLITE_3D_MAP); 
			
			houseIcon = new GIcon(G_DEFAULT_ICON);
			houseIcon.image = "http://www.acorncommercial.co.uk/images/icon_house.png";
			houseIcon.iconSize = new GSize(27, 27);
			houseIcon.shadowSize = new GSize(1, 1);
			houseIcon.iconAnchor = new GPoint(13, 27);
			houseIcon.infoWindowAnchor = new GPoint(22, 2);
			
			flatIcon = new GIcon(G_DEFAULT_ICON);
			flatIcon.image = "http://www.acorncommercial.co.uk/images/icon_apartment.png";
			flatIcon.iconSize = new GSize(27, 37);
			flatIcon.shadowSize = new GSize(1, 1);
			flatIcon.iconAnchor = new GPoint(13, 27);
			flatIcon.infoWindowAnchor = new GPoint(9, 2);
			
			multiIcon = new GIcon(G_DEFAULT_ICON);
			multiIcon.image = "http://www.acorncommercial.co.uk/images/icon_mixed.png";
			multiIcon.iconSize = new GSize(25, 31);
			multiIcon.shadowSize = new GSize(1, 1);
			multiIcon.iconAnchor = new GPoint(12, 27);
			multiIcon.infoWindowAnchor = new GPoint(9, 2);
			
			ThishouseIcon = new GIcon(G_DEFAULT_ICON);
			ThishouseIcon.image = "http://www.acorncommercial.co.uk/images/this_icon_house.png";
			ThishouseIcon.iconSize = new GSize(27, 27);
			ThishouseIcon.shadowSize = new GSize(1, 1);
			ThishouseIcon.iconAnchor = new GPoint(13, 27);
			ThishouseIcon.infoWindowAnchor = new GPoint(22, 2);
			
			ThisflatIcon = new GIcon(G_DEFAULT_ICON);
			ThisflatIcon.image = "http://www.acorncommercial.co.uk/images/this_icon_apartment.png";
			ThisflatIcon.iconSize = new GSize(27, 37);
			ThisflatIcon.shadowSize = new GSize(1, 1);
			ThisflatIcon.iconAnchor = new GPoint(13, 27);
			ThisflatIcon.infoWindowAnchor = new GPoint(9, 2);
			
			ThismultiIcon = new GIcon(G_DEFAULT_ICON);
			ThismultiIcon.image = "http://www.acorncommercial.co.uk/images/this_icon_mixed.png";
			ThismultiIcon.iconSize = new GSize(25, 31);
			ThismultiIcon.shadowSize = new GSize(1, 1);
			ThismultiIcon.iconAnchor = new GPoint(12, 27);
			ThismultiIcon.infoWindowAnchor = new GPoint(9, 2);
			
			var bounds = map.getBounds();
			var southWest = bounds.getSouthWest();
			var northEast = bounds.getNorthEast();
			var lngSpan = northEast.lng() - southWest.lng();
			var latSpan = northEast.lat() - southWest.lat();
			
			if (intLoadIcons == 1) {
				loadIcons();
			}
		}
	} 
}


function checkValidLocation(strLocation, strLatDivID, strLngDivID, strValidationScript) {
	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0] && localSearch.results[0].lat && localSearch.results[0].lng) {
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				document.getElementById(strLatDivID).value=resultLat;
				document.getElementById(strLngDivID).value=resultLng;
				if (strValidationScript=='validateadvancedsearch') {
					validateadvancedsearch();
				} else if (strValidationScript=='validatetopsearch') {
					validatetopsearch();
				} else if (strValidationScript=='validateNewAccountForm') {
					validateNewAccountForm();
				}
			} else {
				document.getElementById(strLatDivID).value='';
				document.getElementById(strLngDivID).value='';
			}
		});
	if (strLocation=='' || strLocation=='Postcode, Street or area') {
		document.getElementById(strLatDivID).value='';
		document.getElementById(strLngDivID).value='';
		if (strValidationScript=='validateadvancedsearch') {
			validateadvancedsearch();
		} else if (strValidationScript=='validatetopsearch') {
			validatetopsearch();
		} else if (strValidationScript=='validateNewAccountForm') {
			validateNewAccountForm();
		}
	} else {
		localSearch.execute(strLocation + ', United Kingdom');
	}
}


function jumpScroll() {
   	window.scroll(0,findPosY(document.getElementById('top'))); // horizontal and vertical scroll targets
}

function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent)
			while(1)
			{
				curtop += obj.offsetTop;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
	else if(obj.y)
			curtop += obj.y;
	return curtop;
}

