scr.js 8.01 KB
var limit_;
var map;
var geocoder;
var city = new Array();
var objFull;
var cc;
var ccc;
var buttonizer = "";
var timer = 0;
var stop_renew;
var osr;
var nums;
var nums_c = 1;
var Ryn = 0;

//$.inArray("Sid", arr);


$(window).on("load",function() {
	

get_shops();


























	
	var options = { 
    success: function(html)
              {
$("#to_hide").hide();
$("#st566").show();

              } 
              };
$("#q").ajaxForm(options);

jQuery("body").on("click", ".location", function(e) {
toggle_view();
c_addr = $(this).attr("alt");
reload_map();
geocodeAddress(geocoder, map, c_addr, stop_renew);
});

jQuery("body").on("click", "#button-dark, #button-light", function(e) {
toggle_view();
});
jQuery("body").on("click", "#red-button", function(e) {
		show_mw();
});
jQuery("body").on("click", "#close_mw, #overlay", function(e) {
		close_mw();
});
jQuery("body").on("click", "#ssh", function(e) { 
		show_shops();
});

jQuery('#button-light').click();


jQuery("body").on("click","#blue-button",function(a) {
	error = 0;
	
	if (!$("#name_").val()) {
		$("#name_").css("border","1px solid red");
		error = 1;
	}
	if ($("#cp").val().length > 12 || $("#cp").val().length < 12 || $("#cp").val().substring(0,3) !== "380") {
		$("#cp").css("border","1px solid red");
		error = 1;
	}
	if (!$("#email").val()) {
		$("#email").css("border","1px solid red");
		error = 1;
	}
	if (!$("#question").val()) {
		$("#question").css("border","1px solid red");
		error = 1;
	}
	
	if (error === 0) {
		$("#q").submit();


	}
    });
	
	
});

function toggle_view() {
	    
		if (!buttonizer) {
		
		$("#button-dark").attr("class", "button-light");
		$("#button-light").attr("class", "button-dark");
		buttonizer = 1;
		$( "#text" ).toggle();
		$( "#mapi" ).toggle();
		//alert(objFull);
		if(!objFull)$.post( "/shops/get_shops", {}).done(function( data ) {objFull = jQuery.parseJSON(data);show_shops();});
		//show_shops();
	} else {
		//show_shops();
		$("#button-dark").attr("class", "button-dark");
		$("#button-light").attr("class", "button-light");
		buttonizer = 0;
		$( "#text" ).toggle();
		$( "#mapi" ).toggle();
		show_shops();
	}
		
		
}

function reload_map() {
stop_renew = Math.random();
osr = stop_renew;
clearTimeout(timer);
initMap();
nums_c = 1;
nums = 1;
$(".loading2").css("display", "none");
$(".load_text").html("");
}
	
function show_shops() { 
reload_map();
	   i = 0; 
	   curent_obj = {};
	   n = 1; 
	   jQuery.each(objFull, function() {		
		if ($("#select_city").val() == objFull[i].city) {
		
		
		curent_obj[i] = objFull[i].id;
		
		}
	   i++;
	   //console.log(i);
       });
	   
	   nums = Object.keys(curent_obj).length;
	   
	   //console.log(curent_obj);
	   
	   $("#texting").html("<div class='marginer'></div><div class='big_text'>"+$('#select_city').val()+"</div><div class='marginer'></div>");
	   jQuery.each(curent_obj, function(indexInArray){

		ccc = curent_obj[indexInArray];
		cc = objFull[ccc].adress;
		cc = cc.replace(/[^ ,.0-9A-Za-zА-Яа-яЁёєЄіІЇїґ]/g, "");
		      
        $("#texting").append("<div style='width:60%;'><div class='rimage'><img src='"+objFull[ccc].pic+"' /></div><div class='text_t'>"+n+". "+objFull[ccc].name.replace(/[^ ,.0-9A-Za-zА-Яа-яЁёєЄіІЇїґ]/g, "")+"</div>"+cc+"<div>"+objFull[ccc].contacts+"</div><div><a href='http://"+objFull[ccc].url+"' target='_blank'>"+objFull[ccc].url+"</a></div><div alt='"+$('#select_city').val()+" "+cc+"' class='location'></div></div><div class='marginer'></div>");

	   geocodeAddress(geocoder, map, $("#select_city").val()+" "+cc, stop_renew);

	   n++;
       });
	   $("#texting").append("<div class='marginer'></div>");

       
}

function get_shops(){	
			        $.ajax({  
                    type: "POST",  
                    url: "/shops/get_shops",					
                    success: function(res) {
						
					objFull = jQuery.parseJSON(res);					
                    i = 0;
                    arr_len = Object.keys(objFull).length;

                    do {
                    //console.log(objFull[i].title);
					//$('body').append(objFull[i].id);
					
					city[i] = objFull[i].city;
					
                    i++;
                    } while (i < arr_len);
					
					
					i = 0;
					city = $.unique(city);
					arr_len = city.length;
					select_str = "<select id='select_city' name='city'>"; 
					
					do {
					select_str = select_str+"<option value='"+city[i]+"'>"+city[i]+"</option>";
                    i++;
                    } while (i < arr_len);
					select_str = select_str+"</select>";
					$("#city_selector").html(select_str);

					loadRayons($('#select_city').val());
					$( "#select_city" ).change(function() {
					  loadRayons($(this).val());
					});
					
					
					
					
//alert(city.length);	
//alert($.unique(city).length);					
					
                    }  
                    });
					
}


function loadRayons(load_city){
	//if(load_city=='Київ'){
		$.post( "/shops/get_rayons", {city:load_city}).done( function( data ) { 
		  $( "#rayons_selector" ).html( data );
		  			$( "#rayon_select" ).change(function() {
					  if($(this).val()!=0){$.post( "/shops/get_rayons_json", {city:load_city,rayon:$(this).val()}).done(function( data ) {objFull = jQuery.parseJSON(data);});
					  loadBrends(load_city,$(this).val());}
					  else {$.post( "/shops/get_shops", {}).done(function( data ) {objFull = jQuery.parseJSON(data);});}
					});
					
		});		
	//}else {$( "#rayons_selector" ).html( '' );$.post( "/shops/get_shops", {}).done(function( data ) {objFull = jQuery.parseJSON(data);});}
	
	$.post( "/shops/get_shops", {}).done(function( data ) {objFull = jQuery.parseJSON(data);});
}

function loadBrends(load_city,load_rayon){
	//if(load_city=='Київ'){
		$.post( "/shops/get_brends", {city:load_rayon}).done( function( data ) { 
		  $( "#brends_selector" ).html( data );
		  			$( "#brends_select" ).change(function() {
					  if($(this).val())$.post( "/shops/get_brends_json", {city:load_city,rayon:load_rayon,brendID:$(this).val()}).done(function( data ) {objFull = jQuery.parseJSON(data);});
						else $.post( "/shops/get_rayons", {city:load_city}).done(function( data ) {objFull = jQuery.parseJSON(data);});
					});
		});		
	//}else {$( "#rayons_selector" ).html( '' );$.post( "/shops/get_shops", {}).done(function( data ) {objFull = jQuery.parseJSON(data);});}
	
	$.post( "/shops/get_rayons", {city:load_city}).done(function( data ) {objFull = jQuery.parseJSON(data);});
}					

 
function close_mw() {
$("#overlay").css({"display": "none"});
$("#modal_form").css({"display": "none"});
}
function show_mw() {
$("#overlay").css({"display": "block"});
$("#modal_form").css({"display": "block"});
}














function initMap() {
	
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 6,
    center: {lat: 48.8967234, lng: 31.0254797}
  });
  geocoder = new google.maps.Geocoder();
  
  //alert(map);

    

}

function geocodeAddress(geocoder, resultsMap, address, sr) {
	if (sr === osr) {
		
		

		
  geocoder.geocode({'address': address}, function(results, status) {
	  
	  	console.log(nums+" "+nums_c);
		if (nums_c < nums) {
		$(".loading2").css("display", "inline-block");
		$(".load_text").html(nums_c+" із "+nums);
		} else {
		$(".loading2").css("display", "none");
		$(".load_text").html("");
		}
		if (nums-1 === nums_c){
		$(".loading2").css("display", "none");
		$(".load_text").html("");
		}		
		
    if (status === google.maps.GeocoderStatus.OK) {
      resultsMap.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
        map: resultsMap,
        center: results[0].geometry.location,
		position: results[0].geometry.location,
		title: address
      });
	  resultsMap.setZoom(11);
	  nums_c++;
	  console.log(address+" -- OK");
	  
	  
      
    } else {
		if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
			
			timer = setTimeout( function () {
		    geocodeAddress(geocoder, map, address, sr);
		
		
		
			}, 4000);
		
		
      //alert('Geocode was not successful for the following reason: ' + status);
		} else {
		console.log(address+" -- NO");
        nums_c++;		
		}
    }
  });
}
}