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(38.930835, 141.135632), 10);

	// 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 admin_load(){
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		map.setCenter(new GLatLng(38.930835, 141.135632), 10);
		document.getElementById("lat").value = map.getCenter().lat();
		document.getElementById("lng").value = map.getCenter().lng();
		document.getElementById("zoom").value = map.getZoom();
	}
	GEvent.addListener(map, "moveend", function() {
		map.clearOverlays();
		var center = map.getCenter();
		var zoom=map.getZoom();
//		document.getElementById("latlng").innerHTML = "（"+center.lat()+","+center.lng()+","+zoom+"）";});
		document.getElementById("lat").value = center.lat();
		document.getElementById("lng").value = center.lng();
		document.getElementById("zoom").value = zoom;
		
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var points = [];
		points[0] = new GLatLng(center.lat()+(northEast.lat()-center.lat())/4, center.lng()+(northEast.lng()-center.lng())/4);
		points[1] = new GLatLng(center.lat(), center.lng());
		points[2] = new GLatLng(center.lat(), center.lng()+(northEast.lng()-center.lng())/8);
		var polyline = new GPolyline(points,"#FF0000", 3, 1);
		map.addOverlay(polyline);
		});
}

function data_load(){
	var request = GXmlHttp.create();
//	request.open("GET", "http://ax3sp/proceed/ichinoseki/db_xml.php?flag=data&dum=Math.random()", true);
//	request.open("GET", "http://a20/ichinoseki/db_xml.php?flag=data&dum=Math.random()", true);
//	request.open("GET", "http://itp003/ichinoseki/db_xml.php?flag=data&dum=Math.random()", true);
//	request.open("GET", "http://www.itpstyle.com/ichinoseki/db_xml.php?flag=data&dum=Math.random()", true);
	request.open("GET", "http://maplog.wgis.net/demo/db_xml.php?flag=data&dum=Math.random()", 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 contents=xmlDoc.documentElement.getElementsByTagName("contents");
			var contents_url=xmlDoc.documentElement.getElementsByTagName("contents_url");
			var lngs=xmlDoc.documentElement.getElementsByTagName("lng");
			var lats=xmlDoc.documentElement.getElementsByTagName("lat");
			var category_id=xmlDoc.documentElement.getElementsByTagName("category_id");
			for (var i = 0; i < datas.length; i++) {
//alert(category_id[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='index.php?data_id="+id[i].childNodes[0].nodeValue+"'>"+titles[i].childNodes[0].nodeValue+"</a>";
				var icon_id=category_id[i].childNodes[0].nodeValue;
				html=html+"<br><p style='font-size:9pt;text-align:left;width:200px'><img src='"+contents_url[i].childNodes[0].nodeValue+"' style='width:50px;float:left'>"+contents[i].childNodes[0].nodeValue+"</p>";
				var marker =createMarker(point, html,icon_id);
				map.addOverlay(marker);
			}
		}
	}
	request.send(null);
}

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

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

	switch(icon_id){
	case "0":
		var marker = new GMarker(point,gIcon0);
		break;
	case "1":
		var marker = new GMarker(point,gIcon1);
		break;
	case "2":
		var marker = new GMarker(point,gIcon2);
		break;
	case "3":
		var marker = new GMarker(point,gIcon3);
		break;
	case "4":
		var marker = new GMarker(point,gIcon4);
		break;
	}

	// クリックされたらこのマーカーのインデックスを情報ウィンドウに表示する。
	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_;
    }
