var australiaCoor = new google.maps.LatLng(-29.509726, 135.625);
var geocoder;
var map;
geocoder = new google.maps.Geocoder();

function mapView(canvas, PropertyList) {
	markers = [];
	var bounds = new google.maps.LatLngBounds();
	var myOptions = {
		streetViewControl: true,
		scrollwheel: false,
		zoom: 5,
		center: australiaCoor,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById(canvas), myOptions);
	var marker_icon = new google.maps.MarkerImage(SITE_PATH+'img/google_pin.png',
		  // This marker is 20 pixels wide by 30 pixels tall.
		  new google.maps.Size(23, 30),
		  // The origin for this image is 0,0.
		  new google.maps.Point(0,0),
		  // The anchor for this image is the base of the flagpole at 0,30.
		  new google.maps.Point(0, 30)
	);
	var marker_shadow = new google.maps.MarkerImage(SITE_PATH+'img/icon_shadow.png',
		  // The shadow image is larger in the horizontal dimension
		  // while the position and offset are the same as for the main image.
		  new google.maps.Size(28, 34),
		  new google.maps.Point(0,0),
		  //shadow offset x,y
		  new google.maps.Point(-11, 34)
	);
	
	var infowindow = new google.maps.InfoWindow();
	var infowindow_size = new google.maps.Size(320,120);
	var marker, i;
	var message = new Array();
	for (var i in PropertyList['full_address']) {
		propCoor = null;
		if(parseFloat(PropertyList['lt_lat'][i]) === 0 || parseFloat(PropertyList['lt_log'][i]) === 0) {
			continue;
		}else {
			if(PropertyList['no_record'][1]) {
				message[i] = "<div id='google-property' style='width: 320px; height: 100px;'><br /><h4>"+PropertyList['office_name'][1]+"</h4><h4>"+PropertyList['full_address'][1]+"</h4><p>No property can be shown on the map because property addresses are hidden.</p></div>";	
				propCoor = australiaCoor;
				geocoder.geocode( { 'address': PropertyList['full_address'][1]}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						propCoor = results[0].geometry.location;
					}
				});
			} else {
				message[i] = '<div id="property-baloon" style="width:320px;height:115px;overflow:hidden;"><h1><span>'+PropertyList['suburb'][i]+'</span> '+PropertyList['address'][i]+'</h1><div class="photo"><a href="'+PropertyList['DetailsLink'][i]+'"><img src="'+PropertyList['photo'][i]+'" /></a></div><div class="info"><div class="basic"><h2>'+PropertyList['price'][i]+'</h2><p>'+PropertyList['headline'][i]+'</p></div><div class="tools"><p class="bbc">'+PropertyList['bbc'][i]+'</p><ul><li><a href="'+PropertyList['BookmarkLink'][i]+'" title="bookmark property" class="bookmark"><span class="icon bookmark"></span></a></li><li><a href="'+PropertyList['DetailsLink'][i]+'" title="view details"><span class="icon view"></span></a></li></ul></div></div><script type="text/javascript">$(document).ready(function() {$("#property-baloon a.bookmark").fancybox({\'centerOnScroll\':true});});</script><div style="clear:both"></div></div>';
				propCoor = new google.maps.LatLng(PropertyList['lt_lat'][i],PropertyList['lt_log'][i]);
			}
			marker = new google.maps.Marker({
				position: propCoor,
				map: map,
				icon: marker_icon,
				shadow: marker_shadow
			});
			bounds.extend(marker.position);
			map.fitBounds(bounds);
			//alert(i);
			
		}
		google.maps.event.addListener(marker, 'click', (function(marker, i) {
			return function() {
				infowindow.setContent(message[i]);
				infowindow.setSize(infowindow_size);
				infowindow.open(map, marker);
			}
		})(marker, i));
	}
}
 
function codeAddress(canvas, PropAddress) {
	var myOptions = {
		streetViewControl: true,
		scrollwheel: false,
		zoom: 15,
		center: australiaCoor,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById(canvas), myOptions);
	
	geocoder.geocode( { 'address': PropAddress}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
			var contentString = '<br /><p>'+PropAddress+'</p>';
			var infowindow = new google.maps.InfoWindow({
				content: contentString
			});
			var marker_icon = new google.maps.MarkerImage(SITE_PATH+'img/google_pin.png',
				  // This marker is 20 pixels wide by 30 pixels tall.
				  new google.maps.Size(23, 30),
				  // The origin for this image is 0,0.
				  new google.maps.Point(0,0),
				  // The anchor for this image is the base of the flagpole at 0,30.
				  new google.maps.Point(0, 30)
			);
			var marker_shadow = new google.maps.MarkerImage(SITE_PATH+'img/icon_shadow.png',
				  // The shadow image is larger in the horizontal dimension
				  // while the position and offset are the same as for the main image.
				  new google.maps.Size(28, 34),
				  new google.maps.Point(0,0),
				  //shadow offset x,y
				  new google.maps.Point(-11, 34)
			);

			var marker = new google.maps.Marker({
				icon: marker_icon,
				shadow: marker_shadow,
				position:results[0].geometry.location,
				map: map
			});
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.open(map,marker);
			});

		} else {
			alert("Geocode was not successful for the following reason: " + status);
		}
	});
}

function streetView(canvas, PropAddress) {
	geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address': PropAddress}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			var panoramaOptions = {
				position:results[0].geometry.location,
				pov: {
					heading: 165,
					pitch:0,
					zoom:0
				}
			};
			var myPano = new google.maps.StreetViewPanorama(document.getElementById(canvas), panoramaOptions);
			map.setStreetView(myPano);
			if($('#'+canvas+' svg:eq(0) path').length=0) {
				alert($('#'+canvas+' svg:eq(0) path').length);
			}
		}
	});
}

