var GMapper = Class.create();
GMapper.prototype =
{
	initialize: function(elementId, key, errorCallbackFunction)
	{
		this.elementId = elementId;
		this.key = key;
		this.errorCallbackFunction = errorCallbackFunction;
		
		this.map = null;
		
		this.baseIcon = new GIcon();
		this.baseIcon.iconSize = new GSize(22, 34);
		this.baseIcon.iconAnchor = new GPoint(10, 34);
		this.baseIcon.infoWindowAnchor = new GPoint(10, 2);
		this.iconUrl = "/tdimg/";
		
		if (GBrowserIsCompatible())
		{
            this.map = new GMap2($(elementId),
            {mapTypes:[G_SATELLITE_MAP]});
            this.map.addControl(new GSmallMapControl());
            this.map.addControl(new GMapTypeControl());
            
            //this.map.enableContinuousZoom();
		}
		
		this.advice = new GIcon();
		this.advice.image = "/tdimg/lm/advice.png";
		this.advice.iconSize = new GSize(18,16);
		this.advice.iconAnchor = new GPoint(13,15);
		this.advice.infoWindowAnchor = new GPoint(13,15);
		
		this.nadvice = new GIcon(this.advice);
		this.nadvice.image = "/tdimg/lm/nadvice.png";

		this.ret = new GIcon();
		this.ret.image = "/tdimg/lm/reticle.png";
		this.ret.iconSize = new GSize(17,17);
		this.ret.iconAnchor = new GPoint(9,9);
		this.ret.infoWindowAnchor = new GPoint(9,9);
		
		this.nret = new GIcon(this.ret);
		this.nret.image = "/tdimg/lm/nreticle.png";
		
		this.carry = new GIcon();
		this.carry.image = "/tdimg/lm/carry.png";
		this.carry.iconSize = new GSize(17, 10);
		this.carry.iconAnchor = new GPoint(8, 9);
		this.carry.infoWindowAnchor = new GPoint(8, 9);
		
		this.ncarry = new GIcon(this.carry);
		this.ncarry.image = "/tdimg/lm/ncarry.png";
			
		this.reach = new GIcon();
		this.reach.image = "/tdimg/lm/reach.png"
		this.reach.iconSize = new GSize(17, 10);
		this.reach.iconAnchor = new GPoint(8, 1);
		this.reach.infoWindowAnchor = new GPoint(8, 1);
		
		this.nreach = new GIcon(this.reach);
		this.nreach.image = "/tdimg/lm/nreach.png";

		this.lr = new GIcon();
		this.lr.image = "/tdimg/lm/lr.png";
		this.lr.iconSize = new GSize(10, 10);
		this.lr.iconAnchor = new GPoint(10, 1);
		this.lr.infoWindowAnchor = new GPoint(10, 1);
		
		this.nlr = new GIcon(this.lr);
		this.nlr.image = "/tdimg/lm/nlr.png";
			
		this.lc = new GIcon();
		this.lc.image = "/tdimg/lm/lc.png";
		this.lc.iconSize = new GSize(10, 10);
		this.lc.iconAnchor = new GPoint(10, 10);
		this.lc.infoWindowAnchor = new GPoint(10, 10);
		
		this.nlc = new GIcon(this.lc);
		this.nlc.image = "/tdimg/lm/nlc.png";
			
		this.rc = new GIcon();
		this.rc.image = "/tdimg/lm/rc.png";
		this.rc.iconSize = new GSize(10, 10);
		this.rc.iconAnchor = new GPoint(1, 10);
		this.rc.infoWindowAnchor = new GPoint(1, 10);
		
		this.nrc = new GIcon(this.rc);
		this.nrc.image = "/tdimg/lm/nrc.png";
			
		this.rr = new GIcon();
		this.rr.image = "/tdimg/lm/rr.png";
		this.rr.iconSize = new GSize(10, 10);
		this.rr.iconAnchor = new GPoint(1, 1);
		this.rr.infoWindowAnchor = new GPoint(1, 1);

		this.nrr = new GIcon(this.rr);
		this.nrr.image = "/tdimg/lm/nrr.png";
	},
	setCenter: function(latitude, longitude, zoom)
	{
        var point = new GLatLng(latitude, longitude);
        this.map.setCenter(point, zoom);
	},
	clear: function()
	{
		this.map.clearOverlays();
	},
	panTo: function(latitude, longitude)
	{
        var point = new GLatLng(latitude, longitude);
        this.map.panTo(point);
	},
	addSimple: function(latitude, longitude, name)
	{
		var point = new GLatLng(latitude, longitude);
		
		var icon = new GIcon(this.baseIcon);
        icon.image = this.iconUrl + name + ".png";
        
        var marker = new GMarker(point, icon);
        this.map.addOverlay(marker);
	},
	addLMAdvice: function(latitude, longitude, html, isNew)
	{
		var point = new GLatLng(latitude, longitude);
		
		var gi = this.advice;
		if (isNew == 'true')
			gi = this.nadvice;
			
        var marker = new GMarker(point, gi);
        this.map.addOverlay(marker);
        
        this.addInfoHtml(marker, point, html);
	},
	addLMPoint: function(latitude, longitude, html, isNew)
	{
		var point = new GLatLng(latitude, longitude);
		
		var gi = this.ret;
		if (isNew == 'true')
			gi = this.nret;
		
        var marker = new GMarker(point, gi);
        this.map.addOverlay(marker);
        
        this.addInfoHtml(marker, point, html);
	},
	addLMRC: function(lat1, lon1, lat2, lon2, html, isNew)
	{
		var p1 = new GLatLng(lat1, lon1);
		var p2 = new GLatLng(lat2, lon2);
		
		var gp = new GPolyline([p1,p2], "#494949", 2, 1);
		this.map.addOverlay(gp);
		
		var gi = this.reach;
		if (isNew == 'true')
			gi = this.nreach;
		
		var m1 = new GMarker(p1, gi);
		this.map.addOverlay(m1);
		
		var hr = (isNew == 'true') ? html + ' [reach]' : html;
		this.addInfoHtml(m1, p1, hr);
		
		gi = this.carry;
		if (isNew == 'true')
			gi = this.ncarry;
			
		var m2 = new GMarker(p2, gi);
		this.map.addOverlay(m2);
		
		var hc = (isNew == 'true') ? html + ' [carry]' : html;
		this.addInfoHtml(m2, p2, hc);
	},
	addLMArea: function(lat1, lon1, lat2, lon2, lat3, lon3, lat4, lon4, clat, clon, html, isNew)
	{
		var p1 = new GLatLng(lat1, lon1);
		var p2 = new GLatLng(lat2, lon2);
		var p3 = new GLatLng(lat3, lon3);
		var p4 = new GLatLng(lat4, lon4);
		var center = new GLatLng(clat, clon);
		
		var gp = new GPolygon([p1,p2,p3,p4,p1], "#494949", 2, 1, "#494949", 0.4);
		this.map.addOverlay(gp);

		this.addInfoHtml(gp, center, html);
		
		var gi = this.lr;
		if (isNew == 'true')
			gi = this.nlr;
			
		var m1 = new GMarker(p1, gi);
		this.map.addOverlay(m1);
		
		var hlr = (isNew == 'true') ? html + '[left reach]' : html;
		this.addInfoHtml(m1, p1, hlr);
		
		gi = this.rr;
		if (isNew == 'true')
			gi = this.nrr;
		var m2 = new GMarker(p2, gi);
		this.map.addOverlay(m2);
		
		var hrr = (isNew == 'true') ? html + '[right reach]' : html;
		this.addInfoHtml(m2, p2, hrr);

		gi = this.rc;
		if (isNew == 'true')
			gi = this.nrc;
		var m3 = new GMarker(p3, gi);
		this.map.addOverlay(m3);
		
		var hrc = (isNew == 'true') ? html + '[right carry]' : html;
		this.addInfoHtml(m3, p3, hrc);
		
		gi = this.lc;
		if (isNew == 'true')
			gi = this.nlc;
		var m4 = new GMarker(p4, gi);
		this.map.addOverlay(m4);
		
		var hlc = (isNew == 'true') ? html + '[left carry]' : html;
		this.addInfoHtml(m4, p4, hlc);
	},
	addInfoHtml: function(marker, point, html)
	{
        var lmap = this.map;
        GEvent.addListener(marker, "click", function() {
        	lmap.openInfoWindowHtml(point, html);
        });
	},
    callException: function()
    {
        if (this.errorCallbackFunction.length > 0)
            eval(this.errorCallbackFunction + "('" + this.elementId + "')");
    }
}






