(function () {

	var id = setInterval(function () {

		Observer.subscribe && clearInterval(id);

		Observer.subscribe && Observer.subscribe('loadPage', function () {
			new JsViewer(document.querySelectorAll('img[data-fancy="on"]'));
		});


		Observer.subscribe && Observer.subscribe('loadPage', function () {

			Core.usedAjaxLoading() && window.yaCounter46989036 && yaCounter46989036.hit(document.location.href, {title: document.title, referer: (!!Cookie.get('previousUrl') ? (document.location.origin + Cookie.get('previousUrl')) : document.referrer)});
			Core.usedAjaxLoading() && window.ga && ga('send', 'pageview', {page: (document.location.href.replace(new RegExp(document.location.origin), '')), title: document.title});

			// mobile menu trigger
			document.querySelector('.menu-trigger').addEventListener('click', function () {
				document.body.classList.toggle('mobile-menu-opener');
			});

			!!document.body.classList.contains('mobile-menu-opener') && document.body.classList.remove('mobile-menu-opener');
		});

		// catalog add to cart event
		Observer.subscribe && Observer.subscribe('CatalogAddToCart', function (itemId) {
			Catalog.getItem(itemId, function (itemData) {
				window.ga && ga('send', 'event', 'cart', 'put-to-cart', itemData.category_name + ' — ' + itemData.name + ' (' + itemData.price + Language.get('Catalog', 'currency_short') + ')');
				window.yaCounter46989036 && !document.querySelector('.catalog-cart-page') && yaCounter46989036.reachGoal('put-to-cart');
			});
		});

		// catalog cart page event
		Observer.subscribe && Observer.subscribe('CatalogCartPage', function () {
			window.ga && ga('send', 'event', 'cart', 'cart-page');
			window.yaCounter46989036 && yaCounter46989036.reachGoal('cart-page');
		});


		// catalog filter event
		Observer.subscribe && Observer.subscribe('CatalogFilter', function (filter) {
			if (!filter) {
				return false;
			}

			document.querySelector('.price-range-slider') && (function () {

				var priceRange = document.querySelector('.price-range-slider'),
					filterData = {
						min: priceRange.getAttribute('data-min'),
						max: priceRange.getAttribute('data-max')
					}

				window.ga && (filterData.min !== filter['price-min']) && ga('send', 'event', 'filter', 'price-min', filter['price-min']);
				window.ga && (filterData.max !== filter['price-max']) && ga('send', 'event', 'filter', 'price-max', filter['price-max']);
			}());
		})


		var cartBlockItemsList = function () {

			Core.ajax('/catalog/get-items-cart-html/', function (json) {
				try {
					var resObj = JSON.parse(json);
				} catch (e) {
					console.error('Failed to get cart items data');
					return;
				}

				[].forEach.call(document.querySelectorAll('.cart-block .cart-list'), function (cartList) {
					cartList.innerHTML = resObj.html;
				});
			});
		}


		// update cart elements
		Observer.subscribe && Observer.subscribe('CatalogAddToCart', cartBlockItemsList);
		Observer.subscribe && Observer.subscribe('CatalogRemoveFromCart', cartBlockItemsList);
		Observer.subscribe && Observer.subscribe('CatalogClearCart', cartBlockItemsList);

		// update cart elements
		Observer.subscribe && Observer.subscribe('loadPage', cartBlockItemsList);

		

		(function () {
			document.addEventListener('click', function (event) {
			    var selectors = document.querySelectorAll('.cart-block .remove-item'),
			        element = event.target,
			        index = -1;

			    if (selectors) {

			        while (element && ((index = Array.prototype.indexOf.call(selectors, element)) === -1)) {
			            element = element.parentElement;
			        }

			        if (index > -1) {
			            (function () {
							var itemUniqId = this.getAttribute('data-item-uniq');

							[].forEach.call(document.querySelectorAll('.cart-block .cart-items-item[data-item-uniq="' + itemUniqId + '"]'), function (element) {
								element.classList.add('-removed');
							});

							Core.ajax('/catalog/cart-action/remove/' + itemUniqId + '/');
			            	Catalog.removeFromCart(itemUniqId);

			            }).call(element, event);
			        }
			    }
			});
		}());








		// catalog crete order action
		Observer.subscribe && Observer.subscribe('CatalogCreateOrder', function (orderData) {

			window.ga && (function (){

				ga('require', 'ecommerce', 'ecommerce.js');
				
				var n;

				for (n in orderData.items) {
					
					// add items to cart
					ga('ecommerce:addItem', {
					  'id': orderData.orderId.toString(),
					  'name': orderData.items[n].item_name,
					  'sku': orderData.items[n].item_id,
					  'category': orderData.items[n].category_name,
					  'price': orderData.items[n].item_price,
					  'quantity': orderData.items[n].item_count
					});
				}
				
				// create order data
				ga('ecommerce:addTransaction', {
					'id': orderData.orderId.toString(),
					'affiliation': orderData.affiliation.toString(),
					'revenue': orderData.revenue.toString(),
					'shipping': orderData.shipping.toString(),
					'tax': orderData.tax.toString(),
					'currency': orderData.currency.toString()
				});

				// register order
				ga('ecommerce:send');
			}());

			window.yaCounter46989036 && yaCounter46989036.reachGoal('create-order');
			window.ga && ga('send', 'event', 'cart', 'create-order', 'action');
		});



		// main menu marker
		Observer.subscribe && Observer.subscribe('loadPage', function () {
			/*if (document.location.pathname === '/' || document.location.pathname.match(/\/catalog\//gi)) {
				document.querySelector('#main-menu a[href="/"]').classList.add('active');
			}*/
			if (document.location.pathname.match(/\/catalog\//gi)) {
				document.querySelector('#main-menu a[href="/catalog/"]').classList.add('active');
			}
		});




		// left categories open nodes trigger
		Observer.subscribe && Observer.subscribe('loadPage', function () {

			Array.prototype.forEach.call(document.querySelectorAll('.left-cat-list .trigger'), function (trigger) {
				trigger.addEventListener('click', function () {
					this.classList.toggle('-expand');
					Array.prototype.forEach.call(this.parentNode.querySelectorAll('[data-image-src]'), function (image) {
						image.style.backgroundImage = 'url("' + image.getAttribute('data-image-src') + '")';
					});
				});
			});
		});





		//set active category





		// mobile menu submenu
		/*Observer.subscribe && Observer.subscribe('loadPage', function () {

			Array.prototype.forEach.call(document.querySelectorAll('.mobile-menu-container ul'), function (elem) {
				if (elem.parentNode && elem.parentNode.tagName === 'LI') {
					var link = elem.parentNode.querySelector('a');
					link.setAttribute('href', '#');
					
					if (!link.querySelector('.icon')) {
						var icon = document.createElement('icon');
					}

					link.addEventListener('click', function () {
						this.parentNode.querySelector('ul').classList.toggle('-visible');
					});
				}
			});
			
		});*/

		// scroll to top
		Observer.subscribe && Observer.subscribe('loadPage', function () {

			var showScroller = function () {
				var topOffset = 1000,
					topButton = document.querySelector('.top-scroller'),
					doc = document.documentElement,
				    left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0),
				    top = (window.pageYOffset || doc.scrollTop)  - (doc.clientTop || 0);


				if (top > topOffset) {
				    
				    if (!topButton.classList.contains('-visible')) {
				        topButton.classList.add('-visible');
				    }

				} else {
				    topButton.classList.remove('-visible');
				}
			}

			showScroller();
			window.addEventListener('scroll', showScroller);
			
			// click event
			document.querySelector('.top-scroller').addEventListener('click', function () {
				new JsScroller().scrollTo(document.querySelector('.cap'));
			});
		});






		// cart block trigger
		Observer.subscribe && Observer.subscribe('loadPage', function () {
			var cartBlock = document.querySelector('.cap .cart-block'),
				cartTrigger = cartBlock && cartBlock.querySelector('.cart-block-trigger');

			cartTrigger && cartTrigger.addEventListener('click', function () {
				cartBlock.classList.toggle('-opened');
			});
		});
		

		// set target to files links
		Observer.subscribe && Observer.subscribe('loadPage', function () {
			[].forEach.call(document.querySelectorAll('a[href*="."]'), function (elem) {
				(['xlsx', 'xls', 'docx', 'rtf', 'doc', 'pptx', 'ppt', 'pdf', 'zip', 'rar', 'jpg', 'jpeg', 'png'].indexOf(elem.href.split('.').pop().toLowerCase()) !== -1) && (elem.setAttribute('target', '_blank'));
			});
		});

		
		// main menu sticker
		Observer.subscribe && Observer.subscribe('loadPage', function () {
			//return;
			new JsTopper(document.querySelector('.main-menu-container'), {
				stickStartOffsetTop: 356
			});
		});






		// footer map
		Observer.subscribe && Observer.subscribe('loadPage', function () {

			Core.loadScript('https://maps.googleapis.com/maps/api/js?key=AIzaSyAA9S1lWF1sQ59vL2M7w2Vmjy2jKTYSpgo', function () {
				var mapContainer = document.querySelector('.page-footer .foot-map'),
					coords = [56.830692, 60.664240];

				    var mapOptions = {
				        zoom: 15,
				        scrollwheel: false,
				        center: new google.maps.LatLng(coords[0], coords[1]),
				    	styles: [{"featureType":"administrative","elementType":"all","stylers":[{"saturation":"-100"}]},{"featureType":"administrative.province","elementType":"all","stylers":[{"visibility":"off"}]},{"featureType":"landscape","elementType":"all","stylers":[{"saturation":-100},{"lightness":65},{"visibility":"on"}]},{"featureType":"poi","elementType":"all","stylers":[{"saturation":-100},{"lightness":"50"},{"visibility":"simplified"}]},{"featureType":"road","elementType":"all","stylers":[{"saturation":"-100"}]},{"featureType":"road.highway","elementType":"all","stylers":[{"visibility":"simplified"}]},{"featureType":"road.arterial","elementType":"all","stylers":[{"lightness":"30"}]},{"featureType":"road.local","elementType":"all","stylers":[{"lightness":"40"}]},{"featureType":"transit","elementType":"all","stylers":[{"saturation":-100},{"visibility":"simplified"}]},{"featureType":"water","elementType":"geometry","stylers":[{"hue":"#ffff00"},{"lightness":-25},{"saturation":-97}]},{"featureType":"water","elementType":"labels","stylers":[{"lightness":-25},{"saturation":-100}]}]
				    };

				    var marker = new google.maps.Marker({
				        position: new google.maps.LatLng(coords[0], coords[1]),
				        map: new google.maps.Map(mapContainer, mapOptions),
				        icon: '/public/img/gmap-marker.png',
				        title: Language.get('Core', 'foot_map_label')
				    });

			});
		});






	}, 15);








	
	

	






}());