function init() {
	var map = new YMaps.Map(YMaps.jQuery(".ymap_current")[0]);
	var eltAddress = 
		$(".ymap_current").parents(".listitem:first").find(".full_address").text();
	var customIco = '/i/public/dot-big.png';
	
	map.addControl(new YMaps.TypeControl());
	map.addControl(new YMaps.ToolBar());
	map.addControl(new YMaps.Zoom());
	map.addControl(new YMaps.MiniMap());
	map.addControl(new YMaps.ScaleLine());
	map.setCenter(new YMaps.GeoPoint(48.383094, 54.318544), 15);
	
	var elt_li = $(".ymap_current").parents(".listitem:first");
	var description_text = "";
	if ($(elt_li).find(".group-title").length > 0) {
		description_text = description_text
				+ '<p style="font-size:12px">'
				+ $(elt_li).find(".group-title").text() + '</p>';
	}
	description_text = description_text + '<h3>' + $(elt_li).find("h2").text()
			+ '</h3>';
	description_text = description_text + '<p class="address">'
			+ $(elt_li).find(".address span").html() + '</p>';
	if ($(elt_li).find(".worktime").length > 0) {
		description_text = description_text + '<p class="worktime">'
				+ $(elt_li).find(".worktime").html() + '</p>';
	}
	
	var s = new YMaps.Style();
	s.iconStyle = new YMaps.IconStyle();
	s.iconStyle.href = customIco;
	s.iconStyle.size = new YMaps.Point(26, 26);
	s.iconStyle.offset = new YMaps.Point(0, -26);
	var geocoder = new YMaps.Geocoder(eltAddress);
	YMaps.Events.observe(geocoder, geocoder.Events.Load, function() {
		if (this.length()) {
			var gp = geocoder.get(0).getGeoPoint();
			var placemark = new YMaps.Placemark(new YMaps.GeoPoint(gp.getLng(),
					gp.getLat()), {
				style : s
			});
			placemark.description = description_text;
			map.addOverlay(placemark);
			map.panTo(this.get(0).getGeoPoint())
		}
	});
	YMaps.Events.observe(
			geocoder,
			geocoder.Events.Fault,
			function(error) {
				$("#YMapsError").text("Извините, произошла досадная ошибка, карта временно недоступна");
			});
}

function initListMap() {
	$(".servicenet-listview").parents("div:first").append(
			'<div id="servicenet-map"></div>');
	
	var map = new YMaps.Map(YMaps.jQuery("#servicenet-map")[0]);
	var customIco = '/i/public/dot-big.png';
	
	map.addControl(new YMaps.TypeControl());
	map.addControl(new YMaps.ToolBar());
	map.addControl(new YMaps.Zoom());
	map.addControl(new YMaps.MiniMap());
	map.addControl(new YMaps.ScaleLine());
	map.setCenter(new YMaps.GeoPoint(48.383094, 54.318544), 11);
	
	var last_geopoint = "";
	
	$(".servicenet-listview .listitem:first").addClass("first");
	
	$(".servicenet-listview .listitem").each(function() {
		var elt_li = $(this);
		var description_text = '<div class="on-map-description">';
		if ($(elt_li).find(".group-title").length > 0) {
			description_text = description_text
					+ '<p style="font-size:12px; color: #888">'
					+ $(elt_li).find(".group-title").text() + '</p>';
		}
		description_text = description_text + '<h3>'
				+ $(elt_li).find("h2").html() + '</h3>';
		description_text = description_text + '<p class="address">'
				+ $(elt_li).find(".address span").html() + '</p>';
		if ($(elt_li).find(".phone").length > 0)
			description_text = description_text + '<p class="phone">'
				+ $(elt_li).find(".phone").html() + '</p>';
		
		if ($(elt_li).find(".worktime").length > 0)
			description_text = description_text
					+ '<p class="worktime">'
					+ $(elt_li).find(".worktime").html() + '</p>';
		
		description_text += '</div>';
		
		var elt_address = $(elt_li).find(".address span").text();
		
		var s = new YMaps.Style();
		s.iconStyle = new YMaps.IconStyle();
		s.iconStyle.href = customIco;
		s.iconStyle.size = new YMaps.Point(26, 26);
		s.iconStyle.offset = new YMaps.Point(-20, -20);
		
		var geocoder = new YMaps.Geocoder(elt_address);
		
		YMaps.Events.observe(geocoder, geocoder.Events.Load,
				function() {
					if (this.length()) {
						var gp = geocoder.get(0).getGeoPoint();
						var placemark = new YMaps.Placemark(
								new YMaps.GeoPoint(gp.getLng(), gp
										.getLat()), {
									style : s
								});
						placemark.description = description_text;
						map.addOverlay(placemark);
						last_geopoint = this.get(0).getGeoPoint()
								.getX();
						if ($(elt_li).hasClass("first")) {
							map.panTo(this.get(0).getGeoPoint());
						}
					}
				});
	});
	YMaps.Events.observe(
			geocoder,
			geocoder.Events.Fault,
			function(error) {
				$("#YMapsError").text("Извините, произошла досадная ошибка, карта временно недоступна");
			});
}

$(document).ready(function() {
	$(".ymap_toggle").css("text-decoration", "none");
	$(".ymap_toggle").css("border-bottom", "1px dotted #777");
	$(".ymap_toggle").click(function(event) {
		event.preventDefault();
		$(".ymap").removeClass("ymap_current");
		var elt = $(this).parent().parent().find(".ymap:first");
		
		if ($(elt).hasClass("is-visible")) {
			$(elt).slideUp("fast", function() {
				$(elt).removeClass("is-visible");
			});
		} else {
			$(elt).addClass("ymap_current");
			if (($(elt).parents(".listitem:first").hasClass("is-map-loaded"))) {
				
			} else {
				YMaps.load(init);
				$(elt).parents(".listitem:first").addClass("is-map-loaded");
			}
			$(elt).show();
			$(elt).addClass("is-visible");
		}
	});
	
	$(".service-net-view-switch a").click(function(event) {
		event.preventDefault();
		$(this).parents("ul:first").find(".selected").removeClass("selected");
		$(this).parents("li:first").addClass("selected");
		
		if ($(this).attr("href") == "#map") {
			$(".servicenet-listview").hide();
			if (($("#servicenet-map").length == 0)) {
				YMaps.load(initListMap);
			}
			$("#servicenet-map").show();
		} else {
			$(".servicenet-listview").show();
			$("#servicenet-map").hide();
		}
	});
	
	$(".service-net-view-switch .selected a").click();
});