(function($){
	'use strict';

	function toggleMenu (evt) {
		$('#homepageMainMenu').toggleClass('open');
	}

	function closeMenu (evt) {
		$('#homepageMainMenu').removeClass('open');
	}

	function scrollToPage( evt ) {
		var pageID = '';
		if( typeof(evt) == 'string' ) {
			pageID = evt;
		}
		else {
			evt.preventDefault();
			pageID = $(this).attr('href');
		}
		console.log(pageID);

		if( $('body').is('.mobile') ) {

			var goToPosition = $(pageID).offset().top - ((window.innerWidth < 600) ? 66 : 0);

			$('html, body').animate({
				'scrollTop': goToPosition
			}, 1500);
			return false;
		}

		window.homepageApp.sides.removeClass('prev next');
		var current = $(pageID).index(),
		direction = current > window.homepageApp.currentSide ? 1 : -1,
		cubeClass = {'-1':'up', '1':'down'},

		slideClass = direction < 0 ? 'prev' : 'next';

		if( current == window.homepageApp.currentSide ) {
			return false;
		}

		$(pageID).addClass(slideClass);

		window.homepageApp.currentSide = current;

		$( "#cubeInner" ).addClass( cubeClass[ String(direction) ] );
		window.setTimeout(function(){
			$(window).trigger('setCube');
		}, 1000);
	}

	function scrollPage (evt) {
		evt.preventDefault();

		var direction = 1,
		cubeClass = '';
		if( window.homepageApp.scroll ) {
			return false;
		}

		window.homepageApp.scroll = true;

		if( evt.type == 'mousewheel' ){
			direction = evt.originalEvent.wheelDelta < 0 ? 1 : -1;
		} else if( evt.type == 'DOMMouseScroll' ) {
			direction = evt.originalEvent.detail > 0 ? 1 : -1;
		} else if ( evt.type == "click" ) {
			var pageID = $(this).attr('href'),
			current = $(pageID).index();
			direction = current > window.homepageApp.currentSide ? 1 : -1;
		}

		cubeClass = direction < 0  ? 'up' : 'down';


		$( "#cubeInner" ).addClass(cubeClass);
		window.setTimeout(function(){
			afterScrollPage (direction);
		}, 1000);

		return false;
	}

	function mobileScrollPage (evt) {
		var currentPage = 0;

		for( var s=0; s < window.homepageApp.sides.length; s++ ){
			var slide = window.homepageApp.sides[s],
			delta = $('#homepageMainMenu').outerHeight(true) + window.innerHeight * .02,
			top = $(slide).offset().top - delta * 1.2,
			bottom = $(slide).offset().top + $(slide).outerHeight(true) - delta * 1.2;

			if( s > 0 && window.scrollY >= top && window.scrollY <= bottom ) {
				currentPage = s;
				break;
			}
		}

		if( currentPage > 0 ) {
			if( $('#main').attr('data-page-title') != $(slide).find('.entry-title').text() ) {
				$('#main').attr('data-page-title', $(slide).find('.entry-title').text());
			}
			$('#cubeInner > .page').removeClass('current').eq(s).addClass('current animate');

			$('body').addClass('innerPage');
		}
		else {
			$('body').removeClass('innerPage');
		}
	}


	function afterScrollPage (direction) {
		var current = window.homepageApp.currentSide + direction,
		sides = window.homepageApp.sides,
		maxSidesNum = sides.length-1;

		window.homepageApp.currentSide = current < 0 ? maxSidesNum : (current > maxSidesNum ? 0 : current);

		$(window).trigger('setCube');
		window.homepageApp.scroll = false;
	}



	function setSides() {
		var sides	= window.homepageApp.sides,
		current		= window.homepageApp.currentSide,
		sidesNum	= sides.length,
		next		= (current > sidesNum-2) ? 0 : current+1,
		prev		= (current > 0) ? current-1 : sidesNum-1;

		sides.removeClass('next prev current');
		sides.eq(current).addClass('current animate');
		sides.eq(next).addClass('next');
		sides.eq(prev).addClass('prev');
		$( "#cubeInner" ).removeClass('up down');

		var allVideos = $('.backgroundVideo'),
		video = sides.eq(current).find('.backgroundVideo');
		allVideos.each(function(){
			this.pause();
		})

		if( video.length > 0 ) {
			video.get(0).play();
		}

		$('#main, body').removeClass('indexSide innerPage').addClass( current == 0 ? 'indexSide' : 'innerPage' );
	}



	function screenResize(evt) {
		if( window.innerWidth < 600 ) {
			$(window)
			.on('mousewheel DOMMouseScroll', scrollPage)
			.on('setCube', setSides)
			.trigger('setCube');

			$(".iconTabs .nav-tabs").each(function() {
				if( !$(this).is('.owl-carousel') ) {
					$(this).owlCarousel({
						'singleItem'		: true,
						'pagination'		: false,
						'navigation'		: true,
						'navigationText'	: false,
						'afterMove'			: function(carousel) {
							$(carousel)
								.closest('.iconTabs')
								.find('> .tab-content > .tab-pane')
								.removeClass('active')
								.eq(this.currentItem)
								.addClass('active');
						}
					});
				}
			});
		}
		else {
			$(window)
			.off('mousewheel DOMMouseScroll', scrollPage)
			.off('setCube', setSides);
		}
	}



	function init () {
		window.homepageApp = {
			'sides'			: $('#cubeInner > .page'),
			'currentSide'	: 0,
			'scroll'		: false
		};

		$('body').addClass('loaded');

		if( $('body').is('.desktop') ) {
			$(window)
			.on('mousewheel DOMMouseScroll', scrollPage)
			.on('setCube', setSides)
			.trigger('setCube')
			.trigger('resize');
		}
		else {
			$(window)
			.on('scroll', mobileScrollPage)
			.on('resize', screenResize)
			.trigger('scroll')
			.trigger('resize');
		}

		$('#cubeInner .page').each(function() {
			var id = $(this).next().attr('id');
			$(this).find('.nextPage').attr('href', '#'+id);
		});

		$('#homepageMainMenu a, .logo, #cubeInner .nextPage').on('click', scrollToPage).on('click', closeMenu);
		$('#menuToggle').on('click', toggleMenu);

		$('.modal').on('shown.bs.modal', function(evt){
			$(this).closest('.page').addClass('modalOpen');
		}).on('hidden.bs.modal', function(evt){
			$(this).closest('.page').removeClass('modalOpen');
		});

		if(location.hash != '') {
			scrollToPage( location.hash );
		}
	}

	$(document).ready(init);
})(jQuery);
