
$().ready(function(){

	// Width
	var min_width = 50;
	var max_width = 300;
	var priced_width = 95;
	var width_prices = new Array(3,4,4,6,2); // 3,4,4,6

	// Length
	var min_length = 140;
	var max_length = 250;
	var priced_length = 200;
	var length_prices = new Array(5,7,7,15,2); // 5,7,7,15

	// Depth
	var min_depth = 7;
	var max_depth = 30;
	var priced_depth = 20;
	var depth_prices = new Array(25,25,25,25,100); // 25,25,25,25

	// Calculator
	$('.calculator_block_index input, #calculator-form input, .calculator_big_block input').filter('[type=text]:not([readonly=yes])').focus(function(){
		$(this).select();
	});

	var typeDescriptions = new Array('A Support Foam only mattress made from reflex foam with a zip off cotton removable cover','Memory foam mattress with a reflex foam support base. with a Zip removable Quilted Cover. Soft medium feel 48 hour Delivery.','Best selling Mattress, Memory Foam Top layer with a Ortho Support Foam Base + Coolmax cover. Shipped within 48 hours','Memory Foam Airflow with laytec middle & Ortho Support base.Offers a medium to firm sleep. Delivery in 5 Days','7cm high grade memory foam topper made to measure with cover. Depth for this topper is a standard 7cm');
	var typeImages = new Array('/site/detox/files/calculator/small_1.jpg','/site/detox/files/calculator/small_2.jpg','/site/detox/files/calculator/small_3.jpg','/site/detox/files/calculator/small_4.jpg','/site/detox/files/calculator/small_9.jpg');
	
	$('#description').text(typeDescriptions[0]);
	$('#calculator-form img').attr('src', typeImages[0]);
	$('#type').change(function(){
		$('#description').text(typeDescriptions[$('#type').val()]);
		$('#calculator-form img').attr('src', typeImages[$('#type').val()]);
	});

	$('.calculator_index input, .calculator_big_block input, .calculator_block_index input, #calculator-form input, #calculator-form select').change(function(){
		var foots = ($('#foots').val());
		if (isNaN(foots)) foots = 0;
		else foots = parseInt(foots);
		
		var inches = ($('#inches').val());
		if (isNaN(inches)) inches = 0;
		else inches = parseInt(inches);
		
		$('#centimeters').val(Math.round((inches+foots*12)*2.54, 2) + ($('.calculator_big_block').length ? '' : ' cm'));

		var width = $('#width').val();
		
		if (isNaN(width)) width = 0;
		else width = parseInt(width);
		
		if (width == 0) {$('#width').val('');}
		else if (width < min_width) {width=min_width; $('#width').val(min_width);}
		else if (width > max_width) {width=max_width; $('#width').val(max_width);}
		
		var length = $('#length').val();
		
		if (isNaN(length)) length = 0;
		else length = parseInt(length);
		
		if (length == 0) {$('#length').val('');}
		else if (length < min_length) {length=min_length; $('#length').val(min_length);}
		else if (length > max_length) {length=max_length; $('#length').val(max_length);}
		
		var depth = $('#depth').val();
		
		if (isNaN(depth)) depth = 0;
		else depth = parseInt(depth);
		
		if (depth == 0) {$('#depth').val('');}
		else if (depth < min_depth) {depth=min_depth; $('#depth').val(min_depth);}
		else if (depth > max_depth) {depth=max_depth; $('#depth').val(max_depth);}
		
		var base_prices = new Array(119,169,345,550,109); // 139, 249, 345, 550
		
		// Calculate price
		var type = $('#type').val();
		var price = base_prices[type];
		var width_price = width_prices[type];
		var length_price = length_prices[type];
		var depth_price = depth_prices[type];
		
		if (width > priced_width) price += width_price*(width-priced_width);
		if (length > priced_length) price += length_price*(length-priced_length);
		if (depth > priced_depth) price += depth_price*(depth-priced_depth);
		
		$('#price').val(price);
	});
	/*
	$('#width').val(min_width);
	$('#length').val(min_length);
	$('#depth').val(min_depth).trigger('change');
	*/
	$('#depth').trigger('change');
});