function MapViewer(mapSelector, objectsTiles) {
	this.cookies = {};
	this.yamaps = new YandexMaps(mapSelector);
	this.yamaps.setObjectsZoomTiles(objectsTiles.zoom)
	this.yamaps.setObjectsTiles    (objectsTiles.list);
	this.map = this.yamaps.map;
	this.withoutClusterer = false;
}

MapViewer.prototype = {
	withoutClusterer: false,
	globalize: function(name) {
		window[name] = this.yamaps;
		return this;
	},
	enableClustererSwitcher: function(logSelector) {
		this.withoutClusterer = location.search.indexOf('clusterer=off') > 0;
		logSelector && $(logSelector).html('Clusterer: ' + (this.withoutClusterer ? 'off' : 'on'));
		return this;
	},
	setLoadUrls: function (ajaxLoadObjectsHost, ajaxLoadObjectsUrl) {
		this.yamaps.ajaxLoadObjectsHost = ajaxLoadObjectsHost;
		this.yamaps.ajaxLoadObjectsUrl  = ajaxLoadObjectsUrl;
		return this;
	},
	zoomButtons: function(cfg) {
		var map = this.map, change = function(multi) {
			return function() { map.zoomBy(cfg.step * multi, {smooth: true}); };
		};
		$(cfg.container)
			.find(cfg.increase)
				.click(change(+1))
				.end()
			.find(cfg.decrease)
				.click(change(-1))
				.end()
			.show();
		return this;
	},
	saveCookies: function(config) {
		var map = this.map, save = function () {
			var center = map.getCenter();
			$.cookie(config.center, [center.getX(), center.getY()].join(' '), config.options);
			$.cookie(config.zoom  , map.getZoom(), config.options);
		};
		$.extend(this.cookies, config);
		YMaps.Events.observe(map, this.map.Events.BoundsChange, save);
		save(map);
		return this;
	},
	limitMapSize: function(sizes) {
		var controller = new MapSizeController(this.map, sizes);
		return this;
	},
	setStyle: function(newStyle) {
		if (newStyle.balloon) {
			newStyle.balloon = new YMaps.BalloonStyle("plain#balloon");
		}
		$.extend(Marker.style, newStyle);
		return this;
	},
	initPositionButton: function(config) {
		new PositionPointer(this.map, config, this.cookies);
		return this;
	},
	initSearching: function (config) {
		new Geocoder(this.map, config, this.cookies);
		return this;
	},
	setupMap: function (fn) {
		fn(this.map);
		return this;
	},
	start: function (config) {
		if (!this.withoutClusterer && config.cluster !== false) {
			this.yamaps.initClusterer(config.cluster, config.nonBlocking);
		}
		this.yamaps.ajaxCache = config.ajaxCache;
		this.yamaps.setCenter(config.x, config.y, config.z);
		config.autoUpdate && this.yamaps.autoUpdate();
		return this;
	},
	errorCfg: function (cfg) {
		this.error.config   = cfg;
		this.error.elem     = $(cfg.elem);
		this.error.timeout  = 0;
		return this;
	},
	error: function (msg) {
		var hide = this.error.config.hide;
		clearTimeout(this.error.timeout);
		var elem = this.error.elem
			.stop(true, true)
			.text(msg).show();
		this.error.timeout = setTimeout(function () {
			elem[hide.method](hide.speed);
		}, hide.delay);
		return this;
	}


};
