﻿    var gLocalSearch;
    var map;
    var gSelectedResults = [];
    var gCurrentResults = [];
    var gSearchForm;
	var CanAdd;
	var PointIcon = new GIcon();
	PointIcon.iconSize=new GSize(22,16);
	PointIcon.iconAnchor=new GPoint(0,0);
	PointIcon.infoWindowAnchor=new GPoint(20,0);
	PointIcon.image='/i/icons/3.png';
	var CanAdd;

	function doSearch() 
	{
		var zip = document.getElementById("search_input").value;
		gLocalSearch.execute(zip);
    }

    // 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]));
      }

      var attribution = gLocalSearch.getAttribution();
      if (attribution) 
	  {
		document.getElementById("searchwell").appendChild(attribution);
      }

    }

	// 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) 
	{
      this.result_ = result;
      this.resultNode_ = this.unselectedHtml();
      document.getElementById("searchwell").appendChild(this.resultNode_);
      map.addOverlay(this.marker(PointIcon));
    }

	function GoToGpoint(lat,lng)
	{
		map.panTo(new GLatLng(parseFloat(lat),parseFloat(lng)));
	}

    // 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());
			if (CanAdd == 1) marker.openInfoWindow("<div class='popupOnMap'><h1>"+getDMS(this.result_.lng, this.result_.lat)+"</h1><h1>"+this.result_.title+"</h1><p><a href='/PointEdit/?Lan=" + this.result_.lat + "&Lat=" + this.result_.lng + "&zoom=" +parseInt(map.getZoom())+ "' class='title' >Сохранить точку</a></p></div>");
			if (CanAdd == 0) marker.openInfoWindow("<div class='popupOnMap'><h1>"+getDMS(this.result_.lng, this.result_.lat)+"</h1><h1>"+this.result_.title+"</h1><p>Чтобы сохранить точку Вы должны быть <a href='/Register' class='title' >зарегистрированы</a></p></div>");
		  });
      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";
//alert( this.result_.title );
    //  container.appendChild(this.result_.html.cloneNode(true));
      var saveDiv = document.createElement("li");
      saveDiv.className = "select";
//      saveDiv.innerHTML = "Save this location";

saveDiv.innerHTML = "<a href='javascript:GoToGpoint("+this.result_.lat+","+this.result_.lng+");' >"+this.result_.title+"</a>";
/*      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_;
	}

	//	GSearch.setOnLoadCallback(OnLoad);
