// JavaScript Document
//<![CDATA[

    var map;
    var geocoder;
	var directionsPanel;
	var directions;

//icon for the center
var iconc = new GIcon();
iconc.image = "img/map_you.png";
iconc.iconSize = new GSize(32, 32);
iconc.iconAnchor = new GPoint(9, 32);
iconc.infoWindowAnchor = new GPoint(9, 2);
iconc.infoShadowAnchor = new GPoint(18, 25);
	
// Create our "tiny" marker icon
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

var num=0; 
var dist='';



    function load() {		
	document.getElementById("loading").style.visibility="visible";
	document.getElementById("loading").style.height="450px";
	
	var address=document.getElementById('zip').value;
	geocoder = new GClientGeocoder();
    geocoder.getLocations(address, addAddressToLoad);
	  
	var latx=document.getElementById('lat').value;
	var longx=document.getElementById('long').value;
	var distx=document.getElementById('distance').value;
	var minlat=parseFloat(latx);
	var maxlat=parseFloat(latx);
	var minlong=parseFloat(longx);
	var maxlong=parseFloat(longx);
      map = new GMap2(document.getElementById("map"));
	  map.addControl(new GLargeMapControl());
	  map.addControl(new GMapTypeControl());
	  map.enableScrollWheelZoom();
	  map.enableContinuousZoom();	
	  
      map.setCenter(new GLatLng(latx,longx), 11);	  
      geocoder = new GClientGeocoder();
		point = new GLatLng(latx,longx);		
        marker = new GMarker(point, iconc);		

		GEvent.addListener(marker,"click", function() {
        var myHtml = "<span class='blue_head'>Your Searched Location</span>";
        map.openInfoWindowHtml(point, myHtml);
      });
        map.addOverlay(marker);
		///load xml
		xmlurl="?lat="+latx+"&long="+longx+"&maxdistance="+distx;
		///more conditions
		if (document.getElementById("opt_h").checked==true) {
		xmlurl=xmlurl+"&opt[]=homestay";
		}
		if (document.getElementById("opt_bb").checked==true) {
		xmlurl=xmlurl+"&opt[]=bedbreakfast";
		}
		if (document.getElementById("opt_rb").checked==true) {
		xmlurl=xmlurl+"&opt[]=roomboard";
		}
		if (document.getElementById("opt_r").checked==true) {
		xmlurl=xmlurl+"&opt[]=rentals";
		}
		if (document.getElementById("opt_sw").checked==true) {
		xmlurl=xmlurl+"&opt[]=swap";
		}
		if (document.getElementById("opt_v").checked==true) {
		xmlurl=xmlurl+"&opt[]=vacationhome";
		}
		if (document.getElementById("opt_cc").checked==true) {
		xmlurl=xmlurl+"&opt[]=cottage";
		}
		if (document.getElementById("opt_camp").checked==true) {
		xmlurl=xmlurl+"&opt[]=camping";
		}
		if (document.getElementById("opt_hm").checked==true) {
		xmlurl=xmlurl+"&opt[]=hotels";
		}
		if (document.getElementById("opt_o").checked==true) {
		xmlurl=xmlurl+"&opt[]=other";
		}
		///homestay options
		if (document.getElementById("no_pets").checked==true) {
		xmlurl=xmlurl+"&no_pets=yes";
		}
		if (document.getElementById("no_smoking").checked==true) {
		xmlurl=xmlurl+"&no_smoking=yes";
		}
		if (document.getElementById("allow_smoking").checked==true) {
		xmlurl=xmlurl+"&allow_smoking=yes";
		}
		if (document.getElementById("mincost").value!='') {
		xmlurl=xmlurl+"&mincost="+document.getElementById("mincost").value;
		}
		if (document.getElementById("maxcost").value!='') {
		xmlurl=xmlurl+"&maxcost="+document.getElementById("maxcost").value;
		}
		htmlurl="htmldata.php"+xmlurl;
		xmlurl="xmldata.php"+xmlurl;
		
	  GDownloadUrl(xmlurl, function(data, responseCode) {
	  
  var xml = GXml.parse(data); 
  var markers = xml.documentElement.getElementsByTagName("marker");
  //alert(markers.length);
  for (var i = 0; i < markers.length; i++) {
  	if (parseFloat(markers[i].getAttribute("lat"))<minlat) { minlat=markers[i].getAttribute("lat"); }
	if (parseFloat(markers[i].getAttribute("lat"))>maxlat) { maxlat=markers[i].getAttribute("lat"); }
	if (parseFloat(markers[i].getAttribute("lng"))<minlong) { minlong=markers[i].getAttribute("lng"); }
	if (parseFloat(markers[i].getAttribute("lng"))>maxlong) { maxlong=markers[i].getAttribute("lng"); }
    var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                            parseFloat(markers[i].getAttribute("lng")));
    //map.addOverlay(new GMarker(point));
	map.addOverlay(createMarker(point,markers[i].getAttribute("name"),Math.round(100*markers[i].getAttribute("dist"))/100,markers[i].getAttribute("type"),markers[i].getAttribute("location"),markers[i].getAttribute("cost"),markers[i].getAttribute("id")));
  }
 
var miles=new GLatLngBounds(new GLatLng(minlat,minlong),new GLatLng(maxlat,maxlong)); 
var avglat=(parseFloat(maxlat)+parseFloat(minlat))/2;
var avglong=(parseFloat(maxlong)+parseFloat(minlong))/2;
///calculate the map features zoom
var zoom=11;
zoom=map.getBoundsZoomLevel(miles);
map.setCenter(new GLatLng(avglat,avglong), zoom);
document.getElementById("loading").style.visibility="hidden";
document.getElementById("loading").style.height="1px";
});

GDownloadUrl(htmlurl, function(data, responseCode) {
document.getElementById('results').innerHTML = data;
});
	}


function login_check() {
	GDownloadUrl("login_check.php", function(data, responseCode) {
document.getElementById('login_check').innerHTML = data;
});
}
// Download the data in data.xml and load it on the map. The format we
// expect is:
// <markers>
//   <marker lat="37.441" lng="-122.141"/>
//   <marker lat="37.322" lng="-121.213"/>
// </markers>

	
// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point,name,dist,type,location,pay,userid) {
  // Create a lettered icon for this point using our icon class
  var icon = new GIcon(baseIcon);
  icon.image = "img/map_"+type+".png";
  var marker = new GMarker(point, icon);
  GEvent.addListener(marker, "click", function() {
  var myHtml = "<span class='blue_head'>"+name+"</span><br>";
  var myHtml = myHtml+"Location: "+location+"<br>";
  var myHtml = myHtml+"approx. distance "+dist+"km<br>";
  var myHtml = myHtml+"Accommodation Cost: $"+pay+"/day<br>";
  var myHtml = myHtml+"<a href='accommodation_profile.php?id="+userid+"'>View Details</a>";
        map.openInfoWindowHtml(point, myHtml);
  });

  return marker;
}




	function addAddressToStart(response) {
      //map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
      } else {
	  	placest = response.Placemark[0];
		document.getElementById('lat').value=placest.Point.coordinates[1];
		document.getElementById('long').value=placest.Point.coordinates[0];
		load();
      }
    }
	
	function addAddressToLoad(response) {
      //map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
      } else {
	  	placest = response.Placemark[0];
		document.getElementById('lat').value=placest.Point.coordinates[1];
		document.getElementById('long').value=placest.Point.coordinates[0];
      }
    }
	
	function showStart() {
	  var address=document.getElementById('zip').value;
	  geocoder = new GClientGeocoder();
      geocoder.getLocations(address, addAddressToStart);
    }
    //]]>