   // Our global state
   var gLocalSearch;
	var gMap;
	var gSelectedResults = [];
	var gCurrentResults = [];
	var gSearchForm;
	var myplaces = new Array();
	var myresults = new Array();
	var varlong;
	var varlat;
	
   // Create our "tiny" marker icon
	var gSmallIcon = new GIcon();	
	gSmallIcon.shadow = "http://www.nottinghamconferences.co.uk/Images/General/pin_shadow.png";
	gSmallIcon.iconSize = new GSize(12, 20);
	gSmallIcon.shadowSize = new GSize(22, 20);
	gSmallIcon.iconAnchor = new GPoint(6, 20);
	gSmallIcon.infoWindowAnchor = new GPoint(5, 1);
	

	
   
	// Set up the map and the local searcher.
   function OnLoad() {
		// create search form
		gSearchForm = new GSearchForm(false, document.getElementById("searchform"));
		gSearchForm.setOnSubmitCallback(null, CaptureForm);
		// Initialize the map
		gMap = new GMap(document.getElementById("map"));
		gMap.addControl(new GSmallMapControl());
		gMap.addControl(new GMapTypeControl());
		gMap.setCenter(new GLatLng(37.4419, 0), 13);
		gMap.zoomTo(2);
		
		// Initialize the local searcher
		gLocalSearch = new GlocalSearch();
		gLocalSearch.setCenterPoint(gMap);
		// set the call back method
		gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);
		
		gSearchForm.execute( 'NG7 2RJ' );
   }

   /* 
		OnLocalSearch
		Called when Local Search results are returned

	*/
   function OnLocalSearch() {
     	if (gLocalSearch.results){
			gMap.closeInfoWindow();
     		var searchWell = document.getElementById("searchwell");
			
			gCurrentResults.push(new LocalResult(gLocalSearch.results[0]));
			var first = gLocalSearch.results[0];
			//setGeoPos( parseFloat(first.lng), parseFloat(first.lat)); // calculate distance
			
			gMap.recenterOrPanToLatLng(new GPoint(parseFloat(first.lng), parseFloat(first.lat)));
			//alert('Map has completed loading!');
			return;
		}else{
			alert("Couldn't initiate Search - the postcode supplied appears to be invalid.");
		}
	}
	
	
	
	
	
	/*
	unrelated methods for postcode generation
	... kept them for use with map if required
	*/
	
	
   // Cancel the form submission, executing an AJAX Search API search.
   function CaptureForm(searchForm) {
     gLocalSearch.execute(searchForm.input.value);
     return false;
   }

   // A class representing a single Local Search result returned by the
   // Google AJAX Search API.
   function LocalResult(result) {
	if(result){
     	this.result_ = result;
     	this.resultNode_ = this.unselectedHtml();
     	document.getElementById("searchwell").appendChild(this.resultNode_);
		
		var tinyIcon = new GIcon(gSmallIcon);	
		tinyIcon.image = "http://www.nottinghamconferences.co.uk/Images/General/pin_" + document.getElementById('venue-details').value + ".png";
		
		
		gMap.addOverlay(this.marker(tinyIcon));		
		gMap.centerAtLatLng(new GLatLng(parseFloat(this.result_.lat),parseFloat(this.result_.lng)));
		
//		if(document.getElementById('venue-details').value != 'home'){
		  this.marker_.openInfoWindowHtml(getAddress(document.getElementById('venue-details').value));
//		}
		
		

	}else{
		alert("No Results Returned!\nPlease check your Postcode for errors.");
		return false;
	}
   }





   // Returns the GMap marker for this result, creating it with the given
   // icon if it has not already been created.
   LocalResult.prototype.marker = function(opt_icon) {
     if (this.marker_) return this.marker_;
	 var varVenue = document.getElementById('venue-details').value;
	 
	 if(varVenue == 'yellow'){
		 this.result_.lat = 52.937266;
		 this.result_.lng = -1.195364;
	 }

	var marker = new GMarker(new GLatLng(parseFloat(this.result_.lat),parseFloat(this.result_.lng)),opt_icon);

    GEvent.bind(marker, "click", this, function() {
//	   if(document.getElementById('venue-details').value != 'home'){
         marker.openInfoWindowHtml(getAddress(document.getElementById('venue-details').value)); //this.selected() ? this.selectedHtml() :this.unselectedHtml()
//	   }
     }); 
     this.marker_ = marker;
     return marker;
   }


	// "Saves" this result if it has not already been saved
   LocalResult.prototype.select = function() {
		if (!this.selected()) {
			 this.selected_ = true;
			// Remove the old marker and add the new marker
			gMap.removeOverlay(this.marker());
			this.marker_ = null;
			gMap.addOverlay(this.marker(G_DEFAULT_ICON));
			// Add our result to the saved set
			//--document.getElementById("selected").appendChild(this.selectedHtml());
			// Remove the old search result from the search well
			this.resultNode_.parentNode.removeChild(this.resultNode_);
			}
		}

   // Returns the HTML we display for a result before it has been "saved"
   LocalResult.prototype.unselectedHtml = function() {
     var container = document.createElement("div");
     container.className = "unselected";
     container.appendChild(this.result_.html.cloneNode(true));
     var saveDiv = document.createElement("div");
     saveDiv.className = "select";
     saveDiv.innerHTML = "Save this location";
     GEvent.bindDom(saveDiv, "click", this, function() {
       gMap.closeInfoWindow();
       this.select();
       gSelectedResults.push(this);
     });
     container.appendChild(saveDiv);
     return container;
   }

   // Returns the HTML we display for a result after it has been "saved"
   LocalResult.prototype.selectedHtml = function() {
     return this.result_.html.cloneNode(true);
   }

   // Returns true if this result is currently "saved"
   LocalResult.prototype.selected = function() {
     return this.selected_;
   }
   
   function getAddress(venue){
	 switch (venue){
		default:
		case 'red':
		 var bubbleCopy = "<div class=\"bubble-copy\"><img src=\"http://www.nottinghamconferences.co.uk/Images/General/logo_emcc.gif\" width=\"206\" height=\"30\" /><p><strong>Tel:</strong> 0115 846 8000<br /><strong>Fax:</strong> 0115 951 5009<br /><br />University Park, Nottingham NG7 2RJ</p></div>";
		break;

		case 'blue':
		 var bubbleCopy = "<div class=\"bubble-copy\"><img src=\"http://www.nottinghamconferences.co.uk/Images/General/logo_jubileecampus.gif\" width=\"113\" height=\"28\" /><p><strong>Tel:</strong> 0115 846 8000<br /><strong>Fax:</strong> 0115 951 5009<br /><br />Wollaton Road, Nottingham NG8 1BB</p></div>";
		break;

		case 'yellow':
		 var bubbleCopy = "<div class=\"bubble-copy\"><img src=\"http://www.nottinghamconferences.co.uk/Images/General/logo_universitypark.gif\" width=\"131\" height=\"30\" /><p><strong>Tel:</strong> 0115 846 8000<br /><strong>Fax:</strong> 0115 951 5009<br /><br />University Park, Nottingham NG7 2RD</p></div>";
		break;

		case 'green':
		 var bubbleCopy = "<div class=\"bubble-copy\"><img src=\"http://www.nottinghamconferences.co.uk/Images/General/logo_suttonbonington.gif\" width=\"139\" height=\"29\" /><p><strong>Tel:</strong> 0115 846 8000<br /><strong>Fax:</strong> 0115 951 5009<br /><br />Sutton Bonington, Loughborough LE12 5RD</p></div>";		
		break;
	 }
	 
	 return bubbleCopy;
   }

  	GSearch.setOnLoadCallback(OnLoad);
