	var isIE = (navigator.appName == "Microsoft Internet Explorer");
	var isFF = (!isIE && !(navigator.appVersion.indexOf("MSIE")>-1));
	var isOPR = (navigator.userAgent.indexOf("Opera")!=-1);
	var isIE7 = navigator.userAgent.indexOf("MSIE 7.0") >= 0;
	if (isOPR || isIE7) { isIE = false; }
	var xPos = 0;
	var yPos = 0;


	var cacheStore = new Array;


	window.onload = function ()	{ init(); }

	// Инит
	function init()
		{
		if ($('YMaps_main')) initMainMap();
		if ($('YMaps_current')) initCurrentMap();
		if ($('YMaps_total')) initTotalMap();
		if ($('YMaps_add')) initAddMap();
		}


	function initAddMap()
		{
		map = new YMaps.Map($('YMaps_add'));
		map.setCenter(new YMaps.GeoPoint(36.751842,55.386802), 13, YMaps.MapType.MAP);

		map.addControl(new YMaps.SmallZoom());
		map.addControl(new YMaps.TypeControl());
		map.addControl(new YMaps.ToolBar());

		clickPlaceListener = YMaps.Events.observe(map, map.Events.Click, function (mEvent)
			{
			placeMark = new YMaps.Placemark(mEvent.getGeoPoint(), {draggable: true});
			placeMark.setIconContent("Объект");
			placeMark.description = "Новый объект";
			map.addOverlay(placeMark);
			clickPlaceListener.cleanup();

			$('add_span').innerHTML = 'Переместите объект в нужное место';


			$('x').value = mEvent.getGeoPoint().getLng();
			$('y').value = mEvent.getGeoPoint().getLat();

			dragListener = YMaps.Events.observe(placeMark, placeMark.Events.DragEnd, function (mEvent)
				{
				$('x').value = mEvent.getGeoPoint().getLng();
				$('y').value = mEvent.getGeoPoint().getLat();
				});
			});
		}

	function initMainMap()
		{
		map = new YMaps.Map($('YMaps_main'));
		map.setCenter(new YMaps.GeoPoint(36.751842,55.386802), 13, YMaps.MapType.MAP);

		map.addControl(new YMaps.SmallZoom());
		map.addControl(new YMaps.TypeControl());
		map.addControl(new YMaps.ToolBar());

		var mapTemplate = new YMaps.Template();
		mapTemplate.text = '<div class="map_bubble"><h3><a href="/catalog/$[type]/$[id]">$[header]</a> ($[price])</h3><small>$[city]</small><p>$[image]$[description]</p></div>';
		YMaps.Templates.add("example#template", mapTemplate);

		mapStyle = new YMaps.Style();
		mapStyle.balloonContentStyle = new YMaps.BalloonContentStyle("example#template");

		gCollection = new YMaps.GeoObjectCollection();
		for (i=0; i<mapData.length; i++)
			{
			placeMark = composeBubble(mapData[i],mapStyle);
			gCollection.add(placeMark);
			}
		map.addOverlay(gCollection);
		}

	function initTotalMap()
		{
		map = new YMaps.Map($('YMaps_total'));
		map.setCenter(new YMaps.GeoPoint(36.751842,55.386802), 13, YMaps.MapType.MAP);

		map.addControl(new YMaps.SmallZoom());
		map.addControl(new YMaps.TypeControl());
		map.addControl(new YMaps.ToolBar());

		var mapTemplate = new YMaps.Template();
		mapTemplate.text = '<div class="map_bubble"><h3><a href="/catalog/$[type]/$[id]">$[header]</a> ($[price])</h3><small>$[city]</small><p>$[image]$[description]</p></div>';
		YMaps.Templates.add("example#template", mapTemplate);

		mapStyle = new YMaps.Style();
		mapStyle.balloonContentStyle = new YMaps.BalloonContentStyle("example#template");

		gCollection = new YMaps.GeoObjectCollection();
		for (i=0; i<mapData.length; i++)
			{
			placeMark = composeBubble(mapData[i],mapStyle);
			gCollection.add(placeMark);
			}
		map.addOverlay(gCollection);
		}

	function initCurrentMap()
		{
		map = new YMaps.Map($('YMaps_current'));
		map.setCenter(new YMaps.GeoPoint(mapData[0].x,mapData[0].y), 13, YMaps.MapType.MAP);

		map.addControl(new YMaps.SmallZoom());
//		map.addControl(new YMaps.TypeControl());
		map.addControl(new YMaps.ToolBar());

		var mapTemplate = new YMaps.Template();
		mapTemplate.text = '<div class="map_bubble"><h3><a href="/catalog/$[type]/$[id]">$[header]</a> ($[price])</h3><small>$[city]</small><p>$[image]$[description]</p></div>';
		YMaps.Templates.add("example#template", mapTemplate);

		mapStyle = new YMaps.Style();
		mapStyle.balloonContentStyle = new YMaps.BalloonContentStyle("example#template");

		gCollection = new YMaps.GeoObjectCollection();
		for (i=0; i<mapData.length; i++)
			{
			placeMark = composeBubble(mapData[i],mapStyle);
			gCollection.add(placeMark);
			}
		map.addOverlay(gCollection);
		}

	// Сооружаем пузырь
	function composeBubble(dataSource)
		{
		placeMark = new YMaps.Placemark(new YMaps.GeoPoint(dataSource.x,dataSource.y), {draggable: false, style: mapStyle});

		placeMark.setIconContent(dataSource.title);
		placeMark.id = dataSource.id;
		placeMark.description = dataSource.description;
		placeMark.header = dataSource.title;
		placeMark.price = dataSource.price;
		placeMark.type = dataSource.type;
		placeMark.city = dataSource.city;
		if (dataSource.file_id)
			placeMark.image = '<a href="/catalog/'+dataSource.type+'/'+dataSource.id+'"><img src="/attach/'+dataSource.file_id+'/w80h50" /></a><br />';
		else
			placeMark.image = '';

		return placeMark;
		}

	// Поиск по карте на главной
	function processMapFind()
		{
		$('find_results').innerHTML = '<img src="/images/find-loader.gif" />';
		$('find_results').style.display = 'block';

		lookup({
				url: '/api/map.filter.api.php',
				values:'type_id='+$('f_type_id').value+'&city_id='+$('f_city_id').value+'&price_from='+$('f_price_from').value+'&price_to='+$('f_price_to').value,
				postFunction:function(transport)
					{
					eval(transport.responseText);
					gCollection.removeAll();
					if (!filterObjects.length)
						{
						$('find_results').innerHTML = 'По вашему запросу объектов на карте не найдено';
						}
					else
						{
						$('find_results').innerHTML = 'Найдено объектов: '+filterObjects.length;
						for (i=0; i<filterObjects.length; i++)
							{
							placeMark = composeBubble(filterObjects[i]);
							gCollection.add(placeMark);
							}
						map.panTo(new YMaps.GeoPoint(filterObjects[0].x,filterObjects[0].y));
						}
					}
				});
		return false;
		}

	function resetFind()
		{
		$('find_form').reset();
		$('find_results').style.display = 'none';
		gCollection.removeAll();
		for (i=0; i<mapData.length; i++)
			{
			placeMark = composeBubble(mapData[i],mapStyle);
			gCollection.add(placeMark);
			}
		map.panTo(new YMaps.GeoPoint(36.751842,55.386802));
		return false;
		}


	function cutsymbols(e)
		{
		e=e||event;
		var key=e.charCode||e.keyCode;
		if (e.shiftKey) return false;
		if ((key>47&&key<58)||key==8||key==46) return true;
		else return false;
		}

	// Сахер
	function $(id)
		{
		return document.getElementById(id);
		}

	function hasClassName(element, className)
		{
		var elementClassName = element.className;
		return (elementClassName.length > 0 && (elementClassName == className ||
		new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)));
		}

	function addClassName(element, className)
		{
		if (!hasClassName(element,className))
			element.className += (element.className ? ' ' : '') + className;
		}

	function removeClassName(element, className)
		{
		element.className = element.className.replace(new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ');
		}

	//аякс
	function getHTTP()
		{
		var xmlhttp;
		/*@cc_on
		@if (@_jscript_version >= 5)
			try
				{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
			catch (e)
				{
				try
					{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
				catch (E)
					{
					xmlhttp = false;
					}
				}
		@else
			xmlhttp = false;
		@end @*/
		if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
			{
			try
				{
				xmlhttp = new XMLHttpRequest();
				}
			catch (e)
				{
				xmlhttp = false;
				}
			}
		return xmlhttp;
		}

	function lookup(lookupSettings)
		{
		if (!lookupSettings) lookupSettings = new Object();

		if (!lookupSettings.values)   lookupSettings.values   = '';
		if (!lookupSettings.dest)  lookupSettings.dest = false;
		if (!lookupSettings.cache)  lookupSettings.cache = false;
		if (!lookupSettings.postFunction)   lookupSettings.postFunction   = '';


		if (lookupSettings.url)
			{
			if (lookupSettings.cache && cacheStore[lookupSettings.url+'?'+lookupSettings.values])
				{
				try { document.getElementById(lookupSettings.dest).innerHTML = cacheStore[lookupSettings.url+'?'+lookupSettings.values].responseText }
				catch (e) { }
				if (lookupSettings.postFunction) lookupSettings.postFunction(cacheStore[lookupSettings.url+'?'+lookupSettings.values]);
				}
			else
				{
				this.working = false;
				if (!this.working && (this.http = getHTTP()))
					{
					var http = this.http;
					this.http.open("POST", lookupSettings.url, true);
					this.http.onreadystatechange = function()
						{
						if (http.readyState == 4)
							{
							if (lookupSettings.cache)
								{
								cacheStore[lookupSettings.url+'?'+lookupSettings.values] = http;
								}
							try { destObj = document.getElementById(lookupSettings.dest).innerHTML = http.responseText }
							catch (e) { }
							if (lookupSettings.postFunction) lookupSettings.postFunction(http);
							this.working = false;
							this.http = false;
							}
						}
					this.working = true;
					this.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
					this.http.send(lookupSettings.values);
					}
				}
			}
		}

	engineLoaded = true;

