$(function() {
	$('#fp-carousel').jCarouselLite({
		visible: 3,
		speed: 1000,
		auto: 6000
	});

	$('.MalermalingProductTemplate .product-image').colorbox({inline: true, href: function() {return $(this).attr("cfImgContainer");}});

	$('.color').click(function() {
		var $box = $('.choose-color');
		$box.attr('_cfProduct', $(this).closest('form').attr('_cfProduct'));
		$('div.color-palette table').css('display', 'none');
		$('#' + $(this).attr('_cfColorCode')).css('display', 'block');
		$.colorbox({ inline: true, href: $box });
	});

	$('div.color-palette table td').click(function() {
		var $color_code = jQuery.trim($(this).text());
		if ($color_code) {
			var $choose_color = $(this).closest('.choose-color');
			var $product_id = $choose_color.attr('_cfProduct');
			$('.color-input-' + $product_id).val($color_code);
			$('.color-container-' + $product_id).html('<div style="height: 20px; width: 60px; background-color: ' + $(this).children().first().css('background-color') + ';">&nbsp;</div>' + $color_code);
			$.colorbox.close();
		}
	});

	$('.choose-color .button').click(function() {
		var $choose_color = $(this).closest('.choose-color');
		var product_id = $choose_color.attr('_cfProduct');
		var color_code = $choose_color.find('.color-code').val();
		var brand = $choose_color.find('.paint-brand').val();
		$('.color-input-' + product_id).val(brand + ' ' + color_code);
		$('.color-container-' + product_id).html(brand + ' ' + color_code);
		$.colorbox.close();
	});

	$('.paint-brand-select').change(function() {
		var brand = $(this).children(':selected').text();
		$(this).closest('.choose-color').find('.paint-brand').val(brand);
	});

	$('.calc').click(function() {
		var $box = $(this).closest('form').find('.calc-amount');
		$.colorbox({ inline: true, href: $box });
	});

	$('.calc-amount .calc-button').click(function() {
		var $calc_amount = $(this).closest('.calc-amount');
		var coverage = $calc_amount.attr('_cfCoverage');
		var area = $calc_amount.find('.amount-input').val();
		$calc_amount.find('.amount-needed').html('Du skal bruge: <b>' + (area / coverage).toFixed(1) + ' L</b>');
	});

	$('form.product').submit(function() {
		if ($(this).find('.choose-color').length && !$(this).closest('form').find('input[name=meta]').val()) {
			var $box = $(this).find('.choose-color');
			$.colorbox({ inline: true, href: $box });
			return false;
		}
	});

	$('.checkout-content form#checkout_customer input[name=delivery]').before($('.delivery-no-receipt').detach());
	$('.checkout-content form#checkout_customer input[name=delivery]').before($('.newsletter').detach());

	$('.tpc_top_left').css('background-position', '-3px -100px')
	$('.tpc_top_right').css('background-position', '-3px -100px')

	$('.col3').height($('#content').height());

	if (!$('#frontpage-content').length) {
		$('#right-section').stickySidebar({ constrain: true });
	}

	$('.checkout-customer-zip-input input').change(function() {
		if ($(this).val().length > 2) {
			if (!$('#checkout-customer-city-loading').length) {
				$('.checkout-customer-input2[name="city"]').css('background', 'url(/img/loading.gif) right no-repeat');
			}
			$.getJSON('/remote.php', { cmd: 'Location::zipToCity', zip: $(this).val() }, function(result) {
				$('.checkout-customer-input2[name="city"]').css('background', '');
				$('.checkout-customer-input2[name="city"]').val(result).keyup();
			});
		}
	});

	$('.checkout-customer-zip-input input').keyup(function() {
		if ($(this).val().length > 2) {
			$(this).change();
		}
	});

	$('.imgzoom').jqzoom({
		 zoomType: 'innerzoom',
		 title: false
	});
});
