/* -----------------------------------------------------------------------------
	iQuilt Pattern - Balkan Puzzle block - scripts
----------------------------------------------------------------------------- */
var units		= [], 
	fabrics		= [], 
	blocks		= {}, 
	fab_width	= 42, 
	owin, 
	alpha		= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];

//	set_preview(cols, rows);
function set_preview(cols, rows) 
{
	var block	= 'images/jewel_box_04.jpg', 
		up_left	= 'images/jewel_box_04_up_left.jpg', 
		up_right	= 'images/jewel_box_04_up_right.jpg', 
		lo_left	= 'images/jewel_box_04_lo_left.jpg', 
		lo_right	= 'images/jewel_box_04_lo_right.jpg', 
		top		= 'images/jewel_box_04_top.jpg', 
		left	= 'images/jewel_box_04_left.jpg', 
		right	= 'images/jewel_box_04_right.jpg', 
		bottom	= 'images/jewel_box_04_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) 
{
	var num	= cols * rows, 
		border	= (size / 8) * 3, 
		width	= (size * cols) + (border * 2), 
		height	= (size * rows) + (border * 2), 
		border1_width	= (size / 8) + 0.5, 
		border2_width	= (size / 4) + 0.5, 
		binding_width	= 2.5, 
		border1_strips	= calc_strips(((width + height + 1) * 2), 1, border1_width, false), 
		border2_strips	= calc_strips(((width + height + 1) * 2), 1, border2_width, false), 
		binding_strips	= calc_strips(((width + height + 1) * 2), 1, binding_width, true), 
		fab1	= document.getElementById('mgq_fab1'), 
		fab2	= document.getElementById('mgq_fab2'), 
		fab3	= document.getElementById('mgq_fab3'), 
		fab4	= document.getElementById('mgq_fab4'), 
		fab5	= document.getElementById('mgq_fab5'), 
		unit1	= document.getElementById('mgq_unit1'), 
		unit2	= document.getElementById('mgq_unit2'), 
		unit3	= document.getElementById('mgq_unit3'), 
		unit4	= document.getElementById('mgq_unit4'), 
		unit5	= document.getElementById('mgq_unit5'), 
		unit6	= document.getElementById('mgq_unit6'), 
		num_unit_a	= num * 2, 
		num_unit_b	= num * 4, 
		strip_width	= (size / 4) + 1, 
		fab_1_strips	= calc_strips(strip_width, (num * 8)), 
		fab_n_strips	= calc_strips(strip_width, (num * 2)), 
		hst_sq			= document.getElementById('mgq_hst_size'), 
		half_1			= document.getElementById('mgq_half_1'), 
		half_2			= document.getElementById('mgq_half_2'), 
		half			= format_fraction((strip_width / 2), 8);
	hst_sq.innerHTML	= format_fraction((strip_width - 0.5), 4);
	half_1.innerHTML	= half + ' inches';
	half_2.innerHTML	= half + ' inches';
	unit1.innerHTML	= num_unit_a;
	unit2.innerHTML	= num_unit_a;
	unit3.innerHTML	= num_unit_a;
	unit4.innerHTML	= num_unit_a;
	unit5.innerHTML	= num_unit_b;
	unit6.innerHTML	= num_unit_b;
	fab1.innerHTML	= calc_yds_str(strip_width, fab_1_strips) + '<br />Cut ' + fab_1_strips + ' ' + ((fab_1_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab_1_strips > 1) ? 'strips' : 'strip') + ' into ' + (num * 8) + ' squares.';
	fab2.innerHTML	= calc_yds_str(((strip_width * fab_n_strips) + (border1_width * border1_strips))) + '<br />Cut ' + fab_n_strips + ' ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' into ' + (num * 2) + ' squares.' + format_cut_instr(border1_width, border1_strips) + ' (<b>Border</b>)';
	fab3.innerHTML	= calc_yds_str(strip_width, fab_n_strips) + '<br />Cut ' + fab_n_strips + ' ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' into ' + (num * 2) + ' squares.';
	fab4.innerHTML	= calc_yds_str(strip_width, fab_n_strips) + '<br />Cut ' + fab_n_strips + ' ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' into ' + (num * 2) + ' squares.';
	fab5.innerHTML	= calc_yds_str(((strip_width * fab_n_strips) + (border2_width * border2_strips) + (binding_width * binding_strips))) + '<br />Cut ' + fab_n_strips + ' ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' into ' + (num * 2) + ' squares.' + format_cut_instr(border2_width, border2_strips) + ' (<b>Border</b>)' + format_cut_instr(binding_width, binding_strips) + ' (<b>Binding</b>)';
	document.getElementById('mgq_num_blocks').innerHTML	= 'Makes ' + num + ((num > 1) ? ' blocks' : ' block') + '.';
	document.getElementById('mgq_finished').innerHTML	= format_fraction(width, 4) + '" by ' + format_fraction(height, 4) + '"';
	document.getElementById('mgq_border_width').innerHTML	= format_fraction(border, 8) + '"';
	document.getElementById('mgq_blocks').innerHTML	= cols * rows;
	//mgq_horiz_width
	document.getElementById('mgq_horiz_width').innerHTML	= format_fraction((width + 0.5), 4);
	//mgq_vert_width
	document.getElementById('mgq_vert_width').innerHTML	= format_fraction((height + 0.5), 4);
	//mgq_backing
	document.getElementById('mgq_backing').innerHTML	= format_fraction((width + 8), 4) + '" by ' + format_fraction((height + 8), 4) + '"';
	document.getElementById('mgq_info').innerHTML	= 'Finished size: ' + format_fraction(width, 4) + '" by ' + format_fraction(height, 4) + '" using ' + ((fab_width > 20) ? (fab_width + 2) + '" fabric bolt width' : 'fat quarters') + '.';
}

//	calc_block(width, num);
function calc_block(width, num) 
{
	var fab1	= document.getElementById('mgq_fab1'), 
		fab2	= document.getElementById('mgq_fab2'), 
		fab3	= document.getElementById('mgq_fab3'), 
		fab4	= document.getElementById('mgq_fab4'), 
		fab5	= document.getElementById('mgq_fab5'), 
		unit1	= document.getElementById('mgq_unit1'), 
		unit2	= document.getElementById('mgq_unit2'), 
		unit3	= document.getElementById('mgq_unit3'), 
		unit4	= document.getElementById('mgq_unit4'), 
		unit5	= document.getElementById('mgq_unit5'), 
		unit6	= document.getElementById('mgq_unit6'), 
		num_unit_a	= num * 2, 
		num_unit_b	= num * 4, 
		strip_width	= (width / 4) + 1, 
		fab_1_strips	= calc_strips(strip_width, (num * 8)), 
		fab_n_strips	= calc_strips(strip_width, (num * 2)), 
		hst_sq			= document.getElementById('mgq_hst_size'), 
		half_1			= document.getElementById('mgq_half_1'), 
		half_2			= document.getElementById('mgq_half_2'), 
		half			= format_fraction((strip_width / 2), 8);
	hst_sq.innerHTML	= format_fraction((strip_width - 0.5), 4);
	half_1.innerHTML	= half + ' inches';
	half_2.innerHTML	= half + ' inches';
	unit1.innerHTML	= num_unit_a;
	unit2.innerHTML	= num_unit_a;
	unit3.innerHTML	= num_unit_a;
	unit4.innerHTML	= num_unit_a;
	unit5.innerHTML	= num_unit_b;
	unit6.innerHTML	= num_unit_b;
	fab1.innerHTML	= calc_yds_str(strip_width, fab_1_strips) + '<br />Cut ' + fab_1_strips + ' ' + ((fab_1_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab_1_strips > 1) ? 'strips' : 'strip') + ' into ' + (num * 8) + ' squares.';
	fab2.innerHTML	= calc_yds_str(strip_width, fab_n_strips) + '<br />Cut ' + fab_n_strips + ' ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' into ' + (num * 2) + ' squares.';
	fab3.innerHTML	= calc_yds_str(strip_width, fab_n_strips) + '<br />Cut ' + fab_n_strips + ' ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' into ' + (num * 2) + ' squares.';
	fab4.innerHTML	= calc_yds_str(strip_width, fab_n_strips) + '<br />Cut ' + fab_n_strips + ' ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' into ' + (num * 2) + ' squares.';
	fab5.innerHTML	= calc_yds_str(strip_width, fab_n_strips) + '<br />Cut ' + fab_n_strips + ' ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab_n_strips > 1) ? 'strips' : 'strip') + ' into ' + (num * 2) + ' squares.';
	document.getElementById('mgq_num_blocks').innerHTML	= 'Makes ' + num + ((num > 1) ? ' blocks' : ' block') + '.';
}

//	update_page();
function update_page() 
{
	var size	= Number(document.mgq_form.block_size.value), 
		num, 
		cols, 
		rows, 
		fab_str, 
		block_str;
	fab_width	= Number(document.mgq_form.fab_width.value);
	if (document.mgq_form.num_blocks) 
	{
		num	= Number(document.mgq_form.num_blocks.value);
		calc_block(size, num);
		fab_str	= (fab_width == 20) ? 'fat quarters' : (fab_width+2) + ' inch fabric bolt width';
		block_str	= (num > 1) ? 'blocks' : 'block';
		document.getElementById('mgq_info').innerHTML	= num + ' - ' + format_fraction(size, 4) + ' inch ' + block_str + ' from ' + fab_str;
	}
	else 
	{
		cols	= Number(document.mgq_form.num_cols.value);
		rows	= Number(document.mgq_form.num_rows.value);
		num		= Number(document.getElementById('mgq_blocks').innerHTML);
		calc_quilt(size, cols, rows);
		if (num != (cols * rows)) 
		{
			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	= '';
	}
}
