//<![CDATA[
	//var localSearch = new GlocalSearch();

	var baseIcon;

	function levels() {
	}
	levels.prototype = new GControl();

	// Creates a one DIV for each of the buttons and places them in a container
	// DIV which is returned as our control element. We add the control to
	// to the map container and return the element for the map class to
	// position properly.
	levels.prototype.initialize = function(map) {
	  var container = document.createElement("div");

	
	  var streetDiv = document.createElement("div");
	  this.setButtonStyle_(streetDiv);
	  container.appendChild(streetDiv);
	  streetDiv.appendChild(document.createTextNode("View at Street level"));
	  GEvent.addDomListener(streetDiv, "click", function() {
        map.setZoom(16);
		//map.closeInfoWindow();
	  });

	  var localDiv = document.createElement("div");
	  this.setButtonStyle_(localDiv);
	  container.appendChild(localDiv);
	  localDiv.appendChild(document.createTextNode("View at Local level"));
	  GEvent.addDomListener(localDiv, "click", function() {
        map.setZoom(14);
		//map.closeInfoWindow();
	  });

	  var cityDiv = document.createElement("div");
	  this.setButtonStyle_(cityDiv);
	  container.appendChild(cityDiv);
	  cityDiv.appendChild(document.createTextNode("View at City level"));
	  GEvent.addDomListener(cityDiv, "click", function() {
        map.setZoom(11);
		//map.closeInfoWindow();
	  });

	  var regionDiv = document.createElement("div");
	  this.setButtonStyle_(regionDiv);
	  container.appendChild(regionDiv);
	  regionDiv.appendChild(document.createTextNode("View at Regional level"));
	  GEvent.addDomListener(regionDiv, "click", function() {
        map.setZoom(8);
		//map.closeInfoWindow();
	  });

	  var countryDiv = document.createElement("div");
	  this.setButtonStyle_(countryDiv);
	  container.appendChild(countryDiv);
	  countryDiv.appendChild(document.createTextNode("View at National level"));
	  GEvent.addDomListener(countryDiv, "click", function() {
        map.setZoom(5);
		//map.closeInfoWindow();
	  });

	  map.getContainer().appendChild(container);
	  return container;
	}

	// By default, the control will appear in the top left corner of the
	// map with 7 pixels of padding.
	levels.prototype.getDefaultPosition = function() {
	  return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(7, 41));
	}

	// Sets the proper CSS for the given button element.
	levels.prototype.setButtonStyle_ = function(button) {
	  button.style.textDecoration = "underline";
	  button.style.color = "#000";
	  button.style.backgroundColor = "white";
	  button.style.font = "1.1em Arial";
	  button.style.border = "1px solid black";
	  button.style.padding = "2px";
	  button.style.marginBottom = "1px";
	  button.style.textAlign = "center";
	  button.style.textDecoration = "none";
	  button.style.width = "193px";
	  button.style.cursor = "pointer";
	}

    function load(mapID) {
      if (GBrowserIsCompatible()) {
		var mapObj = document.getElementById(mapID);
        var map = new GMap2(mapObj);
		map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl(),new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(7,20)));
		map.setCenter(new GLatLng(53.797812,-1.554866), 5);
		//map.setMapType(G_HYBRID_MAP);
		//map.setMapType(G_SATELLITE_MAP);


		map.addControl(new customlinks());
		//map.addControl(new YSliderControl());

		
		GMap2.prototype.wheelZoom = function(event) {
			if(event.cancelable) event.preventDefault(); map.closeInfoWindow(); 
			if((event.detail || -event.wheelDelta) < 0) {map.zoomIn();} else {map.zoomOut();} return false; 
		}

		baseIcon = new GIcon();
		baseIcon.shadow = "http://www.hcwa.co.uk/_lib/_images/_structure/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);

		//GEvent.addDomListener(mapObj, "DOMMouseScroll", map.wheelZoom);
	//	GEvent.addDomListener(mapObj, "mousewheel", map.wheelZoom);

		if(false){
			GEvent.addListener(map,
                 "singlerightclick",
                 function(point)
                 {

                        var message = "Coordinates:  ";
                        var coor = map.fromContainerPixelToLatLng(point);
						//console.log('\t\t"lat"=>"'+(coor.en)+'",\n\t\t"lng"=>"'+(coor.hb)+'",\n');
						console.log(map.getCenter());
                 }); 
			
		}
		

	  }
		map.addControl(new levels());
	
	}
	

		function customlinks() {
		}
		customlinks.prototype = new GControl();

		// Creates a one DIV for each of the buttons and places them in a container
		// DIV which is returned as our control element. We add the control to
		// to the map container and return the element for the map class to
		// position properly.
		customlinks.prototype.initialize = function(map) {
			var container = document.createElement("div");

			container.className = "linksbox";
			container.style.backgroundColor = "#fff";
			container.style.border = "1px solid #000";
			container.style.padding = "5px";

			var headingDiv = document.createElement("h2");
			headingDiv.className = 'linkblockheading';
			container.appendChild(headingDiv);
			headingDiv.appendChild(document.createTextNode("Member firms"));


			var introDiv = document.createElement("p");
			introDiv.className = 'linkblockintro';
			container.appendChild(introDiv);
			introDiv.appendChild(document.createTextNode("Click on the links below or markers on the map to view respective information for each office."));

			var side_bar_html = "";
    		var gmarkers = [];
			var linkarray = [];
			var points = [];
			var ms;
			var lastmarker


			
			
			// Read the data from example.xml
			var request = GXmlHttp.create();
			request.open("GET", "coords.xml.php", true);
			request.onreadystatechange = function() {
				if (request.readyState == 4) {
					var xmlDoc = GXml.parse(request.responseText);
					// obtain the array of markers and loop through it
					var markers = xmlDoc.documentElement.getElementsByTagName("marker");

					for (var i = 0; i < markers.length; i++) {
						// obtain the attribues of each marker
						var lat = parseFloat(markers[i].getAttribute("lat"));
						var lng = parseFloat(markers[i].getAttribute("lng"));
						//usePointFromPostcode(markers[i].getAttribute("postcode"),showcode);//new GLatLng(lat,lng);
						points[i] = new GLatLng(lat,lng);
						var html1 = GXml.value(markers[i].getElementsByTagName("infowindow")[0]);
						var html2 = GXml.value(markers[i].getElementsByTagName("infowindow2")[0]);
						var heading = markers[i].getAttribute("heading");
						var listtitle = markers[i].getAttribute("listtitle");
						var firmid = markers[i].getAttribute("firmid");
						var comp = markers[i].getAttribute("company");
						var loc = markers[i].getAttribute("location");
						var flag = markers[i].getAttribute("flag");
						var sub = markers[i].getAttribute("sub");
						if(markers[i+1]){
							var nxtsub = markers[i+1].getAttribute("sub");
						}
						//console.log(sub);
						var company = markers[i].getAttribute("label");
						var label1 = "Contact";//markers[i].getAttribute("label1");
						var label2 = (firmid==29 || comp=="ASM")?"Directors":"Partners";//markers[i].getAttribute("label2");
						//console.log(label);

						linkarray[i] = document.createElement("div");
						linkarray[i].className = 'linkblock';
						container.appendChild(linkarray[i]);
						//var buttonnode = document.createElement("div"
						//linkarray[i].appendChild(buttonnode);
						//document.createTextNode((i+1)+".) "+label)
						linkarray[i].innerHTML = "";
						var temp = "";
						if(heading){
							temp += "<h3 class=\"subhead\">"+heading+"</h3>";
						}else{
							if(sub==0){
								//temp += "</div>";
							}
						}
						
						if(sub==1){
							temp += "<span class=\"sublink"+(nxtsub==0?" endsub":"")+"\">"+listtitle+"</span>";
						}else{
							temp += "<span class=\"mainlink\">"+listtitle+"</span>";
						}

						//console.log(temp);
						linkarray[i].innerHTML = temp;
						
						//console.log(flag);

						// create the marker
						var marker = createMarker(points[i],label1,html1,label2,html2,flag==1);
						createLinker(marker,linkarray[i]);
						map.addOverlay(marker);
					}
					// put the assembled side_bar_html contents into the side_bar div
					//document.getElementById("side_bar").innerHTML = side_bar_html;
				}
			}
			request.send(null);
		  
// This function picks up the click and opens the corresponding info window

			function createMarker(point,label1,html1,label2,html2,itype) {

				var icon1 = new GIcon(baseIcon);
				icon1.image = "http://www.hcwa.co.uk/_lib/_images/_structure/marker"+(itype==1?"":"2")+".png";

				// Set up our GMarkerOptions object
				var markerOptions = {icon:icon1};

				var marker = new GMarker(point,markerOptions);
				marker.bindInfoWindowTabsHtml([new GInfoWindowTab(label1,html1),new GInfoWindowTab(label2,html2)],{maxWidth:280, maxHeight: 300,autoScroll:true});
				//marker.bindInfoWindowHtml(html);
				GEvent.addDomListener(marker, "click", function() {
					showmarker(marker);
				});
				
				// save the info we need to use later for the side_bar
				gmarkers[ms] = marker;
				// add a line to the side_bar html
				ms++;
				return marker;
			}

			function createLinker(marker,obj){
				GEvent.addDomListener(obj, "click", function() {
					GEvent.trigger(marker, "click");
					//console.log(map.getZoom());
					//alert(obj);
				});
			}

			function showmarker(marker){
				map.panTo(marker.getPoint());					
				lastmarker = marker
				//console.log(map.getZoom());
				if(map.getZoom()<6){
					map.setZoom(6);
					//console.log(marker.getPoint());
					//map.setCenter(marker.getPoint());
					//map.panDirection(200,  0)
				}
				//console.log(marker.T);
				//console.log("marker2: "+marker.getPoint());
				//map.setCenter(new GLatLng(marker.T.y+1.5,(marker.T.x)));
				map.setCenter(marker.getPoint());
			}	
			
			GEvent.addListener(map,"infowindowclose", function() {
				if(lastmarker){
					map.panTo(lastmarker.getPoint());
				}
			}); 
		
			map.getContainer().appendChild(container);
			return container;
		}

		// By default, the control will appear in the top left corner of the
		// map with 7 pixels of padding.
		customlinks.prototype.getDefaultPosition = function() {
		  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7,7));//135
		
		}

function usePointFromPostcode(postcode, callbackFunction) {
  
  localSearch.setSearchCompleteCallback(null,
	function() {
	  
	  if (localSearch.results[0]) {    
		var resultLat = localSearch.results[0].lat;
		var resultLng = localSearch.results[0].lng;
		var point = new GLatLng(resultLat,resultLng);
		console.log("update hcwa_directory set coordx = '"+resultLat+"', coordy = '"+resultLng+"' where postcode = '"+localSearch.results[0].postalCode+"';");//
		//console.log(localSearch.results[0]);
		callbackFunction(point,postcode);
	  }else{
		alert("Postcode not found!");
	  }
	});  
	
 // console.log(postcode);
  localSearch.execute(postcode + ", UK");
}

function showcode(v1,v2){
	//alert(v1+v2)
}




//]]>
