(function($){
"use strict";

function showGalleryThumbnails(evt) {
	console.log('showGalleryThumbnails');
	$(this).closest('.referenceItem').toggleClass('showThumbs');
}

function playYTVideo(evt) {
	evt.target.playVideo();
}

function pauseYTVideo(state) {
	var videos = $('.thumb.hasVideo').each(function(){
		if( this.YTPlayer != undefined ){
			this.YTPlayer.pauseVideo();
		}
	});
};

function openYTPlayer(evt) {
	var video = $(this).find('.thumbVideo'),
	w = video.parent().width(),
	h = video.parent().height();

	this.YTPlayer = new YT.Player(video.attr('id'), {
		'width'			: w,
		'height'		: h,
		'videoId'		: video.data('video-id'),
		'autoplay'		: 1,
		'autohide'		: 1,
		'rel'			: 0,
		'events'		: {
			'onReady': playYTVideo
		}
	});
	$(this).off('click');
}

function showImage(evt) {
	var gall = $(this).closest('.referenceItem').removeClass('showThumbs');
	gall.find('.thumb > img').attr('src', $(this).data('src-toggle')).removeAttr('width height');
}

function prevGallery(evt) {
	var slider = $(this).parent().find('> .referenceItems > .referenceItemsWrapp'),
	current = slider.data('current') < 1 ? slider.children('.referenceItem').length -1 : slider.data('current') - 1,
	leftPosition = -current*105;

	slider.data('current', current);

	slider.css('left', leftPosition+'%');
	slider.find('.showThumbs').removeClass('showThumbs');
	pauseYTVideo();
}

function nextGallery(evt) {
	var slider = $(this).parent().find('> .referenceItems > .referenceItemsWrapp'),
	current = slider.data('current') >= slider.children('.referenceItem').length-1 ? 0 : slider.data('current') + 1,
	leftPosition = -current*105;

	slider.data('current', current);

	slider.css('left', leftPosition+'%');
	slider.find('.showThumbs').removeClass('showThumbs');
	pauseYTVideo();
}

function openGallery(evt) {
	$(this).closest('.page').addClass('galleryOpen');
	var gallery = $(this).closest('.referenceGallery').addClass('open');

	gallery.find('.thumb').each(function(){
		if( $(this).is('.hasVideo') ) {
			$(this).on('click', openYTPlayer);
		}
		else {
			$(this).on('click', showGalleryThumbnails);
		}
	});

	gallery.find('.item > img').on('click', showImage);
	gallery.find('.prevGallery').on('click', prevGallery);
	gallery.find('.nextGallery').on('click', nextGallery);
	gallery.find('> .referenceItems > .referenceItemsWrapp').data('current', 0);
}

function closeGallery(evt) {
	var gallery = $(this).closest('.referenceGallery')
	.removeClass('open');
	$(this).closest('.page').removeClass('galleryOpen');
	gallery.find('.thumb').off('click', showGalleryThumbnails);
}


function init() {
	$('.openGallery').on('click', openGallery);
	$('.closeGallery').on('click', closeGallery);
	$('a[href="#"]').on('click', function(evt){
		evt.preventDefault();
	});

	// load YouTube API
	var tag = document.createElement('script');
	tag.src = "https://www.youtube.com/iframe_api";
	var firstScriptTag = document.getElementsByTagName('script')[0];
	firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}

$(document).ready(init);

})(jQuery);