/*

 88888888b  888888ba  dP    dP  .88888.           8888ba.88ba  
 88         88    `8b Y8.  .8P d8'   `88          88  `8b  `8b 
a88aaaa    a88aaaa8P'  Y8aa8P  88        .d8888b. 88   88   88 
 88         88   `8b.    88    88   YP88 88'  `88 88   88   88 
 88         88    .88    88    Y8.   .88 88.  .88 88   88   88 
 88888888P  88888888P    dP     `88888'  `88888P' dP   dP   dP 

E.B.Y.G.o.M v1.0
Easilly Build Your Google Map - Version 1
http://www.canazza.co.uk/Files/EBYGOM_v1.0

By David McQuillan
	Started		31 March 2008
Last Update 	19 February 2009
*/

function EBYGoM(sLoc,sMapId,sDirectionId) {
		this.mapID = sMapId;
		this.mapDiv = document.getElementById(this.mapID);
		this.directionsId = sDirectionId;
		this.directionsDiv = document.getElementById(sDirectionId);
		this.sLocation = sLoc;
		this.zoom = 1;
		this.gMap = null;
		
		
}
EBYGoM.prototype.onLoad = function() {
	if(!this.mapDiv) this.mapDiv = document.getElementById(this.mapID);
	if(!this.mapDiv) return false;
	if(!this.directionsDiv) this.directionsDiv = document.getElementById(this.directionsId);
	if(!this.directionsDiv) return false;
	this.mapDiv.innerHTML = "Loading Map...";
	
	var def_array = this.sLocation.split(",")
	
	var default_long = 	parseFloat(	def_array[1].substr(	0,	def_array[1].length - 1	)	);
	var default_lat = 	parseFloat(	def_array[0].substr(	1,	def_array[0].length 	)	);
	if(isNaN(default_long))default_long = 0;
	if(isNaN(default_lat))default_lat = 0;	
		
			
	if (GBrowserIsCompatible()) {
		this.gMap = new GMap2(this.mapDiv);				
		this.gMap.addMapType(G_PHYSICAL_MAP); 		
		this.gMap.removeMapType(G_SATELLITE_MAP);
		this.gMap.enableScrollWheelZoom();
     	this.gMap.addControl(new GSmallMapControl());
      	this.gMap.addControl(new GMapTypeControl());
		this.gMap.setCenter(new GLatLng(default_lat,default_long),this.zoom);
      	this.gMapDir = new GDirections(this.gMap, this.directionsDiv);
		this.gMapDir.Parent = this;
		GEvent.addListener(this.gMapDir, "error", this.handleErrors);

	}
	
}
EBYGoM.prototype.Directions = function(start,end) {
	if(!this.mapDiv) this.mapDiv = document.getElementById(this.mapID);
	if(!this.mapDiv) return false;
	if(!this.directionsDiv) this.directionsDiv = document.getElementById(this.directionsId);
	if(!this.directionsDiv) return false;
	if(start.lat ) {
		start = ""+start;
		start = start.substr(1,start.length -2);
	}
	if(end.lat ) {
		end = ""+end;
		end = end.substr(1,end.length -2);
	}
	this.directionsDiv.innerHTML = "from: " + start + " to: " + end;
	
	this.gMapDir.load("from: " + start + " to: " + end)
	
}
EBYGoM.prototype.handleErrors = function() {
	
		switch(this.getStatus().code) {
		case G_GEO_UNKNOWN_ADDRESS:
			this.Parent.directionsDiv.innerHTML += "<br />No location could be found. The address may be relatively new, or it may be incorrect.\nError code: " + this.getStatus().code;
			break;
		case G_GEO_SERVER_ERROR:
			this.Parent.directionsDiv.innerHTML += "<br />Server Error.\n Error code: " + this.getStatus().code;
			break;
		case G_GEO_MISSING_QUERY:
			this.Parent.directionsDiv.innerHTML += "<br />No Query.\n Error code: " + this.getStatus().code;
			break;
		case G_GEO_BAD_KEY:
			this.Parent.directionsDiv.innerHTML += "<br />Invalid Key. \n Error code: " + this.getStatus().code;
			break;
		case G_GEO_BAD_REQUEST:
			this.Parent.directionsDiv.innerHTML += "<br />A directions request could not be successfully parsed.\n Error code: " + this.getStatus().code;
			break;
		default:
			this.Parent.directionsDiv.innerHTML += "<br />An unknown error occurred.";
	}
	
	
}
EBYGoM.prototype.newMarker = function(position,popupHTML) {
	
	var marker = new GMarker(position);
    this.gMap.addOverlay(marker);
	if(popupHTML)
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindow(popupHTML);
	})
	return marker;
}
EBYGoM.prototype.newDragMarker = function(position,onPickUp,onDrop) {


		var marker = new GMarker(position,{draggable: true});//, icon:Icons[0]});
		if(!onPickUp) {
			onPickUp = function() {
          		this.gMap.closeInfoWindow();
       		};
		}
		if(!onDrop) {
			onDrop= function() {
			  marker.openInfoWindowHtml("Location: " + marker.getPoint().toString());
			}	
		}
        GEvent.addListener(marker, "dragstart", onPickUp);

        GEvent.addListener(marker, "dragend", onDrop);
        GEvent.addListener(marker, "click", onDrop);
        this.gMap.addOverlay(marker);
		return marker
}

