;(function($, window, document, undefined) {
	var $win = $(window);
	var $doc = $(document);

	// Fixed Header
	function fixedHeader() {
		var winO = $win.scrollTop();
		var $header = $('.header');
		var $wrapperBody = $('.wrapper_body').offset().top;
        var isHeaderFixed = winO > $wrapperBody;

        $header.toggleClass('fixed', isHeaderFixed);
	}

	// Scroll Section
	function scrollSection() {
		$('.wrapper-primary').addClass('expanded');
    	var triggerScroll = setInterval(function() {$(window).scroll();}, 10);	
    	$('.features-grid').isotope('layout');

		setTimeout(function() {
			$('html').removeClass('alt');
			clearInterval(triggerScroll);
		}, 600);
	}

	// Section Detect Height
	function sectionDetectHeight($container) {
		var $section = $($container);
		var sectionH = $section.outerHeight();
		var sectionInnerH = $section.find('.section_inner').outerHeight();

		if (sectionH < sectionInnerH) 	$section.addClass('alt');
		else 							$section.removeClass('alt');
	}

	$('.section-welcome').on('scroll', function() {
		var $section = $(this);
		var sectionTop = $section.scrollTop();
		var sectionH = $section.outerHeight();
		var sectionInnerH = $section.find('.section_inner').outerHeight();

		if (sectionTop + sectionH >= sectionInnerH) 	$section.removeClass('alt');
		else 											$section.addClass('alt');
	});

	var ww = $(window).width();
	var mcw = $("main .container").outerWidth();
	var mo = Math.ceil(((ww-mcw)+30)/2);		
	$(".fullimg").css({"margin-left":(-1*mo)+"px", "margin-right":(-1*mo)+"px"});

		
    if ($('.wrapper-primary').length && !$('.wrapper-primary').hasClass('expanded'))    	$('html').addClass('alt');

    $('.js-btn-down').on('click', function(e) {
    	e.preventDefault();
    	$('.wrapper-primary').addClass('expanded');

		setTimeout(function() {
			$('html').removeClass('alt');
			$(window).scroll();
		}, 600);
    });


    // Mouse Wheel
    var isScrolled = false;
	$('body').on('mousewheel scroll', function(e){
		if ($('.wrapper-primary').length && !$('.wrapper-primary').hasClass('expanded') && !$('.section-welcome').hasClass('alt')) {
	        if(e.deltaY < 0 && !isScrolled) {
	        	isScrolled = true;
	            scrollSection();
	            $(window).scroll();
	        }
		}
    });

    var touchStartPos;

    $doc.on('touchstart', function(e) {
		touchStartPos = e.originalEvent.touches[0].pageY;
	}).on('touchend', function(e) {
		var touchEndPos = e.originalEvent.changedTouches[0].pageY;

		if ($('.wrapper-primary').length && !$('.wrapper-primary').hasClass('expanded') && !$('.section-welcome').hasClass('alt')) {
			if(touchEndPos < touchStartPos) {
				scrollSection();
				$(window).scroll();
			}
		}
	});

    // Mobile Navigation
    $('.btn-burger').on('click', function(e) {
    	e.preventDefault();
    	$('.header').toggleClass('expanded');
		$(this).toggleClass('active');

		if($(this).hasClass('active')) {
			$('.wrapper').scrollLock('enable');
			$('.nav-outer').scrollLock('enable');
		} 
		else 			$('.wrapper').scrollLock('disable');
    });

	// Animations on scroll
	$('.animated').animated({	animatedIn: 'animated-in',	offset: 1,	reverse: false,	mobileDisabled: true});

	// home page isotope
	$('.features-grid').isotope({ layoutMode: 'vertical', masonry: { columnWidth: '.features'}});
	
	$('.nav-sort').on('click', 'li a', function() {
	    if ($(this).parents('.article-footer').length === 0) {
    		var href = $(this).attr('href');
    		if (href == '#all') 	$('.features-grid').isotope({ filter: '*' });
    		else 	    			$('.features-grid').isotope({ filter: '.' + href.replace('#', '') });
    		
    		$('.nav-sort li.current').removeClass('current');
    		$(this).parent().addClass('current');
    		
    		return false;
	    } 
		else {
	        $('.nav-sort li.current').removeClass('current');
    		$(this).parent().addClass('current');
	    }
	});
	
	$doc.on('ready', function() {
		sectionDetectHeight('.section-welcome');
		$('.features-grid').isotope('layout');
	});

	$win.on('load resize', function() {
		sectionDetectHeight('.section-welcome');
		$('.features-grid').isotope('layout');
	}).on('load scroll', function() {
		fixedHeader();
	});
	
	
	
	var frm_inputs = $('.htmlIText, .htmlIMemo, .htmlIselect');
			
	setTimeout(function() {
		frm_inputs.each(function() {
			var elem = $(this);
			if (elem.val())         	 elem.change();
		});
	}, 250);
	
	frm_inputs.focus(function() {
		$(this).parents('.form-group').addClass('non-empty');
	});
	
	frm_inputs.blur(function() {
		var parent = $(this).parents('.form-group');
		if ( $(this).val() )		parent.addClass('non-empty');
		else 						parent.removeClass('non-empty');				
	});
	
	frm_inputs.change(function() {
		$(this).blur();
	});		
	
})(jQuery, window, document);
