
      // == Some global variables ==
      var YSLIDERLENGTH = 147       // maximum length that the knob can move (slide height minus knob height)
      var MAXZOOM = 21

      // == Create a Custom GControl ==
      function YSliderControl() { }
      YSliderControl.prototype = new GControl();

      // == This function positions the slider to match the specified zoom level ==
      YSliderControl.prototype.setSlider = function(zoom)
	  {
        var top = Math.round((YSLIDERLENGTH/MAXZOOM*zoom));
        this.slide.top = top;
        this.knob.style.top = top +"px";
        //GLog.write("Map was zoomed to:"+zoom+" new Knob position:"+top);
      }

      // == This function reads the slider and sets the zoom level ==
      YSliderControl.prototype.setZoom = function() {
        var z=Math.round(this.slide.top*MAXZOOM/YSLIDERLENGTH);
        this.map.setZoom(z);
        //GLog.write("New knob position:"+this.slide.top+" new zoom: "+z);
      }


      // == This gets called bu the API when addControl(new YSlider()) is used ==
      YSliderControl.prototype.initialize = function(map) {
        // obtain Function Closure on a reference to "this"
        var that=this;
        // store a reference to the map so that we can call setZoom() on it
        this.map = map;

        // Is this MSIE, if so we need to use AlphaImageLoader
        var agent = navigator.userAgent.toLowerCase();
        if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){this.ie = true} else {this.ie = false}

// <img src="/i/map/slider_plus.png" id="slider_plus" width=17 height=16 >
// <img src="/i/map/slider_minus.png" id="slider_minus" width=17 height=16 >

        // create the background graphic as a <div> containing an image
        var container = document.createElement("div");
        container.style.width="17px";
        container.style.height="147px";

        // Handle transparent PNG files in MSIE
        if (this.ie) {
          var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://static.yapiligrim.ru/i/map/slider_layer.png', sizingMethod='scale');";
          container.innerHTML = '<div style="height:147px; width:17px; ' +loader+ '" ></div>';
        } else {
          container.innerHTML = '<img src="http://static.yapiligrim.ru/i/map/slider_layer.png"  width=17 height=147 >';
        }

        // create the knob as a GDraggableObject
        // Handle transparent PNG files in MSIE
        if (this.ie) {
          var loader = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://static.yapiligrim.ru/i/map/slider.png', sizingMethod='scale');";
          this.knob = document.createElement("div");
          this.knob.style.height="9px";
          this.knob.style.width="17px";
          this.knob.style.filter=loader;
        } else {


          this.knob = document.createElement("img");
          this.knob.src = "http://static.yapiligrim.ru/i/map/slider.png";
          this.knob.height = "9";
          this.knob.width = "17";

        }
        container.appendChild(this.knob);
        this.slide=new GDraggableObject(this.knob, {container:container});


		var zoom_load = map.getZoom();
        var top = Math.round((YSLIDERLENGTH/MAXZOOM*zoom_load));

        this.slide.top = top;
        this.knob.style.top = top +"px";

        // attach the control to the map
        map.getContainer().appendChild(container);

        // Listen for other things changing the zoom level and move the slider
        GEvent.addListener(map, "zoomend", function(a,b) {that.setSlider(b)});



        // Listen for the slider being moved and set the zoom level
        GEvent.addListener(this.slide, "dragend", function() {that.setZoom()});

		//YSliderControl.prototype.setSlider(map.getZoom());

        return container;
      }

      // == Set the default position for the control ==
      YSliderControl.prototype.getDefaultPosition = function() {
        return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 24));
      }


function switchZoomIn()
{
	map.zoomIn();
}

function switchZoomOut()
{
	map.zoomOut();
}


function switchMapType(type)
{
	$$('.ctrl').each(
		function(s)
		{
			$(s).removeClassName('ctrl_active');
		}
	);
	$('ctrl_' + type).addClassName('ctrl_active');

	switch (type)
	{
		case 'simple': map.setMapType(G_NORMAL_MAP);
		break;
		case 'satellite': map.setMapType(G_SATELLITE_MAP);
		break;
		case 'physical': map.setMapType(G_PHYSICAL_MAP);
		break;
		case 'earth': map.setMapType(G_SATELLITE_3D_MAP);
		break;
	}
}

if (GBrowserIsCompatible())
{
	var iconTour = new GIcon();
	iconTour.image = 'http://static.yapiligrim.ru/i/map/tour.png';
	iconTour.shadow = 'http://static.yapiligrim.ru/i/map/tour_shadow.png';
	iconTour.iconSize = new GSize(37, 29);
	iconTour.shadowSize = new GSize(37, 29);
	iconTour.iconAnchor = new GPoint(5, 5);
	iconTour.infoWindowAnchor = new GPoint(15, 0);

	var iconAction = new GIcon();
	iconAction.image = 'http://static.yapiligrim.ru/i/map/action.png';
	iconAction.shadow = 'http://static.yapiligrim.ru/i/map/action_shadow.png';
	iconAction.iconSize = new GSize(25, 28);
	iconAction.shadowSize = new GSize(35, 28);
	iconAction.iconAnchor = new GPoint(5, 5);
	iconAction.infoWindowAnchor = new GPoint(15, 0);

	var iconPlace = new GIcon();
	iconPlace.image = 'http://static.yapiligrim.ru/i/map/place.png';
	iconPlace.shadow = 'http://static.yapiligrim.ru/i/map/place_shadow.png';
	iconPlace.iconSize = new GSize(31, 39);
	iconPlace.shadowSize = new GSize(31, 39);
	iconPlace.iconAnchor = new GPoint(5, 5);
	iconPlace.infoWindowAnchor = new GPoint(15, 0);


	var iconBus = new GIcon();
	iconBus.image = 'http://static.yapiligrim.ru/i/map/bus.png';
	iconBus.shadow = 'http://static.yapiligrim.ru/i/map/bus_shadow.png';
	iconBus.iconSize = new GSize(34, 30);
	iconBus.shadowSize = new GSize(34, 30);
	iconBus.iconAnchor = new GPoint(5, 5);
	iconBus.infoWindowAnchor = new GPoint(15, 0);

	var iconPlane = new GIcon();
	iconPlane.image = 'http://static.yapiligrim.ru/i/map/plane.png';
	iconPlane.shadow = 'http://static.yapiligrim.ru/i/map/plane_shadow.png';
	iconPlane.iconSize = new GSize(38, 26);
	iconPlane.shadowSize = new GSize(38, 26);
	iconPlane.iconAnchor = new GPoint(5, 5);
	iconPlane.infoWindowAnchor = new GPoint(15, 0);

	var iconHotel = new GIcon();
	iconHotel.image = 'http://static.yapiligrim.ru/i/map/hotel.png';
	iconHotel.shadow = 'http://static.yapiligrim.ru/i/map/hotel_shadow.png';
	iconHotel.iconSize = new GSize(39, 25);
	iconHotel.shadowSize = new GSize(39, 25);
	iconHotel.iconAnchor = new GPoint(5, 5);
	iconHotel.infoWindowAnchor = new GPoint(15, 0);

	var iconTrain = new GIcon();
	iconTrain.image = 'http://static.yapiligrim.ru/i/map/train.png';
	iconTrain.shadow = 'http://static.yapiligrim.ru/i/map/train_shadow.png';
	iconTrain.iconSize = new GSize(28, 28);
	iconTrain.shadowSize = new GSize(28, 28);
	iconTrain.iconAnchor = new GPoint(5, 5);
	iconTrain.infoWindowAnchor = new GPoint(15, 0);

	var iconFood = new GIcon();
	iconFood.image = 'http://static.yapiligrim.ru/i/map/food.png';
	iconFood.shadow = 'http://static.yapiligrim.ru/i/map/food_shadow.png';
	iconFood.iconSize = new GSize(37, 31);
	iconFood.shadowSize = new GSize(37, 31);
	iconFood.iconAnchor = new GPoint(5, 5);
	iconFood.infoWindowAnchor = new GPoint(15, 0);

	var iconMuseum = new GIcon();
	iconMuseum.image = 'http://static.yapiligrim.ru/i/map/museum.png';
	iconMuseum.shadow = 'http://static.yapiligrim.ru/i/map/museum_shadow.png';
	iconMuseum.iconSize = new GSize(34, 31);
	iconMuseum.shadowSize = new GSize(34, 31);
	iconMuseum.iconAnchor = new GPoint(5, 5);
	iconMuseum.infoWindowAnchor = new GPoint(15, 0);

	var customIcons = [];
	customIcons["tour"] = iconTour;
	customIcons["action"] = iconAction;
	customIcons["place"] = iconPlace;
	customIcons["bus"] = iconBus;
	customIcons["plane"] = iconPlane;
	customIcons["hotel"] = iconHotel;
	customIcons["train"] = iconTrain;
	customIcons["food"] = iconFood;
	customIcons["museum"] = iconMuseum;

	var overlays = [];
}

// edit funcs
var lastmarker;
function removeMarker()
{
	lastmarker.remove();
}

function deleteMarker(m_id, id)
{
	if (confirm('Удалить метку?'))
	{
 		new Ajax.Request('/siteAjax/yap/deleteMarker/?m_id=' + m_id);
 		top.location.href = '?removeMarker';
	}
}

function buildMap()
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("community_map"),{draggableCursor:"default"});

		/* Gsearch */
		var lsc = new google.maps.LocalSearch();
		map.addControl(new google.maps.LocalSearch());
		map.setCenter(new GLatLng(37.4419, -122.1419), 1);
		map.addControl(new YSliderControl());

		GEvent.addListener(map,"click",function(overlay,point)
		{
	        if (!overlay) createInputMarker(point);
		});
	}
}

var iwform = '<p>Описание</p>'
	+ '<form action="" onsubmit="process(this); return false">'
	+ '  <textarea name="data" style="width: 99%; height: 100px;" id="marker_desc"></textarea>'
	+ ' <p><input type="submit" value="Добавить" class="btn" /> &nbsp;&nbsp;&nbsp;<input type="button"  onclick="removeMarker();" value="Удалить" class="btn" /></p>'
	+ '</form>';


function createMarker(point,text)
{
	var marker = new GMarker(point,{draggable:true, icon:customIcons[sel_mm]});
	GEvent.addListener(marker,"click", function()
	{
		marker.openInfoWindow(document.createTextNode(text));
	});
	map.addOverlay(marker);
	return marker;
}



function process(form)
{
	var details = form.data.value;
	var lat = lastmarker.getPoint().lat();
	var lng = lastmarker.getPoint().lng();
	form.data.value = '';
	new Ajax.Request('/siteAjax/yap/saveMarker/?sel_mm=' + sel_mm + '&item_id=' + item_id + '&user_id=' + USER_ID + '&mode=' + mode + '&data=' + encodeURI(details) + '&lat=' + lat + '&lng=' + lng);
	map.closeInfoWindow();
	GEvent.addListener(lastmarker, "click", function()
	{
		lastmarker.openInfoWindowHtml(details);
	});
}


function createInputMarker(point)
{
	var marker = new GMarker(point,{draggable:true, icon:customIcons[sel_mm]});
	GEvent.addListener(marker, "click", function()
	{
		lastmarker = marker;
		marker.openInfoWindowHtml(iwform);
	});

	map.addOverlay(marker);
	marker.openInfoWindowHtml(iwform);
	lastmarker=marker;
	return marker;
}


function createMarkerEdit(point,text, m_id, deleted, mm_type)
{
	var marker = new GMarker(point,{draggable:true, icon:customIcons[mm_type]});

	GEvent.addListener(marker,"click", function()
	{
		marker.openInfoWindowHtml(text);
	});

	GEvent.addListener(marker,"dragend", function()
	{
		var lat = marker.getPoint().lat();
		var lng = marker.getPoint().lng();
		new Ajax.Request('/siteAjax/yap/saveMarkerCoord/?m_id=' + m_id + '&lat=' + lat + '&lng=' + lng);
    });
	overlays[m_id] = marker;
	map.addOverlay(marker);
	return marker;
}


function getMarkersEdit(coord_str)
{
	GDownloadUrl("/gmaps/getMarkersCoordItem/?coord=" + coord_str + '&item_id=' + item_id + '&item_type=' + mode , function(data)
	{
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");
		allmarkers.length = 0;

		for (var i = 0; i < markers.length; i++)
		{
			var m_id = markers[i].getAttribute("m_id");
		  	if (typeof(overlays[m_id]) == 'undefined')
			{
				var content = markers[i].getAttribute("content");
				content = content.replace(/&lt;/gi, '<');
				content = content.replace(/&gt;/gi, '>');
				content = content.replace(/%22/gi, '"');
				content = content.replace(/&quot;/gi, '"');


				var user_tid = markers[i].getAttribute("user_id");
				var deleted = false;

				if (USER_ID == user_tid)
				{
					//	deleted = true;
					content = content + '<div style="margin-top: 10px; font-size: 11px;"><a href="javascript:void(null);" onclick="deleteMarker(' + m_id + ',' + i + ')" class="red">удалить</a></div>';
				}

				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
				parseFloat(markers[i].getAttribute("lng")));
				var mm_type = markers[i].getAttribute("m_type");

				var marker = createMarkerEdit(point, content, m_id, deleted, mm_type);

				//map.addOverlay(marker);
				allmarkers.push(marker);
			}
		}
	});
}
