$(function () {
determineCurrentLocation();
});
var showOverlay = function() {
$.blockUI({ message: '',
css: { backgroundColor: '', color: '', border: '' },
baseZ: '1000000',
overlayCSS: {
backgroundColor: 'white',
opacity: 0.5,
cursor: 'wait'
}
});
}
var hideOverlay = function () {
$.unblockUI();
}
function determineCurrentLocation() {
$('#address, input[name=all], .store-filter-option').on('change', function () {
reapplyStoreLocatorCheckboxesFilters(this);
var stores = $.getJSON(getStoreLocatorJsonUrl(),function(data){
showOverlay();
if (!isEmptyObject(data)){
initStoreLocator();
}else{$('.location-item').addClass('hidden');}
}).done(function() {
setTimeout(function(){hideOverlay();},500);
});
});
var profileCity = $('#location').val();
if (profileCity) {
initCitiesDropdown(profileCity);
} else {
initCitiesDropdown();
}
}
function isEmptyObject(obj) {
var name;
for (name in obj) {
return false;
}
return true;
}
// this logic is defined in main.min.js but due to ajax calls inside the
// checkout page, this is lost
function reapplyStoreLocatorCheckboxesFilters(obj) {
if ($(obj).attr('name') == "all") {
if (obj.checked) {
$('.store-filter-option').prop('checked', true);
$('.location-item').removeClass('hidden');
} else {
$('.location-item').addClass('hidden');
$('.store-filter-option').prop('checked', false);
}
} else {
var element = ($(obj).attr('name'));
if (this.checked) {
$('.' + element + '').removeClass('hidden');
} else {
$('.' + element + '').addClass('hidden');
$('[name="all"]').prop('checked', false);
}
}
}
function geolocationSuccessCallback(position) {
var currentLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude)
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': currentLatLng}, geocodeCallback);
}
function geolocationFailedCallback(error) {
// console.log("Geocode failed " + error.code + " " + error.message);
}
function geocodeCallback(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0].formatted_address) {
$('#current-location').text(results[0].formatted_address);
for (var i = 0; i < results[0].address_components.length; i++) {
for (var j = 0; j < results[0].address_components[i].types.length; j++) {
if (results[0].address_components[i].types[j] == "locality") {
var geolocCity = results[0].address_components[i].long_name;
initCitiesDropdown(geolocCity);
break;
}
}
}
}
}
}
function initCitiesDropdown(selectedCity) {
var capitalCity = 'București';
var url = '/blocks/business/storeLocator/storeLocatorJson.jsp';
if ( $('input#firstName').length > 0 ){
url = '/blocks/business/storeLocator/storeLocatorJson.jsp?isOnlyCosmote=true';
}
$.get(url,function (data) {
var citiesJSON = $.parseJSON(data);
var citiesDropdown = $('select#address');
if(!containsCity(citiesJSON, selectedCity)) {
selectedCity = capitalCity;
}
citiesDropdown.empty();
$(citiesJSON).each(function (k, v) {
citiesDropdown.append('');
});
}).done(function () {
initStoreLocator();
});
}
function containsCity(json, cityName) {
var contains = false;
$(json).each(function (k, v) {
if (v.city == cityName) {
contains = true;
return true;
}
});
return contains;
}
function initStoreLocator(all) {
var dataLocationUrl = getStoreLocatorJsonUrl(all);
$('#map-container').storeLocator({
'formID': 'user-location',
'inputID': 'address',
'autoGeocode': false,
'fullMapStart': true,
'dataType': 'json',
'dataLocation': dataLocationUrl,
'lengthUnit': 'km',
'storeLimit': 199,
'distanceAlert': -1,
'infowindowTemplatePath': '/blocks/storeLocator/infowindowDescription.jsp',
'listTemplatePath': '/blocks/storeLocator/locationListDescription.jsp',
'zoomLevel': 0,
'pinColor': '76EE00',
'slideMap': false,
'loading': true,
'loadingDiv': 'loading-div',
'listColor2':'ffffff'
});
}
function getAllStoreLocatorJsonUrl() {
var url = '/blocks/business/storeLocator/storeLocatorJson.jsp?all=true';
return url;
}
function getStoreLocatorJsonUrl(all) {
if (all == true){
return getAllStoreLocatorJsonUrl();
}
var url = '/blocks/business/storeLocator/storeLocatorJson.jsp';
var storeCity = $('#address').val();
var storeTypes="";
$('.store-filter-wrapper input:checked').each(function(){
var finalSelection = "";
if ($(this).attr('name') != "all") {
if ($(this).attr('name') == "store")
finalSelection = "cosmote";
else
finalSelection = $(this).attr('name');
storeTypes +="storeTypes="+finalSelection+"&";
}
});
storeTypes=storeTypes.slice(0,-1);
if ( $('input#firstName').length > 0 ){
return url + '?storeCity=' + storeCity + '&' + storeTypes + '&isOnlyOwn=true';
}
return url + '?storeCity=' + storeCity + '&' + storeTypes;
}
$('#loc-list').on('click', function (e) {
$.scrollTo('header h1', 200);
});