function calculate_price()

{

	var sel_index;

	var new_price = base_price;

	

	// Get value of size option

	sel_index = $("#size option").index($("#size option:selected"));

	

	// Get value of Base option

	base_index = $("#base option").index($("#base option:selected"));

		

	switch(base_price)

	{
			case 1489: // Jeanius

			if(sel_index < 2)

			{

				if(base_index === 1) { new_price += 299; }	

			}
			
			if(sel_index === 2)	

			{ 

				new_price += 200; // full modifier

				if(base_index === 1) { new_price += 349; }

			}

			else if(sel_index === 3) 

			{ 

				new_price += 290; // queen modifier

				if(base_index === 1) { new_price += 449; }

			}

			else if(sel_index === 4) 

			{ 

				new_price += 705; // king modifier

				if(base_index === 1) { new_price += 599; }

			} else if(sel_index === 5) 

			{ 

				new_price += 823; // cal-king modifier

				// if(base_index === 1) { new_price += 599; } // there is no base for cal-king yet.

			}

		break;

		case 1953: // Classic Skinny

			if(sel_index < 2)

			{

				if(base_index === 1) { new_price += 299; }	

			}

			if(sel_index === 2)	

			{ 

				new_price += 368; // full modifier

				if(base_index === 1) { new_price += 349; }

			}

			else if(sel_index === 3) 

			{ 

				new_price += 383; // queen modifier

				if(base_index === 1) { new_price += 449; }

			}

			else if(sel_index === 4) 

			{ 

				new_price += 1273; // king modifier

				if(base_index === 1) { new_price += 599; }

			} else if(sel_index === 5) 

			{ 

				new_price += 1483; // cal-king modifier

				// if(base_index === 1) { new_price += 599; } // there is no base for cal-king yet.

			}

		break;

		case 2210: // Classic 8

			if(sel_index < 2)

			{

				if(base_index === 1) { new_price += 299; }	

			}

			if(sel_index === 2)	

			{ 

				new_price += 440; // full modifier

				if(base_index === 1) { new_price += 349; }

			}

			else if(sel_index === 3) 

			{ 

				new_price += 469; // queen modifier

				if(base_index === 1) { new_price += 449; }

			}

			else if(sel_index === 4) 

			{ 

				new_price += 1600; // king modifier

				if(base_index === 1) { new_price += 599; }

			} else if(sel_index === 5) 

			{ 

				new_price += 1822; // cal-king modifier

				// if(base_index === 1) { new_price += 599; } // there is no base for cal-king yet.

			}

		break;		

		case 2811: // Energie Opus

			if(sel_index < 2)

			{

				if(base_index === 1) { new_price += 299; }	

			}

			if(sel_index === 2)	

			{ 

				new_price += 700; // full modifier

				if(base_index === 1) { new_price += 349; }

			}

			else if(sel_index === 3) 

			{ 

				new_price += 816; // queen modifier

				if(base_index === 1) { new_price += 449; }

			}

			else if(sel_index === 4) 

			{ 

				new_price += 1833; // king modifier

				if(base_index === 1) { new_price += 599; }

			} else if(sel_index === 5) 

			{ 

				new_price += 2043; // cal-king modifier

				// if(base_index === 1) { new_price += 599; } // there is no base for cal-king yet.

			}

		break;

		case 3089: // Beausommet

			if(sel_index < 2)

			{

				if(base_index === 1) { new_price += 299; }	

			}

			if(sel_index === 2)	

			{ 

				new_price += 862; // full modifier

				if(base_index === 1) { new_price += 349; }

			}

			else if(sel_index === 3) 

			{ 

				new_price += 1050; // Queen modifier

				if(base_index === 1) { new_price += 449; }

			}

			else if(sel_index === 4) 

			{ 

				new_price += 2357; // king modifier

				if(base_index === 1) { new_price += 599; }

			}
			
			else if(sel_index === 5) 

			{ 

				new_price += 2567; // cal-king modifier

				// if(base_index === 1) { new_price += 599; } // there is no base for cal-king yet.

			} 

		break;

		case 3321: // Dormeuse

			if(sel_index < 2)

			{

				if(base_index === 1) { new_price += 299; }	

			}

			if(sel_index === 2)	

			{ 

				new_price += 944; // full modifier

				if(base_index === 1) { new_price += 349; }

			}

			else if(sel_index === 3) 

			{ 

				new_price += 1216; // queen modifier 

				if(base_index === 1) { new_price += 449; }

			}

			else if(sel_index === 4) 

			{ 

				new_price += 2519; // king modifier

				if(base_index === 1) { new_price += 599; }

			} else if(sel_index === 5) 

			{ 

				new_price += 2853; // cal-king modifier

				// if(base_index === 1) { new_price += 599; } // there is no base for cal-king yet.

			}

		break;

		case 4784: // Unity

			

			if(sel_index === 0)

			{

				if(base_index === 1) { new_price += 449; }

			}

			else if(sel_index === 1) 

			{ 

				new_price += 1495; // king modifier

				if(base_index === 1) { new_price += 599; }

			} else if(sel_index === 2) 

			{ 

				new_price += 1705; // cal-king modifier

				// if(base_index === 1) { new_price += 599; } // there is no base for cal-king yet.

			}

		break;
		
		case 4824: // FlexDrive
	 

			if(sel_index === 0)

			{

				if(base_index === 1) { new_price += 299; }

			}

			else if(sel_index === 1) 

			{ 

				new_price += 4824; 

				if(base_index === 1) { new_price += 599; }

			}

		break;		

	}

		

	// Get value of Frame option

	var frame_sel_index = $("#frame option").index($("#frame option:selected"));

	if(frame_sel_index === 1)	{ new_price += 85; }

	

	$("#updated-price").html(new_price);

}



function update_size(mat_id)

{
	// Get the size option ID from the drop-down
	var option_id = $("#size").attr("name");

	// console.log(option_id);

	// Extract just the number from the option_id
	option_id = parseInt(option_id.substring(3, option_id.indexOf("]"))) + 1; // add one to the option_id to get to the "base" option, rather than "size"

	// console.log(option_id);

	$.post("/js/ajax/size-id.php", { prd: option_id, size: mat_id },

		   function(data)

		   { // see backup file when base and frame options are added for this function
		   
		   		var noframe_opt_id;
				var addframe_opt_id;
						
				switch(option_id)
				{
					case 32: // jeanius
					
					noframe_opt_id = 137;
					addframe_opt_id = 138;
					
					break;
					
					case 11: // classic skinny
					
					noframe_opt_id = 43;
					addframe_opt_id = 44;
					
					break;
					
					case 14: // classic 8
					
					noframe_opt_id = 56;
					addframe_opt_id = 57;
					
					break;
					
					case 8: // energie opus
					
					noframe_opt_id = 30;
					addframe_opt_id = 31;
					
					break;
					
					case 17: // beaussomet
					
					noframe_opt_id = 69;
					addframe_opt_id = 70;
					
					break;
					
					case 20: // dormeuse
					
					noframe_opt_id = 82;
					addframe_opt_id = 83;
					
					break;
					
					case 23: // unity
					
					noframe_opt_id = 95;
					addframe_opt_id = 96;
					
					break;
				}
		   
		   
				if(mat_id === 7) // If this is the cal-king size
				{
					
					$("#base").parents('.jquery-selectbox').unselectbox();								
	
					$('#base').find('option')
	
					.remove()
	
					.end()
	
					.append('<option value="' + data.no_base + '" selected="selected">No Box Spring Available</option>')
	
					.selectbox();	
					
					$("#frame").parents('.jquery-selectbox').unselectbox();								
	
					$('#frame').find('option')
	
					.remove()
	
					.end()
	
					.append('<option value="' + noframe_opt_id + '" selected="selected">No Frame Available</option>')
	
					.selectbox();
					
				} else // all other sizes have bases and rails available...
				{
					$("#base").parents('.jquery-selectbox').unselectbox();								
	
					$('#base').find('option')
	
					.remove()
	
					.end()
	
					.append('<option value="' + data.no_base + '" selected="selected">No Box Spring</option><option value="' + data.size_id + '">' + data.size_name + '</option>')
	
					.selectbox()
	
					.bind("change", calculate_price);	
					
					if( $('#frame option').length < 2 )
					{						
						
						$("#frame").parents('.jquery-selectbox').unselectbox();								
	
						$('#frame').find('option')
		
						.remove()
		
						.end()
		
						.append('<option value="' + noframe_opt_id + '" selected="selected">No Metal Rails</option><option value="' + addframe_opt_id + '">Add Metal Rails (+$85)</option>')
		
						.selectbox();
					
					}
					
					
				}
				calculate_price();

				

		   }, "json");

}



$(document).ready(function(){

	

	// let's get going...

	if(product_name == "FlexDrive")						
		{ update_size(2); } else { update_size(5); } 

	// price calculation fxn bindings

	$("#base").selectbox().bind("change", calculate_price);
	$("#frame").selectbox().bind("change", calculate_price);	



	// preload images for faster operation, yo

	(function($) {

	  var cache = [];

	  // Arguments are image paths relative to the current page.

	  $.preLoadImages = function() {

		var args_len = arguments.length;

		for (var i = args_len; i--;) {

		  var cacheImage = document.createElement('img');

		  cacheImage.src = arguments[i];

		  cache.push(cacheImage);

		}

	  }

	})(jQuery)

	// Preload images:
	
	if(window.product_name == "FlexDrive")
	{
		$.preLoadImages("/images/catalog/products/flexdrive/flexdrive-1.jpg", "/images/catalog/products/flexdrive/flexdrive-2.jpg", "/images/catalog/products/flexdrive/flexdrive-3.jpg", "/images/catalog/products/flexdrive/flexdrive-4.jpg");
	} else
	{
		$.preLoadImages("/images/catalog/products/mattress-2.jpg", "/images/catalog/products/mattress-3.jpg", "/images/catalog/products/mattress-4.jpg", "/images/catalog/products/mattress-5.jpg");
	}

	
	// mattress size controller

	$(".size a").click(function(){
							
		var selected_mattress = $(this).parent().prevAll().length; // The index of blue div with the selected mattress (don't forget hidden ones)

		if(product_name == "FlexDrive")						
		{
			if( selected_mattress == 1 ) { mattress_id = 2 }
			if( selected_mattress == 4 ) { mattress_id = 3 }
		} else
		{
			
			var mattress_id = parseInt(selected_mattress + 2); // ID of the mattress SIZE option
			// console.log(mattress_id);
			
	
			if(product_name == "Unity") { selected_mattress -= 3; } // adjustment for lack of sizes
		}

		if(product_name == "FlexDrive")						
		{
			$("#size option:eq(" + (mattress_id - 2) + ")").attr("selected", "selected"); // populate the mattress size dropdown
		} else
		{
			$("#size option:eq(" + (selected_mattress) + ")").attr("selected", "selected"); // populate the mattress size dropdown
		}
		
		$(this).parent().siblings("div.size").removeClass("selected");	// make it look pretty!					

		$(this).blur().parent().addClass("selected");	

		update_size(mattress_id);

	});

	
});
