// catch the input
var bed_size = '';
var bed_type = '';
var mechanism = '';
var mattress = '';
var power_socket = '';
var headboard_size = '';
var headboard_style = '';
var feet = '';
var security_box = '';
var dust_cover = '';
var fabric = '';
var fabric_type = '';
var delivery_option = '';
var pockets = '';
var mechanism_lock = '';
var delivery_guaranteed = '';

// temp
var temp = '';

$(document).ready(function() {
	$("input").click(function() {
		var name = $(this).attr('name');
		switch(name)
		{
		case 'bed_type':
			set_bed_type($(this));
			break;
			
		case 'bed_size':
			set_bed_size($(this));
			break;
		case 'delivery_option':
			set_delivery_option($(this));
			break;
		}
	});	
});

function set_mechanism()
{
	temp = $("#mechanism option:selected").attr('value');
	if(temp != 0)
	{
		mechanism = temp;
		update_totals();
	}
}

function set_mattress()
{
	temp = $("#mattress option:selected").attr('value');
	if(temp != 0)
	{
		mattress = temp;
		update_totals();
		change_mattress_visuals();
	}	
}

function set_mattress_alt()
{
	set_mattress();
	var quilt = mattress_list[mattress];
	if(quilt=='blank')
	{
		$("#design-quilt").hide();
	}
	else
	{
		$("#design-quilt").show();
	}
}

function change_mattress_visuals()
{
	$("#design-mattress img").attr('src','/images/order-images/mattresses/' + mattress_list[mattress] + '.png');
}

function set_power_socket()
{
	temp = $("#power_socket option:selected").attr('value');
	if(temp != 0)
	{
		power_socket = temp;
		update_totals();
	}
}

function set_headboard_size()
{
	temp = $("#headboard_size option:selected").attr('value');

	if(temp != 0)
	{
		headboard_size = temp;
		update_totals();
	}
}

function set_headboard_style()
{
	temp = $("#headboard_style option:selected").attr('value');
	if(temp != 0)
	{
		headboard_size = 'reset';
		headboard_style = temp;
		update_headboard_sizes();
		update_totals();
	}
}

function set_feet()
{
	temp = $("#feet option:selected").attr('value');
	if(temp != 0)
	{
		feet = temp;
		change_feet_visuals();
		update_totals();
	}
}

function set_security_box()
{
	temp = $("#security_box option:selected").attr('value');
	if(temp != 0)
	{
		security_box = temp;
		update_totals();
	}
}
function set_bed_type_alt()
{
	temp = $("#bed_type option:selected").attr('value');
	if(temp != 0)
	{
		bed_type = temp;
		pockets = $("#pockets option:selected").attr('value');
		if(original_bed_type==3 & original_bed_type != bed_type || bed_type == 3)
		{
			show_fabric_message();
			original_bed_type = bed_type;
			reset_fabric_alt();
			reset_headboard_alt();
			
			pockets = 'reset';
			if(bed_type == 3)
			{
				hide_pockets();
			}
			else
			{
				show_pockets();
			}
			
		}
		
		
		change_bed_mask();
		update_totals();
	}
}

function hide_pockets()
{
	$("#pockets_container").hide();
}

function show_pockets() 
{
	$("#pockets_container").show();
}

function set_bed_type(t)
{
	// cause the costs all work in a radge format we need to get the size as well
	bed_size = $("input[name=bed_size]:checked").attr('value');
	bed_type = t.attr('value');
	if(bed_type == 3)
	{
		reset_fabric();
		reset_headboard();
	}
	
	if(original_bed_type==3 && original_bed_type != bed_type)
	{
		original_bed_type = bed_type;
		reset_fabric();
		reset_headboard();
	}
	
	change_bed_mask();
	update_totals();
}

function set_bed_size_alt()
{
	temp = $("#bed_size option:selected").attr('value');
	if(temp != 0)
	{
		bed_size = temp;
		update_totals();
	}
}

function set_bed_size(t)
{
	bed_size = t.attr('value');
	bed_type = $("input[name=bed_type]:checked").attr('value');
	update_totals();
}

function set_dust_cover()
{
	temp = $("#dust_cover option:selected").attr('value');
	if(temp != 0)
	{
		dust_cover = temp;
		update_totals();
	}
}

function set_fabric(fid)
{
	fabric = fid;
	toggle_fabric_message();
	update_totals();
}

function set_fabric_alt()
{
	temp = $("#fabric_id").attr('value');
	if(temp != 0)
	{
		fabric = temp;

		toggle_fabric_message();
		
		update_fabric_visuals(fabric_list[fabric]);
		update_headboard_fabric_visuals(fabric_list[fabric]);
		update_totals();
	}
}

function set_fabric_type()
{
	temp = $("#fabric_type option:selected").attr('value');
	if(temp != 0)
	{
		fabric_type = temp;
		update_totals();
	}
}

function set_delivery_option()
{
	temp = $("#delivery_option option:selected").attr('value');
	if(temp != 0)
	{
		delivery_option = temp;
		update_totals();
	}
}function set_delivery_guaranteed()
{
	temp = $("#delivery_guaranteed option:selected").attr('value');
	if(temp != 0)
	{
		delivery_guaranteed = temp;
		update_totals();
	}
}

function set_pockets()
{
	temp = $("#pockets option:selected").attr('value');
	if(temp != 0)
	{
		pockets = temp;
		update_totals();
	}
}

function set_mechanism_lock()
{
	temp = $("#mechanism_lock option:selected").attr('value');
	if(temp != 0)
	{
		mechanism_lock = temp;
		update_totals();
	}
}

function update_headboard_sizes()
{
	$("#headboard_sizes_sel").load('/design_your_bed/headboard_sizes/' + headboard_style);
}

function reset_headboard()
{
	$("#design-headboard-mask img").attr('src',default_headboard_mask);
	headboard_size = 'reset';
	headboard_style = 'reset';
}

function reset_headboard_alt()
{
	reset_headboard();
	$("#headboard_options").load('/design_your_bed/design-phase5_headboards/' + $("#bed_type").attr('value'));
}

function reset_fabric()
{
	$("#design-fabric img").attr('src','/images/order-images/fabrics/blank.png');
	$("#design-headboard-fabric img").attr('src','/images/order-images/fabrics/blank.png');
	fabric = 'reset';
	fabric_type = 'reset';
}

function reset_fabric_alt()
{
	reset_fabric();
	$("#fabric_options").load('/design_your_bed/design_phase5_fabriclist/' + $("#bed_type").attr('value') + '/' + fabric);
}

function change_fabrics_alt()
{
	$("#fabric_sel").load('/design_your_bed/design-phase5_fabrics/' + $("#fabric_range").attr('value'));
}

function update_headboard_fabric_visuals(file_name)
{
	$("#design-headboard-fabric img").attr('src','/images/order-images/headboards/' + file_name + '.png');
}

function change_bed_mask()
{
	$("#design-simplicity img").attr('src','/images/order-images/frame/' + bed_type_list[bed_type] + '-frame.png');
	$("#design-feet").removeClass();
	var new_class = bed_type_list[bed_type];
	$("#design-feet").addClass(bed_type_list[bed_type] + '-feet');
}

function change_feet_visuals()
{
	$("#design-feet img").attr('src','/images/order-images/feet/' + feet_list[feet] + '.png');
}

function update_totals()
{
	$("#design-progress").load('/design_your_bed/sidebar/',
	{
		dyb: 'TRUE',
		type: bed_type,
		size: bed_size,
		mechanism: mechanism,
		mattress: mattress,
		power_socket: power_socket,
		feet: feet,
		security_box: security_box,
		dust_cover: dust_cover,
		headboard_size: headboard_size,
		headboard_style: headboard_style,
		fabric: fabric,
		fabric_type: fabric_type,
		pockets: pockets,
		mechanism_lock: mechanism_lock,
		delivery_option: delivery_option,
		delivery_guaranteed: delivery_guaranteed
	});
}


function billing_to_delivery(t)
{
	if($(t).is(':checked'))
	{
		$("#delivery_line1").attr('value',$("#billing_line1").attr('value'));
		$("#delivery_line2").attr('value',$("#billing_line2").attr('value'));
		$("#delivery_city").attr('value',$("#billing_city").attr('value'));
		$("#delivery_postcode").attr('value',$("#billing_postcode").attr('value'));
	}
}


function toggle_fabric_message()
{
	if(fabric == '' || fabric == 'reset')
	{
		show_fabric_message();
	}
	else
	{
		hide_fabric_message();
	}
}
function hide_fabric_message()
{
	$("#fabric_message").fadeOut("2000");
}
function show_fabric_message()
{
	$("#fabric_message").fadeIn("2000");
}