function showMap(mapContainer, postcode) {
	if(postcode && showMap.geocoder) {
		var $mapContainer = $(mapContainer),
			$loading = $('<span>Loading map...</span>').appendTo($mapContainer);
		showMap.codeAddress(postcode, function(latLng) {
			var mapElem = $mapContainer
				.css({
					height: '276px',
					width: '276px'
				})
				.get(0),
			map,
			mapOptions,
			marker,
			layer;
			if($mapContainer.hasClass('streetview')) {
				// option for streetview
				mapOptions = {
					position: latLng,
					pov: {
						heading: 34,
						pitch: 10,
						zoom: 1
					}
				};
				map = new google.maps.StreetViewPanorama(mapElem, mapOptions);
			} else { // ordinary map
				mapOptions = {
					zoom: 14,
					center: latLng,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				};
				map = new google.maps.Map(mapElem, mapOptions);
				marker = new google.maps.Marker({
					position: latLng,
					map: map,
					title: postcode
				});
				if($mapContainer.hasClass('locale')) {
					// option for collab map
					layer = new google.maps.KmlLayer("http://maps.google.com/maps/ms?ie=UTF8&hl=en&vps=1&jsv=325c&msa=0&output=nl&msid=203304872355046718911.00049f9d744dc64808023");
					layer.setMap(map);
				}
			}
		});
	}
}

function setupGMaps() {
	showMap.geocoder = new google.maps.Geocoder();
	showMap.codeAddress = function(postcode, callback) {
		showMap.geocoder.geocode( { 'address': postcode, 'region': 'UK' }, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				var latLng = results[0].geometry.location;
				callback(latLng);
			} else {
				alert("Geocode was not successful for the following reason: " + status);
			}
		});
	};
	$('.map div.postcode').css('visibility', 'hidden'); // hide the postcode fields that are used in showMap
	$(document).bind('refreshMaps', function() {
		$('.map:visible').each(function() {
			var postcode = $.trim($(this).find('div.postcode').text()),
				mapContainer = this;
			showMap(mapContainer, postcode);
		});
	});
	// check for any maps that need showing immediately
	$(document).trigger('refreshMaps');
}
  
function loadGMapsScript() {
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=setupGMaps";
	document.body.appendChild(script);
}
  

$(document).ready(function() {

	if(window.has_maps) {
		loadGMapsScript();
	}

	// properties page
	if($('.properties > div > ul li').length) { // JRL - is this code still needed?
		$('.properties > div > ul').tabs({
			fx: { opacity: 'toggle' }
		}).bind('tabsshow', function(event, ui, tab) {
			var index = tab.index,
				$panel = $(tab.panel),
				$mapContainer = $panel.find('.map'),
				postcode = $.trim($panel.find('div.postcode').text());
			showMap($mapContainer,postcode);
		});
	}
	
	// single property page
	// JRL: I don't think this is being used any more; 6/9/11
	$('form.deposit_payment').submit(function() {
		var $custom = $(this.custom);
		if(!$custom.val()) {
			$custom.val('please supply your name!');
			return false;
		} else {
			return true;
		}
	});
	
	// faq page
	$('.faq_hidden').each(function() {
		var $faq = $(this);
		$faq.parent().children('a').click(function() {
			$faq.stop(false, true).slideToggle();
			return false;
		});
	});
	
	// request a callback form on homepage
	$('#requestCallback').click(function() {
		$('#requestCallbackForm').slideToggle();
		return false;
	});
});

