var smartAutocmpleteLoad = false;function Autocomplete(options) {this.el = options.elem;this.parent = options.parent;this.slogan_text = options.slogan_text;this.slogan_use = options.slogan_use;this.listEl = document.createElement('ul');this.listEl.classList.add('autocomplete');this.parent.appendChild(this.listEl);this.re = options.re;this.index = -1;this.values = [];this.el.addEventListener('keyup', this.checkKey.bind(this));this.el.addEventListener('focus', this.onFocus.bind(this), true);this.listEl.addEventListener('click', this.addFromClick.bind(this));}var autocompleteAction = {onFocus: function (e) {var value = this.el.value;if(value[value.length - 1] === ',' || value.length < 2){return}this.el.value += ',';},checkKey: function (e) {var value = e.target.value;if (value.length < 2) {this.listEl.innerHTML = '';this.values = [];return;}if (e.keyCode == 38 || e.keyCode == 40) {this.listScroll(e.keyCode);return;}var parseVal = this.strParse(value);if(parseVal.length > 1){var re = new RegExp(this.re.p1 + parseVal + this.re.p2, "gi");var res = Streets.match(re);}this.createList(res, parseVal);},strParse: function (str) {var index = str.lastIndexOf(',');if(index == -1) {this.values[0] = str;this.addStatistics(str, 'hands', 0);return str;}this.values = str.split(',');var lastIndex = this.values.length - 1;if(this.values[lastIndex][0] === ' '){this.values[lastIndex] = this.values[lastIndex].slice(1);}this.addStatistics(this.values[lastIndex], 'hands', lastIndex);return this.values[lastIndex];},addStatistics: function (value, name, index) {var text = this.slogan_text.value.split(',');var use = this.slogan_use.value.split(',');if(text.length){text[index] = value;use[index] = name;}this.slogan_text.value = text.join(',');this.slogan_use.value = use.join(',');},output: function () {var str = '';for (var i = 0, len = this.values.length; i < len; i++) {if(len > i+1){str += this.values[i] + ',';}else {str += this.values[i];}}this.el.value = str;},listScroll: function (key) {var list = this.listEl.children;var len = list.length;if (!len)return;if(key == 40 && this.index == len-1 || key == 38 && this.index < 0)return;if(this.index >= 0)list[this.index].classList.remove('active');key > 39 ? ++this.index : --this.index;if(this.index < 0)return;var li = list[this.index];li.classList.add('active');this.values[this.values.length - 1] = li.textContent;this.output();},createList: function (data, chunck) {var list = '';var re = new RegExp(chunck, "i");if(data){for (var i = 0, len = data.length; i < len; i++) {var str = data[i].slice(1, -1);str = this.slicing(str, re);list += '
  • ' + str + '
  • ';}}this.listEl.innerHTML = list;this.index = -1;},slicing: function (str, re) {var res = str.replace(re, function (item, offset) {if(offset === 0){var arr = item.split();arr[0].toUpperCase();return '' + arr.join() + '';}else {return '' + item + '';}});return res;},addFromClick: function (e) {var target = e.target;var li = null;if(target.tagName === 'SPAN'){li = target.parentNode;}else if(target.tagName === 'LI') {li = target;}else {return;}var text = li.textContent;this.values[this.values.length - 1] = text;this.output();this.listEl.innerHTML = '';this.addStatistics(text, 'autocomplete', this.values.length - 1);},addSample: function (e) {var el = e.target;if(el.tagName !== 'SPAN'){return}var text = el.textContent;this.values.push(text);this.output();this.addStatistics(text, 'sample', this.values.length - 1);}};Autocomplete.prototype = autocompleteAction;var topComplete = new Autocomplete({elem: document.getElementById('sample_field'),parent: document.getElementById('form_top'),re: {p1: '"[^"]*?', p2: '(.*?)"'},slogan_use: document.getElementById('slogan_use'),slogan_text: document.getElementById('slogan_text')});function ajaxService(url, options) {var method = options.method || 'GET';var data = options.data || '';var xhr = new XMLHttpRequest();xhr.open(method, url, true);xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');xhr.send(data);xhr.onload = function () {if(xhr.status !== 200){console.error('Ошибка' + xhr.status);}else {options.cb(xhr.responseText);}}}function initSmartAutocomplete() {var searchField = document.getElementById('smart-search-address-field');if(searchField){var smartComplete = new Autocomplete({elem: searchField,parent: document.getElementById('complete_box'),re: {p1: '"', p2: '(.*?)"'},slogan_use: document.getElementById('smart-search-sample-use'),slogan_text: document.getElementById('smart-search-sample-text')});var smartSample = document.getElementById('smart-search-sample');if(smartSample){ smartSample.addEventListener('click', smartComplete.addSample.bind(smartComplete))}smartAutocmpleteLoad = true;} else {var openBtns = document.body.getElementsByClassName('js_open_search');if(openBtns){for (var i = 0; i < openBtns.length; i++) {openBtns[i].addEventListener('click', getHtmlSmartAutocomplete);}}}}function getHtmlSmartAutocomplete(e) {if(smartAutocmpleteLoad){return}var resPut = document.getElementById('search-result');var boxRes = document.getElementById('search-result-box');var open = document.getElementById('open-smart-search-box');ajaxService('/get_html/live_serch_mass/', {cb: function (data) {var box = document.createElement('div');box.setAttribute('id', 'smart-search-box');box.innerHTML = data;document.body.insertBefore(box, open);document.body.removeChild(open);var openMap = document.getElementById('load_map');if(openMap){openMap.addEventListener('click', loadMap)}var searchField = document.getElementById('smart-search-address-field');var smartComplete = new Autocomplete({elem: searchField,parent: document.getElementById('complete_box'),re: {p1: '"', p2: '(.*?)"'},slogan_use: document.getElementById('smart-search-sample-use'),slogan_text: document.getElementById('smart-search-sample-text')});if(boxRes){var parent = boxRes.parentNode;parent.removeChild(boxRes);}if(resPut){var text = resPut.textContent;smartComplete.values.push(text);smartComplete.output();smartComplete.addStatistics(text, 'iSearch', smartComplete.values.length - 1);}var smartSample = document.getElementById('smart-search-sample');if(smartSample){ smartSample.addEventListener('click', smartComplete.addSample.bind(smartComplete))}}});smartAutocmpleteLoad = true;}function loadMap() {var btn = document.getElementById('load_map');var script = document.createElement('script');script.src = '/js/map.js';document.body.appendChild(script);script.onload = function () {toggleMap();btn.addEventListener('click', toggleMap);};btn.removeEventListener('click', loadMap);}function init() {var sample = document.getElementById('sample');sample.addEventListener('click', topComplete.addSample.bind(topComplete));initSmartAutocomplete();var lat = document.getElementById('lat');if(lat && lat.value){loadMap();}else {var openMap = document.getElementById('load_map');if(openMap){openMap.addEventListener('click', loadMap)}}}init();