$(function () {
	moreInfoPanels();
	moreInfoImages();
});

function moreInfoPanels() {

	// For each more info panel...
	$('.moreInfo_').each(function () {

		// Grab references to their open buttons.
		var panel = $(this);
		var openButtons = $('[data-open-more-info-panel-with-id=' + panel.attr('id') + ']');

		// Ensure when open buttons are hovered upon, the cursor changes to indicate clickability.
		openButtons.css('cursor', 'pointer');

		// Prepare modal dialogue.
		panel.jqm({
			closeClass: 'moreInfo_close',
			overlay: 85,
			toTop: true,
			trigger: openButtons
		});
	});
}

function moreInfoImages() {
	var moreInfoPanels = $('.moreInfo_');

	moreInfoPanels.each(function () {
		var moreInfoPanel = $(this);
		var thumbnails = $('.moreInfo_thumbnailList a', moreInfoPanel);
		var imageContainer = $('.moreInfo_enlargedImage', moreInfoPanel);

		// When a thumbnail is clicked.
		thumbnails.click(function (e) {
			e.preventDefault();
	
			// Grab references to the image within the thumbnail and its corresponding enlarged image.
			var thumbnail = $(this);
			var thumbnailImage = $('img', thumbnail);
			var image = $('img', imageContainer);
	
			// Calculate new src and href attributes for thumbnail and image.
			var thumbnailSrc = image.attr('data-thumbnail-url');
			var thumbnailHref = image.attr('src');
			var thumbnailAlt = image.attr('alt');
			var imageSrc = thumbnail.attr('href');
			var imageHref = thumbnailImage.attr('src');
			var imageAlt = thumbnailImage.attr('alt');
			
			// Remove old images from the DOM.
			thumbnailImage.remove();
			image.remove();
	
			// Add new images to the DOM.
			imageContainer.append('<img src="' + imageSrc + '" data-thumbnail-url="' + imageHref + '" title="' + imageAlt + '" alt="' + imageAlt + '" />');
			thumbnail.attr('href', thumbnailHref);
			thumbnail.append('<img src="' + thumbnailSrc + '" alt="' + thumbnailAlt + '" />');
		});
	});
}

//
// Set product id to hidden field.
//
function setAddToBasketID(theid) {
	$('#store_addToBasketID').val(theid);
}
