﻿// JavaScript Document
    //<![CDATA[

    // Our global state
    var gLocalSearch;
    var gMap;
    var gSelectedResults = [];
    var gCurrentResults = [];
    var gSearchForm;

// Create our "tiny" marker icon
    var gSmallIcon = new GIcon();
    gSmallIcon.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
    gSmallIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_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(input,Zoom) {

      gSearchForm = new GSearchForm(false, document.getElementById("searchform"));
      gSearchForm.setOnSubmitCallback(null, CaptureForm);
      //gSearchForm.input.focus();

      // Initialize the map
      gMap = new GMap(document.getElementById("map"));
	  //gMap.addControl(new GLargeMapControl());
	    
		gMap.addControl(new TextualZoomControl());

      gMap.addControl(new GMapTypeControl());
      gMap.setCenter(new GLatLng(23.128458,113.305627), Zoom, G_HYBRID_MAP);

/*
GEvent.addListener(gMap, "moveend", function() {
  var center = gMap.getCenter();
   
   window.clipboardData.setData("Text","http://www.52maps.com/Mymapsmarker_show.asp?lat=" + center.lat() + "&lng=" + center.lng() + "&ZoomLevel=" + gMap.getZoom() + "&MapType=" + gMap.getCurrentMapType().getName());

  document.getElementById("message").innerHTML = "当前地图的地址如下，请复制后发送给您的好友一同欣赏！<br /><textarea cols=130 rows=2>http://www.52maps.com/mapsmarker_show.asp?lat=" + center.lat() + "&lng=" + center.lng() + "&ZoomLevel=" + gMap.getZoom() + "&MapType=" + gMap.getCurrentMapType().getName()+"</textarea>";
  document.getElementById("x_lng").value = center.lng();
  document.getElementById("x_lat").value = center.lat();
  document.getElementById("x_ZoomLevel").value = gMap.getZoom();
  document.getElementById("x_MapType").value = gMap.getCurrentMapType().getName();
  });
*/	  

      // Initialize the local searcher
      gLocalSearch = new GlocalSearch();
      gLocalSearch.setCenterPoint(gMap);
      gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);

      // Execute the initial search
      gSearchForm.execute(input);

//        GEvent.addListener(gMap, 'click', function(overlay, point) {
//		  if (overlay) {
//		  	gMap.clearOverlays();
//			var center = gMap.getCenter();
//	  		document.getElementById("x_lng").value = 0;
//	  		document.getElementById("x_lat").value = 0;
//	  		document.getElementById("x_ZoomLevel").value = gMap.getZoom();
//	  		document.getElementById("x_MapType").value = gMap.getCurrentMapType().getName();
//	  		//document.getElementById("textlink").innerHTML = "<p style='font-size:13px;color:Red;'>请在上面的地图上单击要标注的地点，以便生成链接！</p>";
//		  } else if (point) {
//		 	gMap.clearOverlays();
//			gMap.addOverlay(new GMarker(point));	
//			var center = gMap.getCenter();  
//	  		document.getElementById("x_lng").value = point.x;
//	  		document.getElementById("x_lat").value = point.y;
//	  		document.getElementById("x_ZoomLevel").value = gMap.getZoom();
//	  		document.getElementById("x_MapType").value = gMap.getCurrentMapType().getName();
//	  		//var caption=document.all.caption.value.replace(/\r\n/g,'<br>');
//	  		//caption=caption.replace(/\n/g,'<br>');
//	  		//caption=escape(caption);
//	  		//document.getElementById("textlink").innerHTML = "<p style='font-size:13px;color:Red;'>当前地图的地址如下，请复制后发送给您的好友一同欣赏！</p><textarea cols=72 rows=3 onfocus='this.select();'>http://www.52maps.com/mapsmarker_show.asp?lat=" + document.getElementById("x_lat").value + "&lng=" + document.getElementById("x_lng").value + "&ZoomLevel=" + document.getElementById("x_ZoomLevel").value + "&MapType=" + document.getElementById("x_MapType").value +" &caption=" + caption + "</textarea>";
//		 }
//		});
		
    }

    // Called when Local Search results are returned, we clear the old
    // results and load the new ones.
    function OnLocalSearch() {
		try{
      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()) {
          gMap.removeOverlay(gCurrentResults[i].marker());
        }
      }

      gCurrentResults = [];
      for (var i = 0; i < gLocalSearch.results.length; i++) {
        gCurrentResults.push(new LocalResult(gLocalSearch.results[i]));
      }

      var attribution = gLocalSearch.getAttribution();
      if (attribution) {
        document.getElementById("searchwell").appendChild(attribution);
      }

      // move the map to the first result
      var first = gLocalSearch.results[0];
      gMap.recenterOrPanToLatLng(new GPoint(parseFloat(first.lng), parseFloat(first.lat)));

		if (get_cookie('popped')!='prompt'){
		//alert("请自行放大、缩小和拖动地图！");
		document.cookie="popped=prompt"
		}

		}
	catch(e){
		alert("找不到“"+form1.input.value+"”，建议您扩大搜索；\n\n例如在搜索不到县的情况下，搜索上一级的市；\n\n搜索国外地区，请使用英文名，例如：new york");
		}
    }

    // Cancel the form submission, executing an AJAX Search API search.
    function CaptureForm(searchForm) {
		//gMap.setZoom(6);
      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) {
      this.result_ = result;
      this.resultNode_ = this.unselectedHtml();
      //document.getElementById("searchwell").appendChild(this.resultNode_);
      gMap.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());
		//gMap.openInfoWindow(gMap.getCenterLatLng(),
        //           document.createTextNode(document.form1.input.value));
		marker.openInfoWindow("<strong>"+document.form1.input.value+"</strong><br><br><p><a href=http://www.nanpengyou.com target=_blank>在当地找网友</a></p>");
      });
      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_;
    }
	
	
	
// A TextualZoomControl is a GControl that displays textual "Zoom In"
    // and "Zoom Out" buttons (as opposed to the iconic buttons used in
    // Google Maps).
    function TextualZoomControl() {
    }
    TextualZoomControl.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.
    TextualZoomControl.prototype.initialize = function(map) {
      var container = document.createElement("div");

//	  var tuDiv = document.createElement("div");
//      this.setButtonStyle_(tuDiv);
//      container.appendChild(tuDiv);
//      tuDiv.appendChild(document.createTextNode("实景图"));
//      GEvent.addDomListener(tuDiv, "click", function() {
//        window.location="http://www.nanpengyou.com/wallpaper_Album.asp";
//      }
//	  );
	  
      var zoomOutDiv = document.createElement("div");
      this.setButtonStyle_(zoomOutDiv);
      container.appendChild(zoomOutDiv);
      zoomOutDiv.appendChild(document.createTextNode("放大"));
      GEvent.addDomListener(zoomOutDiv, "click", function() {
        map.zoomIn();
      });
	  
	  var zoomInDiv = document.createElement("div");
      this.setButtonStyle_(zoomInDiv);
      container.appendChild(zoomInDiv);
      zoomInDiv.appendChild(document.createTextNode("缩小"));
      GEvent.addDomListener(zoomInDiv, "click", function() {
        map.zoomOut();
      });
	  
	  var moveLeftDiv = document.createElement("div");
      this.setButtonStyle_(moveLeftDiv);
      container.appendChild(moveLeftDiv);
      moveLeftDiv.appendChild(document.createTextNode("左移"));
      GEvent.addDomListener(moveLeftDiv, "click", function() {
		map.panDirection(2, 0)
      });
	  var moveRightDiv = document.createElement("div");
      this.setButtonStyle_(moveRightDiv);
      container.appendChild(moveRightDiv);
      moveRightDiv.appendChild(document.createTextNode("右移"));
      GEvent.addDomListener(moveRightDiv, "click", function() {
		map.panDirection(-2, 0)
      });
	  	  var moveTopDiv = document.createElement("div");
      this.setButtonStyle_(moveTopDiv);
      container.appendChild(moveTopDiv);
      moveTopDiv.appendChild(document.createTextNode("上移"));
      GEvent.addDomListener(moveTopDiv, "click", function() {
		map.panDirection(0, 2)
      });
	  	  var moveXiaDiv = document.createElement("div");
      this.setButtonStyle_(moveXiaDiv);
      container.appendChild(moveXiaDiv);
      moveXiaDiv.appendChild(document.createTextNode("下移"));
      GEvent.addDomListener(moveXiaDiv, "click", function() {
		map.panDirection(0, -2)
      });	  

//	  var quanDiv = document.createElement("div");
//      this.setButtonStyle_(quanDiv);
//      container.appendChild(quanDiv);
//      quanDiv.appendChild(document.createTextNode("全屏"));
//      GEvent.addDomListener(quanDiv, "click", function() {
//        window.location="/ditu.asp";
//      }
//	  );

	  
      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.
    TextualZoomControl.prototype.getDefaultPosition = function() {
      return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
    }

    // Sets the proper CSS for the given button element.
    TextualZoomControl.prototype.setButtonStyle_ = function(button) {
      button.style.textDecoration = "none";
      button.style.color = "#000000";
      button.style.backgroundColor = "white";
      button.style.font = "small Arial";
      button.style.border = "1px solid black";
      button.style.padding = "1px";
      button.style.marginBottom = "3px";
      button.style.textAlign = "center";
      button.style.width = "35px";
      button.style.cursor = "pointer";
    }


	function GetUrl(){
		var center = gMap.getCenter();
		window.clipboardData.setData("Text","http://www.52maps.com/mapsmarker_show.asp?lat=" + center.lat() + "&lng=" + center.lng() + "&ZoomLevel=" + gMap.getZoom() + "&MapType=" + gMap.getCurrentMapType().getName());
		alert("链接已经复制，请发送给您的好友吧 ^_^");
	}

function Checksubmit2()
{
    if (document.db.x_subject.value=="")
    {
        msg="请填写地标名称，尽量具体，例如某省某市某建筑"
        alert(msg);
         document.db.x_subject.focus();
    return false;
    }
    if (document.db.x_Username.value=="")
    {
        msg="请填写您的网名"
        alert(msg);
         document.db.x_Username.focus();
    return false;
    }
    if (document.db.x_explain.value=="")
    {
        msg=document.db.x_subject.value + "详细说明，多多少少也介绍一点啊\n你可以从Google里搜索一点介绍，复制进来！"
        alert(msg);
         document.db.x_explain.focus();
    return false;
    }

document.db.submit();
return true;
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}
//]]>