// JavaScript Document
var map;
var localSearch = new GlocalSearch();
var tempMarker;
var markerPopupHTML = "When happy with the position of the marker <br /><a href='javascript:void(0);' onClick='submitMarker();'>click here</a> to update the form";
var markerPopupHTML2 = "<div class='popup'>When you are happy with the position of the marker, <a href='javascript:void(0);' onClick='submitMarker2();'>click here</a> to populate the latitude and longitude fields above.</div>";

//function loadmap(mypop, cid, zid) {
function loadmap(mypop, cid, zid) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("adminmap"));
		if(zid>0){
			map.addControl(new GSmallMapControl());
		}else{
			map.addControl(new GLargeMapControl());
			map.addControl(new GScaleControl());
			map.addControl(new GOverviewMapControl());
		}
		
		map.addControl(new GMapTypeControl());
		//map.enableScrollWheelZoom();
		map.setCenter(new GLatLng(51.533522879549786,-0.1263427734375), 10);
	}
	
	//(51.533522879549786, -0.1263427734375)

}

function adminSearch(objFrm){
	strtxt = document.getElementById('strSearch').value;
	strtxt = strtxt + ', UK';
	usePointFromPostcode(strtxt);
}

function usePointFromPostcode(postcode) {
	localSearch.setCenterPoint(map);
	//localSearch.setAddressLookupMode(ADDRESS_LOOKUP_DISABLED);
	localSearch.setSearchCompleteCallback(null,
	function() {
	  document.getElementById('admin-results').innerHTML = '';
	  if (localSearch.results[0]) {    
		var resultLat = localSearch.results[0].lat;
		var resultLng = localSearch.results[0].lng;
		var point = new GLatLng(resultLat,resultLng);
		//map.setCenter(point, 10);
		for (x in localSearch.results){
			alertStr = 'title=' +localSearch.results[0].title + '\r\n address=' +localSearch.results[0].streetAddress + '\r\n city=' + localSearch.results[0].city + '\r\n region=' + localSearch.results[0].region + '\r\n country=' + localSearch.results[0].country;
			//alert(alertStr);
			document.getElementById('admin-results').innerHTML +=  '<a href="javascript:searchzoom(' + localSearch.results[x].lat +', ' + localSearch.results[x].lng + ')">' + localSearch.results[x].title + ' : ' + localSearch.results[x].streetAddress +  ' : ' + localSearch.results[x].city + ' : '  + localSearch.results[x].region + ' : ' +  localSearch.results[x].country +'</a><br>'
		}		
		map.setCenter(point, 15);
	  }else{
		alert("location not found!");
	  }
	});  
	
	localSearch.execute(postcode);
}

function searchzoom(lat, lng, id){
	var point = new GLatLng(lat,lng);
	map.setCenter(point, 15);
	//GEvent.trigger(markerIDs[id], "click");
}

function createMarker(point,html) {

	//var marker = new GMarker(point, poicon);
	var marker = new GMarker(point, {icon:shicon});
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function place_marker(){
	if (map) {

		//map.clearOverlays();
	
		if (tempMarker!=undefined){
			  map.removeOverlay(tempMarker);
		}
	
		  tempMarker = new GMarker(map.getCenter(), {draggable:true});
  
		GEvent.addListener(tempMarker, 'click', function() {
			tempMarker.openInfoWindowHtml(markerPopupHTML);
		});
		GEvent.addListener(tempMarker, 'drag', function() {
			tempMarker.openInfoWindowHtml(markerPopupHTML);
		});

		setTimeout('map.addOverlay(tempMarker)', 100);
		setTimeout('tempMarker.openInfoWindowHtml(markerPopupHTML)', 100);
	}
}

function place_marker2(){
	if (map) {

		//map.clearOverlays();
		//map.setCenter(point, 15);
		map.setZoom(15);
		if (tempMarker!=undefined){
			  map.removeOverlay(tempMarker);
		}
	
		  tempMarker = new GMarker(map.getCenter(), {draggable:true});
  
		GEvent.addListener(tempMarker, 'click', function() {
			tempMarker.openInfoWindowHtml(markerPopupHTML2);
		});
		GEvent.addListener(tempMarker, 'drag', function() {
			tempMarker.openInfoWindowHtml(markerPopupHTML2);
		});

		setTimeout('map.addOverlay(tempMarker)', 100);
		setTimeout('tempMarker.openInfoWindowHtml(markerPopupHTML2)', 100);
	}
}

function submitMarker(){
	var lat = tempMarker.getPoint().lat();
    var lng = tempMarker.getPoint().lng();
	//alert('x=' + coordX);
	//alert('y=' + coordY);
	//window.location = 'index.php?page=directory_db/listing_add.php&lat=' + lat + '&lng=' + lng;
	opener.document.getElementById('strLatitude').value=lat;
	opener.document.getElementById('strLongitude').value=lng;
	//self.close ();
	window.close();
	return;
}

function submitMarker2(){
	var lat = tempMarker.getPoint().lat();
    var lng = tempMarker.getPoint().lng();
	//alert('x=' + coordX);
	//alert('y=' + coordY);
	//window.location = 'index.php?page=directory_db/listing_add.php&lat=' + lat + '&lng=' + lng;
	document.getElementById('strLatitude').value=lat;
	document.getElementById('strLongitude').value=lng;
	document.getElementById('divlatitude').innerHTML=lat;
	document.getElementById('divlongitude').innerHTML=lng;
	map.closeInfoWindow();
	//self.close ();
	//window.close();
	return;
}