(function($) {
	$('.shrk-video-popup').click(function(event) {
		event.preventDefault();
		var id       = $(this).attr('data-video-popup-id');
		var url      = $(this).attr('data-video-popup-url');
		var autoplay = $(this).attr('data-video-autoplay');

		// Add the video wrapper
		$('body').append('<div data-video-popup-id="'+id+'" class="shrk-video-popup-wrapper"><div class="loader"></div><div class="close"></div></div>');

		var $video_wrapper = $('.shrk-video-popup-wrapper[data-video-popup-id="'+id+'"]');

		setTimeout(function(){
			$video_wrapper.addClass('visible');
		}, 0);

		data = {
			action: 'shrk_get_video_popup',
			video_url: url,
			video_autoplay: autoplay
		};

		jQuery.post(shrk.ajax.ajaxurl, data, function(response) {
			$video_wrapper.append(response);
			$video_wrapper.find('iframe').addClass('shrk-video-popup-video');
			shrk_resize_video_popup();
			$video_wrapper.addClass('ready');
		});
	});


	// Close Function
	function shrk_close_video_popup() {
		$('.shrk-video-popup-wrapper, .shrk-video-popup-video').fadeOut(200);
		setTimeout(function(){
			$('.shrk-video-popup-wrapper').remove();
		}, 200);
	}

	// Close on click
	$('body').on('click touchend', '.shrk-video-popup-wrapper', function(event) {
		event.stopPropagation();
		shrk_close_video_popup();
	});

	// Close on Escape
	$(document).keyup(function(e) {
		if (e.keyCode == 27) {
			if ( $('.shrk-video-popup-wrapper').is(':visible') ) {
				shrk_close_video_popup();
			}
		}
	});

	function shrk_resize_video_popup() {
		var videoWidth, videoHeight, ratio;
		var $video = $('.shrk-video-popup-video');

		if ( typeof( $video.data('video-ratio') ) === 'undefined' ) {
			videoWidth  = $video.width();
			videoHeight = $video.height();
			ratio       = videoWidth/videoHeight;
			$video.data('video-ratio', ratio);
		}

		$video.css({
			'width' : '80%'
		});

		videoWidth = $video.width();
		$video.height(videoWidth*0.5625);
	}

	$('.shrk-video-popup .thumbnail-bg').each(function () {
		var video_url = $(this).closest('.shrk-video-popup').attr('data-video-popup-url');
		var $thumb    = $(this);
		var thumb_url = false;

		if ( video_url.indexOf('youtube') !== -1 ) {
			var video_id = video_url.match(/(?:https?:\/{2})?(?:w{3}\.)?youtu(?:be)?\.(?:com|be)(?:\/watch\?v=|\/)([^\s&]+)/);
			if ( video_id !== null ) {
				thumb_url = 'https://i1.ytimg.com/vi/' + video_id[1] + '/0.jpg';
			} 

			if ( thumb_url ) {
				   $thumb.css({ 'background-image': 'url('+thumb_url+')' });
			} else {
				shrk.debug__log( 'Error getting video thumbnail for: ' + video_url );
			}
		} else if ( video_url.indexOf('vimeo') !== -1 ) {
			$.ajax({
				type:'GET',
				url: 'https://vimeo.com/api/oembed.json?url=' + encodeURIComponent(video_url),
				dataType: 'json',
				success: function(data) {
					thumb_url = data.thumbnail_url;

					if ( thumb_url ) {
						   $thumb.css({ 'background-image': 'url('+thumb_url+')' });
					} else {
						shrk.debug__log( 'Error getting video thumbnail for: ' + video_url );
					}
				}
			});
		} 
	});

	// Resize
	$(window).resize(function() {
		shrk_resize_video_popup();
	});
})( jQuery );