﻿$(document).ready(function(){

	$('.topMenu > li').hover(function(){
		if(!$(this).is('.current')){
			$(this).find('ul').stop(true, true);
			$(this).find('ul').slideDown(200);
			$(this).addClass('hovered');
		}
	},function(){
		if(!$(this).is('.current')){
			$(this).find('ul').slideUp(200);
			$(this).removeClass('hovered');
		}
	});
	
	if ( $('.introImg img').length > 1 ) {
		$('.introImg').cycle({
			fx: 'fade', 
			speed: 800, 
			timeout: 5000, 
			next: '.introNext', 
			prev: '.introPrev',
			pause: 1
		});
	}
	else {
		$(".intro .arrows").hide();
	}
	
	$('.actions').cycle({
		fx: 'fade', 
		speed: 800, 
		timeout: 4100, 
		next: '.introNext', 
		prev: '.introPrev',
		pause: 1
	});
	
	$('.jcarousel-skin-index').jcarousel({
		scroll: 1,
		wrap: 'circular',
		auto: 7
	});
	
	$('.voteSlider').click(function(){
		if($('.vote').is('.active')){
			$('.voteContent').slideUp(300, function(){
				$('.vote').removeClass('active');
			});
		}else{
			$('.vote').addClass('active');
			$('.voteContent').slideDown(300);
		}
	})
	

	{	// Вставка флеша на титульной странице
	
		if ($('#flashMap').length) {
			var _playerVersion = $.flash.version,
				_majorVersion = _playerVersion.major;
			if (_majorVersion >= 9){
				$('#flashMap').html('');
				$('#flashMap').flash({
					swf: _root + 'public/flash/map.swf',
					width: 655,
					height: 635,
					id: 'map',
					wmode: 'transparent',
					flashvars: {
						xmlURL: _xmlLink, //+ '?rand=' + Math.random(),
						shopsURL: _shopsURL
					}
				});
			} else {
				//showNoFlashPopup();
			}
			
		}
		
	}
	
	
});

	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_NORMAL_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(9, 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;
	}

