
var map;
var mgr;

	function usePointFromPostcode(postcode, control, node_id) {

		localSearch.setSearchCompleteCallback(null, function() {
				
			if (localSearch.results[0]) {		
				
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				var data = [];
										
				// Call an AJAX script to add the new coordinates to the database
				//new Ajax("/includes/ajax/update_coords.php?dealer_id=" + dealer_id + "&x=" + resultLng + "&y=" + resultLat, {
				//	method: 'get'
				//}).request();

				// If the mode is 0 then assume they are a user with a postcode
				if (!map_mode && postcode) {
					var ajax_return = ajax_query("update_coords.php", "postcode=" + postcode + "&y=" + resultLng + "&x=" + resultLat + "&code=" + ajax_request_code);
				} else if (map_mode && node_id) {
					var ajax_return = ajax_query("update_coords.php", "dealer_id=" + dealer_id + "&area_id=" + area_id + "&node_id=" + node_id + "&y=" + resultLng + "&x=" + resultLat + "&mode=1&code=" + ajax_request_code);
				} else if (map_mode && dealer_id) {
					var ajax_return = ajax_query("update_coords.php", "dealer_id=" + dealer_id + "&y=" + resultLng + "&x=" + resultLat + "&mode=1&code=" + ajax_request_code);
				} else {
				}
					
				map_x = resultLat;
				map_y = resultLng;
					
				if (map_mode) {
					
					if (node_id) {
						
					} else {
						dealers[0]['posn'][0] = map_x;
						dealers[0]['posn'][1] = map_y;
					}
					load(control);
				} 
				
			} else {
				//alert("Postcode not found!");
			}
		});	
		
		localSearch.execute(postcode + ", UK");
		
	}

	//<![CDATA[

	function load(control) {

		if (control == "false") {
			control = false;
		}

		if (GBrowserIsCompatible()) {
	  		if (document.getElementById(map_id)) {
				
				map = new GMap2(document.getElementById(map_id));
				
				if (map_x && map_y) {
					var posn = new GLatLng(map_x, map_y);
				} else {
					var posn = new GLatLng(54.5501, -2.3145);
				}
				
				map.setCenter(posn, 5);
				map.setMapType(G_NORMAL_MAP);
				map.setZoom(zoom);
				map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());

		
				mgr = new MarkerManager(map);
				var markersLow = [];
				var markersMid = [];
				var markersMidhigh = [];
				var markersHigh = [];

				if (dealers != undefined) {

					theIcon = new GIcon(G_DEFAULT_ICON);
					theIcon.image = "http://geocoder.ca/marker.png";
		
					// Set up our GMarkerOptions object
					if (control) {
						//alert("draggable");
						markerOptions = { icon:theIcon, draggable: true };
					} else {
						//alert("not draggable");
						markerOptions = { icon:theIcon };
					}
					for (var j=0; j<dealers.length; j++) {
						var this_dealer = dealers[j];
						//if (this_dealer) {

						var posn = new GLatLng(this_dealer["posn"][0], this_dealer["posn"][1]);
						newMarker[j] = new GMarker(posn, markerOptions);
						
						newMarker[j].dealer_id = this_dealer["id"];
		
						if (control) {
							
							GEvent.addListener(newMarker[j], "dragstart", function() {
								map.closeInfoWindow();
							});
							
							GEvent.addListener(newMarker[j], "dragend", function() {
								point = this.getPoint();
								updateCoordinates(point, this.dealer_id, 0, 0);
							});
							
							GEvent.addListener(newMarker[j], "drag", function() {
								//point = newMarker[j].getPoint();
								//updateCoordinates(point);
							});
		
						}
						
	
						//newMarker[j].bindInfoWindowTabsHtml('ID: ' + this_dealer["id"]);
						//markersLow.push(newMarker);
						mgr.addMarker(newMarker[j], 1);
						
					}

				}
				
				if(undefined!=window.areas){
					
					if (jQuery.isArray(areas)) {
								   
					for (var j=0; j<areas.length; j++) {
							var this_area = areas[j];
							this_area['info'] = '<a href="javascript:deleteMarker(' + this_area["id"] + ');">Delete this marker?</a>';;

							newMarker[j] = addMarker(this_area, j, control);
							mgr.addMarker(newMarker[j], 1);
	
					}
					
					}

				}
								
				mgr.refresh();
						
				GEvent.addListener(map, "click", function(overlay, point) {

					if (overlay && overlay.dealer_id) {
						
						var tabs = [];
								
						tabs.push(new GInfoWindowTab("General", overlay.dealer_id));
								
						overlay.openInfoWindowTabs(tabs);	
						
					}
				});
				
			}
			
		}
	
	}

	function addMarker(this_area, j, control) {

		theIcon = new GIcon(G_DEFAULT_ICON);
		theIcon.image = "http://www.leasecars.co.uk/images/brokers/markers/marker-" + this_area["area_id"] + ".png";
				
		// Set up our GMarkerOptions object
		if (control) {
			//alert("draggable");
			markerOptions = { icon:theIcon, draggable: true };
		} else {
			//alert("not draggable");
			markerOptions = { icon:theIcon };
		}
	
		var posn = new GLatLng(this_area["posn"][0], this_area["posn"][1]);
		newMarker[j] = new GMarker(posn, markerOptions);
		
		// If it has a node id then it is a dealer area node, if not it's just an area boundary node
		if (this_area["dealer_id"]) {
			newMarker[j].dealer_id = this_area["dealer_id"];
		} 
		newMarker[j].area_id = this_area["area_id"];
		newMarker[j].info = this_area['info'];
		
		newMarker[j].node_id = this_area['node_id'];
		
		if (control) {
			
			GEvent.addListener(newMarker[j], "dragstart", function() {
				map.closeInfoWindow();
			});
			
			GEvent.addListener(newMarker[j], "dragend", function() {
				point = this.getPoint();
				if (this.dealer_id) {
					updateCoordinates(point, this.dealer_id, this.area_id, this.node_id);
				} else {
					updateCoordinates(point, 0, this.area_id, 0);
				}
			});
			
			GEvent.addListener(newMarker[j], "drag", function() {
				//point = newMarker[j].getPoint();
				//updateCoordinates(point);
			});
			
		}

						
				GEvent.addListener(map, "click", function(overlay, point) {

					if (overlay && overlay.info) {
						
						var tabs = [];
								
						tabs.push(new GInfoWindowTab("General", overlay.info));
								
						overlay.openInfoWindowTabs(tabs);	
						
					}
				});


		return newMarker[j];

	}

	function displayProperty(overlay) {
				
		// Call an AJAX script to get the property information
	
	}
	
	// Update marker position
	function updateMarker(point) {
		map.setCenter(point, 12);
		marker.setPoint(point);
		marker.openInfoWindowHtml(markerText());
		updateCoordinates(point);
	}
	
	function updateCoordinates(point, dealer_id, area_id, node_id) {
		lat = point.lat().toFixed(6);
		lng = point.lng().toFixed(6);
		
		// Call an AJAX script to add the new coordinates to the database
		var ajax_return = ajax_query("update_coords.php", "node_id=" + node_id + "&area_id=" + area_id + "&dealer_id=" + dealer_id + "&mode=1&y=" + lng + "&x=" + lat + "&code=" + ajax_request_code);
					
		if (ajax_return) {
			alert("Thank you!\nThe new location has been saved successfully.");	
		}

	}
	
	function confirmCoords() {

		// Get the postcode
		var postcode = document.getElementById('dealer_company_postcode').value;
		control = false;

		usePointFromPostcode(postcode, control);

	}
	
	function createNewMarker(area_id, dealer_id) {
	
		var marker = new Array();
		var addConfirm = 0;
		
		if (dealer_id) {
			addConfirm = 1;
		}
		
		if (placedAreas[area_id] != undefined) {
			addConfirm = 1;
		} else if (!addConfirm) {
		
			if (confirm("Do you want to add a new boundary marker for this area?")) {
				addConfirm = 1;
			}
		
		}

		if (addConfirm) {
			
			var centrePoint = map.getCenter();
			centrePoint = centrePoint.toString().split(", ");
			centrePoint[0] = centrePoint[0].substring(1);
			centrePoint[1] = centrePoint[1].substring(0,(centrePoint[1].length - 1));
			
			var ajax_return = ajax_query("add_marker.php", "dealer_id=" + dealer_id + "&area_id=" + area_id + "&x=" + centrePoint[0] + "&y=" + centrePoint[1] + "&code=" + ajax_request_code);
			
			if (ajax_return) {

				marker = new Array();
				marker["posn"] = new Array();
				marker["id"] = ajax_return;
				marker["area_id"] = area_id;
				if (dealer_id) {
					marker["dealer_id"] = dealer_id;
				}
				marker["posn"][0] = centrePoint[0];
				marker["posn"][1] = centrePoint[1];
				marker["info"] = '<a href="javascript:deleteMarker(' + marker["id"] + ');">Delete this marker?</a>';
				
				var new_marker = addMarker(marker, 0, true);
				mgr.addMarker(new_marker, 1);
				
				placedAreas[area_id] = true;
				
			} else {
				alert("AJAX Failed");
			}
			
		}
			
	}
	
	function deleteMarker(node_id) {
		
		if (confirm("Are you sure you want to delete this marker?")) {
			
			var ajax_return = ajax_query("delete_marker.php", "area_id=" + node_id + "&code=" + ajax_request_code);
				
			if (ajax_return) {
					
				alert("The marker has been successfully deleted, the page will now reload.");
				window.location.reload();
				
			}
			
		}
		
	}

