/* -----------------------------------------------------------------------------
	myQuiltGenie - Corn &amp; Beans block - scripts
----------------------------------------------------------------------------- */
var fab_width	= 42;

//	calc_block(size, num);
function calc_block(size, num) 
{
	var fab1			= document.getElementById('fab_1'), 
		fab2			= document.getElementById('fab_2'), 
		unit_c			= document.getElementById('mgq_num_c'), 
		num_blocks		= document.getElementById('mgq_num_blocks'), 
		unit_ab_size	= (size / 3) + 0.5, 
		unit_c_size		= (size / 3) + 0.875, 
		num_unit_c		= Math.ceil((num * 7) / 2), 
		fab_strip1		= calc_strips(unit_ab_size, num), 
		fab_strip2		= calc_strips(unit_c_size, num_unit_c), 
		fab_yds		= (unit_ab_size * fab_strip1) + (unit_c_size * fab_strip2);
	fab1.innerHTML	= '&nbsp;- ' + calc_yds_str(fab_yds) + format_cut_instr(unit_ab_size, fab_strip1, '<b>Unit A</b> square', num) + format_cut_instr(unit_c_size, fab_strip2, '<b>Unit C</b> square', num_unit_c);
	fab2.innerHTML	= '&nbsp;- ' + calc_yds_str(fab_yds) + format_cut_instr(unit_ab_size, fab_strip1, '<b>Unit B</b> square', num) + format_cut_instr(unit_c_size, fab_strip2, '<b>Unit C</b> square', num_unit_c);
	unit_c.innerHTML	= num * 7;
	num_blocks.innerHTML	= 'Make ' + num + ' ' + ((num > 0) ? 'blocks' : 'block');
}

//	calc_quilt(size, cols, rows);
function calc_quilt(size, cols, rows) 
{
	// mgq_info, mgq_size, mgq_border_width, mgq_num_blocks, mgq_preview, fab_1, fab_2, fab_3, fab_4, fab_5, fab_6, fab_7, fab_8, mgq_unit_a, mgq_unit_b, mgq_unit_c, mgq_unit_d, mgq_unit_e, mgq_unit_f, mgq_unit_g, mgq_unit_h, mgq_num_units, mgq_horiz_width, mgq_vert_width, mgq_backing
	var num_blocks_a	= Math.ceil((cols * rows) / 2), 
		num_blocks_b	= (cols * rows) - num_blocks_a, 
		border_width	= size / 4, 
		width			= (cols * size) + (border_width * 2), 
		height			= (rows * size) + (border_width * 2), 
		circ			= (width + height) * 2, 
		strip1_width	= (size / 6) + .5, 
		strip2_width	= (size / 3) + .5, 
		border1_width	= (size / 6) + .5, 
		border2_width	= (size / 12) + .5, 
		binding_width	= 2.5, 
		block1_large	= num_blocks_a, 
		block1_small_1	= num_blocks_a * 3, 
		block1_small_2	= num_blocks_a * 2, 
		block2_large	= num_blocks_b, 
		block2_small_1	= num_blocks_b * 3, 
		block2_small_2	= num_blocks_b * 2, 
		fab1_small		= block1_small_2 + block2_small_1, 
		fab1_large		= block2_large, 
		fab1_strip1		= calc_strips(strip1_width, fab1_small), 
		fab1_strip2		= calc_strips(strip2_width, fab1_large), 
		fab1_yds		= (fab1_strip1 * strip1_width) + (fab1_strip2 * strip2_width), 
		fab4_small		= block1_small_2 + block2_small_1, 
		fab4_large		= block2_large, 
		fab4_strip1		= calc_strips(strip1_width, fab4_small), 
		fab4_strip2		= calc_strips(strip2_width, fab4_large), 
		fab4_yds		= (fab4_strip1 * strip1_width) + (fab4_strip2 * strip2_width), 
		fab6_small		= block1_small_2 + block2_small_1, 
		fab6_large		= block2_large, 
		fab6_strip1		= calc_strips(strip1_width, fab6_small), 
		fab6_strip2		= calc_strips(strip2_width, fab6_large), 
		fab6_border		= calc_strips((circ + 2), 1, border2_width, false), 
		fab6_yds		= (fab6_strip1 * strip1_width) + (fab6_strip2 * strip2_width) + (fab6_border * border2_width), 
		fab7_small		= block1_small_2 + block2_small_1, 
		fab7_large		= block2_large, 
		fab7_strip1		= calc_strips(strip1_width, fab7_small), 
		fab7_strip2		= calc_strips(strip2_width, fab7_large), 
		fab7_yds		= (fab7_strip1 * strip1_width) + (fab7_strip2 * strip2_width), 
		fab2_small		= block1_small_1 + block2_small_2, 
		fab2_large		= block1_large, 
		fab2_strip1		= calc_strips(strip1_width, fab2_small), 
		fab2_strip2		= calc_strips(strip2_width, fab2_large), 
		fab2_yds		= (fab2_strip1 * strip1_width) + (fab2_strip2 * strip2_width), 
		fab3_small		= block1_small_1 + block2_small_2, 
		fab3_large		= block1_large, 
		fab3_strip1		= calc_strips(strip1_width, fab3_small), 
		fab3_strip2		= calc_strips(strip2_width, fab3_large), 
		fab3_border		= calc_strips((circ + 2), 1, border1_width, false), 
		fab3_yds		= (fab3_strip1 * strip1_width) + (fab3_strip2 * strip2_width) + (fab3_border * border1_width), 
		fab5_small		= block1_small_1 + block2_small_2, 
		fab5_large		= block1_large, 
		fab5_strip1		= calc_strips(strip1_width, fab5_small), 
		fab5_strip2		= calc_strips(strip2_width, fab5_large), 
		fab5_yds		= (fab5_strip1 * strip1_width) + (fab5_strip2 * strip2_width), 
		fab8_small		= block1_small_1 + block2_small_2, 
		fab8_large		= block1_large, 
		fab8_strip1		= calc_strips(strip1_width, fab8_small), 
		fab8_strip2		= calc_strips(strip2_width, fab8_large), 
		fab8_binding	= calc_strips((circ + 12), 1, binding_width, true), 
		fab8_yds		= (fab8_strip1 * strip1_width) + (fab8_strip2 * strip2_width) + (fab8_binding * binding_width);
	document.getElementById('mgq_info').innerHTML	= 'Finished size: ' + format_fraction(width, 4) + '&quot; by ' + format_fraction(height, 4) + '&quot; using ' + ((fab_width > 20) ? (fab_width + 2) + '&quot; fabric bolt width' : 'fat quarters') + '.';
	document.getElementById('mgq_size').innerHTML	= format_fraction(width, 4) + '&quot; by ' + format_fraction(height, 4) + '&quot;';
	document.getElementById('mgq_border_width').innerHTML	= format_fraction(border_width, 4) + '&quot;';
	document.getElementById('mgq_num_blocks').innerHTML	= cols * rows;
	document.getElementById('fab_1').innerHTML	= '&nbsp;- ' + calc_yds_str(fab1_yds) + format_cut_instr(strip1_width, fab1_strip1, '<b>small</b> quarter square triangle', fab1_small) + format_cut_instr(strip2_width, fab1_strip2, '<b>large</b> quarter square triangle', fab1_large);
	document.getElementById('fab_2').innerHTML	= '&nbsp;- ' + calc_yds_str(fab2_yds) + format_cut_instr(strip1_width, fab2_strip1, '<b>small</b> quarter square triangle', fab2_small) + format_cut_instr(strip2_width, fab2_strip2, '<b>large</b> quarter square triangle', fab2_large);
	document.getElementById('fab_3').innerHTML	= '&nbsp;- ' + calc_yds_str(fab3_yds) + format_cut_instr(strip1_width, fab3_strip1, '<b>small</b> quarter square triangle', fab3_small) + format_cut_instr(strip2_width, fab3_strip2, '<b>large</b> quarter square triangle', fab3_large) + format_cut_instr(border1_width, fab3_border) + ' <b>Border</b>';
	document.getElementById('fab_4').innerHTML	= '&nbsp;- ' + calc_yds_str(fab4_yds) + format_cut_instr(strip1_width, fab4_strip1, '<b>small</b> quarter square triangle', fab4_small) + format_cut_instr(strip2_width, fab4_strip2, '<b>large</b> quarter square triangle', fab4_large);
	document.getElementById('fab_5').innerHTML	= '&nbsp;- ' + calc_yds_str(fab5_yds) + format_cut_instr(strip1_width, fab5_strip1, '<b>small</b> quarter square triangle', fab5_small) + format_cut_instr(strip2_width, fab5_strip2, '<b>large</b> quarter square triangle', fab5_large);
	document.getElementById('fab_6').innerHTML	= '&nbsp;- ' + calc_yds_str(fab6_yds) + format_cut_instr(strip1_width, fab6_strip1, '<b>small</b> quarter square triangle', fab6_small) + format_cut_instr(strip2_width, fab6_strip2, '<b>large</b> quarter square triangle', fab6_large) + format_cut_instr(border2_width, fab6_border) + ' <b>Border</b>';
	document.getElementById('fab_7').innerHTML	= '&nbsp;- ' + calc_yds_str(fab7_yds) + format_cut_instr(strip1_width, fab7_strip1, '<b>small</b> quarter square triangle', fab7_small) + format_cut_instr(strip2_width, fab7_strip2, '<b>large</b> quarter square triangle', fab7_large);
	document.getElementById('fab_8').innerHTML	= '&nbsp;- ' + calc_yds_str(fab8_yds) + format_cut_instr(strip1_width, fab8_strip1, '<b>small</b> quarter square triangle', fab8_small) + format_cut_instr(strip2_width, fab8_strip2, '<b>large</b> quarter square triangle', fab8_large) + format_cut_instr(binding_width, fab8_binding) + ' <b>Binding</b>';
	document.getElementById('mgq_unit_a').innerHTML	= num_blocks_a;
	document.getElementById('mgq_unit_b').innerHTML	= num_blocks_a;
	document.getElementById('mgq_unit_c').innerHTML	= num_blocks_a;
	document.getElementById('mgq_unit_d').innerHTML	= num_blocks_a;
	document.getElementById('mgq_unit_e').innerHTML	= num_blocks_b;
	document.getElementById('mgq_unit_f').innerHTML	= num_blocks_b;
	document.getElementById('mgq_unit_g').innerHTML	= num_blocks_b;
	document.getElementById('mgq_unit_h').innerHTML	= num_blocks_b;
	document.getElementById('mgq_num_units').innerHTML	= 'Make ' + num_blocks_a + ' of Block 1 &amp; ' + num_blocks_b + ' of Block 2';
	document.getElementById('mgq_horiz_width').innerHTML	= format_fraction((width + 0.5), 4);
	document.getElementById('mgq_vert_width').innerHTML	= format_fraction((height + 0.5), 4);
	document.getElementById('mgq_backing').innerHTML	= format_fraction((width + 8), 4) + '&quot; by ' + format_fraction((height + 8), 4) + '&quot;';
	//document.getElementById('').innerHTML	= 0;
	//document.getElementById('').innerHTML	= 0;
	//document.getElementById('').innerHTML	= 0;
	//document.getElementById('').innerHTML	= 0;
	//document.getElementById('').innerHTML	= 0;
	//document.getElementById('').innerHTML	= 0;
}

//	set_preview(cols, rows);
function set_preview(cols, rows) 
{
	var block1		= 'images/corn_beans_01_block_a.jpg', 
		block2		= 'images/corn_beans_01_block_b.jpg', 
		up_left		= 'images/corn_beans_01_up_left.jpg', 
		up_right	= 'images/corn_beans_01_up_right.jpg', 
		lo_left		= 'images/corn_beans_01_lo_left.jpg', 
		lo_right	= 'images/corn_beans_01_lo_right.jpg', 
		top			= 'images/corn_beans_01_top.jpg', 
		left		= 'images/corn_beans_01_left.jpg', 
		right		= 'images/corn_beans_01_right.jpg', 
		bottom		= 'images/corn_beans_01_bottom.jpg', 
		otable		= document.getElementById('mgq_preview'), 
		i, 
		ii, 
		orow, 
		ocell, 
		oimg, 
		row_img	= 1, 
		col_img	= 1;
	//	clear table
	for (i=0; otable.rows.length > 0; i++) 
	{
		otable.deleteRow(0);
	}
	//	top
	orow	= otable.insertRow(-1);
	oimg	= new_image(up_left, 'cornerstone', 'corner');
	ocell	= orow.insertCell(-1);
	ocell.appendChild(oimg);
	for (i=0; i<cols; i++) 
	{
		oimg	= new_image(top, 'top border', 'horiz');
		ocell	= orow.insertCell(-1);
		ocell.appendChild(oimg);
	}
	oimg	= new_image(up_right, 'cornerstone', 'corner');
	ocell	= orow.insertCell(-1);
	ocell.appendChild(oimg);
	//	blocks
	for (i=0; i<rows; i++) 
	{
		//	alternate columns & rows
		orow	= otable.insertRow(-1);
		oimg	= new_image(left, 'left border', 'vert');
		ocell	= orow.insertCell(-1);
		ocell.appendChild(oimg);
		for (ii=0; ii<cols; ii++) 
		{
			oimg	= new_image(((col_img == 1) ? block1 : block2), 'block', 'block');
			ocell	= orow.insertCell(-1);
			ocell.appendChild(oimg);
			col_img	= (col_img == 1) ? 2 : 1;
		}
		oimg	= new_image(right, 'right border', 'vert');
		ocell	= orow.insertCell(-1);
		ocell.appendChild(oimg);
		row_img	= (row_img == 1) ? 2 : 1;
		col_img = row_img;
	}
	//	bottom
	orow	= otable.insertRow(-1);
	oimg	= new_image(lo_left, 'cornerstone', 'corner');
	ocell	= orow.insertCell(-1);
	ocell.appendChild(oimg);
	for (i=0; i<cols; i++) 
	{
		oimg	= new_image(bottom, 'bottom border', 'horiz');
		ocell	= orow.insertCell(-1);
		ocell.appendChild(oimg);
	}
	oimg	= new_image(lo_right, 'cornerstone', 'corner');
	ocell	= orow.insertCell(-1);
	ocell.appendChild(oimg);
}

//	update_page();
function update_page() 
{
	var size	= Number(document.mgq_form.block_size.value), 
		num		= 0, 
		cols	= 0, 
		rows	= 0;
	fab_width	= Number(document.mgq_form.fab_width.value);
	if (document.mgq_form.num_blocks) 
	{
		num		= Number(document.mgq_form.num_blocks.value);
		if (calc_block) 
		{
			calc_block(size, num);
		}
	}
	else 
	{
		cols	= Number(document.mgq_form.num_cols.value);
		rows	= Number(document.mgq_form.num_rows.value);
		num		= Number(document.getElementById('mgq_num_blocks').innerHTML);
		if (calc_quilt) 
		{
			calc_quilt(size, cols, rows);
		}
		if (set_preview && ((cols * rows) != num)) 
		{
			set_preview(cols, rows);
		}
	}
}

//	start_page();
function start_page() 
{
	document.mgq_form.block_size.disabled	= '';
	document.mgq_form.fab_width.disabled	= '';
	if (document.mgq_form.num_blocks) 
	{
		document.mgq_form.num_blocks.disabled	= '';
	}
	else 
	{
		document.mgq_form.num_cols.disabled	= '';
		document.mgq_form.num_rows.disabled	= '';
	}
	update_page();
}
