/* reg_id: 7651019 */
/* staff_id: 107 */

'use strict';

$(function(){
	//localStorage.removeItem('city');
	
	 $(document).click(function(event) {
	    if ($(event.target).closest(".city-popup").length) return;
	    $(".city-popup").fadeOut();
	    event.stopPropagation();
	 });
	 
	 $(document).click(function(event) {
	    if ($(event.target).closest(".city-popup1").length) return;
	    $(".city-popup1").fadeOut();
	    event.stopPropagation();
	 });
 
	setTimeout(function(){
		if ( !localStorage.getItem('city') ) {
			console.log(localStorage.getItem('city'))
			$('.city-popup').fadeIn();
			if ( $('.cityname').data('city') == localStorage.getItem('city') ) {
				document.location.href = $('.city-popup1__blocks a:contains("'+$('.cityname').data('city')+'")').attr('href');
			}
		}
	}, 2000);
	
	if ( localStorage.getItem('city') ) {
		var city = localStorage.getItem('city');
		
		$('.cityname, .city-popup__title span').text( localStorage.getItem('city') );
		
		if ( $('.city-popup1__blocks .pop-it > a:contains("'+city+'")').hasClass('changecontacts') ) {
			var c1 = $('.city-popup1__blocks .pop-it > a:contains("'+city+'")').next().find('.c1').html(),
				c2 = $('.city-popup1__blocks .pop-it > a:contains("'+city+'")').next().find('.c2').html();
			
			$('.site_phones').html(c1);
			$('.site_address').html(c2);
		}
	}
});
	
$(document).on('click', '.city-popup__close', function(){
	$('.city-popup').fadeOut();
	return false;
});
$(document).on('click', '.city-popup1__close', function(){
	$('.city-popup1').fadeOut();
	return false;
});
$(document).on('click', '.cityname', function(){
	$('.city-popup').fadeIn();
	return false;
});
$(document).on('click', '.city-no', function(){
	$('.city-popup').fadeOut();
	$('.city-popup1').fadeIn();
	return false;
});
$(document).on('click', '.city-yes', function(){
	var city = $('.cityname').text();
		localStorage.setItem('city', city);
		console.log(city,'ok')
		
	$('.city-popup').fadeOut();
	
	if ( $('.city-popup1__blocks a:contains("'+city+'")').length ) {
		
		if ( $('.city-popup1__blocks .pop-it > a:contains("'+city+'")').hasClass('changecontacts') ) {
			var c1 = $('.city-popup1__blocks .pop-it > a:contains("'+city+'")').next().find('.c1').html(),
				c2 = $('.city-popup1__blocks .pop-it > a:contains("'+city+'")').next().find('.c2').html();
			
			$('.site_phones').html(c1);
			$('.site_address').html(c2);
		} else {
			document.location.href = $('.city-popup1__blocks a:contains("'+city+'")').attr('href');
		}
		
		
	} else {
		$('.city-popup').fadeOut();
		//$('.city-popup1').fadeIn();
	}
	
	return false;
});
$(document).on('click', '.city-popup1__blocks a', function(){
	var city = $(this).text();
	
	if($('.cityname').data('city') == city){
		localStorage.setItem('city', city);
	}
	
	$('.cityname, .city-popup__title span').text( localStorage.getItem('city') );
	$('.city-popup1').fadeOut();
	if ( $('.city-popup1__blocks .pop-it > a:contains("'+city+'")').hasClass('changecontacts') ) {
		var c1 = $('.city-popup1__blocks .pop-it > a:contains("'+city+'")').next().find('.c1').html(),
			c2 = $('.city-popup1__blocks .pop-it > a:contains("'+city+'")').next().find('.c2').html();
		
		$('.site_phones').html(c1);
		$('.site_address').html(c2);
		
		return false;
	} else {
		document.location.href = $('.city-popup1__blocks a:contains("'+city+'")').attr('href');
	}
});