//LOADER
jQuery(window).on("load", function () {
	"use strict";
	jQuery(".loader").fadeOut(800);
});

jQuery(function ($) {
	"use strict";
	var $window = $(window);

	// ------- Back to Top -------
	var amountScrolled = 700;
	var backBtn = $("a.back-top");
	$window.on("scroll", function () {
		if ($window.scrollTop() > amountScrolled) {
			backBtn.addClass("back-top-visible");
		} else {
			backBtn.removeClass("back-top-visible");
		}
	});
	backBtn.on("click", function () {
		$("html, body").animate({
			scrollTop: 0
		}, 700);
		return false;
	});

	/* ------- Smooth scroll ------- */
	$(".scroll").on("click", function (event) {
		var menu = $("nav.navbar").innerHeight();
		event.preventDefault();
		$("html,body").animate({
			scrollTop: ($(this.hash).offset().top - 10)
		}, 1000);
	});

	/*------ MENU Fixed ------*/
	$window.scroll(function () {
		var $scroll = $window.scrollTop();
		var $navbar = $(".navbar");
		if ($scroll > 100) {
			$navbar.addClass("fixedmenu");
		} else {
			$navbar.removeClass("fixedmenu");
		}
	});
	$window.scroll();

	/* ------- Sidebar Toggle ------ */
	$(".menu-icon").on("click",function () {
		$(".overlay-menu").toggleClass("open");
	});
	if($(".full-nav li a").hasClass("scroll")){
		$(".full-nav li a").on("click",function () {
			$(".overlay-menu").removeClass("open");
		});
	}

	//Full Height Banner BG
	function resizebanner(){
		$("#text-rotate").css("height", $(window).height());
	}
	$(window).resize(function(){
		resizebanner(); 
	});
	resizebanner();



	/* ------ Swiper Slider ------ */

	/*Page bannerr*/
	$(".text-rotator").each(function () {
		var $this = $(this);
		$this.find(".swiper-container").swiper({
			effect: "fade",
			speed: 1600,
			autoplay: 3000,
			paginationClickable: true,
			pagination: $this.find(".swiper-pagination"),
			nextButton: $this.find(".swiper-button-next"),
			prevButton: $this.find(".swiper-button-prev"),
		});
	});


	/*Blog*/
	$(".blog_slides").each(function () {
		var $this = $(this);
		$this.find(".swiper-container").swiper({
			setWrapperSize: 'true',
			slidesPerView: 1,
			controlBy: "container",
			autoplay: false,
			speed: 600,
			paginationClickable: true,
			pagination: $this.find(".swiper-pagination")
		});
	});

	/*Teams Slider with 4 slides*/
	$(".ivy-team").each(function () {
		var $this = $(this);
		$this.find(".swiper-container").swiper({
			slidesPerView: 4,
			slidesPerColumn: 1,
			spaceBetween: 20,
			controlBy: "container",
			grabCursor: true,
			autoplayDisableOnInteraction: true,
			autoplay: 2500,
			// Responsive breakpoints
			breakpoints: {
				// when window width is <= 320px
				320: {
					slidesPerView: 1,
					spaceBetween: 30
				},
				// when window width is <= 480px
				480: {
					slidesPerView: 1,
					spaceBetween: 30
				},
				// when window width is <= 1024px
				767: {
					slidesPerView: 2,
					spaceBetween: 20
				},
				// when window width is <= 1024px
				1024: {
					slidesPerView: 3,
					spaceBetween: 20
				},
				// when window width is <= 1280px
				1280: {
					slidesPerView: 3,
					spaceBetween: 20
				}
			},
			// If we need pagination
			paginationClickable: true,
			pagination: $this.find(".swiper-pagination")

		});
	});


	/*Blog OR News for home PAge */
	$(".our-blog").each(function () {
		var $this = $(this);
		$this.find(".swiper-container").swiper({
			slidesPerView: 3,
			spaceBetween: 20,
			grabCursor: true,
			breakpoints: {
				320: {
					slidesPerView: 1,
					spaceBetween: 20
				},
				480: {
					slidesPerView: 1,
					spaceBetween: 20
				},
				900: {
					slidesPerView: 2,
					spaceBetween: 20
				}
			},
			paginationClickable: true,
			pagination: $this.find(".swiper-pagination")

		});
	});


	/*Work with us LOGOS*/
	$(".partners").each(function () {
		var $this = $(this);
		$this.find(".swiper-container").swiper({
			slidesPerView: 5, 
			spaceBetween: 15,
			controlBy: 'slide',
			grabCursor: true,
			loop: true,
			autoplay: 2500,
			speed: 300,
			// Responsive breakpoints
			breakpoints: {
				320: {
					slidesPerView: 1,
					spaceBetween: 10
				},
				480: {
					slidesPerView: 2,
					spaceBetween: 10
				},
				775: {
					slidesPerView: 3,
					spaceBetween: 10
				},
				900: {
					slidesPerView: 4,
					spaceBetween: 10
				}
			},
		});
	});


	/*Twitter in Slidebar*/
	$(".tweetes").each(function () {
		var $this = $(this);
		$this.find(".swiper-container").swiper({
			slidesPerView: 1,
			autoplay: 3000,
			grabCursor: true,
			controlBy: "container",
		});
	});


	/*Equal Heights*/
	if ($(".equalheight").length) {
		$(".equalheight").matchHeight({
			property: "height"
		});
	}


	/* ------ Revolution*/
	$("#maxo-main").show().revolution({
		sliderType: "standard",
		sliderLayout: "fullscreen",
		fullScreenOffsetContainer: "header",
		scrollbarDrag: "true",
		dottedOverlay: "none",
		delay: 5000,
		navigation: {
			bullets: {
				style: "",
				enable: true,
				rtl: false,
				hide_onmobile: false,
				hide_onleave: false,
				hide_under: 320,
				hide_over: 9999,
				tmp: '',
				direction: "horizontal",
				space: 10,
				h_align: "center",
				v_align: "bottom",
				h_offset: 15,
				v_offset: 30
			},
			arrows: {
				enable: true,
				hide_onmobile: true,
				hide_onleave: false,
				hide_under: 767,
				left: {
					h_align: "left",
					v_align: "bottom",
					h_offset: 20,
					v_offset: 30,
				},
				right: {
					h_align: "right",
					v_align: "bottom",
					h_offset: 20,
					v_offset: 30
				},
			},
			touch: {
				touchenabled: "on",
				swipe_threshold: 75,
				swipe_min_touches: 1,
				swipe_direction: "horizontal",
				drag_block_vertical: false,
			}
		},
		viewPort: {
			enable: true,
			outof: "pause",
			visible_area: "90%"
		},
		responsiveLevels: [4096, 1200, 992, 768],
		gridwidth: [1200, 992, 768, 480],
		gridheight: [600, 500, 500, 350],
		lazyType: "none",
		parallax: {
			type: "mouse",
			origo: "slidercenter",
			speed: 9000,
			levels: [2, 3, 4, 5, 6, 7, 12, 16, 10, 50],
		},
		shadow: 0,
		spinner: "off",
		stopLoop: "off",
		stopAfterLoops: -1,
		stopAtSlide: -1,
		shuffle: "off",
		autoHeight: "off",
		hideThumbsOnMobile: "off",
		hideSliderAtLimit: 0,
		hideCaptionAtLimit: 0,
		hideAllCaptionAtLilmit: 0,
		debugMode: false,
		fallbacks: {
			simplifyAll: "off",
			nextSlideOnWindowFocus: "off",
			disableFocusListener: false,
		}
	});


	// =========== Parallax ===========
	if (window.innerWidth > 991) {
		$(".parallax").parallax("50%", 0.03);
	}

	// +++++  CubeFolio
	// gallery
	$("#work-gallery").cubeportfolio({
		filters: "#work-filters",
		layoutMode: "mosaic",
		defaultFilter: '*',
		animationType: "fadeOut",
		gapHorizontal: 20,
		gapVertical: 20,
		gridAdjustment: "responsive",
		mediaQueries: [{
			width: 1500,
			cols: 3
		}, {
			width: 1100,
			cols: 3
		}, {
			width: 480,
			cols: 2
		}, {
			width: 320,
			cols: 1
		}],
		displayType: "fadeIn",
		displayTypeSpeed: 400,
	});
	
	$("#work-gallery").on('initComplete.cbp', function(){
		$("#work-gallery").cubeportfolio('layout');
	});

	$("#cube-gallery").cubeportfolio({
		filters: "#cube-filters",
		layoutMode: "grid",
		defaultFilter: '*',
		animationType: "fadeOut",
		gapHorizontal: 20,
		gapVertical: 20,
		gridAdjustment: "responsive",
		mediaQueries: [{
			width: 1500,
			cols: 3
		}, {
			width: 1100,
			cols: 3
		}, {
			width: 480,
			cols: 2
		}, {
			width: 320,
			cols: 1
		}],
		displayType: "fadeIn",
		displayTypeSpeed: 400,
	});



	//FancyBox  Popup
	$("[data-fancybox]").fancybox({
		arrows : true,
		infobar : true,
		thumbs: false,
	});

	// gradient layout
	function checkGradeient() {
		//gradient animations
		var colors = new Array(
			[62,35,255],
			[60,255,60],
			[255,35,98],
			[45,175,230],
			[255,0,255],
			[255,128,0]);

		var step = 0;
		//color table indices for:
		// current color left
		// next color left
		// current color right

		// next color right
		var colorIndices = [0,1,2,3];

		//transition speed
		var gradientSpeed = 0.002;

		function updateGradient()
		{

			if ( $===undefined ) return;

			var c0_0 = colors[colorIndices[0]];
			var c0_1 = colors[colorIndices[1]];
			var c1_0 = colors[colorIndices[2]];
			var c1_1 = colors[colorIndices[3]];

			var istep = 1 - step;
			var r1 = Math.round(istep * c0_0[0] + step * c0_1[0]);
			var g1 = Math.round(istep * c0_0[1] + step * c0_1[1]);
			var b1 = Math.round(istep * c0_0[2] + step * c0_1[2]);
			var color1 = "rgb("+r1+","+g1+","+b1+")";

			var r2 = Math.round(istep * c1_0[0] + step * c1_1[0]);
			var g2 = Math.round(istep * c1_0[1] + step * c1_1[1]);
			var b2 = Math.round(istep * c1_0[2] + step * c1_1[2]);
			var color2 = "rgb("+r2+","+g2+","+b2+")";

			$('.gradient').css({
				background: "-webkit-gradient(linear, left top, right top, from("+color1+"), to("+color2+"))"}).css({
				background: "-moz-linear-gradient(left, "+color1+" 0%, "+color2+" 100%)"});

			step += gradientSpeed;
			if ( step >= 1 )
			{
				step %= 1;
				colorIndices[0] = colorIndices[1];
				colorIndices[2] = colorIndices[3];

				//pick two new target color indices
				//do not pick the same as the current one
				colorIndices[1] = ( colorIndices[1] + Math.floor( 1 + Math.random() * (colors.length - 1))) % colors.length;
				colorIndices[3] = ( colorIndices[3] + Math.floor( 1 + Math.random() * (colors.length - 1))) % colors.length;

			}
		}

		setInterval(updateGradient,10);
	}

	if($('body').hasClass("gradientLayout")){
		checkGradeient()
	}
	
	if( $('.testimonials-container .swiper-slide').length > 1 ){
		var mySwiper = new Swiper ('.testimonials-container', {
			spaceBetween: 50,
			loop: true,
			autoplay: 5000
		});
	}
	
	if( $('.rev_slider .tp-revslider-mainul li').length <= 1 ){
		$('.rev_slider').find('.tparrows, .tp-bullets').hide();
	}

	/* Comment form translation
    -------------------------------------------------------*/
	$('#commentform input[required], #commentform textarea[required]').on('invalid', function () {
		this.setCustomValidity($translations.form_field_required);
	});
	
	$('#commentform input[type="email"]').on('invalid', function () {
		if (this.validity.valueMissing) {
			this.setCustomValidity($translations.form_email_required);
		}
		else if (this.validity.typeMismatch) {
			this.setCustomValidity($translations.form_email_invalid);
		}
	});
	
	$('#commentform input[required], #commentform textarea[required], #commentform input[type="email"]').on('input', function () {
		this.setCustomValidity('');
	});

});

jQuery(window).on('load', function(){
	var $timeout = false;
	
	jQuery(window).on('resize scroll', function(){
		if($timeout){
			clearTimeout($timeout);
		}
		$timeout = setTimeout(function(){
			jQuery("#work-gallery").cubeportfolio('layout');
		}, 50);
	});
});