$(document).ready(function() {
	
function ZoomControl(controlDiv, map) {
  
  // Creating divs & styles for custom zoom control
  controlDiv.style.padding = '5px';

  // Set CSS for the control wrapper
  var controlWrapper = document.createElement('div'); 
  controlWrapper.style.cursor = 'pointer';
  controlWrapper.style.textAlign = 'center';
  controlWrapper.style.width = '32px'; 
  controlWrapper.style.height = '85px';
  controlWrapper.style.right = '50px';

  controlDiv.appendChild(controlWrapper);
  
  // Set CSS for the zoomIn
  var zoomInButton = document.createElement('div');
  zoomInButton.style.width = '32px'; 
  zoomInButton.style.height = '32px';
  zoomInButton.style.backgroundPosition = "center";
  zoomInButton.style.backgroundSize = "16px";
  zoomInButton.style.backgroundRepeat = "no-repeat";
  zoomInButton.style.borderRadius = "32px";
  zoomInButton.style.backgroundColor ="white";
  zoomInButton.style.marginBottom = "10px";
  /* Change this to be the .png image you want to use */
  zoomInButton.style.backgroundImage = 'url("/img/gm_max.svg")';
  controlWrapper.appendChild(zoomInButton);
    
  // Set CSS for the zoomOut
  var zoomOutButton = document.createElement('div');
  zoomOutButton.style.width = '32px'; 
  zoomOutButton.style.height = '32px';
  zoomOutButton.style.backgroundPosition = "center";
  zoomOutButton.style.backgroundSize = "16px";
  zoomOutButton.style.backgroundRepeat = "no-repeat";
  zoomOutButton.style.borderRadius = "32px";
  zoomOutButton.style.backgroundColor ="white";
  
  /* Change this to be the .png image you want to use */
  zoomOutButton.style.backgroundImage = 'url("/img/gm_min.svg")';
  controlWrapper.appendChild(zoomOutButton);

  // Setup the click event listener - zoomIn
  google.maps.event.addDomListener(zoomInButton, 'click', function() {
    map.setZoom(map.getZoom() + 1);
  });
    
  // Setup the click event listener - zoomOut
  google.maps.event.addDomListener(zoomOutButton, 'click', function() {
    map.setZoom(map.getZoom() - 1);
  });  
    
}


	$lnglat_en = {lat: 37.177915, lng: 34.897224}
	$lnglat_ru = {lat: 57.583579, lng: 44.745026}
	$lnglat_ua = {lat: 49.145449, lng: 31.538965} //49.145449, 31.538965
	$mapzoom   = 2;
	
	$lnglat = $lnglat_en;
	if ( $('html').attr('lang') == 'ru' ) {$lnglat = $lnglat_ru;$mapzoom   = 4;}
	if ( $('html').attr('lang') == 'ua' ) {$lnglat = $lnglat_ua;$mapzoom   = 7;}
 
	var GM = {
    init: function () {
        this.initCache();
        this.initMap(); 
    },

    initCache: function() {
        this.$body         = $('body'); 
		this.coordinates = $lnglat;
		//this.markerImage= 'http://ap.markergroup.info/upload/map/icons/green_dom.png';
		this.markerImage= 'https://ap.markergroup.info/upload/map/green_empty.png';
		this.map  = {};
    },
  
	clearMarkers: function() {
		for (var i = 0; i < map.markers.length; i++) {
          map.markers[i].setMap(null);
        }
        map.markers = [];
		
     },
	  
    initMap: function () {
        var  zoom = 6;
            map = new google.maps.Map(document.getElementById('map'), {
                center: this.coordinates,
                zoom: $mapzoom,
                disableDefaultUI: true,
                scrollwheel: true,
                zoomControl: true,
				markers: [],
				//styles: [{"elementType":"geometry","stylers":[{"color":"#1d2c4d"}]},{"elementType":"labels.text.fill","stylers":[{"color":"#8ec3b9"}]},{"elementType":"labels.text.stroke","stylers":[{"color":"#1a3646"}]},{"featureType":"administrative.country","elementType":"geometry.stroke","stylers":[{"color":"#4b6878"}]},{"featureType":"administrative.land_parcel","elementType":"labels.text.fill","stylers":[{"color":"#64779e"}]},{"featureType":"administrative.province","elementType":"geometry.stroke","stylers":[{"color":"#4b6878"}]},{"featureType":"landscape.man_made","elementType":"geometry.stroke","stylers":[{"color":"#334e87"}]},{"featureType":"landscape.natural","elementType":"geometry","stylers":[{"color":"#023e58"}]},{"featureType":"poi","elementType":"geometry","stylers":[{"color":"#283d6a"}]},{"featureType":"poi","elementType":"labels.text.fill","stylers":[{"color":"#6f9ba5"}]},{"featureType":"poi","elementType":"labels.text.stroke","stylers":[{"color":"#1d2c4d"}]},{"featureType":"poi.park","elementType":"geometry.fill","stylers":[{"color":"#023e58"}]},{"featureType":"poi.park","elementType":"labels.text.fill","stylers":[{"color":"#3C7680"}]},{"featureType":"road","elementType":"geometry","stylers":[{"color":"#304a7d"}]},{"featureType":"road","elementType":"labels.text.fill","stylers":[{"color":"#98a5be"}]},{"featureType":"road","elementType":"labels.text.stroke","stylers":[{"color":"#1d2c4d"}]},{"featureType":"road.highway","elementType":"geometry","stylers":[{"color":"#2c6675"}]},{"featureType":"road.highway","elementType":"geometry.stroke","stylers":[{"color":"#255763"}]},{"featureType":"road.highway","elementType":"labels.text.fill","stylers":[{"color":"#b0d5ce"}]},{"featureType":"road.highway","elementType":"labels.text.stroke","stylers":[{"color":"#023e58"}]},{"featureType":"transit","elementType":"labels.text.fill","stylers":[{"color":"#98a5be"}]},{"featureType":"transit","elementType":"labels.text.stroke","stylers":[{"color":"#1d2c4d"}]},{"featureType":"transit.line","elementType":"geometry.fill","stylers":[{"color":"#283d6a"}]},{"featureType":"transit.station","elementType":"geometry","stylers":[{"color":"#3a4762"}]},{"featureType":"water","elementType":"geometry","stylers":[{"color":"#0e1626"}]},{"featureType":"water","elementType":"labels.text.fill","stylers":[{"color":"#4e6d70"}]}]
            });
			var zoomControlDiv = document.createElement('div');
			var zoomControl = new ZoomControl(zoomControlDiv, map);
			zoomControlDiv.index = 1;
			map.controls[google.maps.ControlPosition.RIGHT_CENTER].push(zoomControlDiv);
 
    }
};

	function initMarkerMap() {
		
	$('#products').html('');
	
	$.post( "//map.markergroup.info/mpi_map.php", { method: "listRegions", lang: $('#language').val(), country: $('html').attr('lang') }).done(function( data ) {
		var obj = jQuery.parseJSON( data );
		if  ( obj.status === 1 ) $('#regions').html( obj.text + '<a href="javascript:;" class="clse">×</a>');
	});
	$.post( "//map.markergroup.info/mpi_map.php", { method: "listCompanies", lang: $('#language').val(), country: $('html').attr('lang') }).done(function( data ) {
		var obj = jQuery.parseJSON( data );
		if  ( obj.status === 1 ) $('#company').html( obj.text + '<a href="javascript:;" class="clse">×</a>');
	});
	$.post( "//map.markergroup.info/mpi_map.php", { method: "listProducts", lang: $('#language').val(), country: $('html').attr('lang') }).done(function( data ) {
		var obj = jQuery.parseJSON( data );
		if  ( obj.status === 1 ) $('#products').append( obj.text+ '<a href="javascript:;" class="clse">×</a>' );
	});
		setTimeout(function(){
	
		GM.clearMarkers();
		
		$('#infoPanel > div').html( "" );
		$('#infoPanel').removeClass('active');
		
		var prod = [];
		var comp = [];
		var regn = [];
	
		$('#filtPanel ins[rl="prod"]').each(function(){
			if ( $(this).hasClass('active') ) prod.push( $(this).attr('value') );
		});
		$('#filtPanel ins[rl="comp"]').each(function(){
			if ( $(this).hasClass('active') ) comp.push( $(this).attr('value') );
		});
		$('#filtPanel ins[rl="regn"]').each(function(){
			if ( $(this).hasClass('active') ) regn.push( $(this).attr('value') );
		});
 		
		$.post( "//map.markergroup.info/mpi_map.php", { method: "getMarkers", country: $('html').attr('lang'),
			prod: JSON.stringify(prod),
			comp: JSON.stringify(comp),
			regn: JSON.stringify(regn),
			cpt: $('#type_selector span.active').attr('rel')
		}).done(function( data ) {
			var obj = jQuery.parseJSON( data );
			if  ( obj.status === 1 ) {
				$.each(obj.data, function (key, dt) {
				
					var latlng = new Object();
					latlng.lat = parseFloat(dt.lat);
					latlng.lng = parseFloat(dt.lng);
									
					var balon = GM.markerImage;
					if ( dt.balon != '' ) balon = '//ap.markergroup.info/upload/map/' + dt.balon;
					var balon_img = {
    					url: balon,
						size: new google.maps.Size(38, 60),
						origin: new google.maps.Point(0, 0),
						anchor: new google.maps.Point(19, 60)
					};
					console.log( dt.bigmarker );
					if ( dt.bigmarker == 'Y' && dt.balon != '') {
						balon = '//ap.markergroup.info/upload/map/big/' + dt.balon;
						var balon_img = {
    						url: balon,
							size: new google.maps.Size(48, 76),
							origin: new google.maps.Point(0, 0),
							anchor: new google.maps.Point(24, 76)
						};
					}
					
  
					window['m'+key] = new google.maps.Marker({
							position: latlng, 
							map: map, 
							icon: balon_img, 
							title: dt.title, 
							mmapid: dt.place_id, 
							mmappid: dt.placeinfo_id
					});
					map.markers.push( window[ 'm'+key ] ); 
					window[ 'm'+key ].addListener('click', function() { 
						$.post( "//map.markergroup.info/mpi_map.php", {
								lang: $('#langSelect ins').attr('class'), 
								method: "getMarkerInfo", marker_id: this.mmappid }).done(function(d){
							var obj2 = jQuery.parseJSON( d );
							if  ( obj2.status === 1 ) { 
								$('#infoPanel').addClass('active');
								$('.placeinfo[rel="'+dt.placeinfo_id+'"]').remove();
								$('.metaPlaceInfo[rel="'+dt.placeinfo_id+'"]').remove();
								$('#infoPanel > div').append( obj2.text );
								window[ 'm'+key ].setAnimation(google.maps.Animation.BOUNCE);
							} else {
								if ( obj2.text === 'Unautorized' ) {
									$('#qr').addClass('active');
								}
							}
						});
					}); 
				});
 
			
			}
		});
		$.post( "//map.markergroup.info/mpi_map.php", { method: "getMarkersCPT", country: $('html').attr('lang'),
			prod: JSON.stringify(prod),
			comp: JSON.stringify(comp),
			regn: JSON.stringify(regn),
			type: $('#type_selector span.active').attr('rel')
		}).done(function( data ) {
			var obj = jQuery.parseJSON( data );
			if  ( obj.status === 1 ) {
				$.each(obj.data, function (key, dt) {
				
					var latlng = new Object();
					latlng.lat = parseFloat(dt.lat);
					latlng.lng = parseFloat(dt.lng);
									
					var balon = GM.markerImage;
					if ( dt.balon != '' ) balon = '//ap.markergroup.info/upload/map/' + dt.balon;
					if ( dt.bigmarker == 'Y' && dt.balon != '') balon = '//ap.markergroup.info/upload/map/big/' + dt.balon;
					window['m'+key] = new google.maps.Marker({position: latlng, map: map, icon: balon, title: dt.title, mmapid: dt.place_id, mmappid: dt.placeinfo_id});
					map.markers.push( window[ 'm'+key ] ); 
					window[ 'm'+key ].addListener('click', function() { 
						$.post( "//map.markergroup.info/mpi_map.php", { 
								lang: $('#langSelect ins').attr('class'),
								method: "getMarkerInfo", marker_id: this.mmappid }).done(function(d){
							var obj2 = jQuery.parseJSON( d );
							if  ( obj2.status === 1 ) { 
								$('#infoPanel').addClass('active');
								$('.placeinfo[rel="'+dt.placeinfo_id+'"]').remove();
								$('.metaPlaceInfo[rel="'+dt.placeinfo_id+'"]').remove();
								
								$('#infoPanel > div').append( obj2.text );
								window[ 'm'+key ].setAnimation(google.maps.Animation.BOUNCE);
							}
						});
					}); 
				});
 
			
			}
		});
		
		}, 350);
	}
	
	
	
	
	
	function update_markers($t) {
		$('#wait').addClass('active');
		setTimeout(function(){
			
		GM.clearMarkers();
		$cpt = $('#type_selector span.active').attr('rel');
		if ( $t.parent().attr('id') == 'type_selector') {
			//$('#type_selector span').removeClass('active');
			//$t.addClass('active');
			$cpt = $t.attr('rel');
		} 
		/*
		if ( $(this).hasClass('rangeTarget') ) {
			$('.selins').each(function(){
				if ( !$(this).hasClass('rangeTarget') ) $(this).removeClass('active');
			});
		} else {
			$('.rangeTarget').removeClass('active');
		}
		*/
		$('#infoPanel > div').html( "" );
		$('#infoPanel').removeClass('active');
		
		var prod = [];
		var comp = [];
		var regn = [];
	
		$('#filtPanel ins[rl="prod"]').each(function(){
			if ( $(this).hasClass('active') ) prod.push( $(this).attr('value') );
		});
		$('#filtPanel ins[rl="comp"]').each(function(){
			if ( $(this).hasClass('active') ) comp.push( $(this).attr('value') );
		});
		$('#filtPanel ins[rl="regn"]').each(function(){
			if ( $(this).hasClass('active') ) regn.push( $(this).attr('value') );
		});
 		
 		if ( $cpt== 'all' ||  $cpt== 'exw'||  $cpt== 'nauk'||  $cpt== 'othr') {
		$.post( "//map.markergroup.info/mpi_map.php", { method: "getMarkers", country: $('html').attr('lang'),
			prod: JSON.stringify(prod),
			comp: JSON.stringify(comp),
			regn: JSON.stringify(regn),
			cpt: $('#type_selector span.active').attr('rel')
		}).done(function( data ) {
				$('#wait').removeClass('active');
			var obj = jQuery.parseJSON( data );
			if  ( obj.status === 1 ) {
				$.each(obj.data, function (key, dt) {
				
					var latlng = new Object();
					latlng.lat = parseFloat(dt.lat);
					latlng.lng = parseFloat(dt.lng);
									
					var balon = GM.markerImage;
					if ( dt.balon != '' ) balon = '//ap.markergroup.info/upload/map/' + dt.balon;
					if ( dt.bigmarker == 'Y' && dt.balon != '') balon = '//ap.markergroup.info/upload/map/big/' + dt.balon;
					window['m'+key] = new google.maps.Marker({position: latlng, map: map, icon: balon, title: dt.title, mmapid: dt.place_id, mmappid: dt.placeinfo_id});
					map.markers.push( window[ 'm'+key ] ); 
					window[ 'm'+key ].addListener('click', function() { 
						$.post( "//map.markergroup.info/mpi_map.php", { 
							lang: $('#langSelect ins').attr('class'),
							method: "getMarkerInfo", marker_id: this.mmappid }).done(function(d){
							var obj2 = jQuery.parseJSON( d );
							if  ( obj2.status === 1 ) { 
								$('#infoPanel').addClass('active');
								$('.placeinfo[rel="'+dt.placeinfo_id+'"]').remove();
								$('.metaPlaceInfo[rel="'+dt.placeinfo_id+'"]').remove();
								$('#infoPanel > div').append( obj2.text );
								window[ 'm'+key ].setAnimation(google.maps.Animation.BOUNCE);
								
							
							}
						});
					}); 
				});
 
			
			}
		});
 		}
 		
		if (  $cpt== 'all' ||  $cpt== 'rail' || $cpt== 'port' ) {
			
		$.post( "//map.markergroup.info/mpi_map.php", { method: "getMarkersCPT", country: $('html').attr('lang'),
			prod: JSON.stringify(prod),
			comp: JSON.stringify(comp),
			regn: JSON.stringify(regn),
			type: $('#type_selector span.active').attr('rel')
		}).done(function( data ) {
				$('#wait').removeClass('active');
			var obj = jQuery.parseJSON( data );
			if  ( obj.status === 1 ) {
				$.each(obj.data, function (key, dt) {
				
					var latlng = new Object();
					latlng.lat = parseFloat(dt.lat);
					latlng.lng = parseFloat(dt.lng);
									
					var balon = GM.markerImage;
					if ( dt.balon != '' ) balon = '//ap.markergroup.info/upload/map/' + dt.balon;
					if ( dt.bigmarker == 'Y' && dt.balon != '') balon = '//ap.markergroup.info/upload/map/big/' + dt.balon;
					window['m'+key] = new google.maps.Marker({position: latlng, map: map, icon: balon, title: dt.title, mmapid: dt.place_id, mmappid: dt.placeinfo_id});
					map.markers.push( window[ 'm'+key ] ); 
					window[ 'm'+key ].addListener('click', function() { 
						$.post( "//map.markergroup.info/mpi_map.php", { 
							lang: $('#langSelect ins').attr('class'),
							method: "getMarkerInfo", marker_id: this.mmappid }).done(function(d){
							var obj2 = jQuery.parseJSON( d );
							if  ( obj2.status === 1 ) { 
								$('#infoPanel').addClass('active');
								$('.placeinfo[rel="'+dt.placeinfo_id+'"]').remove();
								$('.metaPlaceInfo[rel="'+dt.placeinfo_id+'"]').remove();
								
								$('#infoPanel > div').append( obj2.text );
								window[ 'm'+key ].setAnimation(google.maps.Animation.BOUNCE);
								
									 
							}
						});
					}); 
				});
 
			
			}
		});
		
		}
		
		}, 500);
		
	}
	
	
	$(document).on('click', '#filtPanel i', function(){
		if ( $(this).hasClass('active') ) {
			$(this).removeClass('active');
			$(this).next().removeClass('active');
		} else {
			$('#filtPanel > div').removeClass('active');
			$('#filtPanel i').removeClass('active');
			$(this).addClass('active');
			$(this).next().addClass('active');
		}
	}).on('click', '#filtPanel .parent', function(){
		$(this).toggleClass('active');
		$(this).next().toggleClass('active');
	}).on('click', '#filtPanel ins', function(){
		if ( $(this).attr('value') == '0' ) {
			if ( $(this).hasClass('active') ) {
				$(this).parent().find('ins').removeClass('active');
			} else {
				$(this).parent().find('ins').addClass('active');
			}
		} else {
			$(this).toggleClass('active');
			$rl = $(this).attr('rl');
			$('#filtPanel ins[rl="'+$rl+'"][value="0"]').removeClass('active');
		}
	}).on('click', '#filtPanel ins', function(){
		$t = $(this);
		update_markers($t);

		var id = $(this).parent().attr('id');
		var rl = $(this).attr('value');
		var tx = $(this).html();
		if ( !$(this).hasClass('active')) {
			$('#map_chips > span.' + id + '[rel="'+rl+'"]').remove();
		} else {
			$('#map_chips').append('<span class="' + id + '" rel="'+rl+'">'+tx+'</span>');
		}
		
		
	}).on('click', '#type_selector span', function(){
		$t = $(this);
		update_markers($t);

		var id = $(this).parent().attr('id');
		var rl = $(this).attr('rel');
		var tx = $(this).html();
		$('#map_chips > span.type_selector').remove();
		if ( $(this).hasClass('active')) {
			//$('#map_chips > span.' + id + '[rel="'+rl+'"]').remove();
		} else {
			$('#map_chips').append('<span class="' + id + '" rel="'+rl+'">'+tx+'</span>');
		}
		
		$('#type_selector span').removeClass('active');
		$(this).addClass('active');
		
		
		
		
	}).on('click', '#map_chips span', function(){
		var id = $(this).attr('class');
		var rl = $(this).attr('rel');
		var temp = 0;
		if ( id == 'type_selector' ) {
			$('#type_selector span').removeClass('active');
			$('#type_selector span[rel="all"]').addClass('active');
			//console.log('#'+id+' span[rel="'+rl+'"]');
			temp += 1;
			$t = $('#type_selector span[rel="all"]');
			$t.addClass('active');
			//$('#'+id+' span[rel="'+rl+'"]').click();
			temp += 1;
			update_markers($t);
			temp += 1;
			console.log(temp);
		} else {
			temp += 1;
			//console.log('#'+id+' ins[rel="'+rl+'"]');
			$t = $('#'+id+' ins[value="'+rl+'"]');
			temp += 1;
			$t.removeClass('active');
			//$('#'+id+' ins[rel="'+rl+'"]').click();
			temp += 1;
			temp = update_markers($t);
			temp += 1;
			console.log(temp);
		}
		console.log(temp);
		temp = 1;
		$(this).remove();
		
	}).on('click', '#infoPanel ._close', function(){
		$(this).parent().removeClass('active');
		$('.placeinfo ._placeRem').each(function(){
    		window[ 'm'+$(this).attr('rl') ].setAnimation(null);
	    	$(this).parent().remove();
		    if ( ( $('.placeinfo').length + $('.metaPlaceInfo').length ) < 1 ) {
			    $('#infoPanel').removeClass('active');
		    }
		});
	}).on('click', '.placeinfo ._placeRem', function(){
		window[ 'm'+$(this).attr('rl') ].setAnimation(null);
		$(this).parent().remove();
		if ( ( $('.placeinfo').length + $('.metaPlaceInfo').length ) < 1 ) {
			$('#infoPanel').removeClass('active');
		}
	}).on('click', '._placeRemDir', function(){
		window[ 'm'+$(this).attr('rl') ].setAnimation(null);
		$(this).parent().remove();
		if ( ( $('.placeinfo').length + $('.metaPlaceInfo').length ) < 1 ) {
			$('#infoPanel').removeClass('active');
		}
	}).on('click', '._placePrc', function(){
		$t = $(this);
		
		var prod = [];
		$('#filtPanel ins[rl="prod"]').each(function(){
			if ( $(this).hasClass('active') ) prod.push( $(this).attr('value') );
		});
		
		if ( $(this).hasClass('active') ) {
			$(this).removeClass('active');
			$('#placeprices_'+$t.attr('rl')).removeClass('active');
			$t.parent().find('.placeprices').removeClass('active');
		} else {
			$(this).parent().find('._btn').removeClass('active');
			$.post( "//map.markergroup.info/mpi_map.php", { 
				method: "getPlacePrices", 
				pi: $(this).attr('rl'), 
				prod: JSON.stringify(prod),
				country: $('html').attr('lang'),
			}).done(function( dta ) {
				var j = jQuery.parseJSON( dta );
				$t.addClass('active');
			//	$('.placeprices').removeClass('active');
				$t.parent().find('.placeprices').addClass('active');
				$t.parent().find('.placeprices').html(j.text);
					/*
				if ( $t.hasClass('p771')) {
					$('#placeprices_'+$t.attr('p771')).addClass('active');
					$('#placeprices_'+$t.attr('p771')).html(j.text);
				} else {
					$('#placeprices_'+$t.attr('rl')).addClass('active');
					$('#placeprices_'+$t.attr('rl')).html(j.text);
				}
				*/
			});
		}
	}).on('click', '._placeInf', function(){
		$t = $(this);
		if ( $(this).hasClass('active') ) {
			$(this).removeClass('active');
			$('#placeprices_'+$t.attr('rl')).removeClass('active');
		} else {
			$(this).parent().find('._btn').removeClass('active');
			$.post( "//map.markergroup.info/mpi_map.php", { 
				method: "getPlaceInfo", 
				pi: $t.attr('rl'), 
				country: $('html').attr('lang'),
			}).done(function( dta ) {
				var j = jQuery.parseJSON( dta );
				$t.addClass('active');
				$('#placeprices_'+$t.attr('rl')).addClass('active');
				$('#placeprices_'+$t.attr('rl')).html(j.text);
			});
		}
	}).on('click', '._placeOffer', function(){
		$t = $(this);
		if ( $(this).hasClass('active') ) {
			$(this).removeClass('active');
			$('#placeprices_'+$t.attr('rl')).removeClass('active');
		} else {
			$(this).parent().find('._btn').removeClass('active');
			$.post( "//map.markergroup.info/mpi_map.php", { 
				method: "getPlaceOffer", 
				pi: $t.attr('rl'), 
				country: $('html').attr('lang'),
			}).done(function( dta ) {
				var j = jQuery.parseJSON( dta );
				$t.addClass('active');
				$('#placeprices_'+$t.attr('rl')).addClass('active');
				$('#placeprices_'+$t.attr('rl')).html(j.text);
			});
		}
	}).on('click', '._compInf', function(){
		$t = $(this);
		if ( $(this).hasClass('active') ) {
			$(this).removeClass('active');
			$('#placeprices_'+$t.attr('rl')).removeClass('active');
		} else {
			$(this).parent().find('._btn').removeClass('active');
			$.post( "//map.markergroup.info/mpi_map.php", { 
				method: "getCompInfo", 
				pi: $t.attr('rul'), 
				country: $('html').attr('lang'),
			}).done(function( dta ) {
				var j = jQuery.parseJSON( dta );
				$t.addClass('active');
				$('#placeprices_'+$t.attr('rl')).addClass('active');
				$('#placeprices_'+$t.attr('rl')).html(j.text);
			});
		}
	}).on('click', '._compNews', function(){
		$t = $(this);
		if ( $(this).hasClass('active') ) {
			$(this).removeClass('active');
			$('#placeprices_'+$t.attr('rl')).removeClass('active');
		} else {
			$(this).parent().find('._btn').removeClass('active');
			$.post( "//map.markergroup.info/mpi_map.php", { 
				method: "getCompNews", 
				pi: $t.attr('rul'), 
				country: $('html').attr('lang'),
			}).done(function( dta ) {
				var j = jQuery.parseJSON( dta );
				$t.addClass('active');
				$('#placeprices_'+$t.attr('rl')).addClass('active');
				$('#placeprices_'+$t.attr('rl')).html(j.text);
			});
		}
	}).on('change', '.rangeSel', function(){
		$('#products .selins').removeClass('active');
		$('.rangeTarget').removeClass('active');
		$('.rangeTarget').removeClass('broken');
		$('.rangeSel').each(function(){
			var rel    = $(this).attr('rel');
			var vals   = $(this).val();
			var result = vals.split(',');
			var v1     = parseFloat(result[0]);
			var v2     = parseFloat(result[1]);
			//console.log(rel);
			$('.rangeTarget').each(function(){
				var rt = $(this);
				if ( !$(this).hasClass('broken')) {
					var t_val = parseInt($(this).attr( rel ));
					if ( (v2 >= t_val) && (v1 <= t_val) ) {
						rt.addClass('active');
					} else {
						rt.removeClass('active');
						rt.addClass('broken');
						//rt.addClass('broken_'+rel+'__'+t_val+'-'+v1+'-'+v2);
					}
				}
			});
		});
	}).on('click', '.qrClose', function(){
		$('#qr').removeClass('active');
	}).on('click', '.cnews .mi_large', function(){
		$(this).parent().parent().toggleClass('fiLarge');
	}).on('click', '.cnews .ntit', function(){
		if ( $(this).parent().hasClass('active') ) {
			$('.cnews').removeClass('active');
		} else {
			$('.cnews').removeClass('active');
			$(this).parent().addClass('active');
		}
	}).on('click', '.loginform .submit', function(){
		var login = $('#t_login').val();
		var paswd = $('#t_paswd').val();
			$.post( "//map.markergroup.info/mpi_map.php", {
				method: 't_login',
				login:login, paswd:paswd
			}).done(function( data ) {	
				if ( data != '-' ) {
					$('#mapuid').val(data);
					$('#qr').removeClass('active');
					$(document).location.reload();
				} else {
					alert('Wrong Login and/or Password');
				}
			});
	}).on('click', '.clse', function(){
		$(this).parent().removeClass('active');
		$(this).parent().prev().removeClass('active');
	}).on('keyup', '#searchPanel input', function(e){
		if(e.keyCode == 13) {
			if ( $(this).val() != '' ) {
				search_load();
			}
		}
	}).on('click', '#searchPanel a', function(e){
		if ( $(this).prev().val() != '' ) {
			search_load();
		}
	}).on('click', '.youtube img', function(){
		var key = $(this).parent().attr('rel');
		var ifr = '<iframe width="789" height="444" src="https://www.youtube.com/embed/'+key+'" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
		$('#ytview > div').prepend(ifr);
		$('#ytview > div > span').html( $(this).next().text());
		$('#ytview').addClass('active');
	}).on('click', '#ytview .close', function(){
		$('#ytview iframe').remove();
		$('#ytview > div span').html('');
		$('#ytview').removeClass('active');
	}).on('click', '._compVideo', function(){
		$t = $(this);
		if ( $(this).hasClass('active') ) {
			$(this).removeClass('active');
			$('#placeprices_'+$t.attr('rl')).removeClass('active');
		} else {
			$(this).parent().find('._btn').removeClass('active');
			$.post( "//map.markergroup.info/mpi_map.php", { 
				method: "getCompVideo", 
				lang: $('#langSelect ins').attr('class'), 
				markerId: $t.attr('markerid'), 
				compId: $t.attr('rul'), 
				country: $('html').attr('lang'),
			}).done(function( dta ) {
				var j = jQuery.parseJSON( dta );
				$t.addClass('active');
				$('#placeprices_'+$t.attr('rl')).addClass('active');
				$('#placeprices_'+$t.attr('rl')).html(j.text);
			});
		}
	});
	 
	 
function search_load() {
	$('#wait').addClass('active');
	GM.clearMarkers();
	$('#infoPanel > div').html( "" );
	$('#infoPanel').removeClass('active');
		

		$.post( "//map.markergroup.info/mpi_map.php", { method: "getMarkers", country: $('html').attr('lang'),
			search: $('#searchPanel input').val()
		}).done(function( data ) {
			$('#wait').removeClass('active');
			var obj = jQuery.parseJSON( data );
			if  ( obj.status === 1 ) {
				$.each(obj.data, function (key, dt) {
				
					var latlng = new Object();
					latlng.lat = parseFloat(dt.lat);
					latlng.lng = parseFloat(dt.lng);
									
					var balon = GM.markerImage;
					if ( dt.balon != '' ) balon = '//ap.markergroup.info/upload/map/' + dt.balon;
					if ( dt.bigmarker == 'Y' && dt.balon != '') balon = '//ap.markergroup.info/upload/map/big/' + dt.balon;
					window['m'+key] = new google.maps.Marker({position: latlng, map: map, icon: balon, title: dt.title, mmapid: dt.place_id, mmappid: dt.placeinfo_id});
					map.markers.push( window[ 'm'+key ] ); 
					window[ 'm'+key ].addListener('click', function() { 
						$.post( "//map.markergroup.info/mpi_map.php", { 
							lang: $('#langSelect ins').attr('class'),
							method: "getMarkerInfo", marker_id: this.mmappid }).done(function(d){
							var obj2 = jQuery.parseJSON( d );
							if  ( obj2.status === 1 ) { 
								$('#infoPanel').addClass('active');
								$('.placeinfo[rel="'+dt.placeinfo_id+'"]').remove();
								$('.metaPlaceInfo[rel="'+dt.placeinfo_id+'"]').remove();
								$('#infoPanel > div').append( obj2.text );
								window[ 'm'+key ].setAnimation(google.maps.Animation.BOUNCE);
								
								
							}
						});
					}); 
				});
 
			
			}
		});

 		
			
		$.post( "//map.markergroup.info/mpi_map.php", { method: "getMarkersCPT", country: $('html').attr('lang'),
			search: $('#searchPanel input').val()
		}).done(function( data ) {
			$('#wait').removeClass('active');
			var obj = jQuery.parseJSON( data );
			if  ( obj.status === 1 ) {
				$.each(obj.data, function (key, dt) {
				
					var latlng = new Object();
					latlng.lat = parseFloat(dt.lat);
					latlng.lng = parseFloat(dt.lng);
									
					var balon = GM.markerImage;
					if ( dt.balon != '' ) balon = '//ap.markergroup.info/upload/map/' + dt.balon;
					if ( dt.bigmarker == 'Y' && dt.balon != '') balon = '//ap.markergroup.info/upload/map/big/' + dt.balon;
					window['m'+key] = new google.maps.Marker({position: latlng, map: map, icon: balon, title: dt.title, mmapid: dt.place_id, mmappid: dt.placeinfo_id});
					map.markers.push( window[ 'm'+key ] ); 
					window[ 'm'+key ].addListener('click', function() { 
						$.post( "//map.markergroup.info/mpi_map.php", { 
							lang: $('#langSelect ins').attr('class'),
							method: "getMarkerInfo", marker_id: this.mmappid }).done(function(d){
							var obj2 = jQuery.parseJSON( d );
							if  ( obj2.status === 1 ) { 
								$('#infoPanel').addClass('active');
								$('.placeinfo[rel="'+dt.placeinfo_id+'"]').remove();
								$('.metaPlaceInfo[rel="'+dt.placeinfo_id+'"]').remove();
								
								$('#infoPanel > div').append( obj2.text );
								window[ 'm'+key ].setAnimation(google.maps.Animation.BOUNCE);
								
									
							}
						});
					}); 
				});
 
			
			}
		});
		

		
		
}
	 
	GM.init();
	setTimeout(initMarkerMap(), 500);
	$('body').addClass('inited');
 
	  $( '#infoPanel' ).on('swiperight', function(){
		//console.log('SWIPE');
		$('#infoPanel').removeClass('active');
	})
	 

});