// Map API:
//document.write("<script type=\"text/javascript\" src=\"http://maps.google.com/maps?file=api&amp;v=2.225&amp;key="+GMAP_KEY+"\"></script>");

// ------------------
// Vars declarations
// ------------------
var map;

var centerLatitude =23.241346//10.799752296559271;//1.36;//1.31;
var centerLongitude = 78.486328//78.6837387084961;//103.82;//103.90;

var startZoom = 5;
var minZoom = 5;
var min_layers_zoom = 14;
var geocoder;

// Icons:
var icons_url = "http://www.propertyguru.com.sg/images/ps/icons/"; 
var icons = {  // image, image_h (highlighted image), shortlist (shortlist image), shortlist_h (highlighted shortlist image), shadow, iconSize, shadowSize, pin
	// Listings:
	N:   {image: 'Images/Icons/Apartments.png', image_h: 'Images/Icons/Apartments_hover.png', shortlist: icons_url+'condo_sl.gif', shortlist_h: icons_url+'condo_sl_h.gif', 
		  shadow: icons_url+'shadow_high.png', iconSize: [35,65], shadowSize: [64,56], pin: true},		  
	L:   {image: 'Images/Icons/Servicedvilla.png', image_h: 'Images/Icons/Servicedvilla_hover.png', shortlist: icons_url+'house_sl.gif', shortlist_h: icons_url+'house_sl_h.gif', 
		  shadow: icons_url+'shadow_low.png', iconSize: [38,32], shadowSize: [54,32], pin:true},
	H:   {image: 'Images/Icons/Guesthouse.png', image_h: 'Images/Icons/Guesthouse_hover.png', shortlist: icons_url+'hdb_sl.gif', shortlist_h: icons_url+'hdb_sl_h.gif', 
		  shadow: icons_url+'shadow_high.png', iconSize: [35,32], shadowSize: [64,56], pin:true}, 
	C:	 {image: 'Images/Icons/Coldstorage.png', image_h: 'Images/Icons/Coldstorage_hover.png', shortlist: icons_url+'commercial_sl.gif', shortlist_h: icons_url+'commercial_sl_h.gif',  
		  shadow: icons_url+'shadow_high.png', iconSize: [23,56], shadowSize: [64,56], pin: true},
	// My Listings:
	1:   {image: 'Images/Icons/Villa.png', image_h: 'Images/Icons/Villa_hover.png', shortlist: icons_url+'condo_sl.gif', shortlist_h: icons_url+'condo_sl_h.gif', 
		  shadow: icons_url+'shadow_high.png', iconSize: [38,29], shadowSize: [64,56], pin: true},		  
	2:   {image: 'Images/Icons/Servicedvilla.png', image_h: 'Images/Icons/Servicedvilla_hover.png', shortlist: icons_url+'house_sl.gif', shortlist_h: icons_url+'house_sl_h.gif', 
		  shadow: icons_url+'shadow_low.png', iconSize: [38,29], shadowSize: [54,32], pin:true},
	3:   {image: 'Images/Icons/Farmhose.png', image_h: 'Images/Icons/Farmhose_hover.png', shortlist: icons_url+'hdb_sl.gif', shortlist_h: icons_url+'hdb_sl_h.gif', 
		  shadow: icons_url+'shadow_high.png', iconSize: [39,23], shadowSize: [64,56], pin:true}, 
	4:	 {image: 'Images/Icons/Apartments.png', image_h: 'Images/Icons/Apartments_hover.png', shortlist: icons_url+'commercial_sl.gif', shortlist_h: icons_url+'commercial_sl_h.gif',  
		  shadow: icons_url+'shadow_high.png', iconSize: [39,60], shadowSize: [64,56], pin: true},
	5:	 {image: 'Images/Icons/Servicedapartments.png', image_h: 'Images/Icons/Servicedapartments_hover.png', shortlist: icons_url+'commercial_sl.gif', shortlist_h: icons_url+'commercial_sl_h.gif',  
		  shadow: icons_url+'shadow_high.png', iconSize: [39,60], shadowSize: [64,56], pin: true},
	6:   {image: 'Images/Icons/Residentialland.png', image_h: 'Images/Icons/Residentialland_hover.png', shortlist: icons_url+'condo_sl.gif', shortlist_h: icons_url+'condo_sl_h.gif', 
		  shadow: icons_url+'shadow_high.png', iconSize: [38,22], shadowSize: [64,56], pin: true},		  
	7:   {image: 'Images/Icons/Farmland.png', image_h: 'Images/Icons/Farmland_hover.png', shortlist: icons_url+'house_sl.gif', shortlist_h: icons_url+'house_sl_h.gif', 
		  shadow: icons_url+'shadow_low.png', iconSize: [39,24], shadowSize: [54,32], pin:true},
	8:   {image: 'Images/Icons/Shop.png', image_h: 'Images/Icons/Shop_hover.png', shortlist: icons_url+'hdb_sl.gif', shortlist_h: icons_url+'hdb_sl_h.gif', 
		  shadow: icons_url+'shadow_high.png', iconSize: [40,30], shadowSize: [64,56], pin:true}, 
	9:	 {image: 'Images/Icons/Showroom.png', image_h: 'Images/Icons/Showroom_hover.png', shortlist: icons_url+'commercial_sl.gif', shortlist_h: icons_url+'commercial_sl_h.gif',  
		  shadow: icons_url+'shadow_high.png', iconSize: [39,37], shadowSize: [64,56], pin: true},
	10:	 {image: 'Images/Icons/Officespace.png', image_h: 'Images/Icons/Officespace_hover.png', shortlist: icons_url+'commercial_sl.gif', shortlist_h: icons_url+'commercial_sl_h.gif',  
		  shadow: icons_url+'shadow_high.png', iconSize: [39,59], shadowSize: [64,56], pin: true},
	11:   {image: 'Images/Icons/Land.png', image_h: 'Images/Icons/Land_hover.png', shortlist: icons_url+'condo_sl.gif', shortlist_h: icons_url+'condo_sl_h.gif', 
		  shadow: icons_url+'shadow_high.png', iconSize: [38,22], shadowSize: [64,56], pin: true},		  
	12:   {image: 'Images/Icons/Hotels.png', image_h: 'Images/Icons/Hotels_hover.png', shortlist: icons_url+'house_sl.gif', shortlist_h: icons_url+'house_sl_h.gif', 
		  shadow: icons_url+'shadow_low.png', iconSize: [39,38], shadowSize: [54,32], pin:true},
	13:   {image: 'Images/Icons/Timeshare.png', image_h: 'Images/Icons/Timeshare_hover.png', shortlist: icons_url+'hdb_sl.gif', shortlist_h: icons_url+'hdb_sl_h.gif', 
		  shadow: icons_url+'shadow_high.png', iconSize: [39,33], shadowSize: [64,56], pin:true}, 
	14:	 {image: 'Images/Icons/Space_retailmall.png', image_h: 'Images/Icons/Space_retailmall_hover.png', shortlist: icons_url+'commercial_sl.gif', shortlist_h: icons_url+'commercial_sl_h.gif',  
		  shadow: icons_url+'shadow_high.png', iconSize: [36,30], shadowSize: [64,56], pin: true},
	15:	 {image: 'Images/Icons/Guesthouse.png', image_h: 'Images/Icons/Guesthouse_hover.png', shortlist: icons_url+'commercial_sl.gif', shortlist_h: icons_url+'commercial_sl_h.gif',  
		  shadow: icons_url+'shadow_high.png', iconSize: [39,35], shadowSize: [64,56], pin: true},
	16:   {image: 'Images/Icons/Office_Itpark.png', image_h: 'Images/Icons/Office_Itpark_hover.png', shortlist: icons_url+'condo_sl.gif', shortlist_h: icons_url+'condo_sl_h.gif', 
		  shadow: icons_url+'shadow_high.png', iconSize: [38,59], shadowSize: [64,56], pin: true},		  
	17:   {image: 'Images/Icons/Warehouse.png', image_h: 'Images/Icons/Warehouse_hover.png', shortlist: icons_url+'house_sl.gif', shortlist_h: icons_url+'house_sl_h.gif', 
		  shadow: icons_url+'shadow_low.png', iconSize: [38,29], shadowSize: [54,32], pin:true},
	18:   {image: 'Images/Icons/Coldstorage.png', image_h: 'Images/Icons/Coldstorage_hover.png', shortlist: icons_url+'hdb_sl.gif', shortlist_h: icons_url+'hdb_sl_h.gif', 
		  shadow: icons_url+'shadow_high.png', iconSize: [39,35], shadowSize: [64,56], pin:true}, 
	19:	 {image: 'Images/Icons/Factory.png', image_h: 'Images/Icons/Factory_hover.png', shortlist: icons_url+'commercial_sl.gif', shortlist_h: icons_url+'commercial_sl_h.gif',  
		  shadow: icons_url+'shadow_high.png', iconSize: [38,33], shadowSize: [64,56], pin: true},
	20:	 {image: 'Images/Icons/Manufacturing.png', image_h: 'Images/Icons/Manufacturing_hover.png', shortlist: icons_url+'commercial_sl.gif', shortlist_h: icons_url+'commercial_sl_h.gif',  
		  shadow: icons_url+'shadow_high.png', iconSize: [39,34], shadowSize: [64,56], pin: true},
	21:   {image: 'Images/Icons/Businesscentre.png', image_h: 'Images/Icons/Businesscentre_hover.png', shortlist: icons_url+'condo_sl.gif', shortlist_h: icons_url+'condo_sl_h.gif', 
		  shadow: icons_url+'shadow_high.png', iconSize: [39,55], shadowSize: [64,56], pin: true},		  
	22:   {image: 'Images/Icons/Industrialland.png', image_h: 'Images/Icons/Industrialland_hover.png', shortlist: icons_url+'house_sl.gif', shortlist_h: icons_url+'house_sl_h.gif', 
		  shadow: icons_url+'shadow_low.png', iconSize: [38,28], shadowSize: [54,32], pin:true},
	23:   {image: 'Images/Icons/Agriculturalland.png', image_h: 'Images/Icons/Agriculturalland_hover.png', shortlist: icons_url+'hdb_sl.gif', shortlist_h: icons_url+'hdb_sl_h.gif', 
		  shadow: icons_url+'shadow_high.png', iconSize: [38,48], shadowSize: [64,56], pin:true}, 
	
	// Layers:
	SUP: {image: icons_url+'shop.gif', shadow:'', iconSize: [20,20], shadowSize: [20,20], pin:false},
	MRT: {image: icons_url+'mrt.gif', shadow:'', iconSize: [20,20], shadowSize: [20,20], pin:false},
	KDG: {image: icons_url+'kindergarten.gif', shadow:'', iconSize: [20,20], shadowSize: [20,20], pin:false},
	SCH: {image: icons_url+'school.gif', shadow:'', iconSize: [20,20], shadowSize: [20,20], pin:false},	
	INT: {image: icons_url+'school_i.gif', shadow:'', iconSize: [20,20], shadowSize: [20,20], pin:false},	
	AIR: {image: icons_url+'airport.gif', shadow:'', iconSize: [20,20], shadowSize: [20,20], pin:false},
	CON: {image: icons_url+'underconstruction.png', shadow:'', iconSize: [50,30], shadowSize: [50,30], pin:false}
};
//-------------------------------------------------------------

function init() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 10)));

		//map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
		//map.enableScrollWheelZoom(); // enable zooming with the mouse scroll wheel		
		$.each(G_DEFAULT_MAP_TYPES, function(i, item){ 
			item.getMinimumResolution = function(){return minZoom;}; // set the min zoom allowed for the different map types (normal, satellite, hybrid)
		});
	}
}

function init_large() 
{
	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(5, 5))); // Satellite, Map, ...
		map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(5, 50))); // Zoom
		map.addControl(new GScaleControl(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(80, 7)));	 // Scale	
		//map.addControl(new GOverviewMapControl());	 // little map bottom right
		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom); // center the map
		//map.enableGoogleBar(); // allows searchs
		map.enableScrollWheelZoom(); // enable zooming with the mouse scroll wheel		
		$.each(G_DEFAULT_MAP_TYPES, function(i, item){ 
			item.getMinimumResolution = function(){return minZoom;}; // set the min zoom allowed for the different map types (normal, satellite, hybrid)
		}); 
		geocoder = new GClientGeocoder();
		geocoder.setBaseCountryCode("sg");
	}
}

function init_dialog(lat,lng,icon) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("dialog_map"));	
		map.addControl(new GSmallMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(5, 5))); // Zoom
		map.addControl(new GScaleControl(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(80, 7)));	 // Scale	
		map.addControl(new GOverviewMapControl()); // little map bottom right
		map.setCenter(new GLatLng(lat,lng ), 16); 
		map.enableScrollWheelZoom(); // enable zooming with the mouse scroll wheel	
		var point = new GLatLng(lat,lng);
		if (typeof icon != "undefined") {
			createMarker(point, icon);
		}
		else map.addOverlay(new GMarker(point));	
	}
}



function initPoint(lat, lon) {	
	var point = new GLatLng(lat, lon);
	if (lat == 0 && lon == 0) {
		map.setCenter(new GLatLng(centerLatitude, centerLongitude), 10);
	} else {	
		map.setCenter(point,15);	
		var marker = new GMarker(point);
		map.addOverlay(marker);
	}
}

function initializePoint(pointData) {
	initPoint(pointData.latitude, pointData.longitude);	
}
 
function streetview(lat,lng) {
  var gstreet;
  var location = new GLatLng(lat,lng);
  panoramaOptions = {latlng:location };	      
  gstreet = new GStreetviewPanorama(document.getElementById("streetview"), panoramaOptions);
  GEvent.addListener(gstreet, "error", handleNoFlash);  
}

 function handleNoFlash(errorCode) {   
     
      if (errorCode ==600) {
         $('#street_tab').hide();	
      	 $('#street_tab').next().removeClass('sectiontabEnding_sel');	
		 $('#street_tab').next().removeClass('sectiontabEnding');
        return;
     }
   }
	   
function propertydetailmap() 
{
	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		for(id in markers) 
		{
			initializePoint(markers[id]);
		}
	}
}


function propertydetailmap2() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("bigmap"));
		map.addControl(new GSmallMapControl());
		for(id in markers) {
			initializePoint(markers[id]);
		}
	}
}

function initPosition(){
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
}
function initPosition2(){
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom-1);
}

function getGeocode(block, street, postcode, callback) {
	GUnload();
	initialize();
	var url = null;
	if (postcode != '') {
		// by postcode
		url = "/geoxml.php?action=postcode&postcode=" + escape(postcode);
	} else {
		if (block != '' && street != '') {
			// by street
			url = "/geoxml.php?action=address&block=" + escape(block) + "&street=" + escape(street);
		}
	}
	
	if (url != null) {
		$.getJSON(url,
			function(data) {
				//callback(data.lat, data.lon);
				$.each(data, function(i, item) {	
					if (i == "pois") { 
						callback(item[0].latitude, item[0].longitude);
					}
				});
			}); 
	}
}

function locate(query) {	
	geocoder.getLocations(query + ", Singapore", function(response) {	
		// Check status code:										  
		if (!response || response.Status.code != G_GEO_SUCCESS /*|| 
			(typeof(response.Placemark[0].AddressDetails.Country) != "undefined" && response.Placemark[0].AddressDetails.Country.CountryNameCode != "SG")*/) {  
		
			// Try with our database!!
			
			// No results found:
			alert('Location not found');
			return; 			
		}
		
		// Retrieve the first result:
		var place = response.Placemark[0];
    	var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);		
					
		// Center and zoom the map:
		map.setCenter(point, 15);
		
		// Show a temporary marker:
		var marker = new GMarker(point);
		map.addOverlay(marker);
		var address = place.address.replace("Singapore, Singapore","Singapore"); 
		address = address.replace(",","<br>"); // replace the first comma
		marker.openInfoWindowHtml('<div style="height:50px; margin-top:25px">' + address + '</div>');
		setTimeout(function() { map.removeOverlay(marker); }, 5000);
	});	
}

// ---------------------------------------
// createMarker:
// 	- point: GLatLng object
//	- type: icon type
//	- layer: shortlist, properties or POI
// ---------------------------------------
function createMarker(point, type, layer) 
{	
	// Set up the marker icon:
	var markerIcon = new GIcon(G_DEFAULT_ICON);
	//markerIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";	
	markerIcon.image = (layer == "shortlist") ? icons[type].shortlist : icons[type].image;
	markerIcon.iconSize = new GSize(icons[type].iconSize[0], icons[type].iconSize[1]);
	markerIcon.iconAnchor = new GPoint(icons[type].iconSize[0]/2, icons[type].pin ? icons[type].iconSize[1] : icons[type].iconSize[1]/2);
	markerIcon.infoWindowAnchor = new GPoint(icons[type].iconSize[0]/2, 0);
	markerIcon.shadow = icons[type].shadow;
	markerIcon.shadowSize = new GSize(0, 0);//new GSize(icons[type].shadowSize[0], icons[type].shadowSize[1]);
	markerIcon.imageMap = [0,0, icons[type].iconSize[0],0, icons[type].iconSize[0],icons[type].iconSize[1], 0,icons[type].iconSize[1]];
		
	// Create the marker:
	var marker = new GMarker(point, {icon: markerIcon, zIndexProcess:function(m){ return m.zIndex;} });				
	marker.type = type;
	
	// Set z-index (for same type, lower latitudes and higher longitudes have higher z-index):
	if (layer == "POI") marker.zIndex = parseInt(-100*(10*point.lat()-point.lng())); // lowest z-index for POI
	else marker.zIndex = parseInt(-1000*(10*point.lat()-point.lng())); // middle z-index for properties
	if (layer == "shortlist") marker.zIndex += 1; // highest z-index for shortlisted (only if it's the same position than the not shortlisted marker)

	// Add the marker to the map:	
	//map.addOverlay(marker);
		
	return marker;
}

// ---------------------------------
// mapText: 
// Prints html at a map coordinates
// ---------------------------------
function mapText(html, point, pane, style) {
	function Text(html, point, pane, style) {				  
		this.html = html;
		this.point = point;
		this.pane = pane;
		this.style = style;
	}

	// New GOverlay object:
	Text.prototype = new GOverlay();
	
	// Create the DIV:
	Text.prototype.initialize = function(map) {
	  var div = document.createElement("div");
	  for (key in this.style) div.style[key] = this.style[key];
	  div.style.position = "absolute";
	  div.innerHTML = this.html;
	
	  map.getPane(this.pane).appendChild(div);
		
	  this.map = map;
	  this.div = div;
	};
	
	// Remove the DIV from the map pane:
	Text.prototype.remove = function() {
	  this.div.parentNode.removeChild(this.div);
	};
	
	// Copy our data to a new Text
	Text.prototype.copy = function() {
	 return new Text(this.html, this.point, this.pane, this.style);
	};
	
	// Redraw the Text based on the current projection and zoom level
	Text.prototype.redraw = function(force) {
	  // We only need to redraw if the coordinate system has changed
	  if (!force) return;
	
	  // Calculate the DIV coordinates:
	  var point_pixels = this.map.fromLatLngToDivPixel(this.point);	  
	
	  // Now position our DIV based on the DIV coordinates of our bounds	
	  this.div.style.left = ($(this.div).width()) ? (point_pixels.x - $(this.div).width()/2) + "px" : (point_pixels.x) + "px";
	  this.div.style.top = ($(this.div).height()) ? (point_pixels.y - $(this.div).height()/2) + "px" : (point_pixels.y) + "px";
	};

	var new_text = new Text(html, point, pane, style);
	map.addOverlay(new_text);
	return new_text;
}

// ---------------------------------------------------------------------
// addLayer:
// Adds a set of third-party geographic information based on its domain
// e.g.: "org.wikipedia.en"
// ---------------------------------------------------------------------
function addLayer(namespace) {
	var myLayer = new GLayer(namespace);
    map.addOverlay(myLayer);
    return myLayer;
}

// -------------------------
// Wikipedia layer handling
// -------------------------
var wiki_layer = null;
function showWikipedia() { 
	if (!wiki_layer) wiki_layer = addLayer( "org.wikipedia.en" );
	else wiki_layer.show(); 
}
function hideWikipedia() { if (wiki_layer) wiki_layer.hide(); }


// -----------
// Nearby Map
// -----------
function loadNearbyMap() { //(lat,long) {
	SnbMapConfig = {
	
		SnbCustomIcon: 'http://c1.shownearby.com/images/main.png', //overwrite icon
		SnbIconSize: {width:25,height:25},
		SnbCenterText: 'Propertyguru',

		SnbCategoryMarkerIcon: [
  			{CatId:18,IconUrl:'http://c1.shownearby.com/images/01.png',IconSize:{width:16,height:16}},
			{CatId:29,IconUrl:'http://c1.shownearby.com/images/02.png',IconSize:{width:16,height:16}},
			{CatId:17,IconUrl:'http://c1.shownearby.com/images/03.png',IconSize:{width:16,height:16}},
			{CatId:173,IconUrl:'http://c1.shownearby.com/images/04.png',IconSize:{width:16,height:16}},
			{CatId:256,IconUrl:'http://c1.shownearby.com/images/05.png',IconSize:{width:16,height:16}},
			{CatId:189,IconUrl:'http://c1.shownearby.com/images/06.png',IconSize:{width:16,height:16}},
			{CatId:8,IconUrl:'http://c1.shownearby.com/images/07.png',IconSize:{width:16,height:16}},
			{CatId:267,IconUrl:'http://c1.shownearby.com/images/08.png',IconSize:{width:16,height:16}}
		] //overwrite cat icons
	};

	document.write ("<" + "script language='JavaScript' type='text/javascript' src='");
	document.write ("http://sg.shownearby.com/wg/map_categories2?postal=169878&category=18,29,17,173,256,189,8,267&map_width=900&map_height=450");
	document.write ("'><" + "/script>");	
	//document.location = "http://c1.shownearby.com/allproperty/map.html";
}


// ----------------------
// Maps for CMS Articles
// ----------------------
function resources_article_map() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		for(id in markers) {
			initializePoint(markers[id]);
		}
	}
}

function resources_article_map2() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		for(id in markers) {
			initializePoint(markers[id]);
		}
		var startZoom = 10;//customizing the startZoom
		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	}
}

// -----------------
// Drawing routines
// -----------------
function drawArea(lat, lon, radius) {
	// Convert types:
	var lat_f = parseFloat(lat);
	var long_f = parseFloat(lon);
	var radius_f = parseFloat(radius);
		
	// Convert km to lat, long:
	var coef = Math.PI*6371/180; //pi*earth_radius/180		
	var lat_range = radius_f/coef;
	var long_range = Math.abs(radius_f/(Math.cos(lat_f*Math.PI/180)*coef));
		  
	// Draw the polygon:  
	var polygon = new GPolygon([
	    new GLatLng(lat_f-lat_range, long_f-long_range),
	    new GLatLng(lat_f-lat_range, long_f+long_range),
	    new GLatLng(lat_f+lat_range, long_f+long_range),
	    new GLatLng(lat_f+lat_range, long_f-long_range),
	    new GLatLng(lat_f-lat_range, long_f-long_range)
	  ], "#f33f00", 2, 1, "#ff0000", 0.2);
	map.addOverlay(polygon);
	
	return {center: new GLatLng(lat_f,long_f), radius: radius_f, polygon: polygon};	
}

function removeArea(area) {
	map.removeOverlay(area.polygon);
}

function addAreaDragger() {
	// Create a draggable marker in the center of the area (global variable map_area):
	var marker = new GMarker(map_area.center, {draggable: true}); 
	
	// Catch drag events to repaint the area:
	GEvent.addListener(marker, "drag", function(latlng) {
		removeArea(map_area);
		map_area = drawArea(latlng.lat(), latlng.lng(), map_area.radius);
		$('#latitude').val(latlng.lat());
		$('#longitude').val(latlng.lng());
		$('#latitude').trigger('change'); // (in case you want to bind an event to this field)
	});	
	
	map.addOverlay(marker);	
	return marker;
}

