		var CONTENT_PREFIX;
        
		var build_ngv_icon = function() {
			i = new GIcon(G_DEFAULT_ICON);
			i.iconSize = new GSize(25, 43);
			i.image = "static/images/marker.png";			
			return i;
		}

		var ensure_form_is_valid = function() {
			if (! $("#locationForm").valid() ) {
				return false;
			}
		}

		var return_to_introduction = function(map) {
			if (map && map.getInfoWindow()) { map.closeInfoWindow(); }
			$("#ngv_deco_place").hide();
			$("#ngv_deco_start").show();
		}
        
		var clear_places_for_form = function() {
			$("#ngv_deco_start").hide();
			$("#ngv_deco_place").hide();
			$("#ngv_deco_form").show();			
		}
        
        var reset_page_after_submit = function(map, marker) {
            if (map && map.getInfoWindow()) { map.closeInfoWindow(); }
			if (marker) { map.removeOverlay(marker); }
    
            $("#locationForm").clearForm();
			$("#ngv_deco_form").hide();
			$("#ngv_deco_place").hide();
            $("#ngv_deco_start").show();
			$("#submit_confirm_block").show();
            
            $("#post_submit_reset").bind("click", function(e){
			    $("#submit_confirm_block").hide();
                $("#intro_block").show();
	        });    
            
		}
        

		var create_marker_for_location = function(location, map) 
		{
			var point = new GLatLng(location.latitude, location.longitude);
			var ngv_icon   = build_ngv_icon();
			var ngv_marker = new GMarker(point, { icon: ngv_icon });

		    GEvent.addListener(ngv_marker, "click", function() {
								
				load_entries_for_location(location, map);
				$("#location_lat" ).val(location.lat);
				$("#location_long").val(location.lng);
				$("#location_name").val(location.name);
				$("#location_address").val(location.address);
				$("#ngv_location_name").hide();
				$("#ngv_location_name_title").html("").prepend(html_title(location, "image")).show();
				ngv_marker.openInfoWindowHtml(html_title(location, "thumb") + "<p><a class=\"maplink\" href=\"#add_notes\" onclick=\"clear_places_for_form();\">Add your comment and photo</a></p>", { maxWidth: 400 });			
				GEvent.addListener(map.getInfoWindow(), "closeclick", function() {
					return_to_introduction(map);
				});
			});
			return ngv_marker;
		}

		var html_title = function(location, type) {
			var title = type == "thumb" ? "<div class=\"ngv_thumb\"><a class=\"thickbox\" href=\""+CONTENT_PREFIX+"large/"+location.imagename+"\"><img src=\""+CONTENT_PREFIX+"small/"+location.imagename+"\"></a></div>" : "";
			return title + 
						 "<h3>" + location.name + "</h3>" +
						 "<p>" + location.address + "</p>";
		}

		var load_entries_for_location = function(location, map) {
            
			$.getJSON("api/locations/"+location.id+"/entries", function(json) 
			{
				location.imagename = json.entries[0].imagename;
				var entry_html = "";
				$.each(json.entries, function(i,entry) {
					entry_html += "<div class=\"block thin\">";
					entry_html += "<div id=\"map_form\" class=\"map\">";
					entry_html += "<a name=\"add_notes\"></a>";
					entry_html += "<div class=\"content" + (i != 0 ? " white" : "") + "\">";
                    entry_html += (i != 0 ? " " : "<img id=\"entry_close_button\" src=\"static/images/control25_close.jpg\" alt=\"Exit\" width=\"25\" height=\"25\" border=\"0\" class=\"controlbutton\" />");
					if (entry.imagename) {
                        entry_html += "<div class=\"ngv_image\"><a href='"+CONTENT_PREFIX+"large/"+entry.imagename+"' class='thickbox' ><img src='"+CONTENT_PREFIX+"medium/"+entry.imagename + "'></div></a>";                        
                    } 
					entry_html += "" + (i==0 ? html_title(location, "image") : "");
					entry_html += "" + (i==1 ? "<h3>Comments</h3>" : "") + "";
					entry_html += "<p>" + entry.description + "<br/>&nbsp;</p>";
					entry_html += "<p><em><strong>" + entry.name + "</strong><br/>" + entry.school + "</em></p>";
					entry_html += "" + (i==0?"<a class=\"addlink\" href=\"#add_notes\" onclick=\"clear_places_for_form();\"><img src=\"static/images/butt_addcomment.jpg\" alt=\"Add your comment\" /></a>":"") + "";
					entry_html += "<div class=\"clearing\"></div>";
					entry_html += "</div>";
					entry_html += "</div>";
					entry_html += "</div>";
					entry_html += "</div>";
				});
                
				$("#location_lat" ).val(location.latitude);
				$("#location_long").val(location.longitude);
				$("#ngv_deco_form").hide();
				$("#ngv_deco_start").hide();
                
				//using this because jquery html() func seem to not work with IE6
                $("#ngv_deco_place").attr("innerHTML", entry_html).show();
                tb_init('a.thickbox'); //initialise thickbox to be used for popups for each above entry with an image
                $("#entry_close_button").bind("click", function(e){
					return_to_introduction(map);
				});
			});
		}



		function initialize() 
		{
			if (GBrowserIsCompatible()) 
			{
				CONTENT_PREFIX = $("#content_prefix").attr("name"); //set from html
				
				//var center = new GLatLng(-37.814251, 144.963169); //Melbourne CBD center
				var center = new GLatLng(12, 125); //whole world centered on Aus
                
				// Create and Center a Map
				var map = new GMap2(document.getElementById("map_canvas"));
					//map.setCenter(center, 14); //initial zoom level
                    map.setCenter(center, 2); //initial zoom level		    
					map.addControl(new GLargeMapControl());
					map.addControl(new GMapTypeControl());
					map.addControl(new GOverviewMapControl());

                
                var marker; //put here so we can access marker from ajax submit handler function
                
				var handle_new_user_location = function(overlay, point) {
					
					if (!point) { return };
					
					var ngv_icon   = build_ngv_icon();
					marker  = new GMarker(point, { draggable: true, icon: ngv_icon });
					var message = "Tell us about the great art deco<br/>architecture at this location!<br/><br/>You can drag me to a<br/>new location too if you want."

					map.addOverlay(marker);
					GEvent.clearInstanceListeners(map);
					marker.openInfoWindow(message, marker.getPoint());

					$("#ngv_deco_start").hide();
					$("#ngv_deco_place").hide();
					$("#ngv_deco_form").show();
					$("#locationForm").validate();
					$("#location_lat" ).val(marker.getPoint().lat());
					$("#location_long").val(marker.getPoint().lng());
					$("#location_name").val("");
					$("#location_address").val("");
					$("#ngv_location_name").show();
					$("#ngv_location_name_title").hide();

					GEvent.addListener(marker, "dragstart", function() {
					  marker.closeInfoWindow();
					});
    
    				GEvent.addListener(map.getInfoWindow(), "closeclick", function(e) {
    					reset_page_and_hide_form(e, marker);
    				});

					GEvent.addListener(marker, "dragend", function() {
						$("#location_lat" ).val(marker.getPoint().lat());
						$("#location_long").val(marker.getPoint().lng());
    					marker.openInfoWindow(message);
					});

					$("#location_reset").bind("click", function(e) {
						reset_page_and_hide_form(e, marker);
					});

					$(".reset").bind("click", function(e) {
						reset_page_and_hide_form(e, marker);
					});

				}

				var reset_page_and_hide_form = function(e, marker) {
					if (marker) {
						map.removeOverlay(marker);
					}
					$("#locationForm").validate().resetForm();
					$("#ngv_deco_form").hide();
					$("#ngv_deco_place").hide();
					$("#ngv_deco_start").show();
                    
                    $("#explore_initial").hide();
                    $("#explore_add").show();
                    
					window.setTimeout(function() {
						GEvent.addListener(map, "click", handle_new_user_location);
					}, 500);
				}
                
                var stop_add_mode = function() {
                    $("#explore_initial").show();
                    $("#explore_add").hide();
                    GEvent.clearInstanceListeners(map);
                }

				var set_map_centre = function(e) {
					var chosen_location = this.options[this.options.selectedIndex].value || this.value;
					var geocoder = new GClientGeocoder();
					
                    if (chosen_location.indexOf("World") == 0) {
                        zoom = 2;
                        point = new GLatLng(12, 125);
                        map.setCenter(point, zoom);
                        return;
                    }
                    
					geocoder.getLatLng(chosen_location, function(point) {

						if(!point) { return; }
						var zoom = 15;

						if ( chosen_location.indexOf("Australia") == 0 )
						{
							// Zoom to Country Level
							zoom = 4;
						} 
						else if ( chosen_location.indexOf("New Zealand") == 0 )
						{
							zoom = 5;
						} 
						else if (
								chosen_location.indexOf("ACT, Australia") == 0 || 
								chosen_location.indexOf("New South Wales, Australia") == 0 || 
								chosen_location.indexOf("Northern, Territory, Australia") == 0 || 
								chosen_location.indexOf("Queensland, Australia") == 0 || 
								chosen_location.indexOf("South Australia, Australia") == 0 || 
								chosen_location.indexOf("Tasmania, Australia") == 0 || 
								chosen_location.indexOf("Victoria, Australia") == 0 || 
								chosen_location.indexOf("Western Australia, Australia") == 0
							)
						{
							// Zoom to state level.
							zoom = 6;
						}
						else
						{
							// Zoom to city level.
							zoom = 15;
						}
						map.setCenter(point, zoom);
					});
					return;
				}
                
                
				$("#locationForm").submit(ensure_form_is_valid);

				$("#ngv_flyto").bind("change", set_map_centre);

				$("#ngv_new_location").bind("click", function(e){
                    
                    $("#explore_initial").hide();
                    $("#explore_add").show();
                    
					GEvent.addListener(map, "click", handle_new_user_location);
				});

				$("#location_reset").bind("click", function(e) {
					reset_page_and_hide_form(e, null);
				});
                
                $("#stop_add_location_button").bind("click", function(e) {
					stop_add_mode(e);
				});
                
                function showResponse(responseText, statusText) {
                    if (responseText) {
                        alert(responseText);
                    }
                    else {
                        $("#intro_block").hide();
                        reset_page_after_submit(map, marker);
                    }    
                }
                
                // bind 'myForm' and provide a simple callback function 
                var formPluginOptions = {
                    beforeSubmit: ensure_form_is_valid,
                    success:      showResponse
                };
                
                $('#locationForm').ajaxForm(formPluginOptions); 

				$.getJSON("api/locations/list", function(json) {
					$.each(json.locations, function(i,location) {
							map.addOverlay(create_marker_for_location(location, map));
						});
					});
				}
			}
		GSearch.setOnLoadCallback(initialize);
