var map;
var icon0;
var newpoints = new Array();

function loadMap(latitude, longitude) {
	map = new GMap2(document.getElementById("google_maps"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(latitude, longitude), 15);
	//map.setMapType(G_MAP_TYPE);
 
	icon0 = new GIcon();
	//icon0.image = "/images/icons/housemarker.png";
	//icon0.shadow = "http://www.google.com/mapfiles/shadow50.png";
	//icon0.iconSize = new GSize(20, 34);
	//icon0.shadowSize = new GSize(37, 34);
	//icon0.iconAnchor = new GPoint(9, 34);
	//icon0.infoWindowAnchor = new GPoint(9, 2);
	//icon0.infoShadowAnchor = new GPoint(18, 25);
	icon0.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
	icon0.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	icon0.iconSize = new GSize(12, 20);
	icon0.shadowSize = new GSize(22, 20);
	icon0.iconAnchor = new GPoint(6, 20);
	icon0.infoWindowAnchor = new GPoint(5, 1);
	icon0.infoShadowAnchor = new GPoint(9, 17);
}

function loadMapPlain(latitude, longitude, clickUrl) {
	map = new GMap2(document.getElementById("google_maps"));
	map.setCenter(new GLatLng(latitude, longitude), 15);
	map.addControl(new GSmallZoomControl());
	
	//map.setMapType(G_MAP_TYPE);
 
 	GEvent.addListener(map, "click", function() {
  		 location.href= clickUrl;
	});
 
	icon0 = new GIcon();
	//icon0.image = "/images/icons/housemarker.png";
	//icon0.shadow = "http://www.google.com/mapfiles/shadow50.png";
	//icon0.iconSize = new GSize(20, 34);
	//icon0.shadowSize = new GSize(37, 34);
	//icon0.iconAnchor = new GPoint(9, 34);
	//icon0.infoWindowAnchor = new GPoint(9, 2);
	//icon0.infoShadowAnchor = new GPoint(18, 25);
	icon0.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
	icon0.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	icon0.iconSize = new GSize(12, 20);
	icon0.shadowSize = new GSize(22, 20);
	icon0.iconAnchor = new GPoint(6, 20);
	icon0.infoWindowAnchor = new GPoint(5, 1);
	icon0.infoShadowAnchor = new GPoint(9, 17);
}

 
function addPoints(latitude, longitude) {
 
	newpoints[0] = new Array(latitude, longitude, icon0, 'raj', ' '); 
 
	for(var i = 0; i < newpoints.length; i++) {
		var point = new GPoint(newpoints[i][1], newpoints[i][0]);
		var popuphtml = newpoints[i][4] ;
		var marker = createMarker(point, newpoints[i][2]);
		map.addOverlay(marker);
	}
}
 
function createMarker(point, icon, popuphtml) 
{
	var popuphtml = "<div id=\"popup\">" + popuphtml + "<\/div>";
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {
		//marker.openInfoWindowHtml(popuphtml);
	});
	return marker;
}

function loadMapPropertyAdd(latitude, longitude, zoom, hybrid) 
{
	if($('#latitude').val() == "")
	{
		$('#latitude').val(latitude);
	}
	if($('#longitude').val() == "")
	{
		$('#longitude').val(longitude);
	}

	map = new GMap2(document.getElementById("google_maps"));
	
	if(hybrid == true) {
		map.setMapType(G_HYBRID_MAP);
	}
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	
	var center = new GLatLng(latitude, longitude);
   	map.setCenter(center, zoom);

	map.setZoom(zoom);	//Zoom


 	var houseIcon = new GIcon();
	 // houseIcon.image = "/images/icons/housemarker.png";
	//  houseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	 //houseIcon.iconSize = new GSize(20, 34);
	 // houseIcon.shadowSize = new GSize(37, 34);
	  //houseIcon.iconAnchor = new GPoint(10, 34);
	  //houseIcon.infoWindowAnchor = new GPoint(10, 1);
	  houseIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
	  houseIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	  houseIcon.iconSize = new GSize(12, 20);
	  houseIcon.shadowSize = new GSize(22 ,20);
	  houseIcon.iconAnchor = new GPoint(6, 20);
	  houseIcon.infoWindowAnchor = new GPoint(5, 1);

	var marker = new GMarker(center, {draggable: true, icon:houseIcon});
	GEvent.addListener(marker, "dragstart", function() {
        map.closeInfoWindow();
      });

    GEvent.addListener(marker, "dragend", function()
	{
		var lat = marker.getPoint().lat();
		var lng = marker.getPoint().lng();
	 	$('#latitude').val(lat);
		$('#longitude').val(lng);

		//var myHtml = "Position set";
		//marker.openInfoWindowHtml(myHtml);
    });
	map.addOverlay(marker);

	/*
		GEvent.addListener(marker,"click", function(overlay, latlng) { 
        if (latlng) {   
          var myHtml = "The GLatLng value is: "+latlng+"<br />" + map.fromLatLngToDivPixel(latlng) + " at zoom level " + map.getZoom();
          map.openInfoWindow(latlng, myHtml);
        }
      });
	*/
	
}





function loadMapCircle(latitude, longitude, radiusKM) 
{
	if($('#latitude').val() == "")
	{
		$('#latitude').val(latitude);
	}
	if($('#longitude').val() == "")
	{
		$('#longitude').val(longitude);
	}

	var radiusMi = radiusKM / 1.609;

	//map = new GMap2(document.getElementById("google_maps"));		
		    
	    map.addControl(new GLargeMapControl());
	    map.addControl(new GMapTypeControl());
			   
			    
			    
	    //Zoom on place lat/lon
	    var point = new GLatLng(latitude, longitude);
	    //OLD Version 1 API map.centerAndZoom(point, 2);
	    map.setCenter(point, 2);	
	    var marker = new GMarker(point, {draggable: true});
	    //map.addOverlay(marker);
	    
	    
	    //Calculate latitude and longitude widths of circle
	    var d2r = Math.PI / 180;
    	    var circleLat = radiusMi * 0.014483;  // Convert statute miles into degrees latitude
    	    var circleLng = circleLat / Math.cos(latitude * d2r);
	    
	    //Change zoom level to size of place
	    sw = new GLatLng(latitude - circleLat, longitude - circleLng , true);
	    ne = new GLatLng(latitude + circleLat, longitude + circleLng , true);
	    bounds = new GLatLngBounds(sw, ne);
	    
	    map.setZoom(map.getBoundsZoomLevel(bounds));  

	    GEvent.addListener(marker, "dragend", function()
	    {
			var lat = marker.getPoint().lat();
			var lng = marker.getPoint().lng();
		 	$('#latitude').val(lat);
			$('#longitude').val(lng);

			//var myHtml = "Position set";
			//marker.openInfoWindowHtml(myHtml);
			
			
			
			//Add a circle to the map at this location
     			map.removeOverlay(circle);		//Remove the old one
     			circle = null;
     			circle = new CircleOverlay(marker.getPoint(), radiusMi, circleColour, 1, 1, circleColour, circleTransparency);
     			map.addOverlay(circle);
		    
		
			
	    });
	    map.addOverlay(marker);
			    
			    
			
	    //Add a circle to the map
     		//alert("In here about to add circle");
     		//alert(map.getCenter());
     		var circle = null;
     		circle = new CircleOverlay(map.getCenter(), radiusMi, circleColour, 1, 1, circleColour, circleTransparency);
     		map.addOverlay(circle);
			    
	
}





/* From http://dawsdesign.com/drupal/google_maps_circle_overlay to draw a circle on the map - really a many sided polygon */

// This file adds a new circle overlay to GMaps2
// it is really a many-pointed polygon, but look smooth enough to be a circle.
var CircleOverlay = function(latLng, radius, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity) {
    this.latLng = latLng;
    this.radius = radius;
    this.strokeColor = strokeColor;
    this.strokeWidth = strokeWidth;
    this.strokeOpacity = strokeOpacity;
    this.fillColor = fillColor;
    this.fillOpacity = fillOpacity;
}

// Implements GOverlay interface
CircleOverlay.prototype = new GOverlay;

CircleOverlay.prototype.initialize = function(map) {
	
    this.map = map;
}
//alert("Lat = " + this.latLng.lat() + " Lon = " + this.latLng.lng());	//Testing in
CircleOverlay.prototype.clear = function() {
    if(this.polygon != null && this.map != null) {
        this.map.removeOverlay(this.polygon);
    }
}

// Calculate all the points and draw them
CircleOverlay.prototype.redraw = function(force) {
    var d2r = Math.PI / 180;
    circleLatLngs = new Array();
    var circleLat = this.radius * 0.014483;  // Convert statute miles into degrees latitude
    var circleLng = circleLat / Math.cos(this.latLng.lat() * d2r);
    var numPoints = 40;
   
    // 2PI = 360 degrees, +1 so that the end points meet
    for (var i = 0; i < numPoints + 1; i++) {
        var theta = Math.PI * (i / (numPoints / 2));
        var vertexLat = this.latLng.lat() + (circleLat * Math.sin(theta));
        var vertexLng = this.latLng.lng() + (circleLng * Math.cos(theta));
        var vertextLatLng = new GLatLng(vertexLat, vertexLng);
        circleLatLngs.push(vertextLatLng);
    }
   
   	
    this.clear();
    this.polygon = new GPolygon(circleLatLngs, this.strokeColor, this.strokeWidth, this.strokeOpacity, this.fillColor, this.fillOpacity);
    this.map.addOverlay(this.polygon);
}

CircleOverlay.prototype.remove = function() {
    this.clear();
}

CircleOverlay.prototype.containsLatLng = function(latLng) {
    // Polygon Point in poly
    if(this.polygon.containsLatLng) {
        return this.polygon.containsLatLng(latLng);
    }
}

CircleOverlay.prototype.setRadius = function(radius) {
    this.radius = radius;
}

CircleOverlay.prototype.setLatLng = function(latLng) {
    this.latLng = latLng;
}



