;
(function ($, window) {
	var defaults = {
		ratio: 16 / 9,
		videoId: 'ZCAnLxRvNNc',
		mute: true,
		repeat: true,
		width: $(window).width(),
		wrapperZIndex: 99,
		playButtonClass: 'kkBgVideo-play',
		pauseButtonClass: 'kkBgVideo-pause',
		muteButtonClass: 'kkBgVideo-mute',
		volumeUpClass: 'kkBgVideo-volume-up',
		volumeDownClass: 'kkBgVideo-volume-down',
		increaseVolumeBy: 5,
		start: 0
	};
	var kkBgVideo = function (node, options) {
		var options = $.extend({}, defaults, options),
			videoContainer = $('body > .video-bg');
		var kkBgVideoContainer = '<div id="kk-bgvideo-container" style="overflow: hidden; position: absolute; z-index: 3; width: 100%; height: 100%;"><div id="kk-bgvideo-player" style="position: relative"></div></div><div id="kk-bgvideo-shield" style="width: 100%; height: 100%; z-index: 4; position: absolute;"></div>';
		videoContainer.append(kkBgVideoContainer);
		
		$('#kk-bgvideo-container').css({
			'visibility': 'hidden',
			'opacity': 0
		});
		
		window.player;
		window.onYouTubeIframeAPIReady = function () {
			player = new YT.Player('kk-bgvideo-player', {
				width: options.width,
				height: Math.ceil(options.width / options.ratio),
				videoId: options.videoId,
				playerVars: {
					controls: 0,
					showinfo: 0,
					modestbranding: 0,
					iv_load_policy: 3,
					wmode: 'transparent',
					rel: 0
				},
				events: {
					'onReady': onPlayerReady,
					'onStateChange': onPlayerStateChange,
					'onPlaybackQualityChange': onPlayerPlaybackQualityChange,
				}
			});
		}
		window.onPlayerReady = function (e) {
			resize();
			if (options.mute)
				e.target.mute();
			e.target.setPlaybackQuality('default');
			e.target.setVolume(70);
			e.target.playVideo();
		}
		window.onPlayerPlaybackQualityChange = function (e) {}
		
		var isBuffered = false;
		window.onPlayerStateChange = function (state) {
			if (state.data == YT.PlayerState.PLAYING) {
				$('#kk-bgvideo-container').css({
					'visibility': 'visible',
					'opacity': 1
					
				});
			}
			if (state.data == YT.PlayerState.ENDED && options.repeat) {
				player.playVideo();	
			}			
			if (state.data == YT.PlayerState.BUFFERING && !isBuffered) {
				isBuffered = true;
			}
			if (state.data == YT.PlayerState.UNSTARTED && !player.isMuted() && isBuffered) {
				player.mute();
				player.playVideo();				
            }
			
			
		}
		var resize = function () {
			var width = $('#kk-bgvideo-container').width(),
			pWidth,
			height = $(window).height(),
			pHeight,
			$kkBgVideoPlayer = $('#kk-bgvideo-player');
			if (width / options.ratio < height) {
				pWidth = Math.ceil(height * options.ratio);
				$kkBgVideoPlayer.width(pWidth).height(height).css({
					left: (width - pWidth) / 2,
					top: 0
				});
			} else {
				pHeight = Math.ceil(width / options.ratio);
				$kkBgVideoPlayer.width(width).height(pHeight).css({
					left: 0,
					top: (height - pHeight) / 2
				});
			}
		}
		$(window).on('resize.kkBgVideo', function () {
			resize();
		})
		
		$('body').on('click', '.' + options.playButtonClass, function (e) {
			e.preventDefault();
			player.playVideo();
		})
	}
	var tag = document.createElement('script');
	tag.src = "https://www.youtube.com/iframe_api";
	var firstScriptTag = document.getElementsByTagName('script')[0];
	firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
	$.fn.kkBgVideo = function (options) {
		return this.each(function () {
			if (!$.data(this, 'kkBgVideo_instantiated')) {
				$.data(this, 'kkBgVideo_instantiated', kkBgVideo(this, options));
			}
		});
	}
})(jQuery, window);
