/* -----------------------------------------------------------------------------
	myQuiltGenie - Pansy Star block/quilt - scripts
----------------------------------------------------------------------------- */
var fab_width	= 42;

//	calc_block(size, num);
function calc_block(size, num) 
{
	var units				= num * 8, 					//	number of 4 patch & hst units
	strip_width1	= (size / 4) + 1,			//	width of 4 patch strips
	strip_width2	= (size / 4) + 1.375,	//	width of hst strips
	half				= format_fraction(strip_width1 / 2, 8) + ((strip_width1 > 2) ? ' inches' : ' inch'),			//	unit half cut measurement
	num_sq1			= num * 4,					//	number of fab 1 & 2, hst & 4 patch squares
	num_sq2			= num * 2,					//	number of fab 3 & 4, 4 patch squares
	num_strips1		= calc_strips(strip_width1, num_sq1),	//	number of fab 1, 4 patch strips
	num_strips2		= calc_strips(strip_width2, num_sq1),	// number of fab 1 & 2, hst strips
	num_strips3		= calc_strips(strip_width1, num_sq2),	// number of fab 3 & 4, 4 patch strips
	fab1yds			= (num_strips1 * strip_width1) + (num_strips2 * strip_width2),	//	fab 1 yards
	fab2yds			= num_strips2 * strip_width2,				//	fab 2 yards 
	fab34yds			= num_strips3 * strip_width1;				//	fab 3 & 4 yards
	document.getElementById('mgq_info').innerHTML 			= format_mgq_info(num, size);
	document.getElementById('mgq_num_blocks').innerHTML	= 'Makes ' + num + ' ' + ((num > 1) ? 'blocks.' : 'block.');
	document.getElementById('mgq_half_1').innerHTML			= half;
	document.getElementById('mgq_half_2').innerHTML			= half;
	document.getElementById('mgq_half_3').innerHTML			= half;
	document.getElementById('mgq_num_4p').innerHTML			= units;
	document.getElementById('mgq_num_hst1').innerHTML		= units;
	document.getElementById('mgq_num_hst2').innerHTML		= units;
	document.getElementById('mgq_num_qtr').innerHTML		= num * 4;
	document.getElementById('mgq_hst_size').innerHTML		= strip_width1;
	//format_cut_instr(width, strips, piece, num, height)
	//calc_yds_str(width, strips)
	//calc_strips(height, num_pieces, width, diag)
	document.getElementById('fab_1').innerHTML	= '&nbsp;- ' + calc_yds_str(fab1yds, 1) + format_cut_instr(strip_width1, num_strips1, 'unit A square', num_sq1) + format_cut_instr(strip_width2, num_strips2, 'unit B square', num_sq1);
	document.getElementById('fab_2').innerHTML	= '&nbsp;- ' + calc_yds_str(fab2yds, 1) + format_cut_instr(strip_width2, num_strips2, 'unit B square', num_sq1);
	document.getElementById('fab_3').innerHTML	= '&nbsp;- ' + calc_yds_str(fab34yds, 1) + format_cut_instr(strip_width1, num_strips3, 'unit A square', num_sq2);
	document.getElementById('fab_4').innerHTML	= '&nbsp;- ' + calc_yds_str(fab34yds, 1) + format_cut_instr(strip_width1, num_strips3, 'unit A square', num_sq2);
}

//	set_preview(cols, rows);
function set_preview(cols, rows) 
{
	var block	= 'images/pansy_star_01b.jpg', 
		up_left	= 'images/pansy_star_01_up_left.jpg', 
		up_right	= 'images/pansy_star_01_up_right.jpg', 
		lo_left	= 'images/pansy_star_01_lo_left.jpg', 
		lo_right	= 'images/pansy_star_01_lo_right.jpg', 
		top		= 'images/pansy_star_01_top.jpg', 
		left		= 'images/pansy_star_01_left.jpg', 
		right		= 'images/pansy_star_01_right.jpg', 
		bottom	= 'images/pansy_star_01_bottom.jpg', 
		otable	= document.getElementById('mgq_preview'), 
		i, 
		ii, 
		orow, 
		ocell, 
		oimg;
	//	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++) 
	{
		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(block, 'block', 'block');
			ocell	= orow.insertCell(-1);
			ocell.appendChild(oimg);
		}
		oimg	= new_image(right, 'right border', 'vert');
		ocell	= orow.insertCell(-1);
		ocell.appendChild(oimg);
	}
	//	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);
}

//	calc_quilt(size, cols, rows);
function calc_quilt(size, cols, rows) 
{
	//	finished sizes
	var border_width	= size / 2, 
		horizontal	= (cols * size) + size, 
		vertical		= (rows * size) + size, 
		num			= cols * rows, 
		nA				= (cols * rows) * 8, 
		nB				= (cols * rows) * 4, 
		nC				= (cols * rows) * 4, 
		nD				= 2, 
		nE				= 2, 
		nF				= cols * 2, 
		nG				= rows * 2, 
		nH				= rows * 2, 
		nI				= cols * 2, 
		nJ				= (cols + rows) * 2, 
		nK				= 2, 
		nL				= 2, 
		strip_a		= (size / 4) + 1.375, 
		strip_b		= (size / 4) + 1, 
		strip_c		= (size / 8) + .5, 
		strip_d		= (size / 8) + .875;
	set_info('mgq_info', 'Finished size ' + format_fraction(horizontal, 4) + '" by ' + format_fraction(vertical, 4) + '"');
	document.getElementById('mgq_size').innerHTML	= format_fraction(horizontal, 4) + '" by ' + format_fraction(vertical, 4) + '"';
	document.getElementById('mgq_border_width').innerHTML	= format_fraction(border_width, 4) + '"';
	document.getElementById('mgq_num_blocks').innerHTML	= num;
	//	fabric yardage: fab_1, fab_2, fab_3, fab_4, fab_5, fab_6, fab_7
	var fab1a	= calc_strips(strip_a, ((nB/2) + (nC/2))), 
		fab1b		= calc_strips(strip_b, (nA/2)), 
		fab1c		= calc_strips((size / 4) + .5, nJ) + calc_strips((cols + 1) * size, nK) + calc_strips((rows + 1) * size, nL), 
		fab1d		= calc_strips(strip_d, ((nF/2) + (nG/2) + (nH/2) + (nI/2))), 
		fab1binding	= calc_strips((horizontal * 2) + (vertical * 2) + 12, 1, 2.5, true), 
		fab2a		= calc_strips(strip_a, (nB/2) + (nD/2)), 
		fab2c		= calc_strips(strip_c, (nF) + (nG)), 
		fab2d		= calc_strips(strip_d, (nF) + (nG)), 
		fab3a		= calc_strips(strip_a, (nC/2) + (nE/2)), 
		fab3c		= calc_strips(strip_c, (nH) + (nI)), 
		fab3d		= calc_strips(strip_d, (nH) + (nI)), 
		fab4b		= calc_strips(strip_b, (nA/4)), 
		fab5b		= calc_strips(strip_b, (nA/4)), 
		fab6c		= calc_strips(horizontal, nK) + calc_strips(vertical, nL), 
		fab7a		= calc_strips(strip_a, (nD/2) + (nE/2)), 
		fab7c		= calc_strips(strip_c, (nF) + (nG) + (nH) +(nI)) + calc_strips((size/4) + .5, nJ), 
		fab7d		= calc_strips(strip_d, (nF/2) + (nG/2) + (nH/2) + (nI/2));
	// format_cut_instr(width, strips, piece, num, height)
	element_inner('fab_1', '&nbsp;- ' + calc_yds_str((fab1a * strip_a) + (fab1b * strip_b) + (fab1c * strip_c) + (fab1d * strip_d) + (fab1binding * 2.5)) + format_cut_instr(strip_a, fab1a, '<b>unit B &amp; C</b> square', ((nB/2) + (nC/2))) + format_cut_instr(strip_b, fab1b, '<b>unit A</b> square', (nA/2)) + format_cut_instr(strip_d, fab1d, '<b>unit F, G, H &amp; I</b> square', ((nF/2) + (nG/2) + (nH/2) + (nI/2))) + format_cut_instr(strip_c, fab1c) + ' (<b>unit J &amp; border</b>)' + format_cut_instr(2.5, fab1binding) + ' (<b>binding</b>)');
	element_inner('fab_2', '&nbsp;- ' + calc_yds_str((fab2a * strip_a) + (fab2c * strip_c) + (fab2d * strip_d)) + format_cut_instr(strip_a, fab2a, '<b>unit B &amp; D</b> square', (nB/2) + (nD/2)) + format_cut_instr(strip_d, fab2d, '<b>unit F &amp; G</b> square', (nF) + (nG)) + format_cut_instr(strip_c, fab2c, '<b>unit F &amp; G</b> square', (nF) + (nG)));
	element_inner('fab_3', '&nbsp;- ' + calc_yds_str((fab3a * strip_a) + (fab3c * strip_c) + (fab3d * strip_d)) + format_cut_instr(strip_a, fab3a, '<b>unit C &amp; E</b> squares', (nC/2) + (nE/2)) + format_cut_instr(strip_d, fab3d, '<b>unit H &amp; I</b> square', (nH) + (nI)) + format_cut_instr(strip_c, fab3c, '<b>unit H &amp; I</b> square', (nH) + (nI)));
	element_inner('fab_4', '&nbsp;- ' + calc_yds_str(fab4b * strip_b) + format_cut_instr(strip_b, fab4b, '<b>unit A</b> square', (nA/4)));
	element_inner('fab_5', '&nbsp;- ' + calc_yds_str(fab5b * strip_b) + format_cut_instr(strip_b, fab5b, '<b>unit A</b> square', (nA/4)));
	element_inner('fab_6', '&nbsp;- ' + calc_yds_str(fab6c * strip_c) + format_cut_instr(strip_c, fab6c) + ' (<b>border</b>)');
	element_inner('fab_7', '&nbsp;- ' + calc_yds_str((fab7a * strip_a) + (fab7c * strip_c) + (fab7d * strip_d)) + format_cut_instr(strip_a, fab7a, '<b>unit D &amp; E</b> square', (nD/2) + (nE/2)) + format_cut_instr(strip_d, fab7d, '<b>unit F, G, H & I</b> square', (nF/2) + (nG/2) + (nH/2) + (nI/2)) + format_cut_instr(strip_c, fab7c, '<b>unit F, G, H &amp; I</b> square', (nF) + (nG) + (nH) + (nI)) + ' (remainder for <b>unit J</b>)');
	//	other calculations
	element_inner('mgq_half_1', format_fraction(((size / 4) +1) / 2, 8));
	element_inner('mgq_half_2', format_fraction(((size / 4) +1) / 2, 8));
	element_inner('mgq_half_3', format_fraction(((size / 4) +1) / 2, 8));
	element_inner('mgq_num_4p', num * 8);
	element_inner('mgq_num_bc', num * 4);
	element_inner('mgq_num_fg', (cols + rows) * 2);
	element_inner('mgq_num_hi', (cols + rows) * 2);
	element_inner('mgq_bc_sq', format_fraction((size / 4) + 1.375, 8));
	element_inner('mgq_de_sq', format_fraction((size / 4) + 1.375, 8));
	element_inner('mgq_fg_sq', format_fraction((size / 8) + .875, 8));
	element_inner('mgq_hi_sq', format_fraction((size / 8) + .875, 8));
	element_inner('mgq_num_bc_2', num * 4);
	element_inner('mgq_num_qtr_type', num * 2);
	element_inner('mgq_num_blocks2', num);
	element_inner('mgq_fg_sq2', format_fraction((size / 8) + .5, 8));
	element_inner('mgq_fg_sq3', format_fraction((size / 8) + .875, 8));
	element_inner('mgq_fg_sq4', format_fraction((size / 8) + .875, 8));
	element_inner('mgq_num_f', cols * 2);
	element_inner('mgq_num_g', rows * 2);
	element_inner('mgq_hi_sq2', format_fraction((size / 8) + .5, 8));
	element_inner('mgq_hi_sq3', format_fraction((size / 8) + .875, 8));
	element_inner('mgq_hi_sq4', format_fraction((size / 8) + .875, 8));
	element_inner('mgq_num_h', rows * 2);
	element_inner('mgq_num_i', cols * 2);
	element_inner('mgq_j_width', format_fraction((size / 8) + .5, 8));
	element_inner('mgq_j_length', format_fraction((size / 2) + .5, 8));
	element_inner('mgq_num_fij', cols * 2);
	element_inner('mgq_num_ghj', rows * 2);
	element_inner('mgq_horiz_width', format_fraction(horizontal + 1, 8));
	element_inner('mgq_vert_width', format_fraction(vertical + 1, 8));
	element_inner('mgq_backing', (format_fraction(horizontal + 8, 4) + '&quot; by ' + format_fraction(vertical + 8, 4) + '&quot;'));
	element_inner('mgq_num_j', ((cols + rows) * 2));
	//	preview
	set_preview(cols, rows);
}

//	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);
		if (calc_quilt) 
		{
			calc_quilt(size, 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	= '';
	}
}
