﻿$(function () {
	

	var maps = new Array()
	$(".ymap").each(function(i, map){
		maps.push(createMap(map))
	})

	$(".ymap-toggle").click(function(){
		$map = $(this).closest(".map").find(".ymap-wrapper")
		if ($map.is(":hidden")) $map.show()
		else $map.hide()
		$.each(maps, function(i, map){ map.redraw() })
		return false
	})
	
	
	function createMap(mapElement)
	{
		var map = new YMaps.Map(mapElement);
		map.setCenter(new YMaps.GeoPoint(30.381466,60.06577), 13, YMaps.MapType.MAP);
		map.addControl(new YMaps.Zoom());
		map.addControl(new YMaps.ToolBar());
		YMaps.MapType.PMAP.getName = function () { return "Народная"; };
		map.addControl(new YMaps.TypeControl([
			YMaps.MapType.MAP,
			YMaps.MapType.SATELLITE,
			YMaps.MapType.HYBRID,
			YMaps.MapType.PMAP
		], [0, 1, 2, 3]));

		YMaps.Styles.add("constructor#pmgnlPlacemark", {
			iconStyle : {
				href : "http://api-maps.yandex.ru/i/0.3/placemarks/pmgnl.png",
				size : new YMaps.Point(36,41),
				offset: new YMaps.Point(-13,-40)
			}
		});

	   	map.addOverlay(createObject("Placemark", new YMaps.GeoPoint(30.381466,60.06577), "constructor#pmgnlPlacemark", "Крещатик"));
	   	
	   	return map;
	}
	

	function createObject (type, point, style, description)
	{
		var allowObjects = ["Placemark", "Polyline", "Polygon"],
			index = YMaps.jQuery.inArray( type, allowObjects),
			constructor = allowObjects[(index == -1) ? 0 : index];
			description = description || "";

		var object = new YMaps[constructor](point, {style: style, hasBalloon : !!description});
		object.description = description;

		return object;
	}
})

