var map, geocoder, marker, center, icon, provinceCenter, regionCenter, url_m, style_var, lastAddressSearched;
var kill_time = new Date("January 1, 1970");
var province, distance, results, longitude, latitude, zoom, region, searchterm, mode;		
var regionLoaded = false;

url_m = "#";
style_var = "bnb-nfo-window";

// GLOBAL FUNCTIONS START ----------------------------------------------------

function clear_dropdown(dropdown) {
	dropdown.options.length = 0;
}

function roundit(Num, Places) {
   if (Places > 0) {
      if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) {
         var Rounder = Math.pow(10, Places);
         return Math.round(Num * Rounder) / Rounder;
      }
      else return Num;
   }
   else return Math.round(Num);
}


// PRIMARY AJAX FUNCTIONS START ----------------------------------------------		
function loadXMLDoc(url) {
	var randomnumber = Math.floor(Math.random()*100001);
	if (url.indexOf("?") == -1) {
		calledURL = url + "?rnd=" + randomnumber;
	} else {
		calledURL = url + "&rnd=" + randomnumber;
	}	
	
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", calledURL, true);
		req.send(null);
	} else if (window.ActiveXObject) {
		isIE = true;
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", calledURL, true);
			req.send();
		}
	}
} // -- END loadXMLDoc

function processReqChange() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			html_code = "";	
			//clear();
			build();
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText + "\n" + req.status);
		}
	}
		
} // -- END processReqChange

function build() {
	if (global_view_action == "provinces") {
		
		var latitude = req.responseXML.getElementsByTagName("latitude")[0].firstChild.nodeValue;
    var longitude = req.responseXML.getElementsByTagName("longitude")[0].firstChild.nodeValue;
    var zoomy = req.responseXML.getElementsByTagName("zoom")[0].firstChild.nodeValue;
		map.setCenter(new GLatLng(latitude, longitude));
		map.setZoom((zoomy-1));
		
			populateRegions(req.responseXML.getElementsByTagName("region"));
	} else if (global_view_action == "reloadProvinces") {
		populateRegions(req.responseXML.getElementsByTagName("region"));
		(document.getElementById("regionDropdown")).selectedIndex = region;
		(document.getElementById("distanceDropdown")).selectedIndex = distance;
		(document.getElementById("numberDropdown")).selectedIndex = results;
		
		loadBnBs();
		
	} else if (global_view_action == "regions") {
		var latitude = req.responseXML.getElementsByTagName("latitude")[0].firstChild.nodeValue;
    var longitude = req.responseXML.getElementsByTagName("longitude")[0].firstChild.nodeValue;
    var zoomy = req.responseXML.getElementsByTagName("zoom")[0].firstChild.nodeValue;
	
		regionCenter = new GLatLng(latitude, longitude);
	
		map.setZoom((zoomy-1));
		
	
		
		document.getElementById("distanceDropdown").disabled = false;	
		document.getElementById("numberDropdown").disabled = false;				
		
		marker = new GMarker(regionCenter, {draggable: true});
	
		GEvent.addListener(marker, "dragstart", function() {
	
			map.closeInfoWindow();
		
		});
		
		GEvent.addListener(marker, "dragend", function() {																																					 
			
			map.panTo(marker.getPoint());
			markerPoint = marker.getPoint();
			map.setCenter (marker.getPoint());
			loadBnBs();

		});			
		
		map.addOverlay(marker);			
			
		map.setCenter(regionCenter, (zoomy-1));	

		loadBnBs();		
	
	
	} else if (global_view_action == "markers") {
		
			
		show_markers(req.responseXML.getElementsByTagName("bnb"));
		
	
		
	} else if (global_view_action == "findAttraction") {
		
		handleAttractionResults(req.responseXML);
	}
}


// PRIMARY AJAX FUNCTIONS END ----------------------------------------------		

function load() {
		
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("bnb-map"));
		geocoder = new GClientGeocoder();	
		map.addControl(new GLargeMapControl());	
	
		icon = new GIcon();
		icon.image = "images/bnb-marker.png";
		icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		icon.iconSize = new GSize(12, 20);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);	
	
		if (document.cookie) {
			
			var cookie = document.cookie;
			var cookie_pieces = cookie.split(';');
	
			for(var i=0; i<cookie_pieces.length; i++){
	
				var piece = trim(cookie_pieces[i]);
	
				var a = piece.indexOf('=');
			
				if (a == -1) {
					var key = piece;
					var value = '';
				} else {	
					var key = piece.substr(0,a);
					var value = piece.substr(a+1);
				}
				
				if (key == "province") {
					province = value;
				} else if (key == "region") {
					region = value;					
				} else if (key == "distance") {
					distance = value;										
				} else if (key == "results") {
					results = value;										
				} else if (key == "longitude") {
					longitude = value;															
				} else if (key == "latitude") {
					latitude = value;															
				} else if (key == "zoom") {
					zoom = value;															
				} else if (key == "mode") {
					mode = value;
				} else if (key == "searchterm") {
					searchterm = value;
				}
								
			}
			
	

					
			if (province == null) {
				
				if (region == -1) {
					
					regionLoaded = false;
				
					(document.getElementById("provinceDropdown")).selectedIndex = province;
				
					global_view_action = "reloadProvinces"
					loadXMLDoc("GeoSearchXML.aspx?querytype=province&id=" + (document.getElementById("provinceDropdown")).value);
					reloadCenter = new GLatLng(latitude, longitude);
					map.setCenter(reloadCenter, (zoom-1));
				
		
					marker = new GMarker(reloadCenter, {draggable: true});
		
					GEvent.addListener(marker, "dragstart", function() {
						map.closeInfoWindow();
					});
					
					GEvent.addListener(marker, "dragend", function() {																																					 
						map.panTo(marker.getPoint());
						markerPoint = marker.getPoint();
						map.setCenter (marker.getPoint());
						loadBnBs();
					});		
			
					map.addOverlay(marker);						
					
					
				} else {
					
					center = new GLatLng(62.186014,-98.261719); // center of canada
	
					map.setCenter(center, 3);
				
					clear_dropdown(document.getElementById("regionDropdown"));
					
					document.getElementById("regionDropdown").disabled = true;	
					document.getElementById("distanceDropdown").disabled = true;	
					document.getElementById("numberDropdown").disabled = true;							
				
				}
				
				toggleSearchMode(mode, false);
				
			} else { 
			
				document.getElementById("distanceDropdown").disabled = false;	
				document.getElementById("numberDropdown").disabled = false;	
								
				regionLoaded = false;
			
				(document.getElementById("provinceDropdown")).selectedIndex = province;
			
				global_view_action = "reloadProvinces"
				loadXMLDoc("GeoSearchXML.aspx?querytype=province&id=" + (document.getElementById("provinceDropdown")).value);
				reloadCenter = new GLatLng(latitude, longitude);
				map.setCenter(reloadCenter, (zoom-1));
			
	
	
				marker = new GMarker(reloadCenter, {draggable: true});
	
				GEvent.addListener(marker, "dragstart", function() {
					map.closeInfoWindow();
				});
				
				GEvent.addListener(marker, "dragend", function() {																																					 
					map.panTo(marker.getPoint());
					markerPoint = marker.getPoint();
					map.setCenter (marker.getPoint());
					loadBnBs();
				});		
		
				map.addOverlay(marker);	
				
				toggleSearchMode(mode, false);
	
			}		
							
		} else { // FIRES WHEN THERE ARE NO SAVED COOKIES		
	

			center = new GLatLng(62.186014,-98.261719); // center of canada

			map.setCenter(center, 3);
			
			toggleSearchMode("geocode", true);
			
	

		}
	} else { // FIRES WHEN THE GOOGLE MAP IS NOT LOADED
		document.getElementById("bnb-search-results").innerHTML = "<div id='bnb-search-message'>This browser does not support GeoSearch.</div>";			
		clear_dropdown(document.getElementById("regionDropdown"));
			
		document.getElementById("regionDropdown").disabled = true;	
		document.getElementById("distanceDropdown").disabled = true;	
		document.getElementById("numberDropdown").disabled = true;				
	}
	
}

function checkProvince() {
	
	if ((document.getElementById("provinceDropdown")).value != 0) {
		map.clearOverlays();
		global_view_action = "provinces";
		loadXMLDoc("GeoSearchXML.aspx?querytype=province&id=" + (document.getElementById("provinceDropdown")).value);
	} else {
		document.getElementById("bnb-search-results").innerHTML = "<div id='bnb-search-message'>Please choose a <strong>PROVINCE</strong> from the dropdown.</div>";			
		map.setCenter(center, 3);
		clear_dropdown(document.getElementById("regionDropdown"));
		
		(document.getElementById("distanceDropdown")).selectedIndex = 5;
		(document.getElementById("numberDropdown")).selectedIndex = 2;		
		
		document.getElementById("regionDropdown").disabled = true;		
		document.getElementById("distanceDropdown").disabled = true;	
		document.getElementById("numberDropdown").disabled = true;			
			
		map.clearOverlays();
	}
	
}


function populateRegions(data) {
	regionLoaded = false;
	
	clear_dropdown(document.getElementById("regionDropdown"));
	
	var ioptn = document.createElement("OPTION");	
	ioptn.innerHTML = "-- Select Region -- ";
	ioptn.value = 0;
	
	(document.getElementById("regionDropdown")).appendChild(ioptn);
	
	for (var i = 0; i < data.length; i++) {


		id = req.responseXML.getElementsByTagName("value")[i].firstChild.nodeValue;
		name = req.responseXML.getElementsByTagName("name")[i].firstChild.nodeValue;
	
		var optn = document.createElement("OPTION");	
		optn.innerHTML = name;
		optn.value = id;
		
		(document.getElementById("regionDropdown")).appendChild(optn);
	}			


	
	if (mode != "geocode") {
		document.getElementById("regionDropdown").disabled = false;	
	}

	
	document.getElementById("bnb-search-results").innerHTML = "<div id='bnb-search-message'>Please choose a <strong>REGION</strong> from the dropdown.</div>";	
	
	regionLoaded = true;
	
}

function checkRegion() {

	if ((document.getElementById("regionDropdown")).value != 0) {
		map.clearOverlays();
		global_view_action = "regions";
		loadXMLDoc("GeoSearchXML.aspx?querytype=region&id=" + (document.getElementById("regionDropdown")).value);

		
	} else {
		document.getElementById("bnb-search-results").innerHTML = "<div id='bnb-search-message'>Please choose a <strong>REGION</strong> from the dropdown.</div>";			
		map.setCenter(provinceCenter, 4);

		(document.getElementById("distanceDropdown")).selectedIndex = 5;
		(document.getElementById("numberDropdown")).selectedIndex = 2;	

		document.getElementById("distanceDropdown").disabled = true;	
		document.getElementById("numberDropdown").disabled = true;			
		map.clearOverlays();
	}
}


function show_markers (data) {
	
	if (data.length > 0 ) {

		html_code = "<div id='bnb-search-results-box'> <div id='bnb-search-results-header'>"+data.length+" Bed and Breakfast(s) were found:</div><table cellpadding='5' cellspacing='0' width='97%'>"

	} else {

		html_code = "<div id='bnb-search-message'>No Bed and Breakfasts were found. <br /> Please reposition/change your <strong>search radius</strong> -OR- change the <strong>region</strong>.</div>"

	}

	var colour = "#FFFFFF";

	for (var i = 0; i < data.length; i++) {

		var name = 	req.responseXML.getElementsByTagName("name")[i].firstChild.nodeValue;
		var latitude = req.responseXML.getElementsByTagName("latitude")[i].firstChild.nodeValue;
    var longitude = req.responseXML.getElementsByTagName("longitude")[i].firstChild.nodeValue;
		var bnbMarker = "marky" + req.responseXML.getElementsByTagName("id")[i].firstChild.nodeValue + "_" +i;
		var bnbName = "namey_" + i;

		id = req.responseXML.getElementsByTagName("id")[i].firstChild.nodeValue;
		name = req.responseXML.getElementsByTagName("name")[i].firstChild.nodeValue;
		address = req.responseXML.getElementsByTagName("address1")[i].firstChild.nodeValue;
		city  = req.responseXML.getElementsByTagName("city")[i].firstChild.nodeValue;
		province  = req.responseXML.getElementsByTagName("province")[i].firstChild.nodeValue;
		postalcode  = req.responseXML.getElementsByTagName("postalcode")[i].firstChild.nodeValue;
		distance  = roundit(req.responseXML.getElementsByTagName("distance")[i].firstChild.nodeValue, 2);
		
		var BnBPoint = new GLatLng(parseFloat(latitude), parseFloat(longitude));
		map.addOverlay(createBnBMarker(BnBPoint, id, name, address, city, province, postalcode, distance));
		url_m = "../ShowBnB.aspx?bnbid=" + id;
		style_var = "bnb-nfo-window";
		img = "NotAvailable.jpg";
		if (colour == "#FFFFFF") {
			colour = "#EEEEEE";
		} else {
			colour = "#FFFFFF";
		}
		html_code += "				<tr>";
		if(img == "NotAvailable.jpg")
		    html_code += "					<td bgcolor='"+colour+"' width='1%'></td>";
		else
		html_code += "					<td bgcolor='"+colour+"' width='1%'><a href='../ShowBnB.aspx?bnbid="+id+"' target='top'><img src='../PropertyImages/"+img+"' width='115' height='85' border='0' class='bnb-search-results-thumb' /></a></td>";
		html_code += "					<td align='left' valign='top' bgcolor='"+colour+"'>";
		html_code += "						<div id='bnb-search-results-text'>";
		html_code += "							<div id='bnb-search-results-name'><strong><a href='../ShowBnB.aspx?bnbid="+id+"'>"+name+"</a></strong></div>";
		html_code += "							<div style='padding-bottom:7px;'>"+address+", "+city+", " + province + ", "+ postalcode + "</div>";		
		html_code += "						</div>";
		html_code += "					</td>";		
		html_code += "					<td align='center' bgcolor='"+colour+"' width='1%' nowrap='nowrap'>";	
		html_code += "						<div id='bnb-search-results-text' style='padding-right: 5px;'>";		
		html_code += "							<div><a href='#' onclick='map.openInfoWindowHtml((new GLatLng(parseFloat("+latitude+"), parseFloat("+longitude+"))), \"<div id=\"+style_var+\"> <div><strong><a href=../ShowBnB.aspx?bnbid="+ id +">"+name.replace(/'/, "&acute;")+"</a></strong></div><div>&nbsp;</div><div>"+address.replace(/'/, "&acute;")+", "+ city.replace(/'/, "&acute;")+", "+province.replace(/'/, "&acute;")+", "+postalcode.replace(/'/, "&acute;")+" </div><div>Distance: "+ distance+" km</div>     \")'><img src='images/show_map.png' border='0' /></a></div>";	
		html_code += "							<div id='bnb-distance'>"+ distance + " km</div>";						
		html_code += "						</div>";		
		html_code += "					</td>";		
		html_code += "				</tr>";
	}
	if (data.length > 0 ) {
		html_code += "</table></div>";	
	} else {
		html_code += "</div>";	
	}
	document.getElementById("bnb-search-results").innerHTML = html_code;	
	document.cookie = "province=" + (document.getElementById("provinceDropdown")).selectedIndex;
	document.cookie = "region=" + (document.getElementById("regionDropdown")).selectedIndex;
	document.cookie = "distance=" + (document.getElementById("distanceDropdown")).selectedIndex;
	document.cookie = "results=" + (document.getElementById("numberDropdown")).selectedIndex;
	if(marker) {
	   document.cookie = "latitude=" + (marker.getPoint()).lat();
	   document.cookie = "longitude=" + (marker.getPoint()).lng();
	}
	document.cookie = "zoom=" + (map.getZoom() + 1);
	
	document.cookie = "mode=" + mode;
	document.cookie = "searchterm=" + (document.getElementById("addressfield")).value;
	
}

function loadBnBs() {
	
	if (!marker) {
		currentPoint = new GLatLng(62.186014,-98.261719);
	} else {
		currentPoint = marker.getPoint();
	}
	
	map.clearOverlays();	
	
	xmlURL = "GeoSearch.aspx?";
	
	if ((document.getElementById("regionDropdown")).value == "") {
		xmlURL += "Region=0";
	} else {
		xmlURL += "Region=" + (document.getElementById("regionDropdown")).value;	
	}
	
	
	xmlURL += "&Province=" + (document.getElementById("provinceDropdown")).value;
	
	if(marker) {
	    xmlURL += "&latitude=" + (marker.getPoint()).latRadians();
	    xmlURL += "&longitude=" + (marker.getPoint()).lngRadians();
	}
	
	
	if((document.getElementById("distanceDropdown")).disabled == true) {
		xmlURL += "&distance=100";
	} else {
		xmlURL += "&distance=" + (document.getElementById("distanceDropdown")).value;
	}
	
	if((document.getElementById("numberDropdown")).disabled == true) {
		xmlURL += "&number=10";
	} else {
		xmlURL += "&number=" + (document.getElementById("numberDropdown")).value;
	}	
		
	global_view_action = "markers";	
	loadXMLDoc(xmlURL);
	
	createMarkerAtLocation(currentPoint);
		
}

function createBnBMarker(point, id, name, address, city, province, postalcode, distance) {
  var bnbmarker = new GMarker(point, icon);
  GEvent.addListener(bnbmarker, "click", function() {
		map.panTo(marker.getPoint());
		map.setCenter(marker.getPoint());
    bnbmarker.openInfoWindowHtml("<div id='bnb-nfo-window'><div><strong><a href='../ShowBnB.aspx?bnbid="+id+"'>" + name + "</a></strong></div><div>&nbsp;</div><div>"+address+", "+ city+", "+province+", "+postalcode+" </div><div>Distance: "+ distance+" km</div></div>");		
  });
  return bnbmarker;
}


function resetSearch() {
	center = new GLatLng(62.186014,-98.261719); // center of canada

	map.setCenter(center);
	
	(document.getElementById("provinceDropdown")).selectedIndex = 0;
	
	checkProvince();
	killCookie();
	
	document.getElementById("addressfield").value = "";
	
}

function show_all_cookies(){
	var cookie = document.cookie;
	var cookie_string = "";

	var cookie_pieces = cookie.split(';');

	for(var i=0; i<cookie_pieces.length; i++){

		var piece = trim(cookie_pieces[i]);

		var a = piece.indexOf('=');
		if (a == -1){
			var key = piece;
			var value = '';
		}else{
			var key = piece.substr(0,a);
			var value = piece.substr(a+1);
		}
		cookie_string += 'Key: ' + key + "  Value : "+ value + "\n";
	}
	
	alert (cookie_string);
}
	
function trim(str){
		
	while (str.charAt(0) == ' '){
		str = str.substring(1);
	}
	
	while (str.charAt(str.length-1) == ' '){
		str = str.substring(0,str.length-1);
	}

	return str;
}
	
function killCookie () {
		
	var cookies = new Array();
	cookies[0] = "province";
	cookies[1] = "region";
	cookies[2] = "distance";
	cookies[3] = "results";		
	cookies[4] = "longitude";		
	cookies[5] = "latitude";	
	cookies[6] = "zoom";
	cookies[7] = "mode";
	cookies[8] = "searchterm"	;
	
	for (i = 0; i < cookies.length; i++){
		document.cookie = cookies[i] + "=stub;expires=" + kill_time.toGMTString();
	}
}

function showID() {
	alert(url_m);
	alert(id);
}

function toggleSearchMode(searchtype, resetval) {
	
	if (searchtype == null) {
		searchtype = "geocode";
	}
	
	mode = searchtype;
	if (searchtype == "geocode") {
		document.getElementById("provinceDropdown").disabled = true;	
		document.getElementById("regionDropdown").disabled = true;	
		document.getElementById("distanceDropdown").disabled = true;	
		document.getElementById("numberDropdown").disabled = true;	
		
		document.getElementById("addressfield").disabled = false;	
		document.getElementById("mybutton").disabled = false;	
		
		document.getElementById("manualbutton").style.backgroundColor='#F0F9F6'
		document.getElementById("manualform").style.backgroundColor='#F0F9F6'
		
		document.getElementById("geocodebutton").style.backgroundColor='#FFFFFF'
		document.getElementById("geocodeform").style.backgroundColor='#FFFFFF'
		
		document.getElementById("searchtype").checked = true;
		document.getElementById("searchtype1").checked = false;
		
		if (searchterm != null) {
			document.getElementById("addressfield").value = searchterm;
		}

		document.getElementById("bnb-search-results").innerHTML = "<div id='bnb-search-message'>Please enter an address or location to search near.</div>";			
		
	
		
	} else if (searchtype == "manual") {
		document.getElementById("provinceDropdown").disabled = false;	
		document.getElementById("regionDropdown").disabled = true;	
		
		if(resetval == true) {
			document.getElementById("distanceDropdown").disabled = true;	
			document.getElementById("numberDropdown").disabled = true;	
		} else {
			document.getElementById("distanceDropdown").disabled = false;	
			document.getElementById("numberDropdown").disabled = false;			
		}


		document.getElementById("addressfield").disabled = true;			
		document.getElementById("mybutton").disabled = true;	
			
		document.getElementById("manualbutton").style.backgroundColor='#FFFFFF'
		document.getElementById("manualform").style.backgroundColor='#FFFFFF'
		
		document.getElementById("geocodebutton").style.backgroundColor='#F0F9F6'
		document.getElementById("geocodeform").style.backgroundColor='#F0F9F6'	
		
		document.getElementById("searchtype").checked = false;
		document.getElementById("searchtype1").checked = true;
		
	

	}
}

function trapKey(target, event)
{ 	
    //alert(event.keyCode);
	if ((event.keyCode == 13 || event.which == 13) && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == 'textarea'))) 
	{
	    //alert(document.getElementById(target));
		if(document.getElementById(target) != null && target=="addressfield")
		{
			var code = document.getElementById('addressfield').value;
			//alert(code);
			
			if(code != '') findAttractionCode(code);
			return false;
		}
	}
	else return true;
} 
    

function findAttractionCode(code) {
		
		url = 'AtrSearch.aspx?atrCode=' + code;
		global_view_action = "findAttraction";	
		lastAddressSearched = code;
		loadXMLDoc(url);	
		
}

function handleAttractionResults(xmlResponse) {
	
	if(xmlResponse.getElementsByTagName('atr').length==0) goToGeocodeLocation(lastAddressSearched);
	
	else{
		
		map.clearOverlays();
		var latitude = req.responseXML.getElementsByTagName("latitude")[0].firstChild.nodeValue;
    var longitude = req.responseXML.getElementsByTagName("longitude")[0].firstChild.nodeValue;	
		var attractionName = req.responseXML.getElementsByTagName("name")[0].firstChild.nodeValue;
		var description= req.responseXML.getElementsByTagName("description")[0].firstChild.nodeValue;
		var start= req.responseXML.getElementsByTagName("start")[0].firstChild.nodeValue;
		var end= req.responseXML.getElementsByTagName("end")[0].firstChild.nodeValue;
		
		var infoHTML = "<div id='attractiondiv'><h1>"+attractionName+"</h1><div id='attractiondesc'>"+description+"</div>";
		if (start!='undefined') infoHTML= infoHTML+"<div id='startdate'><strong>Start:</strong> "+start + "</div>";
		if (end!='undefined') infoHTML= infoHTML+"<div id='enddate'><strong>End:</strong> "+end + "</div>";
		
		
		infoHTML += "</div>"
		
		attractionLocation = new GLatLng(latitude,longitude); 
		map.setCenter(attractionLocation);
	
		xmlURL = "GeoSearch.aspx?province=0&region=0";
		xmlURL += "&latitude=" + attractionLocation.latRadians();
		xmlURL += "&longitude=" + attractionLocation.lngRadians();
		xmlURL += "&distance=" + (document.getElementById("distanceDropdown")).value;
		xmlURL += "&number=" + (document.getElementById("numberDropdown")).value;
		global_view_action = "markers";
	
		loadXMLDoc(xmlURL);
		createMarkerAtLocation(attractionLocation);
		marker.openInfoWindowHtml(infoHTML);
		
		
		map.setZoom(13);
	}
}

function createMarkerAtLocation(point) {
	
	marker = new GMarker(point, {draggable: true});
		
		
		
		GEvent.addListener(marker, "dragstart", function() {
			map.closeInfoWindow();
			});
		
		GEvent.addListener(marker, "dragend", function() {
			
			map.panTo(marker.getPoint());
			
			markerPoint = marker.getPoint();
	

			map.setCenter (marker.getPoint());



			loadBnBs();

	
			});
		
		map.addOverlay(marker);	
}


function goToGeocodeLocation (address) {
	
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
		
		map.clearOverlays();	  
		  
        map.setCenter(point, 13);
                
		xmlURL = "GeoSearch.aspx?province=0&region=0";
		xmlURL += "&latitude=" + point.latRadians();
		xmlURL += "&longitude=" + point.lngRadians();
		xmlURL += "&distance=" + (document.getElementById("distanceDropdown")).value;
		xmlURL += "&number=" + (document.getElementById("numberDropdown")).value;
		
		
		global_view_action = "markers";	
	
		loadXMLDoc(xmlURL);
		
		createMarkerAtLocation(point);
	
      }
    }
  );

	
}
