var map;
var notfoundInfo = "<img src='images/logo.gif' alt='360pixs.com' id='logo'/><br>There is no home within this boundary, drag the map to view surroundings</b>";
var gmapsSurroundingQueryBusy = false;
var highlightCircle;
var currentMarker;

if (window.XMLHttpRequest)
{
  var gmapsRequest = new XMLHttpRequest();
  var gmapsRequestS = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
  var gmapsRequest = new ActiveXObject("Microsoft.XMLHTTP");
  var gmapsRequestS = new ActiveXObject("Microsoft.XMLHTTP");
}

var redIcon1 = new GIcon(G_DEFAULT_ICON);
redIcon1.image = "http://360pix.com/images/pin.png";
redIcon1.iconAnchor = new GPoint(5, 5);
redIcon1.infoWindowAnchor = new GPoint(5, 2);


// Creates a "circle" using 20-sided GPolygon at the given point
// Circle polygon object is global variable as there is only one highlighted marker at a time
// and we want to remove the previously placed polygon before placing a new one.

function highlightCurrentMarker(){
  var markerPoint = currentMarker.getPoint();

  var polyPoints = Array();

  if (highlightCircle) {
	map.removeOverlay(highlightCircle);
  }

  var mapNormalProj = G_NORMAL_MAP.getProjection();
  var mapZoom = map.getZoom();
  var clickedPixel = mapNormalProj.fromLatLngToPixel(markerPoint, mapZoom);

  var polySmallRadius = 20;

  var polyNumSides = 20;
  var polySideLength = 18;

  for (var a = 0; a<(polyNumSides+1); a++) {
	var aRad = polySideLength*a*(Math.PI/180);
	var polyRadius = polySmallRadius; 
		var pixelX = clickedPixel.x + polyRadius * Math.cos(aRad);
	var pixelY = clickedPixel.y + polyRadius * Math.sin(aRad);
	var polyPixel = new GPoint(pixelX,pixelY);
	var polyPoint = mapNormalProj.fromPixelToLatLng(polyPixel,mapZoom);
	polyPoints.push(polyPoint);
  }
  // Using GPolygon(points,  strokeColor?,  strokeWeight?,  strokeOpacity?,  fillColor?,  fillOpacity?)
  highlightCircle = new GPolygon(polyPoints,"#000000",2,0.0,"#333d96",.5);
  map.addOverlay(highlightCircle);
   }


function load() {
  map = new GMap2(document.getElementById("left-cont"));
  map.setCenter(new GLatLng(38.8461111,-77.3066667), 13);
  geocoder = new GClientGeocoder();
  //map.addControl(new GLargeMapControl());
 // map.addControl(new GMapTypeControl());

  var extLargeMapControl = new ExtLargeMapControl();
        map.addControl(extLargeMapControl);

  map.enableScrollWheelZoom();
  //gmapsSurroundingQuery();

  GEvent.addListener(map, "zoomend", function() {
	  highlightCurrentMarker();	
        });

	gmapsCreateListner();
}
function showLocation() {

   var address = document.getElementById('where').value ;
   if(address=="")
	{
		alert("Please enter a location to search near");
		document.getElementById('where').focus();
		return false;
	}
 // var address = document.forms[1].q.value;
  geocoder.getLocations(address, addAddressToMap);
}
// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
  if (!response || response.Status.code != 200) {
	alert("Not able to find. Please enter at least a valid city state, ZIP code, or landmark");
  } else {
	var place = response.Placemark[0];
	var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
	map.setCenter(point,11);
	gmapsSurroundingQuery();
  }
}

 // This line highlights the marker when its moused over

var gmapsSupressSurroundingQuery = 0;

	function gmapsCreateListner()
	{
	  GEvent.addListener(map, "dragend", function()
		{
		  if (!gmapsSupressSurroundingQuery)
		  {
			gmapsSurroundingQuery();
		  }

		 gmapsSupressSurroundingQuery = 0;
		}
		);
	}

function gmapsSurroundingQuery()
{	
	map.clearOverlays();
	var pType = document.getElementById('pType').value ;
	var rooms = document.getElementById('rooms').value ;
	var baths = document.getElementById('baths').value ;
	var hbaths = document.getElementById('hbaths').value ;
	var sminp = document.getElementById('sminp').value ;
	var smaxp = document.getElementById('smaxp').value ;

	var bounds = map.getBounds();
	var query = "markers.php?bounds="+bounds+"&pType="+pType+"&rooms="+rooms+"&baths="+baths+"&hbaths="+hbaths+"&sminp="+sminp+"&smaxp="+smaxp;
//alert(query);
	gmapsRequestS.abort();
	gmapsRequestS.onreadystatechange = gmapsSurroundingQueryResponse;
	gmapsRequestS.open("GET", query, true);
	gmapsRequestS.send(null);

}
function gmapsSurroundingQueryResponse()
{
  document.getElementById("loading1").style.visibility = "visible";
  if (gmapsRequestS.readyState == 4)
  {
	gmapsResponseText = gmapsRequestS.responseText;	
	//alert(gmapsResponseText)
	if(gmapsResponseText=="")
	{
		map.openInfoWindow(map.getCenter(), "No Record found");
	}
	else
	{	
		var lines = gmapsResponseText.split("\n");
		for(var i=0;i<lines.length;i++)
		{	
			var fields = lines[i].split("|");
			var lat = fields[0];
			var lon = fields[1];
			var marker = createMarker(lat,lon);
		    map.addOverlay(marker);
		}
	}
	 document.getElementById("loading1").style.visibility = "hidden";
  }
}

function createMarker(lat,lon) {
	var point = new GLatLng(lat,lon);
	var marker = new GMarker(point,redIcon1);
	GEvent.addListener(marker, "click", function() {
	  //marker.openInfoWindowHtml(html);
	  gmapsSupressSurroundingQuery=1;
	  marker.openExtInfoWindow(
		  map,
		  "custom_info_window_red",
		  "<div>Loading...</div>",
		  {
			ajaxUrl: "tourdetail.php?lat="+lat+"&lng="+lon, 
			beakOffset: 3
		  }
		); 
	});
	 // This line highlights the marker when its moused over
          GEvent.addListener(marker, "mouseover", function() {
	  currentMarker = marker;
	  highlightCurrentMarker();	
          });

	return marker;
  }

    