var productBasePrice = 0;
$(document).ready(function() {
	$('#productAttributes :input').change(function() {
		updateSelectionPrice();
	});
	productBasePrice = parseFloat($('#productBasePrice').val());
	if (isNaN(productBasePrice)) {
		productBasePrice = 0;
	}
	$('#productAttributes :input').each(function() {
		if ($(this).attr('rel') && $(this).attr('rel').indexOf('price:') > -1) {
			var priceParts = $(this).attr('rel').split(/[:,]+/);
			var j = priceParts.length;
			for(var i=0; i < j; i = i +2) {
				switch(priceParts[i].toLowerCase()) {
					case 'price':
						var optionPrice = priceParts[i+1];
						break;
					case 'base':
						productBasePrice -= optionPrice;
					break;
				}
			}
		}
	});
	updateSelectionPrice();
});

function updateSelectionPrice() {
	var productTotal = productBasePrice;
	$('#productAttributes :input:checked').each(function() {
		if ($(this).attr('rel') && $(this).attr('rel').indexOf('price:') > -1) {
			var priceParts = $(this).attr('rel').split(/[:,]+/);
			var j = priceParts.length;
			for(var i=0; i < j; i = i +2) {
				switch(priceParts[i].toLowerCase()) {
					case 'price':
						productTotal += parseFloat(priceParts[i+1]);
					break;
				}
			}
		}
	});
	if ($('.multiplyAttributeContainer input').not('input[type="hidden"]').length > 0) {
		var total = 1;
		$('.multiplyAttributeContainer input').not('input[type="hidden"]').each(function() {
			total = total * $(this).val();
		});
		total = total * parseFloat($('#attributePerUnit').html());
		if ($('#attributeOneTime').length > 0) {
			total += parseFloat($('#attributeOneTime').html());
		}
		productTotal += total;
	}
	$('#attributeRateFinal').html(productTotal.toFixed(parseInt($('#attributeRateFinal').attr('rel'))));
}


