/**
 * добавление на яндекс-карту кнопок управления зумом и типом карты
 * @param object map
 */

var IS_HTTPS = (location.protocol.replace(/:/,'') === 'https');
var HTTP_S = location.protocol.replace(/:/,'');
if (typeof(userDevice)=='undefined') { var userDevice = ''; }

function createZoomControl(map){
	this.map = map;
	var that = this;
	this.plusUI = document.createElement('div');
	this.plusUI.title = 'Увеличить';
	this.plusUI.style.margin = '10px';
	this.plusUI.style.background = 'url("'+HTTP_S+'://www.tourister.ru/images/spritenext.svg")';
	if (userDevice=='mobile') {
		this.plusUI.style.backgroundPosition = '-481px -1480px'; //default
		this.plusUI.style.backgroundSize = '592px 2368px';
		this.plusUI.style.width  = '37px'; this.plusUI.style.height = '37px';
	} else {
		this.plusUI.style.backgroundPosition = '-338px -1040px'; //default
		this.plusUI.style.backgroundSize = '416px 1664px';
		this.plusUI.style.width  = '26px'; this.plusUI.style.height = '26px';
	}
	//клик по плюсу
	this.plusUI.addEventListener("click", function() { if (that.map.getZoom()<that.map.getMaxZoom()) { that.map.setZoom(that.map.getZoom()+1); } 	}, false);
	//hover
	this.plusUI.addEventListener("mouseover", function() { that.plusUI.style.backgroundPosition = (userDevice=='mobile' ? '-481px -1554px' : '-338px -1092px'); }, false);
	this.plusUI.addEventListener("mouseout", function() { that.plusUI.style.backgroundPosition = (userDevice=='mobile' ? '-481px -1480px' : '-338px -1040px'); }, false);

	this.minusUI = document.createElement('div');
	this.minusUI.title = 'Уменьшить';
	this.minusUI.style.margin = '10px';
	this.minusUI.style.background = 'url("'+HTTP_S+'://www.tourister.ru/images/spritenext.svg")';
	if (userDevice=='mobile') {
		this.minusUI.style.backgroundPosition = '-518px -1480px'; //default
		this.minusUI.style.backgroundSize = '592px 2368px';
		this.minusUI.style.width  = '37px'; this.minusUI.style.height = '37px';
	} else {
		this.minusUI.style.backgroundPosition = '-364px -1040px'; //default
		this.minusUI.style.backgroundSize = '416px 1664px';
		this.minusUI.style.width  = '26px'; this.minusUI.style.height = '26px';
	}
	//клик по минусу
	this.minusUI.addEventListener("click", function() { if(that.map.getZoom()>that.map.getMinZoom()) { that.map.setZoom(that.map.getZoom()-1); } }, false);
	//hover
	this.minusUI.addEventListener("mouseover", function() { that.minusUI.style.backgroundPosition = (userDevice=='mobile' ? '-518px -1554px' : '-364px -1092px'); }, false);
	this.minusUI.addEventListener("mouseout", function() { that.minusUI.style.backgroundPosition = (userDevice=='mobile' ? '-518px -1480px' : '-364px -1040px'); }, false);

	this.setPosition();
	
	return this;
}
createZoomControl.prototype = {
	// переменные //
	'map': null,
	'controlDiv': null,
	'plusUI': null,
	'minusUI': null,
	// функции //
	'show': function() { this.controlDiv.style.display = 'block'; },
	'hide': function() { this.controlDiv.style.display = 'none'; },
	'setPosition': function() { 
		var that = this; 
		var zoomControl = L.Control.extend({ 
			options: { position: 'topright' }, //control position - allowed: 'topleft', 'topright', 'bottomleft', 'bottomright' 
			onAdd: function (map) {
				var container = L.DomUtil.create('div', '');
				container.style.padding = '5px';
				container.style.cursor = 'pointer';
				container.style.margin = '0px';
				container.appendChild(that.plusUI);
				container.appendChild(that.minusUI);
				//disable double click and double right click zoom in/out
				L.DomEvent.on(container, 'click', L.DomEvent.stopPropagation);
 			 	L.DomEvent.disableClickPropagation(container);
				L.DomEvent.on(container, 'contextmenu', L.DomEvent.stopPropagation);
				that.controlDiv = container; 
				//container.onclick = function(){ console.log('buttonClicked'); }
				return container;
			}, 
		});
		that.map.addControl(new zoomControl());
	}
};

/**
 * добавление на яндекс-карту кнопок выбора типа карты
 * @param object map
 * @param string position
 * @param bool expand_btn - надо ли кнопку "развернуть карту"
 */
function createTypeControl(map, expand_btn, buttonz_together, close_btn){
	//надо подключить /js/leaflet/leaflet-providers.js на страницу
	if (typeof(L.tileLayer.provider) == 'undefined' || typeof(L.tileLayer.provider) != 'function') { return false; }
	var that = this; that.map = map;


	//кнопка "карта"
	this.typeMapUI = document.createElement('div');
	this.typeMapUI.style.cssFloat = 'left';
	this.typeMapUI.title = 'Карта';
	if (typeof(buttonz_together)!='undefined' && buttonz_together === true) {
		this.typeMapUI.style.margin = '10px 0';
	} else {
		this.typeMapUI.style.margin = '10px';
	}
	if (userDevice!='mobile') this.typeMapUI.style.marginRight = '0px';
	this.typeMapUI.style.background = 'url("'+HTTP_S+'://www.tourister.ru/images/spritenext.svg")';
	if (userDevice=='mobile') {
		this.typeMapUI.style.backgroundPosition = '-124px -1480px'; //default
		this.typeMapUI.style.backgroundSize = '592px 2368px';
		this.typeMapUI.style.width  = '122px';
		this.typeMapUI.style.height = '37px';
	} else {
		this.typeMapUI.style.backgroundPosition = '-87px -1040px'; //default
		this.typeMapUI.style.backgroundSize = '416px 1664px';
		this.typeMapUI.style.width  = '86px';
		this.typeMapUI.style.height = '26px';
	}
	//hover
	this.typeMapUI.addEventListener("mouseover", function() { that.typeMapUI.style.backgroundPosition = (userDevice=='mobile' ? '-124px -1517px' : '-87px -1066px'); }, false);
	this.typeMapUI.addEventListener("mouseout", function() { that.checkType(); }, false);
	//клик по кнопке "карта"
	this.typeMapUI.addEventListener("click", function() { 	
		that.activeMap = 'default';
		if (that.map.hasLayer(TileLayerSatellite)) {
			that.map.removeLayer(TileLayerSatellite);
			that.map.removeLayer(TileLayerSatelliteMarks);
		}
		that.map.addLayer(TileLayerDefault); 
		that.checkType();
	}, false);

	//кнопка "спутник"
	this.typeSputnicUI = document.createElement('div');
	this.typeSputnicUI.style.cssFloat = 'left';
	this.typeSputnicUI.title = 'Спутник';
	if (typeof(buttonz_together)!='undefined' && buttonz_together === true) {
		this.typeSputnicUI.style.margin = '10px 0';
	} else {
		this.typeSputnicUI.style.margin = '10px';
	}
	if (userDevice!='mobile') this.typeSputnicUI.style.marginLeft = '0px';
	this.typeSputnicUI.style.background = 'url("'+HTTP_S+'://www.tourister.ru/images/spritenext.svg")';
	if (userDevice=='mobile') {
		this.typeSputnicUI.style.backgroundPosition = '-272px -1480px'; //default
		this.typeSputnicUI.style.backgroundSize = '592px 2368px';
		this.typeSputnicUI.style.width  = '122px';
		this.typeSputnicUI.style.height = '37px';
	} else {
		this.typeSputnicUI.style.backgroundPosition = '-191px -1040px'; //default
		this.typeSputnicUI.style.backgroundSize = '416px 1664px';
		this.typeSputnicUI.style.width  = '86px';
		this.typeSputnicUI.style.height = '26px';
	}
	//hover
	this.typeSputnicUI.addEventListener("mouseover", function() { that.typeSputnicUI.style.backgroundPosition = (userDevice=='mobile' ? '-272px -1517px' : '-191px -1066px'); }, false);
	this.typeSputnicUI.addEventListener("mouseout", function() { that.checkType(); }, false);
	//клик по кнопке "спутник"
	this.typeSputnicUI.addEventListener("click", function() { 
		that.activeMap = 'satellite';
		that.map.addLayer(TileLayerSatellite);
		that.map.addLayer(TileLayerSatelliteMarks);
		if (that.map.hasLayer(TileLayerDefault)) {
			that.map.removeLayer(TileLayerDefault);
		} 
		that.checkType();
	}, false);

	//this.checkType();
	this.activeMap = 'default';
	this.typeMapUI.style.backgroundPosition = (userDevice=='mobile' ? '-124px -1554px' : '-87px -1092px'); //select
	this.typeSputnicUI.style.backgroundPosition = (userDevice=='mobile' ? '-272px -1480px' : '-191px -1040px'); //default

	//ежели надо кнопку-крестик "закрыть карту"
	if (typeof(close_btn)!='undefined' && close_btn === true) {
		this.closeMapUI = document.createElement('div');
		this.closeMapUI.style.cssFloat = 'left';
		this.closeMapUI.title = 'Свернуть карту';
		this.closeMapUI.style.margin = '10px'; //думаю кнопка закрытия карты не должна же прилипать к другим
		this.closeMapUI.style.backgroundColor = '#fff';
		this.closeMapUI.style.padding = '7px';
		this.closeMapUI.innerHTML = '<div style="background:url(\'https://www.tourister.ru/images/spritenext.svg\') no-repeat -277px -384px /341px 1365px; width:22px; height:22px;"></div>';
		//клик 
		this.closeMapUI.addEventListener("click", function(){ that.closeMap(); }, false);
	}

	//ежели надо кнопку "развернуть карту"
	if (typeof(expand_btn)!='undefined' && expand_btn === true) {
		//кнопка "развернуть" 	
		this.expandMapUI = document.createElement('div');
		this.expandMapUI.style.cssFloat = 'left';
		this.expandMapUI.title = 'Развернуть карту на весь экран';
		this.expandMapUI.style.margin = '10px';
		this.expandMapUI.style.background = 'url("https://www.tourister.ru/images/spritenext.svg")';
		if (userDevice=='mobile') {
			this.expandMapUI.style.backgroundPosition = '-407px -1480px'; //default
			this.expandMapUI.style.backgroundSize = '592px 2368px';
			this.expandMapUI.style.width  = '37px'; this.expandMapUI.style.height = '37px';
		} else {
			this.expandMapUI.style.backgroundPosition = '-286px -1040px'; //default
			this.expandMapUI.style.backgroundSize = '416px 1664px';
			this.expandMapUI.style.width  = '26px'; this.expandMapUI.style.height = '26px';
		}
		
		//клик 
		this.expandMapUI.addEventListener("click", function(){ 
			that.expandMapUI.style.display = 'none'; that.unExpandMapUI.style.display = 'block'; that.expandMap();
			jQuery('#closemap_big').show().css({ 'left' : (jQuery(window).width()-160)/2+'px' }); 
		}, false);

		//hover
		this.expandMapUI.addEventListener("mouseover", function() { that.expandMapUI.style.backgroundPosition = (userDevice=='mobile' ? '-407px -1554px' : '-286px -1092px'); }, false);
		this.expandMapUI.addEventListener("mouseout", function(){ that.expandMapUI.style.backgroundPosition = (userDevice=='mobile' ? '-407px -1480px' : '-286px -1040px'); }, false);
		
		//кнопка "свернуть"
		this.unExpandMapUI = document.createElement('div');
		this.unExpandMapUI.style.cssFloat = 'left';
		this.unExpandMapUI.title = 'Свернуть карту';
		this.unExpandMapUI.style.display = 'none';
		this.unExpandMapUI.style.margin = '10px';
		this.unExpandMapUI.style.background = 'url("'+HTTP_S+'://www.tourister.ru/images/spritenext.svg")';
		if (userDevice=='mobile') {
			this.unExpandMapUI.style.backgroundPosition = '-444px -1480px'; //default
			this.unExpandMapUI.style.backgroundSize = '592px 2368px';
			this.unExpandMapUI.style.width  = '37px'; this.unExpandMapUI.style.height = '37px';
		} else {
			this.unExpandMapUI.style.backgroundPosition = '-312px -1040px'; //default
			this.unExpandMapUI.style.backgroundSize = '416px 1664px';
			this.unExpandMapUI.style.width  = '26px'; this.unExpandMapUI.style.height = '26px';
		}
		//клик по кнопке "свернуть" 
		this.unExpandMapUI.addEventListener("click", function() { 
			that.unExpandMapUI.style.display = 'none'; that.expandMapUI.style.display = 'block'; that.unExpandMap(); 
			jQuery('#closemap_big').hide(); 
		}, false);
		
		//hover
		this.unExpandMapUI.addEventListener("mouseover", function() { that.unExpandMapUI.style.backgroundPosition = (userDevice=='mobile' ? '-444px -1554px' : '-312px -1092px'); }, false);
		this.unExpandMapUI.addEventListener("mouseout", function() { that.unExpandMapUI.style.backgroundPosition = (userDevice=='mobile' ? '-444px -1480px' : '-312px -1040px'); }, false);
	}
	
	this.setPosition();
	return this;
}
createTypeControl.prototype = {
	// переменные //
	'map': null,
	'controlDiv': null,
	'activeMap': null,
	'typeSputnicUI': null,
	'typeMapUI': null,
	'closeMapUI': null,
	'expandMapUI': null,
	'unExpandMapUI': null,
	'HeightDivMap': null,
	// функции //
	'show': function() { this.controlDiv.style.display = 'block'; },
	'hide': function() { this.controlDiv.style.display = 'none'; },
	'setPosition': function() { 
		var that = this; 
		var typeControl = L.Control.extend({ 
			options: { position: 'topright' }, //control position - allowed: 'topleft', 'topright', 'bottomleft', 'bottomright' 
			onAdd: function (map) {
				var container = L.DomUtil.create('div', '');
				container.style.padding = '5px';
				container.style.cursor = 'pointer';
				container.style.margin = '0px';
				container.appendChild(that.typeMapUI);
				container.appendChild(that.typeSputnicUI);
				if (typeof(that.expandMapUI)!='undefined' && that.expandMapUI!=null) { container.appendChild(that.expandMapUI); }
				if (typeof(that.unExpandMapUI)!='undefined' && that.unExpandMapUI!=null) { container.appendChild(that.unExpandMapUI); }
				if (typeof(that.closeMapUI)!='undefined' && that.closeMapUI!=null) { container.appendChild(that.closeMapUI); }
				//disable double click and double right click zoom in/out
				L.DomEvent.on(container, 'click', L.DomEvent.stopPropagation);
 			 	L.DomEvent.disableClickPropagation(container);
				L.DomEvent.on(container, 'contextmenu', L.DomEvent.stopPropagation);
				that.controlDiv = container; 
				//container.onclick = function(){ }
				return container;
			}, 
		});
		that.map.addControl(new typeControl());
	},
	'checkType': function(){
		var that = this;
		if (that.activeMap == 'default') {
			that.typeMapUI.style.backgroundPosition = (userDevice=='mobile' ? '-124px -1554px' : '-87px -1092px'); //select
			that.typeSputnicUI.style.backgroundPosition = (userDevice=='mobile' ? '-272px -1480px' : '-191px -1040px'); //default
		} else if (that.activeMap == 'satellite') {
			that.typeMapUI.style.backgroundPosition = (userDevice=='mobile' ? '-124px -1480px' : '-87px -1040px'); //default
			that.typeSputnicUI.style.backgroundPosition = (userDevice=='mobile' ? '-272px -1554px' : '-191px -1092px'); //select
		}
	},
	'expandMap': function(){ console.log('Раскрытие карты. Перегрузи метод в js.'); },
	'unExpandMap': function() { console.log('Норм размер карты. Перегрузи метод в js.'); },
	'closeMap': function(){  console.log('Зарытие карты. Перегрузи метод в js.'); },
};
