/* -----------------------------------------------------------------------------
	myQuiltGenie - Double Irish Chain block - scripts
----------------------------------------------------------------------------- */
var fab_width	= 42;

/* -----------------------------------------------------------------------------
	calc_block(size, num);
----------------------------------------------------------------------------- */
function calc_block(size, num) 
{
	size			= (size) ? size : 5;			// block size
	num				= (num) ? num : 1;				// number of blocks
	var block_str	= (num > 1) ? 'Blocks' : 'Block', 
		p1_width	= (size / 5) + 0.5, 			// piece 1 width/piece 3 width
		p2_width	= ((size / 5) * 3) + 0.5, 		// piece 2 width/piece 3 height
		fab1_p1		= num * 4, 						// number of fab 1 piece 1
		fab1_p2		= num * 1, 						// number of fab 1 piece 2
		fab1_p3		= num * 4, 						// number of fab 1 piece 3
		fab2_p1		= num * 16, 					// number of fab 2 piece 1
		fab3_p1		= num * 9, 						// number of fab 3 piece 1
		fab1_strip1	= calc_strips(p1_width, fab1_p1), // num of fab 1 strip 1
													// num of fab 1 strip 2
		fab1_strip2	= calc_strips([p2_width, p1_width], [fab1_p2, fab1_p3]), 
		fab2_strip	= calc_strips(p1_width, fab2_p1), // num of fab 2 strips
		fab3_strip	= calc_strips(p1_width, fab3_p1), // num of fab 3 strips
		fab1_cut, 									// fabric 1 yards & cutting instr
		fab2_cut, 									// fabric 2 yards & cutting instr
		fab3_cut, 									// fabric 3 yards & cutting instr
		fab1_obj	= document.getElementById('fab_1'), // fabric 1 DOM object
		fab2_obj	= document.getElementById('fab_2'), // fabric 2 DOM object
		fab3_obj	= document.getElementById('fab_3'); // fabric 3 DOM object
	fab1_cut	= '&nbsp;- ' + calc_yds_str(((fab1_strip1 * p1_width) + (fab1_strip2 * p2_width))) + format_cut_instr(p1_width, fab1_strip1, '<b>Small</b> square', fab1_p1) + format_cut_instr(p2_width, fab1_strip2) + ' Subcut into ' + fab1_p2 + ' <b>Large</b> ' + ((fab1_p2 > 1) ? 'squares' : 'square') + ' &amp; ' + fab1_p3 + ' <b>Rectangles</b> ' + format_fraction(p1_width, 8) + ' by ' + format_fraction(p2_width, 8) + ' inches.';
	fab2_cut	= '&nbsp;- ' + calc_yds_str(fab2_strip, p1_width) +  format_cut_instr(p1_width, fab2_strip, '<b>Small</b> square', fab2_p1);
	fab3_cut	= '&nbsp;- ' + calc_yds_str(fab3_strip, p1_width) + format_cut_instr(p1_width, fab3_strip, '<b>Small</b> square', fab3_p1);
	fab1_obj.innerHTML	= fab1_cut;
	fab2_obj.innerHTML	= fab2_cut;
	fab3_obj.innerHTML	= fab3_cut;
	document.getElementById('mgq_num_unita').innerHTML	 = 'Make ' + num + ' ' + block_str + '.';
	document.getElementById('mgq_num_unitb').innerHTML	 = 'Make ' + num + ' ' + block_str + '.';
	document.getElementById('mgq_info').innerHTML	= num + ' pair of ' + format_fraction(size, 8) + '" blocks using ' + ((fab_width >= 40) ? (fab_width + 2) + '" fabric bolt width' : ' fat quarters') + '.';
}

//	calc_quilt(size, cols, rows);
function calc_quilt(size, cols, rows) 
{
	var info	= document.getElementById('mgq_info'), 	// mgq_info object
		width	= (cols + 2) * size, 					// quilt width
		height	= (rows + 2) * size, 					// quilt height
		circumfrence	= (width + height) * 2, 		// quilt circumfrence
		strip1_width	= (size / 5) + 0.5, 			// small strip width
		strip2_width	= ((size / 5) * 3) + 0.5, 		// large strip width
		strip3_width	= 2.5, 							// binding strip width
		border1_length	= width + 0.5, 					// border width length
		border2_length	= height + 0.5, 				// border height length
		block1			= Math.ceil((cols * rows) / 2), // number of block 1
		block2			= (cols * rows) - block1, 		// number of block 2
		fab1_p1			= block1 * 4, 					// number of fab 1 small sq
		fab1_p2			= block2 * 4, 					// number of fab 1 rectangles
		fab1_p3			= block2, 						// number of fab 1 large sq
		fab2_p1			= (block1 * 12) + (block2 * 4), // number of fab 2 small sq
		fab3_p1			= block1 * 9, 					// number of fab 3 small sq
		fab1_strip1		= calc_strips(strip1_width, fab1_p1), 
		fab1_strip2		= calc_strips([strip1_width, strip2_width], [fab1_p2, fab1_p3]) + calc_strips((circumfrence + 2), 1, strip2_width, false), 
		fab1_inches		= (fab1_strip1 * strip1_width) + (fab1_strip2 * strip2_width), 
		fab2_strip1		= calc_strips(strip1_width, fab2_p1) + calc_strips((circumfrence + 2), 1, strip1_width, false), 
		fab2_inches		= fab2_strip1 * strip1_width, 
		fab3_strip1		= calc_strips(strip1_width, fab3_p1) + calc_strips((circumfrence + 2), 1, strip1_width, false), 
		fab3_bind_strip	= calc_strips((circumfrence + 10), 1, strip3_width, true);
		fab3_inches		= (fab3_strip1 * strip1_width) + (fab3_bind_strip * strip3_width);
	document.getElementById('fab_1').innerHTML		= '&nbsp;- ' + calc_yds_str(fab1_inches) + format_cut_instr(strip1_width, fab1_strip1, '<b>small</b> square', fab1_p1) + format_cut_instr(strip2_width, fab1_strip2, '<b>rectangle</b>', fab1_p2, strip1_width) + format_subcut_instr('strips', '<b>large</b> square', fab1_p3, strip2_width, strip2_width) + ' Use remainder for <b>Border</b>.';
	document.getElementById('fab_2').innerHTML		= '&nbsp;- ' + calc_yds_str(fab2_inches) + format_cut_instr(strip1_width, fab2_strip1, '<b>small</b> square', fab2_p1) + ' Use remainder for <b>Border</b>.';
	document.getElementById('fab_3').innerHTML		= '&nbsp;- ' + calc_yds_str(fab3_inches) + format_cut_instr(strip1_width, fab3_strip1, '<b>small</b> square', fab3_p1) + ' Use remainder for <b>Border</b>.' + format_cut_instr(strip3_width, fab3_bind_strip) + ' <b>Binding</b>';
	document.getElementById('mgq_num_unita').innerHTML	= 'Make ' + block1 + ' Blocks.';
	document.getElementById('mgq_num_unitb').innerHTML	= 'Make ' + block2 + ' Blocks.';
	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_finished').innerHTML	= format_fraction(width, 4) + '" by ' + format_fraction(height, 4) + '"';
	document.getElementById('mgq_border_width').innerHTML	= format_fraction(size, 8) + '"';
	document.getElementById('mgq_num_blocks').innerHTML	= cols * rows;
	document.getElementById('mgq_backing').innerHTML	= Math.floor(width + 8) + '&quot; by ' + Math.floor(height + 8) + '&quot';
	document.getElementById('mgq_info').innerHTML	= 'Finished size: ' + format_fraction(width, 4) + '&quot; by ' + format_fraction(height, 4) + '&quot; using ' + ((fab_width > 22) ? (fab_width + 2) + '&quot; fabric bolt width' : 'fat quarters') + '.';
}

//	set_preview(cols, rows);
function set_preview(cols, rows) 
{
	var block1	= 'images/double_irish_01_blk_a.jpg', 
		block2	= 'images/double_irish_01_blk_b.jpg', 
		up_left	= 'images/double_irish_01_up_left.jpg', 
		up_right	= 'images/double_irish_01_up_right.jpg', 
		lo_left	= 'images/double_irish_01_lo_left.jpg', 
		lo_right	= 'images/double_irish_01_lo_right.jpg', 
		top		= 'images/double_irish_01_top.jpg', 
		left	= 'images/double_irish_01_left.jpg', 
		right	= 'images/double_irish_01_right.jpg', 
		bottom	= 'images/double_irish_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	= '';
	}
}
