//----------------------------------------------------
// D23-Google Member Map Javascript Library
// Written By:  Dean
// Website:     http://www.dscripting.com
// Copyright 2007 - All Rights Reserved
//----------------------------------------------------

function d23gmap()
{
	this.map        = null;
	this.mapo       = null;
	this.grip       = null;
	this.grip_y     = null;
	this.mid        = 0;
	this.min_height = 135;
	this.in_me      = false;
	this.icon_path  = '';
	this.maptypes   = new Array();
	this.points     = new Array();
	this.langs      = new Array();
	this.icons      = new Array();
	this.keys       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
	this.startup    =
	{
		'lat'       : 0,
		'lng'       : 0,
		'zoom'      : 4,
		'maptype'   : 'Map',
		'mapheight' : 400
	}

	this.defst      = this.startup;
	this.prefs      =
	{
		'scrollzoom' : false,
		'keyboard'   : false,
		'controls'   : '',
		'maptypes'   : false,
		'persistent' : false
	}
}

d23gmap.prototype.initiate = function(mid, lat, lng)
{
	if (GBrowserIsCompatible())
	{
		this.mid          = parseInt(mid);
		this.defst['lat'] = this.startup['lat'] = parseFloat(lat);
		this.defst['lng'] = this.startup['lng'] = parseFloat(lng);

		this.mapo = my_getbyid('d23gmap');
		this.map  = new GMap2(this.mapo);
		var xx    = this.map.getMapTypes();

		for (var x=0; x<xx.length; x++)
		{
			this.maptypes[xx[x].getName()] = xx[x];
		}

		for (var x in this.prefs)
		{
			switch (x)
			{
				case 'scrollzoom':
					if (this.prefs[x])
					{
						this.map.enableScrollWheelZoom();
						window.addEventListener('DOMMouseScroll', d23gmap.wheel_handler, false);
					}

					break;
				case 'keyboard':
					if (this.prefs[x])
					{
						new GKeyboardHandler(this.map);
					}

					break;
				case 'controls':
					switch (this.prefs[x].toLowerCase())
					{
						case 'l':
						case 'large':
							this.map.addControl(new GLargeMapControl());
							break;
						case 's':
						case 'small':
							this.map.addControl(new GSmallMapControl());
							break;
						default:
							break;
					}

					break;
				case 'maptypes':
					if (this.prefs[x] != '')
					{
						this.map.addControl(new GMapTypeControl());
					}

					break;
				case 'overview':
					if (this.prefs[x] != '')
					{
						this.map.addControl(new GOverviewMapControl());
					}

					break;
				case 'scale':
					if (this.prefs[x] != '')
					{
						this.map.addControl(new GScaleControl());
					}

					break;
				case 'persistent':
					if (this.prefs[x])
					{
						var c = ipsclass.my_getcookie('d23gmap_data');
						if (!c)
						{
							break;
						}

						c = c.split('::');
						for (var i=0; i<c.length; i++)
						{
							var d = c[i].split(':');
							this.startup[d[0]] = d[1];

							switch (d[0])
							{
								case 'lat':
								case 'lng':
									this.startup[d[0]] = parseFloat(this.startup[d[0]]);
									break;
								case 'zoom':
								case 'mapheight':
									this.startup[d[0]] = parseInt(this.startup[d[0]]);
									break;
								default:
									break;
							}
						}

						if (this.startup['zoom'] < 0)
						{
							this.startup['zoom'] = 0;
						}
						else if (this.startup['zoom'] > 17)
						{
							this.startup['zoom'] = 17;
						}
					}
				default:
					break;
			}
		}

		if (!this.maptypes[this.startup['maptype']])
		{
			this.startup['maptype'] = this.defst['maptype'];
		}

		if (isNaN(this.startup['lat']) && !isNaN(this.defst['lat']))
		{
			this.startup['lat'] = this.defst['lat'];
		}

		if (isNaN(this.startup['lng']) && !isNaN(this.defst['lng']))
		{
			this.startup['lng'] = this.defst['lng'];
		}

		if (!isNaN(this.startup['mapheight']) && !this.in_me)
		{
			this.mapo.style.height = Math.max(this.min_height, this.startup['mapheight'])+'px';
		}

		this.map.setCenter(new GLatLng(this.startup['lat'], this.startup['lng']), this.startup['zoom']);
		this.map.panTo(new GLatLng(this.startup['lat'], this.startup['lng']));
		this.map.setMapType(this.maptypes[this.startup['maptype']]);

		if (this.points.length > 0)
		{
			for (var i=0; i<this.points.length; i++)
			{
				this.points[i]['micon']                  = new GIcon();
				this.points[i]['micon'].image            = d23gmap.icon_path+'/'+this.points[i]['icon'];
				this.points[i]['micon'].shadow           = d23gmap.icon_path+'/'+d23gmap.icons[this.points[i]['icon']]['shadow']['image'];
				this.points[i]['micon'].iconSize         = new GSize(d23gmap.icons[this.points[i]['icon']]['width'], d23gmap.icons[this.points[i]['icon']]['height']);
				this.points[i]['micon'].shadowSize       = new GSize(d23gmap.icons[this.points[i]['icon']]['shadow']['width'], d23gmap.icons[this.points[i]['icon']]['shadow']['height']);
				this.points[i]['micon'].iconAnchor       = new GPoint(9, d23gmap.icons[this.points[i]['icon']]['height']);
				this.points[i]['micon'].infoWindowAnchor = new GPoint(9, 2);

				this.points[i]['point']       = new GLatLng(this.points[i]['lat'], this.points[i]['lng']);
				this.points[i]['marker']      = new GMarker(this.points[i]['point'], {icon : this.points[i]['micon']});	
				this.points[i]['marker']._idx = i;

				this.map.addOverlay(this.points[i]['marker']);
				GEvent.addListener(this.points[i]['marker'], 'click', function()
				{
					d23gmap.map.closeInfoWindow();
					d23gmap.prep_window(this._idx);

					var x = my_getbyid('d23gmap-info-window');

					x.style.display = 'block';
					if (d23gmap.is_mod)
					{
						var t = new Array();
						if (d23gmap.mod_edit || d23gmap.mod_delete)
						{
							var _d = d23gmap.get_by_class_name(my_getbyid('d23gmap-info-window'), 'info-window-tab');
							t[t.length] = new GInfoWindowTab('Main', _d[0]);
							t[t.length] = new GInfoWindowTab('Mod', _d[1]);
						}

						if (t.length > 0)
						{
							d23gmap.points[this._idx]['marker'].openInfoWindowTabs(t);
						}
						else
						{
							d23gmap.points[this._idx]['marker'].openInfoWindow(my_getbyid('d23gmap-info-window'));
						}
					}
					else
					{
						d23gmap.points[this._idx]['marker'].openInfoWindow(my_getbyid('d23gmap-info-window'));
					}
				});
			}

			GEvent.addListener(d23gmap.map, 'infowindowopen', function()
			{
				if (d23gmap.is_mod)
				{
					var x0 = my_getbyid('d23gmap-info-window');
					var x1 = my_getbyid('d23gmap-info-window-tab-main').cloneNode(true);
					var x2 = my_getbyid('d23gmap-info-window-tab-mod').cloneNode(true);

					x0.style.display = 'none';
					x0.appendChild(x1);
					x0.appendChild(x2);
				}
				else
				{
					var x = my_getbyid('d23gmap-info-window').cloneNode(true);

					x.style.display = 'none';
					my_getbyid('d23gmap').parentNode.appendChild(x);
				}
			});
		}

		if (this.prefs['persistent'])
		{
			var a = new Array('moveend', 'zoomend', 'maptypechanged', 'dragend');
			for (var i=0; i<a.length; i++)
			{
				GEvent.addListener(this.map, a[i], d23gmap.update_persistent);
			}
		}

		try
		{
			var a = my_getbyid('d23gmap-opt-link-persistent');
			var b = my_getbyid('d23gmap-opt-link-reset');

			a.innerHTML                = (this.prefs['persistent']) ? this.langs['disable_persistent'] : this.langs['enable_persistent'];
			a.parentNode.className     = a.parentNode.className.replace('popupmenu-item-last', 'popupmenu-item');
			b.parentNode.style.display = (this.prefs['persistent']) ? '' : 'none';

			a.onclick = d23gmap.switch_persistent;
			b.onclick = d23gmap.reset_persistent;
		}

		catch(me){}

		d23gmap.load_resizer();
		window.onunload = GUnload;
	}
	else
	{
		return false;
	}
}

d23gmap.prototype.load_resizer = function()
{
	var o = this.grip = my_getbyid('d23gmap_gripper');
	if (!this.grip)
	{
		return false;
	}

	o.onmousedown = d23gmap.resizer_start;
}

d23gmap.prototype.resizer_start = function(e)
{
	if (!d23gmap.grip)
	{
		return false;
	}

	if (!e)
	{
		e = window.event;
	}

	d23gmap.grip_y = parseInt(d23gmap.mapo.style.height)-e.clientY;
	d23gmap.mapo.style.opacity = .5;

	document.oldonmousemove = document.onmousemove;
	document.oldonmouseup   = document.onmouseup;

	document.onmousemove = d23gmap.resizer_move;
	document.onmouseup   = d23gmap.resizer_end;

	return false;
}

d23gmap.prototype.resizer_move = function(e)
{
	if (!d23gmap.grip)
	{
		return false;
	}

	if (!e)
	{
		e = window.event;
	}

	d23gmap.mapo.style.height = Math.max(d23gmap.min_height, ((!isNaN(d23gmap.grip_y)) ? parseInt(d23gmap.grip_y) : 0)+e.clientY)+'px';
	return false;
}

d23gmap.prototype.resizer_end = function(e)
{
	if (!d23gmap.grip)
	{
		return false;
	}

	if (!e)
	{
		e = window.event;
	}

	document.onmousemove = document.oldonmousemove;
	document.onmouseup   = document.oldonmouseup;

	d23gmap.mapo.style.opacity = 1;
	d23gmap.grip_y             = null;

	d23gmap.map.checkResize();
	d23gmap.update_persistent();
}

d23gmap.prototype.add_lang = function()
{
	for (var i=0; i<arguments.length; i+=2)
	{
		if (arguments[i] != '')
		{
			this.langs[arguments[i]] = arguments[i+1];
		}
	}
}

d23gmap.prototype.load_icons = function(a)
{
	try
	{
		for (var x in a)
		{
			this.icons[x] = a[x];
		}
	}

	catch(me){}
}

d23gmap.prototype.load_prefs = function(a)
{
	try
	{
		for (var x in a)
		{
			this.prefs[x] = a[x];
		}
	}

	catch(me){}
}

d23gmap.prototype.update_persistent = function(e)
{
	if (!d23gmap.mid)
	{
		return;
	}

	var a = new Array();

	a['lat']       = d23gmap.map.getCenter().lat();
	a['lng']       = d23gmap.map.getCenter().lng();
	a['zoom']      = d23gmap.map.getZoom();
	a['maptype']   = d23gmap.map.getCurrentMapType().getName();
	a['mapheight'] = Math.max(d23gmap.min_height, parseInt(d23gmap.mapo.style.height));

	var c = new Array();
	for (var x in a)
	{
		c[c.length] = x+':'+a[x];
	}

	ipsclass.my_setcookie('d23gmap_data', c.join('::'), true);
}

d23gmap.prototype.switch_persistent = function(e)
{
	if (!d23gmap.mid)
	{
		return;
	}

	e = ipsclass.cancel_bubble(e);

	var a = new Array('moveend', 'zoomend', 'maptypechanged', 'dragend');
	var b = my_getbyid('d23gmap-opt-link-persistent');
	var c = my_getbyid('d23gmap-opt-link-reset');

	if (d23gmap.prefs['persistent'])
	{
		for (var i=0; i<a.length; i++)
		{
			GEvent.clearListeners(d23gmap.map, a[i]);
		}

		try
		{
			b.innerHTML                = d23gmap.langs['enable_persistent'];
			b.parentNode.className     = b.parentNode.className.replace('popupmenu-item', 'popupmenu-item-last');
			c.parentNode.style.display = 'none';
		}

		catch(me){}
	}
	else
	{
		for (var i=0; i<a.length; i++)
		{
			GEvent.addListener(d23gmap.map, a[i], d23gmap.update_persistent);
		}

		try
		{
			b.innerHTML                = d23gmap.langs['disable_persistent'];
			b.parentNode.className     = b.parentNode.className.replace('popupmenu-item-last', 'popupmenu-item');
			c.parentNode.style.display = '';
		}

		catch(me){}
	}

	d23gmap.ajax = new ajax_request();
	d23gmap.ajax.process(ipb_var_base_url+'autocom=gmap&code=prefs&do=ajax&pref=persistent&value='+((d23gmap.prefs['persistent']) ? 0 : 1));

	d23gmap.prefs['persistent'] = (d23gmap.prefs['persistent']) ? 0 : 1;
	if (typeof(this.blur) == 'function')
	{
		this.blur();
	}

	if (is_ie)
	{
		var x = ipsmenu.menu_cur_open;
		var z = my_getbyid('d23gmap-options');

		ipsmenu.menu_registered[x].close();
		ipsmenu.menu_registered[x].open(z);
	}
}

d23gmap.prototype.reset_persistent = function(e)
{
	if (!d23gmap.mid)
	{
		return;
	}

	e = ipsclass.cancel_bubble(e);

	message_pop_up_lang['d23gmap_persistent_reset'] = d23gmap.langs['persistent_reset'];

	menu_action_close();
	ipsclass.my_setcookie('d23gmap_data', '', true);
	show_inline_messages_instant('d23gmap_persistent_reset');

	if (typeof(this.blur) == 'function')
	{
		this.blur();
	}
}

d23gmap.prototype.wheel_handler = function(e)
{
	e = ipsclass.cancel_bubble(e);
}

d23gmap.prototype.prep_window = function(x)
{
	try
	{
		var a = d23gmap.points[x];
		var b = my_getbyid('d23gmap-info-window');
		var c = my_getbyid('info-window-avatar');
		var d = my_getbyid('info-window-user');
		var e = my_getbyid('info-window-msg');
		var f = my_getbyid('info-window-link-zoom:in');
		var g = my_getbyid('info-window-link-zoom:out');
		var h = my_getbyid('info-window-link-center');
		var i = my_getbyid('info-window-link-my:marker');
		var j = my_getbyid('info-window-link-edit:link');
		var k;
		var l;

		if (d23gmap.is_mod)
		{
			if (d23gmap.mod_edit)
			{
				var k = my_getbyid('info-window-mod:edit');
			}

			if (d23gmap.mod_delete)
			{
				var l = my_getbyid('info-window-mod:delete');
			}
		}

		b.idx       = x;
		c.innerHTML = '';

		if (typeof(a['ava']) != 'undefined' && a['ava'] != null && a['ava'] != '')
		{
			c.innerHTML = d23gmap.decode_64(a['ava']);
		}

		d.mid       = a['mid'];
		d.innerHTML = a['name'];
		e.innerHTML = a['msg'];

		d.onclick = d23gmap.user_profile;
		f.onclick = d23gmap.window_zoom_in;
		g.onclick = d23gmap.window_zoom_out;
		h.onclick = d23gmap.window_center_here;

		d.idx = f.idx = g.idx = h.idx = x;

		i.style.display = 'none';
		if (a['mid'] == d23gmap.mid)
		{
			i.style.display = 'block';
			j.onclick       = d23gmap.edit_marker;
		}

		if (d23gmap.is_mod)
		{
			if (d23gmap.mod_edit)
			{
				k.idx     = x;
				k.mid     = a['mid'];
				k.onclick = d23gmap.mod_edit_marker;
			}

			if (d23gmap.mod_delete)
			{
				l.idx     = x
				l.mid     = a['mid'];
				l.onclick = d23gmap.mod_delete_marker;
			}
		}
	}

	catch(me){alert(me);}
}

d23gmap.prototype.edit_marker = function(e)
{
	if (!d23gmap.mid)
	{
		return;
	}

	e = ipsclass.cancel_bubble(e);
	window.location = ipb_var_base_url+'autocom=gmap&code=me';
}

d23gmap.prototype.user_profile = function(e)
{
	e = ipsclass.cancel_bubble(e);
	window.location = ipb_var_base_url+'showuser='+parseInt(this.mid);
}

d23gmap.prototype.window_zoom_in = function(e)
{
	e = ipsclass.cancel_bubble(e);

	try
	{
		d23gmap.map.setZoom(15);
		if (typeof(this.blur) == 'function')
		{
			this.blur();
		}
	}

	catch(me){}
}

d23gmap.prototype.window_zoom_out = function(e)
{
	e = ipsclass.cancel_bubble(e);

	try
	{
		d23gmap.map.setZoom(4);
		if (typeof(this.blur) == 'function')
		{
			this.blur();
		}
	}

	catch(me){}
}

d23gmap.prototype.window_center_here = function(e)
{
	e = ipsclass.cancel_bubble(e);

	try
	{
		d23gmap.map.closeInfoWindow();
		d23gmap.map.setCenter(d23gmap.points[parseInt(this.idx)]['point']);

		if (typeof(this.blur) == 'function')
		{
			this.blur();
		}
	}

	catch(me){}
}

d23gmap.prototype.mod_edit_marker = function(e)
{
	e = ipsclass.cancel_bubble(e);
	if (!d23gmap.is_mod || !d23gmap.mod_edit || !d23gmap.mid)
	{
		return false;
	}

	try
	{
		window.location = ipb_var_base_url+'autocom=gmap&code=me&edit:id='+this.mid;
	}

	catch(me){}
}

d23gmap.prototype.mod_delete_marker = function(e)
{
	e = ipsclass.cancel_bubble(e);
	if (!d23gmap.is_mod || !d23gmap.mod_delete || !d23gmap.mid)
	{
		return false;
	}

	try
	{
		if (confirm(d23gmap.langs['mod_confirm_delete']))
		{
			var a        = new Array();
			a['autocom'] = 'gmap';
			a['code']    = 'delete';
			a['del:id']  = this.mid;

			d23gmap.me.ajax = null;
			d23gmap._idx    = parseInt(this.idx);

			func = function()
			{
				if (!d23gmap.me.ajax.readystate_ready_and_ok())
				{
					d23gmap.me.status(true, 'mod-status', d23gmap.langs['deleting']);
					return;
				}

				d23gmap.me.status(false, 'mod-status');
				var d = d23gmap.me.ajax.xmlhandler.responseText;

				if (d == 'deleted')
				{
					message_pop_up_lang['d23gmap_marker_deleted'] = d23gmap.langs['marker_deleted'];
					show_inline_messages_instant('d23gmap_marker_deleted');

					d23gmap.map.removeOverlay(d23gmap.points[parseInt(d23gmap._idx)]['marker']);
					d23gmap._idx = null;
				}
			}

			d23gmap.me.ajax = new ajax_request();
			d23gmap.me.ajax.onreadystatechange(func);
			d23gmap.me.ajax.process(ipb_var_base_url, 'POST', d23gmap.me.ajax.format_for_post(a));
		}
	}

	catch(me){}
	return false;
}

d23gmap.prototype.add_point = function(a)
{
	var x = this.points.length;
	this.points[x] = a;
}

d23gmap.prototype.add_points = function(a)
{
	for (var i=0; i<a.length; i++)
	{
		d23gmap.add_point(a[i]);
	}
}

d23gmap.prototype.get_parent = function(o, n)
{
	while (o)
	{
		if (o.nodeName.toLowerCase() == n.toLowerCase())
		{
			return o;
		}

		o = o.parentNode;
	}

	return o;
}

d23gmap.prototype.gen_rand_id = function()
{
	return Math.floor(Math.random()*99999999999);
}

d23gmap.prototype.get_by_class_name = function(o, c)
{
	var a = (((typeof(o) == 'string') ? my_getbyid(o) : ((typeof(o) == 'object') ? o : document.body)) || document.body).getElementsByTagName('*');
	var b = [];
	var x;

	for (var i=0; i<a.length; i++)
	{
		x = a[i];
		if (d23gmap.has_class(x, c))
		{
			b.push(x);
		}
	}

	return b;
}

d23gmap.prototype.has_class = function(o, c)
{
	if (o == null || !RegExp)
	{
		return false;
	}

	var e = new RegExp("(^|\\s)"+c+"(\\s|$)");
	if (typeof(o) == 'string')
	{
		return e.test(o);
	}
	else if (typeof(o) == 'object' && o.className)
	{
		return e.test(o.className);
	}

	return false;
}

d23gmap.prototype.encode_64 = function(t)
{
	var i = 0;
	var o = '';
	var c1, c2, c3;
	var e1, e2, e3, e4;

	do
	{
		c1 = t.charCodeAt(i++);
		c2 = t.charCodeAt(i++);
		c3 = t.charCodeAt(i++);

		e1 = c1 >> 2;
		e2 = ((c1 & 3) << 4) | (c2 >> 4);
		e3 = ((c2 & 15) << 2) | (c3 >> 6);
		e4 = c3 & 63;

		if (isNaN(c2))
		{
			e3 = e4 = 64;
		}
		else if (isNaN(c3))
		{
			e4 = 64;
		}

		o = o+d23gmap.keys.charAt(e1)+d23gmap.keys.charAt(e2)+d23gmap.keys.charAt(e3)+d23gmap.keys.charAt(e4);
	}

	while (i < t.length);
	return o;
}

d23gmap.prototype.decode_64 = function(t)
{
	var i = 0;
	var o = '';
	var c1, c2, c3;
	var e1, e2, e3, e4;

	t = t.replace(/[^A-Za-z0-9\+\/\=]/g, '');
	do
	{
		e1 = d23gmap.keys.indexOf(t.charAt(i++));
		e2 = d23gmap.keys.indexOf(t.charAt(i++));
		e3 = d23gmap.keys.indexOf(t.charAt(i++));
		e4 = d23gmap.keys.indexOf(t.charAt(i++));

		c1 = (e1 << 2) | (e2 >> 4);
		c2 = ((e2 & 15) << 4) | (e3 >> 2);
		c3 = ((e3 & 3) << 6) | e4;

		o = o+String.fromCharCode(c1);
		if (e3 != 64)
		{
			o = o+String.fromCharCode(c2);
		}

		if (e4 != 64)
		{
			o = o+String.fromCharCode(c3);
		}
	}

	while (i < t.length);
	return o;
}

d23gmap.prototype.me = (function()
{
	var me =
	{
		'lat'    : null,
		'lng'    : null,
		'point'  : null,
		'marker' : null,
		'zoom'   : null,
		'loc'    : null,
		'icon'   : null
	}

	me.update_display = function()
	{
		d23gmap.me.update_coords();
		if (d23gmap.me.lat == null || d23gmap.me.lng == null || isNaN(d23gmap.me.lat) || isNaN(d23gmap.me.lng))
		{
			return;
		}

		d23gmap.me.micon                  = new GIcon();
		d23gmap.me.micon.image            = d23gmap.icon_path+'/'+d23gmap.me.icon;
		d23gmap.me.micon.shadow           = d23gmap.icons[d23gmap.me.icon]['shadow']['image'];
		d23gmap.me.micon.iconSize         = new GSize(d23gmap.icons[d23gmap.me.icon]['width'], d23gmap.icons[d23gmap.me.icon]['height']);
		d23gmap.me.micon.shadowSize       = new GSize(d23gmap.icons[d23gmap.me.icon]['shadow']['width'], d23gmap.icons[d23gmap.me.icon]['shadow']['height']);
		d23gmap.me.micon.iconAnchor       = new GPoint(9, d23gmap.icons[d23gmap.me.icon]['height']);
		d23gmap.me.micon.infoWindowAnchor = new GPoint(9, 2);
		d23gmap.me.micon.infoShadowAnchor = new GPoint(18, 25);

		if (d23gmap.me.marker)
		{
			d23gmap.map.removeOverlay(d23gmap.me.marker);
		}

		d23gmap.me.point  = new GLatLng(d23gmap.me.lat, d23gmap.me.lng);
		d23gmap.me.marker = new GMarker(d23gmap.me.point, {draggable : true, icon : d23gmap.me.micon});

		GEvent.addListener(d23gmap.me.marker, 'dragstart', function()
		{
			d23gmap.map.closeInfoWindow();
		});

		GEvent.addListener(d23gmap.me.marker, 'drag', function()
		{
			my_getbyid('my_lat').value  = d23gmap.me.marker.getPoint().lat();
			my_getbyid('my_long').value = d23gmap.me.marker.getPoint().lng();
		});

		GEvent.addListener(d23gmap.me.marker, 'dragend', function()
		{
			my_getbyid('my_lat').value  = d23gmap.me.marker.getPoint().lat();
			my_getbyid('my_long').value = d23gmap.me.marker.getPoint().lng();

			d23gmap.map.setCenter(d23gmap.me.marker.getPoint(), d23gmap.me.zoom = d23gmap.map.getZoom());
			setTimeout("d23gmap.map.panTo(d23gmap.me.marker.getPoint())", 1000);
		});

		d23gmap.map.addOverlay(d23gmap.me.marker);
		d23gmap.map.setCenter(d23gmap.me.point, (!isNaN(d23gmap.me.zoom) && d23gmap.me.zoom != null) ? parseInt(d23gmap.me.zoom) : 13);
		setTimeout("d23gmap.map.panTo(d23gmap.me.point)", 1000);
	}

	me.set_coords = function()
	{
		d23gmap.me.lat = (typeof(arguments[0]) != 'undefined' && arguments[0] != null) ? parseFloat(arguments[0]) : null;
		d23gmap.me.lng = (typeof(arguments[1]) != 'undefined' && arguments[1] != null) ? parseFloat(arguments[1]) : null;

		my_getbyid('my_lat').value  = (d23gmap.me.lat) ? d23gmap.me.lat : '';
		my_getbyid('my_long').value = (d23gmap.me.lng) ? d23gmap.me.lng : '';
	}

	me.update_coords = function()
	{
		d23gmap.me.lat = parseFloat(my_getbyid('my_lat').value.toString());
		d23gmap.me.lng = parseFloat(my_getbyid('my_long').value.toString());

		if (isNaN(d23gmap.me.lat))
		{
			d23gmap.me.lat = null;
		}

		if (isNaN(d23gmap.me.lng))
		{
			d23gmap.me.lng = null;
		}
	}

	me.update_zoom = function()
	{
		my_getbyid('my_zoom').innerHTML = parseInt(d23gmap.me.zoom);
	}

	me.find_loc = function()
	{
		d23gmap.me.loc = my_getbyid('loc').value;
		if (d23gmap.me.loc == '')
		{
			return;
		}

		d23gmap.me.ajax_find = null;
		func = function()
		{
			if (!d23gmap.me.ajax_find.readystate_ready_and_ok())
			{
				d23gmap.me.status(true, 'status', 'Searching...');
				return;
			}

			d23gmap.me.status(false, 'status');
			var d = d23gmap.me.ajax_find.xmlhandler.responseText;

			if (d == 'none')
			{
				d23gmap.me.status(true, 'status', 'No matches found.');
			}
			else
			{
				eval(d);
			}
		}

		d23gmap.me.ajax_find = new ajax_request();
		d23gmap.me.ajax_find.onreadystatechange(func);
		d23gmap.me.ajax_find.process(ipb_var_base_url+'autocom=gmap&code=find&loc='+escape(d23gmap.me.loc));
	}

	me.find_process = function(a)
	{
		try
		{
			var o       = my_getbyid('find_loc_res');
			o.innerHTML = '';

			for (var x=0; x<a.length; x++)
			{
				var d = document.createElement('div');
				var l = document.createElement('a');

				l.href      = '#';
				l.onclick   = d23gmap.me.find_choose;
				l.lat       = a[x]['lat'];
				l.lng       = a[x]['lng'];
				l.innerHTML = d23gmap.decode_64(a[x]['address']);

				d.appendChild(l);
				o.appendChild(d);
			}
		}

		catch(me){alert(me);}
	}

	me.find_choose = function(e)
	{
		e = ipsclass.cancel_bubble(e);
		if (this.lat && this.lng)
		{
			d23gmap.me.set_coords(this.lat, this.lng);
			d23gmap.me.update_display();
		}
	}

	me.save_marker = function()
	{
		d23gmap.me.update_coords();
		d23gmap.me.update_zoom();

		if (d23gmap.me.lat == null || isNaN(d23gmap.me.lat))
		{
			d23gmap.me.lat = '';
		}

		if (d23gmap.me.lng == null || isNaN(d23gmap.me.lng))
		{
			d23gmap.me.lng = '';
		}

		var a = new Array();
		a['autocom'] = 'gmap';
		a['code']    = 'dome';
		a['lat']     = d23gmap.me.lat;
		a['lng']     = d23gmap.me.lng;
		a['zoom']    = d23gmap.me.zoom;
		a['icon']    = d23gmap.me.icon;
		a['msg']     = my_getbyid('my_msg').value;
		a['edit:id'] = my_getbyid('edit:id').value;

		d23gmap.me.ajax_save = null;
		func = function()
		{
			if (!d23gmap.me.ajax_save.readystate_ready_and_ok())
			{
				d23gmap.me.status(true, 'status2', 'Saving...');
				return;
			}

			d23gmap.me.status(false, 'status2');
			var d = d23gmap.me.ajax_save.xmlhandler.responseText;

			if (d == 'saved')
			{
				d23gmap.me.status(true, 'status2', 'Marker location saved.');
				setTimeout("d23gmap.me.status(false, 'status2')", 2500);
			}
			else
			{
				alert(d);
			}
		}

		d23gmap.me.ajax_save = new ajax_request();
		d23gmap.me.ajax_save.onreadystatechange(func);
		d23gmap.me.ajax_save.process(ipb_var_base_url+'autocom=gmap&code=dome', 'POST', d23gmap.me.ajax_save.format_for_post(a));
	}

	me.clear_marker = function()
	{
		d23gmap.me.update_coords();
		d23gmap.me.update_zoom();

		d23gmap.me.lat = '';
		d23gmap.me.lng = '';

		var a = new Array();
		a['autocom'] = 'gmap';
		a['code']    = 'dome';
		a['do']      = 'clear';
		a['edit:id'] = my_getbyid('edit:id').value;

		d23gmap.me.ajax_clear = null;
		func = function()
		{
			if (!d23gmap.me.ajax_clear.readystate_ready_and_ok())
			{
				d23gmap.me.status(true, 'status2', 'Clearing...');
				return;
			}

			d23gmap.me.status(false, 'status2');
			var d = d23gmap.me.ajax_clear.xmlhandler.responseText;

			if (d == 'cleared')
			{
				my_getbyid('my_lat').value  = '';
				my_getbyid('my_long').value = '';
				my_getbyid('my_msg').value  = '';

				d23gmap.me.update_coords();
				d23gmap.me.status(true, 'status2', 'Marker location cleared.');

				setTimeout("d23gmap.me.status(false, 'status2')", 2500);
			}
		}

		d23gmap.me.ajax_clear = new ajax_request();
		d23gmap.me.ajax_clear.onreadystatechange(func);
		d23gmap.me.ajax_clear.process(ipb_var_base_url+'autocom=gmap&code=dome&do=clear', 'POST', d23gmap.me.ajax_clear.format_for_post(a));
	}

	me.status = function()
	{
		var o = my_getbyid(arguments[1]);
		if (!o)
		{
			return false;
		}

		if (arguments[0] === true)
		{
			o.innerHTML     = arguments[2];
			o.style.display = 'block';
		}
		else
		{
			o.innerHTML     = '';
			o.style.display = 'none';
		}
	}

	me.select_icon = function(i)
	{
		if (i.toLowerCase().match(/-shadow/i))
		{
			return false;
		}

		menu_action_close();

		d23gmap.me.icon = i;
		my_getbyid('my_icon').src = d23gmap.icon_path+'/'+i;

		d23gmap.me.update_display();
	}

	me.initiate = function()
	{
		my_getbyid('update_display').onclick = d23gmap.me.update_display;
		my_getbyid('save_marker').onclick    = d23gmap.me.save_marker;
		my_getbyid('clear_marker').onclick   = d23gmap.me.clear_marker;
		my_getbyid('find_loc').onclick       = d23gmap.me.find_loc;

		GEvent.addListener(d23gmap.map, 'zoomend', function()
		{
			my_getbyid('my_zoom').innerHTML = d23gmap.me.zoom = d23gmap.map.getZoom();
		});

		if (!d23gmap.me.zoom)
		{
			d23gmap.me.zoom = d23gmap.map.getZoom();
		}

		d23gmap.me.icon = my_getbyid('my_icon').src.replace(d23gmap.icon_path+'/', '');

		d23gmap.me.update_zoom();
		d23gmap.me.update_display();
	}

	return me;
})();

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g, '')
}
