(function ($) {
	"use strict";
	
	$(document).ready(function () {
		qodefGinevraSwitchNavMenu.init();
	});
	
	/**
	 * Function object that represents vertical menu area.
	 * @returns {{init: Function}}
	 */
	var qodefGinevraSwitchNavMenu = {
		calcDropdown: function($ginevraSwitchMenuObject) {
			var $menu = $ginevraSwitchMenuObject.find('.qodef-header-ginevra-switch-navigation'),
				$dropdowns = $menu.find('.qodef-drop-down-second').children().children('.sub-menu'),
				top = 0;

			if ($dropdowns.length) {
				$dropdowns.each(function(){
					var $thisDropdown = $(this),
						$subMenu = $thisDropdown.find('.sub-menu'),
						$items = $thisDropdown.children().children('a'),
						maxWidth = 0,
						translation = 0;

					top = ($thisDropdown.parent().offset().top - $menu.offset().top) + 10;

					$items.each(
						function () {
							var $this = $(this);

							//470 is max width of parent holder
							if ( 470 <= $this.width() ) {
								$this.addClass('qodef-wider');
								maxWidth = 470;

							} else if ( maxWidth < $this.width() ) {
								maxWidth = $this.width();
							}
						}
					);

					translation = $thisDropdown.parent().width() - maxWidth;

					$thisDropdown.css({'top': -top,'width': maxWidth, 'transform': 'translateX(' + translation + 'px)'});

					$thisDropdown.prepend('<li class="qodef-menu--back"><a href="#"></a></li>');

					if ($subMenu.length) {
						$subMenu.each(function () {
							var $thisSub = $(this),
								$items = $thisSub.children().children('a'),
								maxWidth = 0,
								translation = 0;

							top = $thisSub.parent().offset().top - $menu.offset().top;

							$items.each(
								function () {
									var $this = $(this);

									//470 is max width of parent holder
									if ( 470 <= $this.width() ) {
										$this.addClass('qodef-wider');
										maxWidth = 470;

									} else if ( maxWidth < $this.width() ) {
										maxWidth = $this.width();
									}
								}
							);

							translation = $thisSub.parent().width() - maxWidth;

							$thisSub.css({'top': -top,'width': maxWidth, 'transform': 'translateX(' + translation + 'px)'});

							$thisSub.prepend('<li class="qodef-menu--back"><a href="#"></a></li>');
						});
					}
				});
			}

		},
		dropdownClickToggle: function ($ginevraSwitchMenuObject) {
			var $menuItems = $ginevraSwitchMenuObject.find('.qodef-header-ginevra-switch-navigation ul li.menu-item-has-children'),
				$backItems = $ginevraSwitchMenuObject.find('.qodef-menu--back');

			$menuItems.each(function () {
				var $menuItem = $(this),
					$dropdownOpener = $(this).find('> a');

				$dropdownOpener.on('click tap', function (e) {
					e.preventDefault();
					e.stopPropagation();

					$menuItem.siblings().addClass('qodef-menu-sibling--open');
					$menuItem.addClass('qodef-menu-item--open');
				});
			});

			$backItems.each(function () {
				var $backItem = $(this),
					$dropdownClose = $backItem.find('a'),
					$parentMenuItem = $backItem.closest('.menu-item-has-children');

				$dropdownClose.on('click tap', function (e) {
					e.preventDefault();
					e.stopPropagation();

					$parentMenuItem.siblings().removeClass('qodef-menu-sibling--open');
					$parentMenuItem.removeClass('qodef-menu-item--open');
				});
			})
		},
		initNavigationAreaScroll: function ($ginevraSwitchNavigation) {
			if (typeof qodefCore.qodefPerfectScrollbar === 'object') {
				qodefCore.qodefPerfectScrollbar.init($ginevraSwitchNavigation);
			}
		},
		init: function () {
			var $ginevraSwitchMenuObject = $('.qodef-header--ginevra-switch #qodef-page-header');

			if ($ginevraSwitchMenuObject.length) {
				qodefGinevraSwitchNavMenu.calcDropdown($ginevraSwitchMenuObject);
				qodefGinevraSwitchNavMenu.dropdownClickToggle($ginevraSwitchMenuObject);
				qodefGinevraSwitchNavMenu.initNavigationAreaScroll($ginevraSwitchMenuObject.find('.qodef-header-ginevra-switch-navigation'));
			}
		}
	};
	
})(jQuery);
