function slideSwitch() {
	var $active = $('#frontpage-slideshow DIV.active');
	var $next = $active.next();
	if (!$next.length) {
		$next = $('#frontpage-slideshow').children().first();
	}
	var $prev = $active.prev();
	if (!$prev.length) {
		$prev = $('#frontpage-slideshow').children().last();
	}
	// Fade out / in
	$prev.fadeOut('slow', function() { $active.fadeIn('slow') });

	$next.addClass('active');
	$active.removeClass('active');
}

$(function() {
	$('.product-quantity INPUT').change(function() {
		if ($('.product-stock-left').val() - $(this).val() < 0) {
			$('.insufficient-stock').remove();
			$('.product-buy').css('display', 'none');
			$('.product-buy').before('<div class="product-buy insufficient-stock">' + ($(this).val() == 1 || $('.product-stock-left').val() < 1 ? 'The specified size and color is currently not in stock.' : 'We currently only have ' + $('.product-stock-left').val() + ' of the specified size and color in stock.') + '</div>');
		} else {
			$('.insufficient-stock').remove();
			$('.product-buy').css('display', 'block');
		}
	}).keyup(function() { $(this).change(); });

	$('form.product').submit(function() {
		if ($('.product-stock-left').val() - $('.product-quantity INPUT').val() < 0) {
			return false;
		}
	});

	$('.variant-selector').change(function() {
		$('.product-quantity INPUT').change();
	}).keyup(function() { $(this).change(); });

	var hiddenVariants = $('<select/>');
	if ($('.color-picker').length) {
		$('.variant-selector').children().each(function() {
			hiddenVariants.append($(this).detach());
		});
	}

	$('.product-variants').prepend($('.color-panel').detach());

	$('.color-picker').click(function() {
		var color = $(this).attr('_cfColor');
		var selected = (hiddenVariants.children().length != 0 ? $('.variant-selector option:selected').text() : null);
		var selected_found = false;
		$('.variant-selector').append(hiddenVariants.children().detach());
		$('.variant-selector').children().each(function() {
			if (color != $(this).attr('_cfColor') || ($(this).attr('_cfStock') - 0 == 0 && $(this).text() != selected)) {
				hiddenVariants.append($(this).detach());
			}
		});

		// Select the last picked size if it exists
		$('.variant-selector').children().each(function() {
			if ($(this).text() == selected) {
				$('.variant-selector').val($(this).attr('value'));
				selected_found = true;
				return false;
			}
		});

		// Make a default select if existing size is not found
		if (!selected_found) {
			$('.variant-selector').children().each(function() {
				$('.variant-selector').val($(this).attr('value'));
				return false;
			});
		}

		$('.variant-selector').change();
	});

	$('.color-picker').first().click();

	if ($('#frontpage-slideshow').length) {
		slideSwitch();
		setInterval( "slideSwitch()", 3000 );
	}

	$('#product-fabric-popup H2').text($('#product-fabric').text());

	$('#product-fabric').colorbox({inline: true, href: "#product-fabric-popup"});

	$('#product-size-chart-popup H2').text($('#product-size-chart').text());

	$('#product-size-chart').colorbox({inline: true, href: "#product-size-chart-popup"});

	$('.checkout-cart-promotion-code INPUT').first().keypress(function(e) {
		if (e.keyCode == 13) {
			e.preventDefault();
		}
	});

	$('.product-quantity INPUT').keypress(function(e) {
		if (e.keyCode == 13 && $('.product-stock-left').val() - $(this).val() < 0) {
			e.preventDefault();
		}
	});

	$('.contact-email').html('&#119;&#101;&#098;&#115;&#104;&#111;&#112;&#064;&#112;&#117;&#114;&#101;&#108;&#105;&#109;&#101;&#046;&#099;&#111;&#109;')
	$('.contact-email').attr('href', 'mailto:' + $('.contact-email').text());

	$('.contact-email-uk').html('&#109;&#97;&#114;&#116;&#105;&#110;&#104;&#117;&#100;&#115;&#111;&#110;&#64;&#112;&#117;&#114;&#101;&#108;&#105;&#109;&#101;&#46;&#99;&#111;&#109;')
	$('.contact-email-uk').attr('href', 'mailto:' + $('.contact-email-uk').text());

	$('.contact-email-us').html('&#112;&#117;&#114;&#101;&#108;&#105;&#109;&#101;&#117;&#115;&#97;&#64;&#112;&#117;&#114;&#101;&#108;&#105;&#109;&#101;&#46;&#99;&#111;&#109;')
	$('.contact-email-us').attr('href', 'mailto:' + $('.contact-email-us').text());

	$('.contact-email-nl').html('&#105;&#110;&#102;&#111;&#64;&#112;&#117;&#114;&#101;&#108;&#105;&#109;&#101;&#46;&#110;&#108;')
	$('.contact-email-nl').attr('href', 'mailto:' + $('.contact-email-nl').text());

	$('.checkout-receipt-print').click(function() { window.print(); });

	$('.menu.info a').first().css('color', 'white');
	$('.menu.info a').first().css('font-weight', 'bold');

	$('.checkout-content form#checkout_customer input[name=delivery]').before($('.newsletter').detach());

	$('#language-menu').colorbox({ inline: true, href: "#language-select" });

	if ($('#language-select').hasClass('missing-country')) {
		$.colorbox({ inline: true, href: "#language-select" });
	}

	$('html[lang="da"] .checkout-customer-country').css('display', 'none');
	$('html[lang="da"] .checkout-customer-country-input').css('display', 'none');
	$('html[lang="da"] .checkout-customer-country-input select').detach();
	$('html[lang="da"] .checkout-customer-country-input').append('<input type="hidden" value="Danmark" name="country" />');

	$('html[lang="da"] .checkout-customer-delivery-country').css('display', 'none');
	$('html[lang="da"] .checkout-customer-delivery-country-input').css('display', 'none');
	$('html[lang="da"] .checkout-customer-delivery-country-input select').detach();
	$('html[lang="da"] .checkout-customer-delivery-country-input').append('<input type="hidden" value="Danmark" name="delivery_country" />');

	$('html[lang="en"] .checkout-customer-country-input select option').each(function() {
		if ($(this).text() == 'Denmark') {
			$(this).text('Denmark (switch to Danish webshop)')
			$(this).attr('disabled', 'disabled');
		}
	});

	$('html[lang="da"] .icons img').each(function() {
		if (!$(this).hasClass('DANKORT')) {
			$(this).css('display', 'none');
		}
	});
	$('html[lang="en"] .icons img').each(function() {
		if ($(this).hasClass('DANKORT')) {
			$(this).css('display', 'none');
		}
	});
	var count = 0;
	$('html[lang="da"] #checkout-paymenttypes fieldset').each(function() {
		if (count > 0) {
			$(this).css('display', 'none');
		}
		count++;
	});

});
