jQuery(document).ready(function($){
	var windowWidth = $(window).width();
	var windowScroll = $(window).scrollTop();
	var $window_height = $(window).height();
	var $window_width = $(window).width();
	var $win = $(window);
	var responsiveWidth = 980;
	
	$(window).on('scroll', function(){
		checkHeader();
	});
	$(window).on('resize', function(){
		checkHeader();
		addMobileSubmenu();
	});
	checkHeader();
	addMobileSubmenu();
	
	function checkHeader(){
		if(!checkWindowSize()){ return; } // return on mobile
		
		var doc = document.documentElement;
		var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
		var top = (window.pageYOffset || doc.scrollTop)  - (doc.clientTop || 0);
		
		var windowScroll = top;
		if(windowScroll < 50){
			if($('.top-menu-wrap').hasClass('top-menu-scrolling')){
				$('.top-menu-wrap').removeClass('top-menu-scrolling');	
			}
			return;
		}
		else {
			if(!$('.top-menu-wrap').hasClass('top-menu-scrolling')){
				$('.top-menu-wrap').addClass('top-menu-scrolling');	
			}
			return;
		}
		return;
	}	
		
	function checkWindowSize(){
		var windowWidth = window.innerWidth;
		if(windowWidth > responsiveWidth){
			// close all open sub-menus with menu
			$('.sub-menu, .header-menu-wrap').removeAttr('style');
			// remove sub-menu + links
			if($('.mobile-submenu').length){
				$('.mobile-submenu').remove();
			}
			$('.main-navigation .menu li').find('.we-blue').removeClass('we-blue');

			if($('html.nav-shown').length){
				$('.mobile-menu').toggleClass('active');
				$('html').toggleClass('nav-shown');
				$('body').toggleClass('we-mobile');				
			}
			
			mobileAddOnce = false;
			mobileOpen = false;

			return true;
		}		
		return false;
	}
	
	var mobileOpen, mobileAddOnce = false;	
	$(document).on('click touch', '.mobile-menu', function(e){
		e.preventDefault();
		
		$(this).toggleClass('active');
		$('html').toggleClass('nav-shown');
		
		if(!mobileOpen){
			// open menu
			$('.header-menu-wrap').slideDown();
			
			mobileOpen = true;			
		}
		else {
			// close menu
			$('.header-menu-wrap').slideUp(function(){
				// close open sub-menus
				$(this).find('.sub-menu').slideUp();				
				$(this).find('.mobile-submenu').removeClass('mobile-submenu-open we-blue');
			});
			
			mobileOpen = false;			
		}

	});
		
	function addMobileSubmenu(){		
		if(mobileAddOnce || checkWindowSize()){ return; }
		
		$('.header-search, .main-navigation .menu').removeAttr('style');
		
		$('.main-navigation .menu li').each(function(){
			if($(this).find('.sub-menu').length && !$(this).find('.mobile-submenu').length){
				// add + to menu items
				$('<a href="#" class="mobile-submenu">+</a>').insertBefore($(this).find('.sub-menu'));
				if($(this).hasClass('current-menu-parent')){
					$(this).find('.mobile-submenu').addClass('mobile-submenu-open we-blue');
				}
			}
		});		
		
		mobileAddOnce = true;
	}
	
	$(document).on('click touch', '.mobile-submenu', function(e){
		e.preventDefault();
		$(this).toggleClass('mobile-submenu-open');
		
		var $this = $(this);
		var parent = $this.parent();
		
		$('.main-navigation .menu .sub-menu').not($this).slideUp();
		$('.main-navigation .menu li').not($(this)).find('.we-blue').removeClass('we-blue');
		$('.mobile-submenu').not($this).removeClass('mobile-submenu-open');
		
		if($(this).hasClass('mobile-submenu-open')){
			// open it
			parent.find('.sub-menu').slideDown('fast', 'linear');
			$(this).addClass('we-blue');
		}
		else {
			// close it
			$(this).removeClass('we-blue');
		}
	});
	
	var fadein_speed = 1500;
	$('#logoSlideshow').on('cycle-before',function(e, optionHash, outgoingSlideEl, incomingSlideEl, forwardFlag){
		$('.cycle-slide-active').addClass('cycle-fadeout');
		var windowWidth = window.innerWidth;
		$win.on('resize', function(){
			windowWidth = window.innerWidth;
		});
		if(windowWidth < 470){
			//$('.cycle-slide-active').next().next().next().css('opacity', '0').addClass('cycle-fade-in');
			$('.cycle-slide-active').next().next().next().hide().fadeIn(fadein_speed);
		}
		else if(windowWidth >= 470 && windowWidth <= 630){
			//.fadeIn(fadein_speed)
			//$('.cycle-slide-active').next().next().next().next().css('opacity', '0').addClass('cycle-fade-in');
			$('.cycle-slide-active').next().next().next().next().hide().fadeIn(fadein_speed);
		}
		else if(windowWidth > 630 && windowWidth <= 767){
			//$('.cycle-slide-active').next().next().next().next().next().css('opacity', '0').addClass('cycle-fade-in');
			$('.cycle-slide-active').next().next().next().next().next().hide().fadeIn(fadein_speed);
		}
		else if(windowWidth > 767 && windowWidth <= 980){			
			$('.cycle-slide-active').next().next().next().next().next().next().hide().fadeIn(fadein_speed);
		}
		else {
			//$('.cycle-slide-active').next().next().next().next().next().next().next().next().css('opacity', '0').addClass('cycle-fade-in');
			$('.cycle-slide-active').next().next().next().next().next().next().next().next().hide().fadeIn(fadein_speed);
		}
	});
	
	$('#logoSlideshow').on('cycle-after',function(e, optionHash, outgoingSlideEl, incomingSlideEl, forwardFlag){
		$('.cycle-fadeout:not(.cycle-slide-active)').removeClass('cycle-fadeout');		
	});
	/*
	if($('.testimonials-slideshow').length){
		$('.testimonials-slideshow').cycle({
			timeout: 6000,
			slides: "> div.testimonials-wrap",
			speed: 600,
			pauseOnHover: true,
			autoHeight: 'calc',
		});
	}
	*/
	// remove last animated element on frontpage
	var last_widget = $('.home .frontpage-wrap .widget-last .textwidget');
	if(last_widget.length){
		if(last_widget.hasClass('animated-element')){
			last_widget.removeClass('animated-element');
		}
		if(last_widget.hasClass('animation-top')){
			last_widget.removeClass('animation-top');
		}
	}
	
	$('.animated-element').each(function() {
		var $animatedSection = $(this);
		var animatedSectionOffsetDiff = parseInt($animatedSection.data('animation-offset'));			
		var scrollTop = 0;
		if (isNaN(animatedSectionOffsetDiff)) {
			animatedSectionOffsetDiff = 0;
		}
		$win.on('load resize', function() {
			var animatedSectionOffset = $animatedSection.offset().top + animatedSectionOffsetDiff;
			var winHeight = $win.height();
			scrollTop = $win.scrollTop();
			if (scrollTop + winHeight > animatedSectionOffset) {
				$animatedSection.addClass('animation-start');
			}
			$win.on('scroll', function() {
				scrollTop = $win.scrollTop();
				if (scrollTop + (winHeight) > animatedSectionOffset - animatedSectionOffsetDiff) {
					$animatedSection.data('animated', false);
					$animatedSection.addClass('animation-start');
				}				
			});
		});
	});
});
					   
(function() {
	'use strict';

	function initSocialSharingClick() {

		var links;

		links = document.querySelectorAll('a.cssb');
		for (var i = 0; i < links.length; i++) {
			addEvent(links[i], 'click', openPopup);
		}
	}

	function openPopup(e) {

		var top = (screen.availHeight - 500) / 2;
		var left = (screen.availWidth - 500) / 2;

		var popup = window.open(
			this.href,
			'social',
			'width=550,height=420,left='+ left +',top='+ top +',location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1'
		);

		if(popup) {
			popup.focus();
			e.preventDefault();
			return false;
		}

		return true;
	}

	function addEvent(element, eventName, callback) {
		if (element.addEventListener) {
			element.addEventListener(eventName, callback, false);
		} else {
			element.attachEvent("on" + eventName, callback);
		}
	}

	addEvent(window, 'load', initSocialSharingClick);

})();