let vid = {
	onReady() {
		vid.videoModal();
	},
	videoModal() {
		// define modal
		let modal = '<div class="videoModal"><div class="videoContainer"><div class="videoWrap"></div><</div></div>';
		// add modal
		$('body').append(modal);
		// remove linking
		$('.btn-launch-video').each(function(){
			$(this).removeAttr("onclick");
		});
		// get video from link value
		$("body").on("click", ".btn-launch-video", (function(e){
			// prevent default
			e.preventDefault();
			// define video link from data url or href fallback
			let vidLink = jQuery(this).attr("data-url") !== undefined ? jQuery(this).attr("data-url") : jQuery(this).attr("href");
			// create video element with link
			let video = '<video controls autoplay><source src="'+vidLink+'" type="video/mp4"></video>';
			// add video to modal
			$('.videoWrap').append(video);
			// show modal
			$('.videoModal').addClass("show");
		}));
		$('.videoModal').click(function(e){
			if($(e.target).hasClass('videoModal')) {
				$('.videoModal .videoWrap video').remove();
				$('.videoModal').removeClass("show");
			}
		});
	},
	productPage() {
		$('.toggle').each(function(){
			$(this).slideUp();
		});
		$('.btn-toggle').click(function(e){
			e.preventDefault();
			$(this).next().slideToggle();
		});
	}
}

let customPopup = {
	onReady() {
		customPopup.showPopup();
	},
	showPopup() {
		if(jQuery('#home').length > 0) {
			setTimeout(
				function(){
					jQuery('#popup-modal').modal('show')
				}, 3000
			);
		}
	}
}

let navClickStickyDD = {
	onReady(){		
		$('nav.navbar ul.navbar-nav > li.dropdown').find('a').each(function(i,dropdown){
			if( $(dropdown).data('url') == 'javascript:void(0)' || $(dropdown).data('url') == '#' ){
				let parentLi = $(dropdown).closest('li.dropdown');
				parentLi.click(function(){
					if( $(this).hasClass('dropdown-stays-open') ){
						$(this).removeClass('dropdown-stays-open');
						$('nav.navbar ul.navbar-nav > li.dropdown').removeClass('dropdown-stays-open');
					} else {
						$('nav.navbar ul.navbar-nav > li.dropdown').removeClass('dropdown-stays-open');
						$(this).addClass('dropdown-stays-open');
					}
				});
			}
		});
	}
}

$(document).ready(function(){
	vid.onReady();
	vid.productPage();
	customPopup.onReady();
	$('#manually-open-popup').on('click', function(){
		$('#popup-modal').modal('show');
	});
	//
	navClickStickyDD.onReady();
});