
(function($){

	$(function(){

		$('#page').headroom();
 		// this will disable dragging of all images
		$("img").on('mousedown', function(e){
           e.preventDefault()
		});

		// this will disable right-click on all images
		$("body").on("contextmenu",function(e){
		   // return false;
		});

		function addLink() {
			var body_element = document.getElementsByTagName('body')[0];
			var selection;
			selection = window.getSelection();
			var pagelink = "<br /><br /> Source: <a href='"+document.location.href+"'>"+document.location.href+"</a><br />Copyright &copy; Stathis Livathinos"; // change this if you want
			var copytext = selection + pagelink;
			var newdiv = document.createElement('div');
			newdiv.style.position='absolute';
			newdiv.style.left='-99999px';
			body_element.appendChild(newdiv);
			newdiv.innerHTML = copytext;
			selection.selectAllChildren(newdiv);
			window.setTimeout(function() {
				body_element.removeChild(newdiv);
			},0);
		}
		document.oncopy = addLink;

		var $menu = $("#mobile-navigation").mmenu({
		   //   options
		   "extensions": [
				  "position-right",
				  "theme-dark"
			   ]
		});
		var $icon = $("#menu-trigger");
		var API = $menu.data( "mmenu" );

		$icon.on( "click", function() {
		   API.open();
		});

		API.bind( "open:finish", function() {
		   setTimeout(function() {
			  $icon.addClass( "is-active" );
		   }, 100);
		});
		API.bind( "close:finish", function() {
		   setTimeout(function() {
			  $icon.removeClass( "is-active" );
		   }, 100);
		});

		/* HOME */


		$('.equal').matchHeight();
		$('#filters, .tab-menu').headroom({
		 	 offset : 320,
		});
		/* Swiper */
		var mySwiper = new Swiper ('.swiper-container', {
		    // Optional parameters
		    direction: 'horizontal',
		    effect: 'fade',
		    fadeEffect: {
		       crossFade: true
		    },
		    speed: 1200,

		    loop: true,
		    autoplay: {
		        delay: 12000,
		      },

		    // If we need pagination
		    pagination: {
		      el: '.swiper-pagination',
			  clickable : true
		    },

		    // Navigation arrows
		    navigation: {
		      nextEl: '.swiper-button-next',
		      prevEl: '.swiper-button-prev',
		    },

		    // And if we need scrollbar
		    //scrollbar: {
		    //  el: '.swiper-scrollbar',
		    //},
		  });

		  var workSlider, worksSlider;

		  homeSlider = $('#home-slider').owlCarousel({
		   	margin		: 	1,
		   	loop		: 	true,
		   	center		:	false,
		   	nav			: 	false,
			dots		:	false,
		   	autoWidth 	:	true,
		   	autoPlay	:	false,
		   	responsive : {
		   	    0 : {
		   	        items : 1,
		   	        slideBy		:	1,
		   	    },
		   	    600 : {
		   	        items :	1,
		   	        slideBy		:	2,
		   	    },
		   	    800 : {
		   	       	items :	1,
		   	       	slideBy		:	2,
		   	    }
		   	}

		});



		/* WORKS */
		$('#works-slider').imagesLoaded(function(){
			worksSlider = $('#works-slider').owlCarousel({
			 	margin		: 	0,
			 	loop		: 	false,
			 	center		:	false,
			 	nav			: 	false,
			 	autoWidth 	:	false,
			 	autoPlay	:	false,
			 	lazyLoad	:	false,
			 	responsive : {
			 	    0 : {
			 	        items 	: 	1,
			 	        slideBy		:	1,
			 	    },
			 	    600 : {
			 	        items 	:	2,
			 	       slideBy		:	1,
			 	    },
			 	    800 : {
			 	       	items 	:	3,
			 	       	slideBy		:	2,

			 	    },
			 	    1024 : {
			 	    	items	:	4,
			 	    	slideBy		:	3,

			 	    },
			 	    1280 : {
			 	     	items	:	6,
			 	     	slideBy		:	5,

			 	     }
			 	},
			 	onTranslated	: function(){

			 	}
			 });
		});

		 /* WORK */
		 $('#row-slider').imagesLoaded(function(){
			 workSlider = $('#row-slider').owlCarousel({
			  	margin		: 	0,
			  	loop		: 	true,
			  	center		:	false,
			  	nav			: 	false,
			  	autoWidth 	:	true,
			  	autoPlay	:	false,
			  	responsive : {
			  	    0 : {
			  	        items : 1,
			  	        slideBy		:	1,
			  	    },
			  	    600 : {
			  	        items :	2,
			  	        slideBy		:	2,
			  	    },
			  	    1280 : {
			  	       	items :	6,
			  	       	slideBy	: 2
			  	    }
			  	}
			  });
	  	});

		 $('.box.item').on('click', function(e){
		 	e.preventDefault();
		 	var var_href = $(this).data('href');
		 	window.location.href= var_href;
		 });

		 var var_speed=500;

		 $('.next.btn').click(function() {
			if(homeSlider.length){
		 		homeSlider.trigger('next.owl.carousel', [var_speed]);
			}
			if(worksSlider.length){
		 		worksSlider.trigger('next.owl.carousel', [var_speed]);
			}
			if(workSlider.length){
		 		workSlider.trigger('next.owl.carousel', [var_speed]);
			}
		 })

		 $('.prev.btn').click(function() {
			if(homeSlider.length){
		 		homeSlider.trigger('prev.owl.carousel', [var_speed]);
			}
			if(worksSlider.length){
		 		worksSlider.trigger('prev.owl.carousel', [var_speed]);
			}
			if(workSlider.length){
		 		workSlider.trigger('prev.owl.carousel', [var_speed]);
			}
		 })


		 $('#tabslist, #tab-container').easytabs({
		 	tabs: '.etabs > li',
		 	panelContext: $(document),
		 	updateHash: false
		 });

		$('.tab a').on('click', function(e){
			e.preventDefault();
			 $('iframe').each(function(){
			 	$(this).attr('src', $(this).attr('src'));
			 })
		});

		$(".fancybox").fancybox({
		    'nextEffect': 'fade',
		    'prevEffect': 'fade',
			helpers     : {
	        title: {
	            type: 'inside'
	        }
	    }
		});


		$('.acc-content').slideUp(0);
		$(document).on('click','.acc-title', function(){
			$('.acc-title').removeClass('active');
			$(this).addClass('active');
			$('.acc-content').stop().slideUp(60);
			$(this).next('.acc-content').stop().slideToggle(160);
		});

		$('#toggle-search').on('click', function(e){
			$('body').toggleClass('search-active');
		});

		var $container = $('#news-items');
		// init
		$container.imagesLoaded(function(){
			$container.packery({
			  itemSelector: '.post-item',
			  gutter: 0
			});
		});

		 /* Filters */
		 $(document).on('click', '#filters-menu  li a', function(e){
		 	e.preventDefault();
		 	$('.current-filter').removeClass('current-filter');
		 	$(this).parent().addClass('current-filter');
		 	var var_href = $(this).attr('href');
		 	var_s = '.'+var_href.substring(1);
		 	console.log(var_s);
		 	$('.owl-item, .worklist-item').addClass('hidden');
		 	$(var_s+'.worklist-item').removeClass('hidden');
		 	$(var_s).parent().removeClass('hidden');
		 	$('#works-slider').trigger('to.owl.carousel', [0,0]).trigger('refresh.owl.carousel');
		 });


	  	players = new Array();

	     function onYouTubeIframeAPIReady() {
	         var temp = $("iframe.yt_players");
	         for (var i = 0; i < temp.length; i++) {
	             var t = new YT.Player($(temp[i]).attr('id'), {
	                 events: {
	                     'onStateChange': onPlayerStateChange
	                 }
	             });
	             players.push(t);
	         }
	     }
	     onYouTubeIframeAPIReady();

	     function onPlayerStateChange(event) {
	         if (event.data == YT.PlayerState.PLAYING) {
	             var temp = event.target.a.src;
	             console.log(temp);
	             var tempPlayers = $("iframe.yt_players");
	             for (var i = 0; i < players.length; i++) {
	                 if (players[i].a.src != temp)
	                     players[i].stopVideo();
	             }
	         }
	     }


		 if($('.featured-link').length){
			 $('.swiper-slide').each(function(){
				 var targetLink = $(this).find('.featured-link a').attr('href');
				 $(this).on('click', function(e){
					 if ($(this).hasClass('swiper-slide-active')){
					 	window.location.href=targetLink;
					}
				 });
			 })
		 }

	 });
})(jQuery);
