$(document).ready(function(){

	{	// Удаляем пустые блоки контактов
		$('.thumbText').each(function () {
	
			if (!$(this).html()) {
				$(this).parents('.thumbBlock').hide();
			}
			
		})
	}
	
	$('.defvalue').defaultValue();
	
	$('.topmenu li:last').css('border', 0);
	$('.topmenu > li').hover(function(){
		$(this).find('.submenu').show();
	},function(){
		$(this).find('.submenu').hide();
	})
	$('.jcarousel-skin-intro').jcarousel({scroll: 1});
	if($.browser.msie && $.browser.version == 6){
		$('.jcarousel-skin-intro .jcarousel-prev').hover(function(){
			$(this).css('background-position', '0 -25px');
		},function(){
			$(this).css('background-position', '0 0');
		})
		$('.jcarousel-skin-intro .jcarousel-next').hover(function(){
			$(this).css('background-position', '0 -75px');
		},function(){
			$(this).css('background-position', '0 -50px');
		})
	}
	
	var _playerVersion = $.flash.version,
		_majorVersion = _playerVersion.major; 
		
		
	{	// Вставка флеша на титульной странице
		if ($('#introFlash').length) {
			if (_majorVersion >= 9){
				$('#introFlash').flash({
					swf: _root + 'public/flash/indexslideshow.swf',
					width: 955,
					height: 297,
					params: {
						wmode: 'transparent',
						flashvars: {
							URLXML: _root + 'xml?rand=' + Math.random(),
							URLImages: _root
						}
					}
				});
			}
			else {
				$('.noflash-message').show();
			}
		}
	}
	
	
	{	// Вставка флеша на внутренней странице
		if ($('#secondFlash').length) {
			if (_majorVersion >= 9){
				$('#secondFlash').flash({
					swf: _root + 'public/flash/secondslideshow.swf',
					width: 731,
					height: 200,
					params: {
						wmode: 'transparent',
						flashvars: {
							URLXML: _root + 'xmlsecond?rand=' + Math.random(),
							URLImages: _root
							//URLXML: _root + 'public/flash/secondslideshow.xml',
							//URLImages: _root + 'public/flash/secondslideshow/'
						}
					}
				});
			}
			else {
				$('.noflash-message').show();
			}
		}
	}
/*	
	$(window).unload(function () {
		GUnload();
	});
*/

	$('#map_container .close').click(function () {
	
		$('#map_container').hide();

	});

});

	var icon_type = [
		'',
		_root + 'i/icons_map/offices.png',
		_root + 'i/icons_map/bankimats.png',
		_root + 'i/icons_map/infokiosk.png'
	];

	var buildMap = function (id, coord_x, coord_y, name) {
		
		this._center = [0, 0];
		this._resolution = [1, 19];
		this._layers = [];
		this._map = null;
		
		this.setCenter = function (lng, lat) {
			this._center = [lng, lat];
			this._map.setCenter(new GLatLng(lng, lat), this._resolution[0]);
		}

		this.setResolution = function (min, max) {
			this._resolution = [min, max];
		}

		this.addLayer = function (name, xmlFilePath, ico) {
			this._layers[name] = {file: xmlFilePath, icon: ico};

			this.redrawMap();
		}

		this.removeLayer = function (name) {
			delete(this._layers[name]);
			this.redrawMap();
		}

		this.redrawMap = function () {
			
			var _this = this;

			_this._map.clearOverlays();

			for (var item in this._layers) {
			
				var xmlFilePath = this._layers[item].file;
				
				GDownloadUrl(xmlFilePath, function(data) {
				
					var xml = GXml.parse(data);
					var icon = false;
					var markers = xml.documentElement.getElementsByTagName("marker");
					var points = [];
					
					for (var i = 0; i < markers.length; i++) {
						
						points.push({
							text: markers[i].firstChild.nodeValue ? markers[i].firstChild.nodeValue : markers[i].textContent,
							type: markers[i].getAttribute("type"),
							lng: markers[i].getAttribute("lng"),
							lat: markers[i].getAttribute("lat")
						})
						
					}
					
					for (var i in  points) {
		
						(function (point) {
							var ico = null;
							if (point.type) {
								ico = new GIcon(G_DEFAULT_ICON, icon_type[point.type]);
								ico.iconSize = new GSize(27, 27);
								ico.shadow = false;//new GSize(27, 27);
							}
							var marker = new GMarker(new GLatLng(point.lat, point.lng), ico);
							
							GEvent.addListener(marker, "click", function() {
								marker.openInfoWindowHtml(point.text);
							});
							
							_this._map.addOverlay(marker);
							
						})(points[i])
							
					}
					
				});
			
			}
			
		}

		var _constructor = function () {
			var _this = this;
			
			if (GBrowserIsCompatible()) {
				
				G_PHYSICAL_MAP.getMinimumResolution = 
					G_NORMAL_MAP.getMinimumResolution =
					G_SATELLITE_MAP.getMinimumResolution =
					G_HYBRID_MAP.getMinimumResolution = function () { return _this._resolution[0] };
		
				G_PHYSICAL_MAP.getMaximumResolution =
					G_NORMAL_MAP.getMaximumResolution = 
					G_SATELLITE_MAP.getMaximumResolution = 
					G_HYBRID_MAP.getMaximumResolution = function () { return _this._resolution[1] };
					
				this._map = new GMap2(document.getElementById(id));

				//this._map.removeMapType(G_HYBRID_MAP);
				//this._map.setCenter(new GLatLng(42.366662,-71.106262), 11);
				var mapControl = new GMapTypeControl();
				this._map.addControl(mapControl);
				this._map.addControl(new GLargeMapControl3D());	
				this._map.setMapType(G_HYBRID_MAP);
		
				var marker = new GMarker( new GLatLng(coord_x,coord_y) );
				this._map.addOverlay(marker);
				marker.openInfoWindowHtml(name);

				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(name);
				});

				this._map.getPane(G_MAP_FLOAT_SHADOW_PANE).style.display = "none";
				
			}
		}.call(this);

	}

	var showMinskMap = function ( map_coord_x, map_coord_y, name ) {
		
		var map = new buildMap('map', map_coord_x, map_coord_y, name);
		map.setResolution(14, 18);
		map.setCenter( map_coord_x, map_coord_y );
		//map.addLayer('minsk', _markers_minsk_file);
		
		$('.selected').removeClass('selected');
		$('.minsk').addClass('selected');
	}


	function initializeMap( map_coord_x, map_coord_y, name ) {
		
		$('#map_container').show();

		showMinskMap( map_coord_x, map_coord_y, name );
		return false;
	}

