function load() {
	var input = document.getElementById("q");
	input.focus();

	if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl());
	map.setCenter(new GLatLng(36, 136), 1);

	// Initialize the local searcher
	gLocalSearch = new GlocalSearch();
	gLocalSearch.setCenterPoint(map);
	gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);

	// Execute the initial search
	gLocalSearch.execute(input.value);
	}

	first_move();
	data_load();

	GEvent.addListener(map, "moveend", function() {
		var center = map.getCenter();
		var zoom=map.getZoom();
		document.getElementById("latlng").innerHTML = "（"+center.lat()+","+center.lng()+","+zoom+"）";});
}

function data_load(){
	var request = GXmlHttp.create();
	request.open("GET", "data.xml", true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var xmlDoc = request.responseXML;
			var datas = xmlDoc.documentElement.getElementsByTagName("data");
			var id=xmlDoc.documentElement.getElementsByTagName("id");
			var titles=xmlDoc.documentElement.getElementsByTagName("title");
			var sub_titles=xmlDoc.documentElement.getElementsByTagName("sub_title");
			var lngs=xmlDoc.documentElement.getElementsByTagName("lng");
			var lats=xmlDoc.documentElement.getElementsByTagName("lat");
			for (var i = 0; i < datas.length; i++) {
//alert(lngs[i].childNodes[0].nodeValue);
//				var point = new GPoint(parseFloat(lngs[i].text),parseFloat(lats[i].text));
				var point = new GLatLng(parseFloat(lats[i].childNodes[0].nodeValue),parseFloat(lngs[i].childNodes[0].nodeValue));
				var html="<a href='data-"+id[i].childNodes[0].nodeValue+".html'>"+titles[i].childNodes[0].nodeValue+"</a>";
				html=html+"<br>"+sub_titles[i].childNodes[0].nodeValue;
				var marker =createMarker(point, html);
				map.addOverlay(marker);
			}
		}
	}
	request.send(null);
}

function go_location(x,y,z){
	map.setCenter(new GLatLng(x, y), z);
}

// 与えられた文字を表示する情報ウィンドウを持つマーカーを生成する。
function createMarker(point, html) {
	var marker = new GMarker(point);

	// クリックされたらこのマーカーのインデックスを情報ウィンドウに表示する。
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});

	return marker;
}

    // Called when Local Search results are returned, we clear the old
    // results and load the new ones.
    function OnLocalSearch() {
      if (!gLocalSearch.results) return;
      var searchWell = document.getElementById("searchwell");

      // Clear the map and the old search well
      searchWell.innerHTML = "";
      for (var i = 0; i < gCurrentResults.length; i++) {
        if (!gCurrentResults[i].selected()) {
          map.removeOverlay(gCurrentResults[i].marker());
        }
      }

      gCurrentResults = [];
      for (var i = 0; i < gLocalSearch.results.length; i++) {
        gCurrentResults.push(new LocalResult(gLocalSearch.results[i]));
      }

      // move the map to the first result
      var first = gLocalSearch.results[0];
      if(first==null){
	  }else{
     // map.recenterOrPanToLatLng(new GPoint(parseFloat(first.lng), parseFloat(first.lat)));
     // map.recenterOrPanToLatLng(new GLatLng(parseFloat(first.lat), parseFloat(first.lng)));
        map.setCenter(new GLatLng(parseFloat(first.lat), parseFloat(first.lng)), map.getZoom());
	  }
    }

    // Cancel the form submission, executing an AJAX Search API search.
    function CaptureForm(form) {
      gLocalSearch.execute(form["q"].value);
      return false;
    }


    // A class representing a single Local Search result returned by the
    // Google AJAX Search API.
    function LocalResult(result) {
      this.result_ = result;
      this.resultNode_ = this.unselectedHtml();
      document.getElementById("searchwell").appendChild(this.resultNode_);
      map.addOverlay(this.marker(gSmallIcon));
    }

    // 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 marker = new GMarker(new GLatLng(parseFloat(this.result_.lat),
                                         parseFloat(this.result_.lng)),
                               opt_icon);
      GEvent.bind(marker, "click", this, function() {
        marker.openInfoWindow(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
        map.removeOverlay(this.marker());
        this.marker_ = null;
        map.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() {
        map.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_;
    }
