$(document).ready(function() {
	$('input.spin').spin({
		max: 99,
		min: 0,
		lock: false
	});
});

// --- Item detail ---
function initOrder() {
	$(document).ready(function() {
		// --- Init ---
		setTotalPrice();

		// --- Handle total price ---
		$('#orderForm .accessoryBox').change(function() {
			setTotalPrice();
		});

		$('#orderForm .colorBox').click(function() {
			setTotalPrice();
		});

	});
}

// --- Order form page ---
function initOrder2() {
	$(document).ready(function() {
		
		// --- Init ---
		setTotalPrice2();

		// --- Handle total price ---
		$('#kalkulace .accessoryBox').change(function() {
			setTotalPrice2();
		});

		$('#kalkulace .productBox').change(function() {
			setTotalPrice2();
		});

		$('#kalkulace .colorBox').click(function() {
			setTotalPrice2();
		});

	});
}

// --- Re-count total price ---
function setTotalPrice() {
	var totalPrice = 0;

	// --- Product Price ---
	totalPrice += parseInt($('#item-price').html());

	// --- Accessories ----
	$('#orderForm .accessoryBox').each(function() {
		var amount = parseInt($(this).val());
		if (amount > 0) {
			var nameFull = $(this).attr('name');
			var re = /[%]+/;
			nameFull = nameFull.split(re);
			var price = nameFull[1];
			
			totalPrice += parseInt(price) * amount;
		}
	});

	// --- Colors ----
	$('#orderForm .colorBox:checked').each(function() {
		var nameFull = $(this).val();
		var re = /[%]+/;
		nameFull = nameFull.split(re);
		if (nameFull.lenght != 0) {
			var price = nameFull[1];
			totalPrice += parseInt(price);
		}
	});

	$('#total-price span').html(totalPrice);
}

// --- Re-count total price ---
function setTotalPrice2() {
	var totalPrice = 0;

	// --- Products ----
	$('#kalkulace .productBox').each(function() {
		var amount = parseInt($(this).val());
		if (amount > 0) {
			var nameFull = $(this).attr('name');
			var re = /[%]+/;
			nameFull = nameFull.split(re);
			var price = nameFull[1];
			price = price.slice(0, price.length - 1);

			totalPrice += parseInt(price) * amount;
			
			$(this).parent().parent().find('.colorDiv').show();
		}
		else {
			$(this).parent().parent().find('.colorDiv').hide();
		}
	});

	// --- Accessories ----
	$('#kalkulace .accessoryBox').each(function() {
		var amount = parseInt($(this).val());
		if (amount > 0) {
			var nameFull = $(this).attr('name');
			var re = /[%]+/;
			nameFull = nameFull.split(re);
			var price = nameFull[1];
			price = price.slice(0, price.length - 1);

			totalPrice += parseInt(price) * amount;
		}
	});

	// --- Colors ----
	$('#kalkulace .colorBox:checked').each(function() {
		var nameFull = $(this).val();
		var re = /[%]+/;
		nameFull = nameFull.split(re);
		if (nameFull.lenght != 0 && $(this).parent().parent().parent().find('.productBox').val() > 0) {
			var price = nameFull[1];
			totalPrice += parseInt(price);
		}
	});

	$('#total-price span').html(totalPrice);
}
