var map, geocoder, listings_map, bounds, map_icon, markers = [], marker_point = [];

function contact_map() {
	if (GBrowserIsCompatible()) {
		
		contact_map = new GMap2(document.getElementById("GmapContact"), {size: new GSize(737, 365)});
		var mapControl = new GMapTypeControl();
		contact_map.addControl(mapControl);
		contact_map.addControl(new GLargeMapControl());
		contact_map.addControl(new GMapTypeControl());
		
		
		contact_map.setCenter(new GLatLng(contact_points[0][1], contact_points[0][2]),14);
		
		// bounding box object
		bounds = new GLatLngBounds();
		
		map_icon = new GIcon(G_DEFAULT_ICON);
		map_icon.image = "./images/marker.png";
		map_icon.shadow = "./images/marker-shadow.png";
		map_icon.iconSize = new GSize(16, 16);
		map_icon.shadowSize = new GSize(0, 0);
		map_icon.iconAnchor = new GPoint(6, 12);
		map_icon.infoWindowAnchor = new GPoint(6, 6);
		
		for (i in contact_points) {
			
			marker_point[i] = new GLatLng(contact_points[i][1], contact_points[i][2]);
			contact_map.addOverlay(createMarker(marker_point[i], map_icon, contact_points[i][0])); //markers[i]);
			// extend the bounding box
			bounds.extend(marker_point[i]);
			
		}
		
		// All markers are done, set the zoom
		contact_map.setZoom(18);
		
	}
}

function listings_map() {
	if (GBrowserIsCompatible()) {
		
		listings_map = new GMap2(document.getElementById("GmapListings"), {size: new GSize(697, 365)});
		var mapControl = new GMapTypeControl();
		listings_map.addControl(mapControl);
		listings_map.addControl(new GLargeMapControl());
		listings_map.addControl(new GMapTypeControl());
		
		// we don't know the center yet, set office
		listings_map.setCenter(new GLatLng(-33.8779151, 151.2426758),14);
		
		// bounding box object
		bounds = new GLatLngBounds();
		
		map_icon = new GIcon(G_DEFAULT_ICON);
		map_icon.image = "./images/marker.png";
		map_icon.shadow = "./images/marker-shadow.png";
		map_icon.iconSize = new GSize(16, 16);
		map_icon.shadowSize = new GSize(0, 0);
		map_icon.iconAnchor = new GPoint(6, 12);
		map_icon.infoWindowAnchor = new GPoint(6, 6);
		
		for (i in points) {
			
			marker_point[i] = new GLatLng(points[i][1], points[i][2]);
			listings_map.addOverlay(createPropertyMarker(marker_point[i], map_icon, points[i][0], points[i][3], points[i][4], points[i][5])); //markers[i]);
			// extend the bounding box
			bounds.extend(marker_point[i]);
			
		}
		
		// All markers are done, set the zoom
		listings_map.setZoom(listings_map.getBoundsZoomLevel(bounds));
		// set the center
		listings_map.setCenter(bounds.getCenter());
		
	}
}
function createPropertyMarker(m_point, m_icon, m_title, m_id, m_img, m_desc) {
	
	var options = {icon:m_icon, title:m_title};
	var marker = new GMarker(m_point, options);
	
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml('<div class="marker-overlay ofh"><div class="marker-image left"><img src="' + m_img + '" /></div><div class="marker-content left"><p><strong><nobr>' + m_title + '</nobr></strong></p><p>' + m_desc +'</p><p><a href="' + m_id + '" class="property-link"><nobr>View Property Details</nobr></a></p></div></div>');
	});
	
	return marker;
}

function createMarker(m_point, m_icon, m_title) {
	
	var options = {icon:m_icon, title:m_title};
	var marker = new GMarker(m_point, options);
	
	GEvent.addListener(marker, "click", function() {
		//marker.openInfoWindowHtml('<div class="marker-overlay ofh"><div class="marker-image"><img src="' + m_img + '" /><div class="marker-content right"><p><strong><nobr>' + m_title + '</nobr></strong></p><p>' + m_desc +'</p><p><a href="' + m_id + '" class="property-link"><nobr>View Property Details</nobr></a></p></div></div></div>');
		marker.openInfoWindowHtml('<p style="color: black"><strong><nobr>' + m_title + '</nobr></strong></p>');
	});
	
	return marker;
}

function createSimpleMarker(m_point, m_icon, m_title) {
	
	var options = {icon:m_icon, title:m_title};
	var marker = new GMarker(m_point, options);
	
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml('<p style="color: black;"><strong><nobr>' + m_title + '</nobr></strong></p>');
	});
	
	return marker;
}

function showAddress(address) {
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				if (address.match(/^\d+/)) {
					streetOnly = address.replace(/^(\d|\s|\/)*/, '');
					showAddress(streetOnly);
					return;
				} else if (address.match(/,/g)) {
					suburbOnly = address.substring(address.indexOf(',')+1, address.length);
					showAddress(suburbOnly);
					return;
				} else {
					alert(address + " could not be found!");
					return;
				}
			} else {
				map.setCenter(point, 16);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				foundSuburbOnly = '';
				if (address.match(/^[^(,|\d)]+, Australia$/)) {
					foundSuburbOnly = '<br />(Found suburb location only!)';
				}
				marker.openInfoWindowHtml('<span style="color:black;">'+address+foundSuburbOnly+'</span>');
			}
		}
	);
	$('#photo-container img#main-image').fadeOut('slow', function () {
		map.checkResize();
		var gmap_offset = $('#Gmap').offset();
		$('html, body').scrollTop(gmap_offset.top);
	});
}

/* Google Maps */
var map,geocoder;
function GMapLoad() {
  if (GBrowserIsCompatible()) {
  var mapOptions = { size:new GSize(615,409) };
	map = new GMap2(document.getElementById("Gmap"), mapOptions);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	geocoder = new GClientGeocoder();
  }
}

function showLatsLongs(lat, long, address) {
	var point = new GLatLng(lat, long);
	map_icon = new GIcon(G_DEFAULT_ICON);
	map_icon.image = "./images/marker.png";
	map_icon.shadow = "./images/marker-shadow.png";
	map_icon.iconSize = new GSize(16, 16);
	map_icon.shadowSize = new GSize(0, 0);
	map_icon.iconAnchor = new GPoint(6, 12);
	map_icon.infoWindowAnchor = new GPoint(6, 6);
	listings_map.setCenter(point, 16);
	listings_map.setZoom(17);
	listings_map.addOverlay(createSimpleMarker(point, map_icon, address));
}

function showAuctionLocation(address) {
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				if (address.match(/^\d+/)) {
					streetOnly = address.replace(/^(\d|\s|\/)*/, '');
					showAddress(streetOnly);
					return;
				} else if (address.match(/,/g)) {
					suburbOnly = address.substring(address.indexOf(',')+1, address.length);
					showAddress(suburbOnly);
					return;
				} else {
					alert(address + " could not be found!");
					return;
				}
			} else {
				map.setCenter(point, 16);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				foundSuburbOnly = '';
				if (address.match(/^[^(,|\d)]+, Australia$/)) {
					foundSuburbOnly = '<br />(Found suburb location only!)';
				}
				marker.openInfoWindowHtml('<span style="color:black;">Auction Location:<br /> '+address+foundSuburbOnly+'</span>');
			}
		}
	);
	$('#photo-container img#main-image').fadeOut('slow', function () {
		map.checkResize();
		var gmap_offset = $('#Gmap').offset();
		$('html, body').scrollTop(gmap_offset.top);
	});
}

function showLatsLongs(lat, long, address) {
	var point = new GLatLng(lat, long);
	map_icon = new GIcon(G_DEFAULT_ICON);
	map_icon.image = "./images/marker.png";
	map_icon.shadow = "./images/marker-shadow.png";
	map_icon.iconSize = new GSize(16, 16);
	map_icon.shadowSize = new GSize(0, 0);
	map_icon.iconAnchor = new GPoint(6, 12);
	map_icon.infoWindowAnchor = new GPoint(6, 6);
	map.setCenter(point, 16);
	map.addOverlay(createSimpleMarker(point, map_icon, address));
	$('#photo-container img#main-image').fadeOut('slow', function () {
		map.checkResize();
		// Get location of map
		var gmap_offset = $('#Gmap').offset();
		$('html, body').scrollTop(gmap_offset.top);
	});
}
