(function ($) {

	$.fn.gmap = function (options) {
		return new Map(this[0], options);
	};

	function Map(element, options) {
		return this instanceof Map ? this.startMap(element, options) : new Map(element, options);
	}

	$.extend(Map.prototype, {
		map:null,
		options:null,
		startMap:function (element, options) {
			var self = this;

			this.options = {
				center:{
					Lat:55.755798014798095,
					Lng:37.617563009262085
				},
				zoom:16
			};

			$.extend(this.options, options || {});

			this.map = new YMaps.Map(element);

			$.each(this.options, function (key, value) {
				switch (key) {
					case 'type':
						self.map.setMapType(value);
						break;
					case 'center':
						self.setCenter(new YMaps.GeoPoint(value.Lng, value.Lat));
						break;
					case 'zoom':
						self.setZoom(value);
						break;
				}
			});
		},
		addMarker:function (content, position, position_1, iconType) {
			var point = position instanceof YMaps.GeoPoint ? position : new YMaps.GeoPoint(position_1, position);
			var s = new YMaps.Style();
			s.iconStyle = new YMaps.IconStyle();
			s.iconStyle.shadow = new YMaps.IconShadowStyle();

			switch (iconType) {
				default:
				case 'small':
					s.iconStyle.href = siteRoot + "/i/icons/pin_blue_small.png";
					s.iconStyle.size = new YMaps.Point(22, 34);
					s.iconStyle.shadow.href = siteRoot + "/i/icons/pin_shadow_small.png";
					s.iconStyle.shadow.size = new YMaps.Point(32, 34);
					s.iconStyle.offset = new YMaps.Point(-11, -34);
					s.iconStyle.shadow.offset = new YMaps.Point(-10, -34);
					break;
				case 'big':
					s.iconStyle.href = siteRoot + "/i/icons/pin_orange_big.png";
					s.iconStyle.size = new YMaps.Point(32, 49);
					s.iconStyle.shadow.href = siteRoot + "/i/icons/pin_shadow_big.png";
					s.iconStyle.shadow.size = new YMaps.Point(40, 49);
					s.iconStyle.offset = new YMaps.Point(-16, -49);
					s.iconStyle.shadow.offset = new YMaps.Point(-16, -49);
					break;
				case 'mothercare':
					s.iconStyle.image = siteRoot + "/i/icons/pin_blue_big.png";
					s.iconStyle.size = new YMaps.Point(32, 49);
					s.iconStyle.shadow.href = siteRoot + "/i/icons/pin_shadow_big.png";
					s.iconStyle.shadow.size = new YMaps.Point(40, 49);
					s.iconStyle.offset = new YMaps.Point(0, -49);
					s.iconStyle.shadow.offset = new YMaps.Point(0, -49);
					break;
				case 'begemot':
					s.iconStyle.href = siteRoot + "/i/partners/begemot/map_pin.png";
					s.iconStyle.size = new YMaps.Point(26, 35);
					s.iconStyle.shadow.href = siteRoot + "/i/partners/begemot/map_pin_shadow.png";
					s.iconStyle.shadow.size = new YMaps.Point(33, 35);
					s.iconStyle.offset = new YMaps.Point(0, -35);
					s.iconStyle.shadow.offset = new YMaps.Point(0, -35);
					break;
			}


			// iconWindowAnchor = new YMaps.Point(-9, -2);

			var options = {style:s};

			if (typeof(arguments[4]) === 'object') {
				jQuery.extend(options, arguments[4]);
			}

			var marker = new YMaps.Placemark(point, options);

			if (content) {
				marker.setBalloonContent(content);
				marker.setBalloonOptions({maxWidth:300});
			}

			this.map.addOverlay(marker);

			return marker;
		},
		setCenter:function () {
			var point, zoom;

			if (arguments[0] instanceof YMaps.GeoPoint || arguments[0] instanceof YMaps.GeoPoint) {
				point = arguments[0];
				if (arguments[1]) zoom = arguments[1];
			} else {
				point = new YMaps.GeoPoint(arguments[1], arguments[0]);
				if (arguments[2]) zoom = arguments[2];
			}

			this.map.setCenter(point, zoom);
		},
		setZoom:function (zoom) {
			this.map.setZoom(zoom);
		},
		addSmallControl:function () {
			this.map.addControl(new YMaps.SmallZoom());
		},
		addBigControl:function () {
			this.map.addControl(new YMaps.TypeControl());
			this.map.addControl(new YMaps.ToolBar());
			this.map.addControl(new YMaps.Zoom());
			this.map.addControl(new YMaps.MiniMap());
			this.map.addControl(new YMaps.ScaleLine());
		},
		addEvent:function (eventname, callback) {
			var self = this;
			YMaps.Events.observe(this.map, this.map.Events[eventname], function () {
				callback(self);
			});
		},
		findCity:function (address, customCallback) {
			var self = this;
			var geocoder = new YMaps.Geocoder(address, {results:1, boundedBy:self.map.getBounds()});
			YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {
				if (this.length()) {
					self.map.addOverlay(this.get(0));
					self.map.panTo(this.get(0).getGeoPoint());
					if (customCallback)  customCallback();

				} else {
					alert("Адрес не найден");
				}
			});
		},
		clearOverlays:function () {
			this.map.removeAllOverlays();
		},
		getBounds:function () {
			var bounds = this.map.getBounds();
			var southWest = bounds.getLeftBottom();
			var northEast = bounds.getRightTop();

			return [
				[northEast.getX(), southWest.getX()],
				[northEast.getY(), southWest.getY()]
			];
		},
		stopUI:function () {
			this.map.disableDragging();
			this.map.disableDblClickZoom();
		},
		startUI:function () {
			this.map.enableDragging();
			this.map.enableDblClickZoom();
		}

	});
})(jQuery);
