var map;
$(document).ready(function(){
	init_search_on_map();
	$('div.YMaps-buttons:eq(1) a.YMaps-button:eq(2)').remove();
});

function init_search_on_map()
{
	map = new YMaps.Map(document.getElementById("YMapsID_search"));
	map.addControl(new YMaps.TypeControl());
	//map.addControl(new YMaps.ToolBar);
	
	// Rectangle Selection //
	
	var btn_search = new YMaps.ToolBarRadioButton('default', {
		icon: presets.siteURL() + "img/ico-map-select-" + presets.locale() + ".png",
		hint: trans_form.hint_btn_search
	});
	var toolbar = new YMaps.ToolBar();
	map.addControl(toolbar);
	toolbar.add(btn_search);
	map.addControl(new YMaps.Zoom());
	map.addControl(new YMaps.MiniMap());
	
	var s_points = {},
		map_mousedown = false,
		map_mousemove = false,
		map_mouseup = false;
		
	var observer_btn_search = YMaps.Events.observe(btn_search, btn_search.Events.Select, function(){
		$("#YMapsID_search").css({"cursor":"crosshair"});
		map_mousedown = YMaps.Events.observe(map, map.Events.MouseDown, function(ev){
			for (var i = 1; i < 5; i++) {
				s_points[i] = {
					x: ev._posInPixels.x,
					y: ev._posInPixels.y,
					lat: ev._point.__lat,
					lng: ev._point.__lng
				};
			}
			$('#YMapsID_search').append('<div class="YMaps-select-frame"></div>');
			
			map.disableDragging();
			map.listenMouseEvent(map.Events.MouseMove, true);
			map.listenMouseEvent(map.Events.MouseUp, true);
			
			map_mousemove = YMaps.Events.observe(map, map.Events.MouseMove, function(ev){
				s_points[2].x = ev._posInPixels.x;
				s_points[3] = {
					x: ev._posInPixels.x,
					y: ev._posInPixels.y,
					lat: ev._point.__lat,
					lng: ev._point.__lng
				};
				s_points[4].y = ev._posInPixels.y;
				$('.YMaps-select-frame').css({
					'top': (s_points[1].y - s_points[3].y > 0) ? s_points[3].y : s_points[1].y,
					'left': (s_points[1].x - s_points[3].x > 0) ? s_points[3].x : s_points[1].x,
					'width': Math.abs(s_points[1].x - s_points[3].x),
					'height': Math.abs(s_points[1].y - s_points[3].y)
				}); // end of css
			}); // end of mousemove
			
			map_mouseup = YMaps.Events.observe(map, map.Events.MouseUp, function(mEvent){
				//map.listenMouseEvent(map.Events.MouseUp, false);
				//map.listenMouseEvent(map.Events.MouseMove, false);
				//map.enableDragging();
				map_mousemove.disable();
				$('#s_x1').val(s_points[1].lng);
				$('#s_x2').val(s_points[3].lng);
				$('#s_y1').val(s_points[1].lat);
				$('#s_y2').val(s_points[3].lat);
				$('#go_s').val(1);
				$('#go_search').submit();
			}); // end of mouseup
		}); // end of mousedown
	}, toolbar); // end of search button select observer
	
	YMaps.Events.observe(btn_search, btn_search.Events.Deselect, function(){
		$("#YMapsID_search").css({"cursor":""});
		if (map_mousedown) map_mousedown.cleanup();
		if (map_mousemove) map_mousemove.cleanup();
		if (map_mouseup) map_mouseup.cleanup();
		if ($('.YMaps-select-frame').length != 0) $('.YMaps-select-frame').remove();
		s_points = {};
	}, toolbar); // end of search button deselect observer
	
	map.addControl(new YMaps.ScaleLine());
	map.setCenter(new YMaps.GeoPoint(presets.init_lng(), presets.init_lat()), presets.init_zoom());
//	map.setCenter(new YMaps.GeoPoint(30.981762, 48.618455), 5);
}

function set_user_defined_map_center(lng, lat)
{
	map.setCenter(new YMaps.GeoPoint(lng, lat), presets.init_zoom());
}