(function($) {
	'use strict';
	
	var touchDevice = false;	
	var origW;
    
    /*window.onbeforeunload = function () {
        window.scrollTo(0, 0);
    };*/
	
	$(document).ready(function() {
		touchTest();
		orientationWarning();
		initMenu();
		//resizeHeaderPadding();
		resizeFullDimensions();
		
		origW = $(window).width();
	});
	
	$(window).on('load', function() {
		//resizeHeaderPadding();
        resizeFullDimensions();
        if ($('.chronoforms.semanticui-body').length > 0) {
            removeSemantic();
        } else {
            closeLoader();
        }
		equalHeight();
        scrollReveal();
	});
	
	$(window).on('resize', function() {
		fixedScroll();
        orientationWarning();
		//resizeHeaderPadding();
		resizeFullDimensions();
        scrollMenu();
		
		origW = $(window).width();
		equalHeight();
        scrollReveal();
	});
    
    $(window).on('scroll', function() {
		fixedScroll();
		scrollMenu();
		activeAnchor();
        scrollReveal();
    });
	
	function touchTest() {
		var testExp = new RegExp('Android|webOS|iPhone|iPad|' +
										'BlackBerry|Windows Phone|'  +
										'Opera Mini|IEMobile|Mobile' , 
										'i');
		if (!testExp.test(navigator.userAgent)) {
			touchDevice = false;
			$('body').addClass('non-touch');
		}else{
			touchDevice = true;
			$('body').addClass('touch-me');
		}
	}
	
	function orientationWarning(){
		var landscape = (window.innerHeight > window.innerWidth) ? false : true;
		var small  = (window.innerHeight > 500) ? false : true;
		if( landscape ){
			$('body').addClass('landscape').removeClass('portrait');
		}else{
			$('body').addClass('portrait').removeClass('landscape');
		}
		if( small ){
			$('body').addClass('small');
		}else{
			$('body').removeClass('small');
		}
	}
	
	function initMenu() {
		$('.burger-menu').on('click', function() {
			$('body').toggleClass('menu-open');
		});
		$('#mobile-menu ul li a').on('click', function() {
			$('body').toggleClass('menu-open');
		});
		$('body a[href]').on('click',function(e){
			var a_href = $(this).attr('href');
			a_href = (a_href.indexOf(location.hostname)!==-1) ? a_href.split(location.hostname)[1] : a_href;
			var l_href = (window.location.href.indexOf(location.hostname)!== -1) ? window.location.href.split(location.hostname)[1] : window.location.href;
			l_href = (l_href.indexOf('#')!==-1) ? l_href.split('#')[0] : l_href;
			if(a_href.indexOf('#') !== -1 && (a_href.split('#')[0] === l_href || a_href.split('#')[0] === '') ){
				e.preventDefault();
				$('html, body').animate({ scrollTop: $('#'+a_href.split('#')[1]).offset().top }, 500);
			}
			if( $('body').hasClass('menu-open') ){
				$('#menu-btn').click(); //Close menu
			}
		});
		
		$('.read-more[data-open]').each(function(){
			$(this).on('click', function(e){
				e.preventDefault();
				var findToOpen = $(this).attr('data-open');
				if( !$('#'+findToOpen).hasClass('open') ){ 
					$('html, body').animate({ scrollTop: $('#'+findToOpen).offset().top - 200 }, 500);
				}
				$('#'+findToOpen).height( $('#'+findToOpen+' > .inner').outerHeight() );
				$('#'+findToOpen).toggleClass('open');
				$(this).toggleClass('open');
				$(this).addClass('hidden');
			});
		});
		scrollMenu();
	}
	
	function equalHeight() {
        /*
		$('section.columns-block').each(function(){
			var findClass = document.getElementsByClassName('equal-height');
			var tallest = 0;
			var i = '';
			// Loop over matching divs
			for(i = 0; i < findClass.length; i++)
			{
				var ele = findClass[i];
				var eleHeight = ele.offsetHeight;
				tallest = (eleHeight>tallest ? eleHeight : tallest);
			}
			for(i = 0; i < findClass.length; i++)
			{
				findClass[i].style.height = tallest + "px";
			}
		});
        */
	}

    function scrollReveal() {
		$('.reveal:not(.revealed)').each(function () {
			var extra = 0;
			if ($(window).scrollTop() + $(window).height() >= $(this).offset().top + extra) {
				$(this).addClass('revealed');
			}
		});
	}
    
	function fixedScroll() {
		var windowTop = $(window).scrollTop();
		//var childnum = 1;
		$('.fixed-box-wrap').each(function(){
			if($(window).width() > 768 ){
				$(this).css('height', 'auto');
				$(this).css('height', ($(this).parent().find('.scroll-past').outerHeight()));
				//let numb = $('.fixed-menu-bar a').length;
				var headerHeight = $('header').outerHeight();
				var boxTop = $(this).offset().top - headerHeight;
				var boxHolderHeight = $(this).parent().find('.scroll-past').outerHeight();
				var fixedBox = $(this).children('.fixed-box');
				var boxHeight = fixedBox.outerHeight();
				var boxStop = boxHolderHeight - boxHeight;
				if (windowTop >= boxTop) {
					fixedBox.addClass('fixed');
				}else{
					fixedBox.removeClass('fixed');
				}
				if(windowTop >= boxTop + boxStop){
					fixedBox.addClass('scrolled');
				}else{
					fixedBox.removeClass('scrolled');
				}
			}
			if($(window).width() < 768 ){
				$(this).css('height', 'auto');
			}
		});
	}
	function activeAnchor() {
		/*var navNum = 0;
		$('.fixed-box-wrap > .fixed-box.fixed').each(function(){			
			navNum++;
			if ($('.has-nav'+navNum+'').visible(true)) {
				$('.fixed-box-wrap .fixed-menu-bar > a:nth-of-type('+navNum+')').addClass('active');
			} else {
				$('.fixed-box-wrap .fixed-menu-bar > a:nth-of-type('+navNum+')').removeClass('active');
			}
		});*/
	}

    
	function scrollMenu() {
		if ($(window).scrollTop() > parseInt($('main').css('padding-top'))) {
			$('body').addClass('menu-min');
		} else {
			$('body').removeClass('menu-min');
		}
		
		$('#comp-content > div[data-logo]').each(function(){
			//console.log($(this).attr('data-logo'), $(this).offset().top, $(this).offset().top + $(this).height(), $(window).scrollTop()  )
			if( $(this).offset().top <= $(window).scrollTop() + 100 && $(this).offset().top + $(this).height() > $(window).scrollTop() ){
				$('body').attr( 'data-logo', $(this).attr('data-logo') + '-logo');
			}
		});
		
		$('#comp-content > div[data-logo]').each(function(){
			//console.log($(this).attr('data-logo'), $(this).offset().top, $(this).offset().top + $(this).height(), $(window).scrollTop()  )
			if( $(this).offset().top <= $(window).scrollTop() + 100 && $(this).offset().top + $(this).height() > $(window).scrollTop() ){
				$('body').attr( 'data-logo', $(this).attr('data-logo') + '-logo');
			}
		});
		
		/*$('#comp-content > section > div.fix-scroll').each(function(){			
			var $fixed_element = $(".fix-scroll");
			if($(this).parent().offset().top <= $(window).scrollTop()){
				if($(window).width() > 768 ){
					$(this).addClass('fixed-inside');
				}else{
					$(this).removeClass('fixed-inside');
				}
				if($fixed_element.length){
					var $offset = $(".scroll-stop").position().top,
						 $wh = $(window).innerHeight(),
						 $diff = $offset - $wh,
						 $scrolled = $(window).scrollTop();
					$fixed_element.css("bottom", Math.max(0, $scrolled-$diff));
				}
			}
		});*/
				
        function whereToBuy(){
            if( $('#purchaseLinks').hasClass('hidden') ){
               $('#purchaseLinks').removeClass('hidden');
            }else{
                $('#purchaseLinks').addClass('hidden');
            }
        }
        
/*		$('#comp-content > section > div.fix-scroll').each(function(){
			//Enable this for locked blocks on Mobile.
			//$(this).siblings().css('paddingTop',$(this).height());
			
			if($(this).parent().offset().top <= $(window).scrollTop() && $(this).parent().offset().top + $(this).parent().height() > $(window).scrollTop() ){
				if( $(window).width() < 768 ){
					var lastChildPos = ( $(this).siblings().first().find('div.divblock').length > 1 ) ? $(this).siblings().first().find('div.divblock:last-child').offset().top : $(this).siblings().first().find('div.divblock > .inner:last-child').offset().bottom;
					if( lastChildPos - $(window).height()/1 < $(window).scrollTop() ){
						$(this).removeClass('fixed-inside');
					}else{
						$(this).addClass('fixed-inside');
					}
					//Mobile
					//$(this).parent().css('paddingTop',$(this).height());
					//$(this).parent().find('.block').css('minHeight', $(window).height() - $(this).height() );
				}else{
					$(this).addClass('fixed-inside');
					if( $(this).parent().offset().top + $(this).parent().height() < $(window).scrollTop() + $(window).height()/1 ){
						$(this).addClass('locked1');
					}else{
						$(this).removeClass('locked1');
					}
				}
			}else{
				$(this).removeClass('fixed-inside');
			}
		});*/
		
    }
	
	function resizeHeaderPadding() {
		if (!$('body').hasClass('home')) {
			$('main').css('paddingTop', $('#header-wrap').outerHeight() + 'px');
		}
	}
	
	function resizeFullDimensions() {
		// Sort mob/tab/smldesk/desk dimensions later when supplied responsive design work
		$('.desk-full-height').each(function() {
			if ($(window).width() >= 1200) {
				var newH = getFullH($(this));
				$(this).height(newH + 'px');
			} else {
				if (!!$(this).removeAttr('style')) { 
					$(this).removeAttr('style');
				}
			}
		});
		
		$('.full-height').each(function() {
			var newH = getFullH($(this));
			$(this).height(newH + 'px');
		});
	}
	
	function getFullH(el) {
		var newH = $(window).height() - parseInt($('main').css('paddingTop'));
		if (!!el.attr('style')) {
			el.removeAttr('style');
		}
		if (el.outerHeight() > newH) {
			newH = el.outerHeight();
		}
		
		return newH;
	}
    
    function removeSemantic() {
        var interval = '';
        interval = setInterval(function() {
            if ($('.chronoforms.semanticui-body').length > 0) {
                $('.chronoforms.semanticui-body').removeClass('semanticui-body');
                //$('.chronoforms .ui').removeClass('ui');
                //$('.chronoforms button').removeClass('button');
                closeLoader();
                clearInterval(interval);
                $(window).trigger('resize');
            }
        }, 200);
    }
	
    function closeLoader() {
		$('body').addClass('loaded');
		//Scroll to place on page if request made
		var hash = window.location.hash.substr(1)
		if(typeof(hash) !== 'undefined' && hash !== ''){
			$('html, body').animate({ scrollTop: $('#'+hash).offset().top -100 }, 500);
		}
	}
	
	
})(jQuery);