jQuery(function ($) {

	/**
	 * Window resize: adjust summary and scrollbar heights.
	 */
	$(window).on('resize', function () {
		var height = $('.remodal').height();
		$('#qvipp_content .qvipp_summary').css('height', height);

		var scrollable = document.getElementsByClassName('qvipp_scrollable')[0];
		if (scrollable) {
			if (scrollable.scrollHeight > scrollable.clientHeight) {
				$('.qvipp_scrollbar_bg').css('height', height).show();
			} else {
				$('.qvipp_scrollbar_bg').hide();
			}
		}
	});

	/**
	 * Remodal opened.
	 */
	$(document).on('opened', '.remodal', function () {
		$('body').css('overflow', 'hidden');
		$('.qvipp_spinner').remove();
	});

	/**
	 * Remodal closed.
	 */
	$(document).on('closed', '.remodal', function () {
		$('body').css('overflow', 'auto');
		$('.qvipp_btn').hide();
	});

	/**
	 * Quick View button click (shop page).
	 */
	$(document).on('click', '.qvipp_quick_view', function () {
		var product_id = $(this).data('product-id');
		$(this).append('<div class="qvipp_spinner"></div>');
		qvipp_get_product_details(product_id);		
	});

	/**
	 * WooCommerce gallery image click inside modal. 
	 */
	$(document).on('click', '#qvipp_content .qvipp_product .qvipp_images a', function (e) {
		e.preventDefault();

		var img_url = $(this).attr('href');
		var img_src = $(this).find('img').attr('srcset');

		$('.qvipp_woocommerce-main-image img').attr({
			src: img_url,
			srcset: img_src
		});
		$('.qvipp_woocommerce-main-image').closest('a').attr('href', img_url);

		// re-initialize prettyPhoto
		$("a.qvipp_zoom").prettyPhoto({
			hook: 'data-rel',
			social_tools: false,
			theme: 'pp_woocommerce',
			horizontal_padding: 20,
			opacity: 0.8,
			deeplinking: false
		});
		$("a[data-rel^='prettyPhoto']").prettyPhoto({
			hook: 'data-rel',
			social_tools: false,
			theme: 'pp_woocommerce',
			horizontal_padding: 20,
			opacity: 0.8,
			deeplinking: false
		});
	});

	/**
	 * Show custom scrollbar when summary is scrollable.
	 */
	$(document).on('mouseenter', '#qvipp_content .qvipp_summary', function () {
		var scrollable = document.getElementsByClassName('qvipp_scrollable')[0];
		if (scrollable && scrollable.scrollHeight > scrollable.clientHeight) {

			var $scrollable = $('.qvipp_scrollable'),
				$scrollbar = $('.qvipp_scrollbar'),
				H = $scrollable.outerHeight(true),
				sH = $scrollable[0].scrollHeight,
				sbH = H * H / sH;

			$scrollbar.height(sbH).hide();

			$scrollable.on('scroll', function () {
				$scrollbar.css({ top: $scrollable.scrollTop() / H * sbH });
			});
			$('.qvipp_scrollbar').show();
		}
	});

	$(document).on('mouseleave', '#qvipp_content .qvipp_summary', function () {
		var scrollable = document.getElementsByClassName('qvipp_scrollable')[0];
		if (scrollable && scrollable.scrollHeight > scrollable.clientHeight) {
			$('.qvipp_scrollbar').hide();
		}
	});

	/**
	 * Ajax: get product details and load into modal.
	 */
	function qvipp_get_product_details(product_id) {
		if (product_id !== undefined) {
			$.ajax({
				type: 'POST',
				url: qvipp_frontend_obj.ajaxurl,
				data: {
					action: 'qvipp_get_product',
					product_id: product_id,
					qvipp_nonce: qvipp_frontend_obj.qvipp_nonce
				},
				success: function (response) {
					$('#qvipp_content').html(response);
					$('#qvipp_content .qvipp_summary').addClass('qvipp_scrollable');
					$('.remodal').show();

					// open modal
					var inst = $('[data-remodal-id=modal]').remodal();
					if (inst.getState() === 'closed') {
						inst.open();
					}

					var height = $('.remodal').height();
					$('#qvipp_content .qvipp_summary').css('height', height);

					// scrollbar background color
					var color = $('.remodal').css('background-color');
					$('#qvipp_content .qvipp_scrollbar_bg')
						.css('background', color)
						.html('<div class="qvipp_scrollbar"></div>')
						.css('height', height);

					var scrollable = document.getElementsByClassName('qvipp_scrollable')[0];
					if (scrollable && scrollable.scrollHeight <= scrollable.clientHeight) {
						$('.qvipp_scrollbar_bg').hide();
					}
				}
			});
		}
	}

	/**
	 * Hover effect: show quick view button.
	 */
	$('.qvipp_btn').hide();
	$(document).on('mouseover', '.size-woocommerce_thumbnail, .qvipp_btn', function () {
		$(this).parent().find('.qvipp_btn').show();
	}).on('mouseout', '.size-woocommerce_thumbnail, .qvipp_btn', function () {
		$(this).parent().find('.qvipp_btn').hide();
	});

	/**
	 * Validate Add to Cart click.
	 */
	$(document).on('click', '.single_add_to_cart_button', function () {
		var qty = $('.qty').val();
		if (qty === undefined || qty == 0) {
			$('.qty').css('border', '1px solid red');
			return false;
		}

		// check variations selected
		var pa_color = $('#pa_color').val();
		var pa_size = $('#pa_size').val();
		if (pa_color === '' && pa_size === '') {
			$('table').css({
				border: '1px solid red',
				'border-collapse': 'separate'
			});
			return false;
		}
	});

});
