var winWidth = $(window).width(),
winHeight = $(window).height(),
animationStartoffset = winHeight / 4,
bannerPrev = '<button class="slick-prev slick-arrow" aria-label="Previous" type="button"><img src="'+customVariables.baseURL+'img/svg/ic-arrow.svg" alt="" class="js-tosvg"></button>',
bannerNext = '<button class="slick-next slick-arrow" aria-label="Next" type="button"><img src="'+customVariables.baseURL+'img/svg/ic-arrow.svg" alt="" class="js-tosvg"></button>';

$(function () {
	onreadyfunctions();
	$('body').on('click', '.js-menubtn:not(.is--active)', function () {
		sideMenuOpen();
	}).on('click', '.js-menubtn.is--active', function () {
		sideMenuClose();
	}).on('click', '.js-menuclose', function () {
		sideMenuClose();
	}).on('click touchstart', '.js-defaultOverlay', function () {
		sideMenuClose();
	});

	$("#selection").change(function() {
		location = $("#selection option:selected").val();
	});
	
});

$(window).on('load', function () {
	// Do after Page ready
	doOnLoad();
});

$(window).on('resize orientationchange', function () {
	// Do on resize
	winWidth = $(window).width(), winHeight = $(window).height();
	adjustCounter();
	ChangeToSvg();
});

$(window).on('scroll', function () {
	headerFixed();
	parallaxeffect('.js-parallax');
});

$(document).keyup(function (e) {
	if (e.keyCode == 27) {
		// Do on ESC press
	}
});



function onreadyfunctions() {
	
	adjustCounter();
	browserDetect();
	initCarousel('.js-carousel', 3, 1);
	initCarousel('.js-serviceCaro', 3, 1);
	headerFixed();
	parallaxeffect('.js-parallax');
	targetScroll();
	initTimer('.js-timer');
	onmousetoggleclass();
	ChangeToSvg();

	

	
	
}

function doOnLoad() {
	// OnLoad Function
	adjustCounter();
	headerFixed();
	ChangeToSvg();
	//addOnLoadAnimation();
	//pageLoaded();
	activeLink();
	$('.js-loaderscreen').fadeOut();
}

function browserDetect() {
	navigator.sayswho = function () {
		var ua = navigator.userAgent,
		tem,
		M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
		if (/trident/i.test(M[1])) {
			tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
			return 'IE ' + (tem[1] || '');
		}
		if (M[1] === 'Chrome') {
			tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
			if (tem != null) return tem.slice(1).join('').replace('OPR', 'Opera');
		}
		M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
		if ((tem = ua.match(/version\/(\d+)/i)) != null) M.splice(1, 1, tem[1]);
		return M.join(' ');
	}();
	$('body').addClass(navigator.sayswho);
}

function ChangeToSvg() {
	jQuery('img.js-tosvg').each(function () {
		var $img = jQuery(this);
		var imgID = $img.attr('id');
		var imgClass = $img.attr('class');
		var imgURL = $img.attr('src');
		jQuery.get(imgURL, function (data) {
			var $svg = jQuery(data).find('svg');
			if (typeof imgID !== 'undefined') {
				$svg = $svg.attr('id', imgID);
			}
			if (typeof imgClass !== 'undefined') {
				$svg = $svg.attr('class', imgClass + ' replaced-svg');
			}
			$svg = $svg.removeAttr('xmlns:a');
			if (!$svg.attr('viewBox') && $svg.attr('height') && $svg.attr('width')) {
				$svg.attr('viewBox', '0 0 ' + $svg.attr('height') + ' ' + $svg.attr('width'));
			}
			$img.replaceWith($svg);
		}, 'xml');
	});
}

function sideMenuOpen() {
	$('.js-menubtn').addClass('is--active');
	$('.navigation').not('.is--open').addClass('is--open');
	$('body').addClass('is--menu');
}

function sideMenuClose() {
	$('.js-menubtn').removeClass('is--active');
	$('.navigation.is--open').removeClass('is--open');
	$('body').removeClass('is--menu');
}

function initCarousel(target, stshow, stscroll) {
	var $target = $(target).not('.slick-initialized');
	var sliderArrows = $(target).parents('.carousel-sec').find('.js-sliderArrows');
	$target.each(function (i) {
		var itemDesktop = $target.data('item-desktop');
		var itemIpadPro = $target.data('item-ipad-pro');
		var itemTablet = $target.data('item-tablet');
		var itemMobile = $target.data('item-mobile');
		$target.eq(i).slick({
			infinite: true,
			slidesToShow: stshow,
			slidesToScroll: stscroll,
			appendArrows: sliderArrows,
			prevArrow: bannerPrev,
            nextArrow: bannerNext,
			responsive: [{
				breakpoint: 1200,
				settings: {
					slidesToShow: itemDesktop
				}
			}, {
				breakpoint: 992,
				settings: {
					slidesToShow: itemIpadPro
				}
			}, {
				breakpoint: 768,
				settings: {
					slidesToShow: itemTablet
				}
			}, {
				breakpoint: 375,
				settings: {
					slidesToShow: itemMobile
					// arrows: false,
					// dots: true
				}
			}]
		});
	});
}

function headerFixed() {
    var scroll = $(window).scrollTop();
	if (scroll > 50) {
		$('.js-fixed').addClass("sticky-header");
	} else {
		$('.js-fixed').removeClass("sticky-header");
	}
}

function parallaxeffect(target){
	$(target).css('background-position', "-" + (1920 - $(window).width()) / 2 + "px " + -(Math.max(document.body.scrollTop, document.documentElement.scrollTop) / 4) + "px");
}

function targetScroll() {
    $('[data-scrolltarget]').click(function () {
        var t = $(this).data('scrolltarget');
        $('html,body').animate({
            scrollTop: $('.' + t).offset().top
        }, 'slow');
    });
}

function adjustCounter(){
	var $container = $('.container');
	var cleft = $container[0].offsetLeft + 15;
	if(winWidth > 991){
		$('.js-adjustCounter').css({
			width: winWidth-cleft,
		});
	} else {
		$('.js-adjustCounter').css({
			width: '100%',
		});
	}
}

function initTimer(target){
	var self = $(target);
	self.countTo();
}

function pageLoaded() {
    $('body').addClass('page-loaded');
    setTimeout(function () {
        AOS.init({
            duration: 800,
            once: true,
            offset: animationStartoffset,
            easing: 'ease',
        });
    }, 200);
}

function addOnLoadAnimation() {
    $('.aos-anim, .banner-content, .light-content, .ico-post, .adbox, .counter-bg ul>li, .parallax-sec, .imgov-post, .bigThumbnil, .smallThumbnil, .cont-sec').attr('data-aos', 'fade-up');
    $('').attr('data-aos', 'no');
    $('').each(function (i) {
        $(this).attr('data-aos-delay', 50 + (50 * i));
    });
    if (winWidth < 992) {
        $('.sec-about-sec3').attr('data-aos', 'fade-up');
    } else {
        $('.sec-about-sec3 .about-box').attr('data-aos', 'fade-up');
        $('.sec-about-sec3 .about-box').each(function (i) {
            $(this).attr('data-aos-delay', 50 + (100 * i));
        });
    }
}

function activeLink(){
	var currentUrl = window.location.pathname;
	$('[href="'+currentUrl+'"]').parent('li').siblings().removeClass('is--active');
	$('[href="'+currentUrl+'"]').parent('li').addClass('is--active');
}

function onmousetoggleclass(){
	$('body').on('mouseenter', '.service-box', function(){
		$(this).parents('.service-carousel').find('.service-box').removeClass('is--active');
		$(this).addClass('is--active');
	});
}


$(function () {
	onreadyfunctions();
});

function onreadyfunctions() {
	getCopyYear();
}

function getCopyYear(){
	var copyright= new Date().getFullYear();
	$(".copyrightyear").text(copyright);
}


	function loadSelectValue(){
		var pathname = location.pathname;
		var dl = document.getElementById('selection');
		var el =0;
		for (var i=0; i<dl.options.length; i++){
			if (dl.options[i].value == pathname){
				el=i;
				break;
			}
		}
		dl.selectedIndex = el;
	}

