function GMapObj(init)
{
	if (init===undefined)
		if (GMapObj.init===undefined) init={};
		else init=GMapObj.init;
	delete GMapObj.init;
	this._object_id=init.id || 'gmap_place';

	if (!GMapObj.objs)
	{
		GMapObj.objs={_default:this};
		GMapObj.objs[this._object_id]=this;
	}

	this._object=new GMap2($(this._object_id));
	if (init.wheel===undefined||init.wheel)
	{
		obj=function(){
			GMapObj.get(arguments.callee.id)._object.enableScrollWheelZoom();
			GEvent.removeListener(arguments.callee._listener);
		}
		obj.id=this._object_id;
		obj._listener=GEvent.addListener(this._object, "click", obj);
	}
	this._center={
		x: init.x || 55.752622,
		y: init.y || 37.621307,
		zoom: init.zoom || 10
	};
	this._center.obj=new GLatLng(this._center.x,this._center.y, this._center.zoom);
	this._object.addControl(new GSmallMapControl());
	this._object.addControl(new GMapTypeControl());
	gp=new GScreenPoint(0.5, 0.5,'fraction', 'fraction');
	this._loading=new GScreenOverlay('/f/company/loading.gif',gp,gp,new GScreenSize(170,30));
	this._reloading=null;
	this._no_reload=false;
	this._reload_fail=false;
	this._movable=null;
	this._geocoder=null;
	this._points={};
	this._object.addOverlay(this._loading);
	this.center();
	if (init.update_vars)
	{
		this._update_vars=init.update_vars;
		obj=function(){GMapObj.get(arguments.callee.id).reload();}
		obj.id=this._object_id;
		GEvent.addListener(this._object, "moveend", obj);
		GEvent.addListener(this._object, "zoomend", obj);
		if (this._update_vars.execute) this.update();
	}
	if (init.complete) this.loading(false);
}


GMapObj.prototype.reload=function (init,add)
{
	if (this._no_reload) return this._no_reload=false;
	if (this._reloading) clearTimeout(this._reloading);
	obj=function(){GMapObj.get(arguments.callee.id).update(arguments.callee.init,arguments.callee.add);}
	obj.id=this._object_id;obj.init=init;obj.add=add;
	this._reloading=setTimeout(obj,this._reload_timeout);
}

GMapObj.prototype.update=function (init,add)
{
	if (this._reloading) clearTimeout(this._reloading);
	if (this._reload_fail) return;
	if (init)
	{
		this._update_vars=init;
		if (add)
		{
			for (add in init) if (init.hasOwnProperty(add)) this._update_vars[add]=init[add];
			init=this._update_vars;
		}
	}else if (!this._update_vars) return false;
	else init=this._update_vars;

	if (init.before_update)
	{
		init._object=this;
		if (init.before_update(init)===false) return false;
	}

	this.loading(true);
	add=this._object.getBounds();
	add={sw:add.getSouthWest(),ne:add.getNorthEast()};
	add.time=new Date();
	this._updating=true;

	new Ajax.Request(this._update_url+"?"+add.time.getTime(),{
		method: "post",
		parameters: {
			target:this._object_id,type:init.type,value:init.value.toJSON(),
			x1:add.sw.lat(),x2:add.ne.lat(),y1:add.ne.lng(),y2:add.sw.lng(),limit:''
		},
		onFailure: function(obj){
			GMapObj.get(obj.request.parameters.target).loading(false);
			GMapObj.get(obj.request.parameters.target)._reload_fail=true;
			alert("К сожалению, не получилось получить список точек. Попробуйте позже.");
		},
		evalJS: "force"
	});
	if (init.clean) this.points_clean();
	delete this._updating;
	return true;
}

GMapObj.prototype.movable_add=function(x,y,dragend)
{
	if (this._movable) this._movable.remove();
	x=x || this._center.x;
	y=y || this._center.y;
	this._movable=new GMarker(new GLatLng(x,y),{draggable: true});
	this._object.addOverlay(this._movable);
	if (dragend) GEvent.addListener(this._movable, "dragend", dragend);
	return this._movable;
}

var mouse_over=false;

GMapObj.prototype.point_add=function(x,y,html,title,icon_index,url){
	if(predefined_index!=0){
		if(icon_index<0){
			icon_index=-predefined_index;
		}
		else{
			icon_index=predefined_index;
		}
	}
	if (GMapObj.__points[x+'='+y])
		return x+'='+y;
	GMapObj.__points[x+'='+y]=this;

	if(myIcons[icon_index]==undefined){
		icon=create_icon(icon_index);
	}
	else{
		icon=myIcons[icon_index];
	}

	point=new GMarker(
		new GLatLng(x, y),{
			draggable: false,
			title: title,
			icon:icon
		}
	);

	this._object.addOverlay(point);
	point._html=html;
	point._title=title;
	point._icon_index=icon_index;
	point._url=url;

	GEvent.addListener(point, 'mouseover',
		function(p){
			GMapObj.__points[p.lat()+"="+p.lng()].point_show(p.lat(),p.lng());
		}
	);
	GEvent.addListener(point, 'mouseout',
		function(p){
			GMapObj.__points[p.lat()+"="+p.lng()].point_hide(p.lat(),p.lng());
		}
	);

	if(url!='' && url!=undefined){
		GEvent.addListener(point, 'click',
			function(p){
				GMapObj.__points[p.lat()+"="+p.lng()].change_location(p.lat(),p.lng());
			}
		);
	}

	this._points[x+'='+y]=point;
	return x+'='+y;
}
GMapObj.prototype.point_get=function(x,y){return this._points[x+'='+y];}
GMapObj.prototype.point_show=function(x,y){
	if (!(point=this._points[x+'='+y]))
		return false;
	this._no_reload=true;
	if(point._icon_index<0){
		point.setImage('/i/icons/'+type2icon[-point._icon_index]+'_common_marked.png');
	}
	else{
		point.setImage('/i/icons/'+type2icon[point._icon_index]+'_platnik_marked.png');
	}
	if(t!=undefined){
		clearTimeout(t);
	}
	if(point._show!=true){
		point._show=true;
		point.openInfoWindowHtml(point._html,{maxWidth:300});
	}
	return true;
}

GMapObj.prototype.point_hide=function(x,y){
	if (!(point=this._points[x+'='+y]))
		return false;
	if(point._icon_index<0){
		point.setImage('/i/icons/'+type2icon[-point._icon_index]+'_common.png');
	}
	else{
		point.setImage('/i/icons/'+type2icon[point._icon_index]+'_platnik.png');
	}
	t=setTimeout("if(point._show==true){point.closeInfoWindow();point._show=false;};",500);
	return true;
}

GMapObj.prototype.change_location=function(x,y){
	if (!(point=this._points[x+'='+y])){
		return false;
	}
	location.href=point._url;
}

GMapObj.prototype.points_clean=function()
{
	for (i in this._points) if (this._points.hasOwnProperty(i))
	{
		i=this._points[i];
		tmp=i.getLatLng();
		delete GMapObj.__points[tmp.lat()+'='+tmp.lng()];
		i.remove();
	}
	this._points={};
}

GMapObj.prototype.geo_find=function(addr,func)
{
	if (!addr || !func) return false;
	if (!this._geocoder) this._geocoder=new GClientGeocoder();
	this._geocoder.setBaseCountryCode("ru");
	return this._geocoder.getLatLng(addr,func);
}

GMapObj.prototype.center=function(point,zoom){this._object.setCenter(point || this._center.obj, zoom || this._center.zoom);}
GMapObj.prototype.loading=function(show)
{
	if (show) {if (this._loading.isHidden()) this._loading.show();}
	else this._loading.hide();
}
GMapObj.get=function(id)
{
	if (!GBrowserIsCompatible()) GMapObj.get=function(){alert("Your browser doesn't support Google Maps.");GMapObj.get=function(){};}
	else GMapObj.get=function(id)
	{
		if (!GMapObj.objs) new GMapObj();
		if (id!==undefined) return GMapObj.objs[id];
		return GMapObj.objs._default;
	}
	return GMapObj.get(id);
}
GMapObj.__points={};
GMapObj.prototype._update_url="/company/showpoints.html";
GMapObj.prototype._reload_timeout=2000;

var koords=[22,0,23,1,24,2,24,3,24,4,24,5,24,6,24,7,24,8,24,9,24,10,24,11,24,12,24,13,24,14,24,15,24,16,24,17,24,18,24,19,23,20,22,21,21,22,21,23,21,24,21,25,21,26,21,27,21,28,21,29,21,30,21,31,21,32,21,33,21,34,21,34,20,33,20,32,19,31,19,30,18,29,18,28,17,27,17,26,16,25,16,24,15,23,15,22,2,21,1,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,1,1,2,0];
var predefined_index=0;
var myIcons = new Array();
var type2icon= new Array();
type2icon[24683]='avtosalon';
type2icon[24686]='avtoservis';
type2icon[24688]='shinomontazh';
type2icon[24690]='moika';
type2icon[24692]='azs';

var t=false;

function create_icon(id){
	myIcons[id] = new GIcon();
	if(id<0){
		myIcons[id].image = '/i/icons/'+type2icon[-id]+'_common.png';
	}
	else{
		myIcons[id].image = '/i/icons/'+type2icon[id]+'_platnik.png';
	}

	myIcons[id].iconSize = new GSize(25,35);
	myIcons[id].iconAnchor = new GPoint(25,35);
	myIcons[id].infoWindowAnchor = new GPoint(13,0);
	myIcons[id].imageMap = koords;

	return myIcons[id];
}