/*
 * (C) 2009 Foamshop.co.uk
 * Author: Christian Piper (Christian@design-desires.co.uk)
 */

/*
 * Avoid conflicts with prototype
 */
var $j = jQuery.noConflict();

/*
 * Setup the initial product view, we check teh values of fields due to the
 * fact FF retains the last values on a page reload, keeps things pretty :)
 */
$j(document).ready(function () {
	checkType();
	tickingPic();
	pingQuote();
});

/*
 * Fired when a select box value has changed
 */
function pingQuote() {
	var type = $j('[name=type]').val();
	if(type == 5) {
		checkType();
		var depth = parseInt($j('[name=toplayer]').val())+parseInt($j('[name=midlayer]').val())+parseInt($j('[name=botlayer]').val());
	} else {
		var depth = $j('[name=depth]').val();
	}
	var width = $j('[name=width]').val();
	var length = $j('[name=length]').val();
	var extras = $j('[name=extras]').val();
	var sand = $j('[name=sandwich]').val();
	var call = $j.ajax({
		type: "POST",
		url: "/tools/getmattressprice.php",
		data: ({t: type, w: width, d: depth, l: length, e: extras, s: sand}),
		cache: false,
		dataType: "html",
		success: function(foo, textStatus){
			$j('#price').html(foo);
		}
	});
}

function tickingPic() {
	var i = $j('[name=extras]').get(0).selectedIndex;
	switch(i){
		case 0:
			$('tickingimage').update('');
		break;
		case 1:
			$('tickingimage').update('<img src="/media/mattress1.jpg" style="float: right" class="cms-image" alt="" />');
		break;
	}
}

function checkType() {
	var i = $j('[name=type]').get(0).selectedIndex;
	switch(i){
		case 0:
			$('foamimage').update('<img src="/media/mattress/regal.jpg" style="float: right" class="cms-image" alt="" />');
			$('foam_choice_text').update('<p style="padding: 30px 20px 20px 0"><strong>This high quality foam provides excellent shape retaining properties which give many years of comfort and support, making it a first choice for daily/nightly use.</strong></p>');
		break;
		case 1:
			$('foamimage').update('<img src="/media/mattress/imperial.jpg" style="float: right" class="cms-image" alt="" />');
			$('foam_choice_text').update('<p style="padding: 30px 20px 20px 0"><strong>This foam is an ideal choice when price is critical. It will provide a springy comfort and is suitable for daily/nightly use.</strong></p>');
		break;
		case 2:
			$('foamimage').update('<img src="/media/mattress/stately_memory.jpg" style="float: right" class="cms-image" alt="" />');
			$('foam_choice_text').update('<p style="padding: 30px 20px 20px 0"><strong>The ultimate in quality and comfort. A combination of high grade, deep visco elastic "memory" foam and melamine free reflex foam.</strong></p>');
		break;
		case 3:
			$('foamimage').update('<img src="/media/mattress/latex.jpg" style="float: right" class="cms-image" alt="" />');
			$('foam_choice_text').update('');
		break;
		case 4:
			$('foamimage').update('');
			$('foam_choice_text').update('<p style="padding: 30px 20px 20px 0"><strong>We can offer a combination of foam thicknesses and densities that can be bonded together to suit your requirements.</strong></p>');
		break;
	}
	if (i == 4) {
		$j('[name=sandwich]').attr("disabled", false);
		$j('#sandfield').show();
		$j('#depth-select').hide();
	} else {
		$j('[name=sandwich]').attr("disabled", true);
		$j('#sandfield').hide();
		$j('#depth-select').show();
	}
}
