// 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();
		/* Updated by Kavitha */
		reset_dust_cover(temp);
		reset_security(temp);
		/* End update */
	}
}

/* Updated by Kavitha */
 function reset_dust_cover(mech_value)
 {
	var temp = $("#dust_cover option:selected").attr('value');
	$("#dustcover_options").load('/design_your_bed/dustcover/'+mech_value+'/'+temp);
 }
 
 function reset_security(mech_value)
 {
   if(mech_value == 3){
     $("#security_box").val(1);
     $("#security_options").css('display', 'none');
     set_security_box();
   }
   else
     $("#security_options").css('display', 'block');
 }
/* End update */
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()
{
	/* Updated by Kavitha */
	//temp = $("#power_socket option:selected").attr('value');
	temp = $("#power_socket").attr('value');
	/* End update */
	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');
		/* Updated by Kavitha */
		if(original_bed_type==5 & original_bed_type != bed_type || bed_type == 5) //air model
		{
          reset_headboard_air(temp);
		}//if
		
		if(original_bed_type==2 & original_bed_type != bed_type || bed_type == 2){ //plus model
			reset_fabric_alt();
		}
		
		original_bed_type = bed_type;
		/* End update */
		change_bed_mask();
		/* Updated by Kavitha */
		bed_size = $("#bed_size option:selected").attr('value');
		change_image_title(bed_type, bed_size);
		/* End update */
		update_totals();

		/* Updated by Kavitha */
		reset_feet_options(temp);
		/* End update */
	}
}

/* Updated by Kavitha */
function reset_feet_options(new_bedtype){
	var sel = $("#feet option:selected").attr('value');
    var loadfeet = false;
    
	if((sel == 6) && (new_bedtype!=4)) //wooden feet and not max
     loadfeet = true;
	else if((sel != 6) && (new_bedtype==4)) //not wooden feet and max
     loadfeet = true;
    else if((sel == 7) && (new_bedtype!=5)) //metal feet and not air
     loadfeet = true;
	else if((sel != 7) && (new_bedtype==5)) //not metal feet and air
     loadfeet = true;

    if(loadfeet == true)
      $("#feet_options").load('/design_your_bed/feetoptions/' + new_bedtype );
}//fn
/* End update */

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 == 5) //air model
	{
       	$("#design-headboard-fabric img").attr('src','/images/order-images/fabrics/blank.png');
		reset_headboard();
	}

	if(original_bed_type==5 && original_bed_type != bed_type)
	{
	    $("#design-headboard-fabric img").attr('src','/images/order-images/fabrics/blank.png');
		reset_headboard();
	}
    original_bed_type = bed_type;
	change_bed_mask();
	/* Updated by Kavitha */
	if(bed_type == 4)//max model
	{
		feet = 6;
		$("#design-feet img").attr('src','/images/order-images/feet/wood-max.png');
	}
	else if(bed_type == 5)//air model
	{
		feet = 7;
		$("#design-feet img").attr('src','/images/order-images/feet/metal.png');
		headboard_style = 9;
		headboard_size = 21;
		$("#design-headboard-mask img").attr('src','/images/order-images/headboards/masks/horizon-full.png');
		var fimage = $("#design-fabric img").attr('src');
        if(fimage.indexOf('blank')==-1) //fabric is set
        {
          var patt = /\/images\/order-images\/fabrics\//;
          fimage = fimage.replace(patt, "");
	      $("#design-headboard-fabric img").attr('src','/images/order-images/headboards/' + jQuery.trim(fimage));
        }//if
	}
	else
	{
		feet = 1;
		$("#design-feet img").attr('src','/images/order-images/feet/standard.png');
	}
	change_image_title(bed_type, bed_size);
	/* End update */
	update_totals();
}

/* Updated by Kavitha */
function change_image_title(bt, bs)
{
	$("#design-name").load('/design_your_bed/updateimagetitle/' + bt + '/' + bs);
}
/* End update */

function set_bed_size_alt()
{
	temp = $("#bed_size option:selected").attr('value');
	if(temp != 0)
	{
		bed_size = temp;
		bed_type = $("#bed_type option:selected").attr('value');
		/* Updated by Kavitha */
		change_image_title(bed_type, bed_size);
		/* End update */
		update_totals();
	}
}

function set_bed_size(t)
{
	bed_size = t.attr('value');
	bed_type = $("input[name=bed_type]:checked").attr('value');
	/* Updated by Kavitha */
	change_image_title(bed_type, bed_size);
	/* End update */
	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();
	/* Updated by Kavitha */
	var fr = $("#fabric_range option:selected").attr('value')
	if((fr== 7) || (fr==5))//wood or high gloss
	{
		reset_headboard();
	}//if
	else
		update_totals();
	/* End update */
}

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();
		/* Updated by Kavitha */
		reset_headboard_alt();
		/* End update */
	}
}

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()
{
	/* Updated by Kavitha */
	//$("#design-headboard-mask img").attr('src',default_headboard_mask);
	$("#design-headboard-mask img").attr('src','/images/order-images/headboards/masks/blank.png');
	/* end update */
	headboard_size = 'reset';
	headboard_style = 'reset';
	update_totals();
}

function reset_headboard_alt()
{
	/* Updated by Kavitha */
	//reset_headboard();
	//$("#headboard_options").load('/design_your_bed/design-phase5_headboards/' + $("#bed_type").attr('value'));
	$("#headboard_options").load('/design_your_bed/design-phase5_headboards/' + $("#fabric_id").attr('value'));
	/* End update */
}

function reset_headboard_air(bt)
{
	/* Updated by Kavitha */
	$("#headboard_options").load('/design_your_bed/design-phase5_headboards/' + $("#fabric_id").attr('value') + '/' + bt);
	/* End update */
}

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()
{
	/* Updated by Kavitha */
	//reset_fabric();
	$("#fabric_options").load('/design_your_bed/design_phase5_fabriclist/' + $("#bed_type").attr('value') + '/' + $("#fabric_range").attr('value') + '/' + $("#fabric_id").attr('value'));
	$("#fabrictype_options").load('/design_your_bed/fabrictype/1/1/5');
	/* End update */
}

function change_fabrics_alt()
{
	$("#fabric_sel").load('/design_your_bed/design-phase5_fabrics/' + $("#fabric_range").attr('value'));
	/* Updated by Kavitha */
	var selected_woodtype = false;
	if($("#fabric_type").length==0) //fabric type dropdown dont exist
		selected_woodtype = true;
	var new_fabric_range = $("#fabric_range").attr('value');
	if((selected_woodtype) && (new_fabric_range!=7))
		$("#fabrictype_options").load('/design_your_bed/fabrictype/1/1/5');
	else if((!selected_woodtype) && (new_fabric_range==7))
		$("#fabrictype_options").load('/design_your_bed/fabrictype/0/'+$("#fabric_type option:selected").attr('value')+'/5');
	/* End update */
}

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");
}

