function EMap(id) {
	if (!id || id=='') 
		id="map";
	try {
	if (GBrowserIsCompatible()) {
		this.map = new GMap2(document.getElementById("map"));
		this.localSearch=false;
		if (google.maps.LocalSearch) {
			this.search=new google.search.LocalSearch();
			this.localSearch=true;
		}
		this.geocoder = new GClientGeocoder();
		this.geocoder.setBaseCountryCode("uk");
		this.place=null;
		this.addressArray=Array();
		this.title="";
		this.directionsErrorContent="<h3>No directions found</h3><p>You can try <a href=\"http://maps.google.co.uk/maps?%%mapstring%%\">Google maps</a> directly.</p>";
		this.controlCount=0;
	}
        } catch(e) {
                this.broken=true;
                document.getElementById("map").innerHTML='<div class="maperror">There appears to be a problem with google maps at the moment. Please come back later.</div>';
        }

}

EMap.prototype.getText = function(i) {
	i = document.getElementById(i);
	if (!i)
		return "";

	if (i.innerHTML)
		return i.innerHTML;

	if (i.firstChild && i.firstChild.data)
		return i.firstChild.data;

	return "";
};

EMap.prototype.setAddressArray = function(a) {
	this.addressArray=a;
}
EMap.prototype.getAddressArray = function(i) {
	return this.addressArray;
};

EMap.prototype.getAddress = function(i) {
	return this.getAddressArray(i).join(",");
};

EMap.prototype.getDisplayAddressArray = function(i) {
	var address = Array();
	var elements = Array("address1","address2","address3","address4","postcode");
	var text;

	if(!i) 
		i=0;
	
	for(e in elements) {
		if (e>=i) {
			text=this.getText(elements[e]);
			if (text && text!="")
				address.push(text);
		}
	}
	return address;
};

EMap.prototype.showAddress = function(a) {
	if (a) {
		a = a.split(",");
		a = new GLatLng(a[0],a[1]);
	} else 
		a = this.getAddress();

	var o=this;
	if (this.map.directions)
		this.map.directions.clear();
//	this.map.enableScrollWheelZoom();
	this.geocoder.getLocations(a,function(r){o.setLocationResponse(r,o.getDisplayAddressArray().join("<br/>"))});
};


EMap.prototype.setLocationResponse= function(r,t) {
	if (!r || r.Status.code!=200) {
		return;
	}
	var place=r.Placemark[0];
	var p = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
	var m = new GMarker(p);
	var h = this.title;

	this.map.setCenter(p,15);
	this.map.addOverlay(m);
	if (this.icon) {
		m.setImage(this.icon);
	}
	if (!t)
		t=place.address;
	if (h && h!="") 
		h="<b>"+h+"</b><br/>";
	else 
		h="";
	m.openInfoWindowHtml("<p>"+h+t+"</p>");
	this.map.addControl(new GLargeMapControl());
	this.map.addControl(new GScaleControl());
	this.map.addControl(new GMapTypeControl());
//	this.map.enableScrollWheelZoom();
	this.map.savePosition();
	this.place=place;
};

EMap.prototype.directionsErrorResponse= function(r) {
	if (!r || r.code!=200) {
		var d=document.getElementById('directions');
		var s=this.getDirectionsString(this.lastDirectionRequest,this.lastDirectionDirection,true);
		//alert('bob');
		var c=this.directionsErrorContent;
		c=c.replace(/%%mapstring%%/g,s);
		d.innerHTML=c;
	}
};

EMap.prototype.directionsLoadResponse= function(r) {
	document.getElementById('directions').innerHTML="";
};

EMap.prototype.getDirectionsString= function(d,f,g) {
	if (f==null) {
		f=true;
	}
	if (g==null)
		g=false;

	if(f) 
		if (g)
			return "saddr="+escape(d)+"&daddr="+escape(this.place.address);
		else
			return "from: "+this.place.address+" to: "+d;
	else 
		if (g)
			return "saddr="+escape(this.place.address)+"&daddr="+escape(d);
		else
			return "from: "+d+" to: "+this.place.address;
};

EMap.prototype.localSearchResponse=function() {
	if (this.search.results[0]) {
		var res=this.search.results[0];
		d=(res.streetAddress==""?"":res.streetAddress)+(res.city==""?"":','+res.city)+(res.region==""?"":','+res.region)+(res.country==""?"":','+res.country);
		d= parseFloat(res.lat)+","+ parseFloat(res.lng);
	} else 
		d=this.searchTerm;

	this.lastDirectionRequest=d;
	d=this.getDirectionsString(d,this.directionbool);

	this.map.disableScrollWheelZoom();
	if (!this.map.directions) {
		this.map.directions=new GDirections(this.map,document.getElementById('directions'));
	}
	this.map.directions.clear();
	var o = this;
	GEvent.addListener(this.map.directions,"error",function(r){o.directionsErrorResponse(r);});
	GEvent.addListener(this.map.directions,"load",function(r){o.directionsLoadResponse(r);});
	this.map.directions.load(d);
};

EMap.prototype.setDirections=function(d,f) {
	if (!this.place)
		return;

	document.getElementById('directions').innerHTML="<p>Please wait...</p>";
	if (this.localSearch) {
		this.searchTerm=d;
		this.directionbool=f;
		this.search.setCenterPoint(this.map);
		this.search.setSearchCompleteCallback(this, EMap.prototype.localSearchResponse);
		this.search.execute(d);
	} else {

		this.lastDirectionRequest=d;
		this.lastDirectionDirection=f;
		d=this.getDirectionsString(d,f);

		this.map.disableScrollWheelZoom();
		if (!this.map.directions) {
			this.map.directions=new GDirections(this.map,document.getElementById('directions'));
		}
		this.map.directions.clear();
		var o = this;
		GEvent.addListener(this.map.directions,"error",function(r){o.directionsErrorResponse(r);});
		GEvent.addListener(this.map.directions,"load",function(r){o.directionsLoadResponse(r);});
		this.map.directions.load(d);
	}
}
