/* -----------------------------------------------------------------------------
	iQuilt Pattern - Dancing Pinwheel block - scripts
----------------------------------------------------------------------------- */
var units		= [], 
	fabrics		= [], 
	blocks		= {}, 
	fab_width	= 42, 
	owin, 
	alpha		= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];

//	new_image(src, title, class_name);
function new_image(src, title, class_name) 
{
	var oimg	= new Image();
	oimg.src	= src;
	oimg.alt	= title;
	oimg.title	= title;
	oimg.className	= class_name;
	return oimg;
}

//	set_preview(cols, rows);
function set_preview(cols, rows) 
{
	var block	= 'images/ribbon_star_05.jpg', 
		corner	= 'images/fab0021.jpg', 
		top		= 'images/ribbon_star_05_top.jpg', 
		left	= 'images/ribbon_star_05_left.jpg', 
		right	= 'images/ribbon_star_05_right.jpg', 
		bottom	= 'images/ribbon_star_05_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(corner, '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(corner, '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(corner, '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(corner, 'cornerstone', 'corner');
	ocell	= orow.insertCell(-1);
	ocell.appendChild(oimg);
}

//	calc_units(cols, rows, block_size, bor_width);
function calc_units(cols, rows, block_size, bor_width) 
{
	var num		= cols * rows, 
		unit_size	= block_size / 3, 
		num_a	= num, 
		a_size	= unit_size + 0.5, 
		num_b	= num * 4, 
		b_size	= unit_size + 0.875, 
		num_c	= num * 4, 
		c_size	= unit_size + 0.875, 
		bor_length	= ((cols + rows) * block_size) * 2 + (4 * 0.5), 
		bor_corner	= bor_width + 0.5, 
		bor_strip_width	= (bor_width / 3) + 0.5, 
		bind_length	= (((cols + rows) * block_size) * 2) + 10, 
		bind_strips	= calc_strips(bind_length, 1, 2, true), 
		fab_1_unit_a_strips	= calc_strips(a_size, num_a), 
		fab_1_unit_b_squares	= Math.ceil(num_b / 2), 
		fab_1_unit_b_strips	= calc_strips(b_size, fab_1_unit_b_squares), 
		fab_1_border_strips = calc_strips(bor_length, 1, bor_strip_width, false), 
		fab_1	= (fab_1_unit_a_strips * a_size) + (fab_1_unit_b_strips * b_size) + (fab_1_border_strips * bor_strip_width), 
		fab_2_unit_bc_squares	= Math.ceil((num_b + num_c) / 2), 
		fab_2_unit_bc_strips	= calc_strips(b_size, fab_2_unit_bc_squares), 
		fab_2_border_strips = calc_strips(bor_length, 1, bor_strip_width, false), 
		fab_2	= (b_size * fab_2_unit_bc_strips) + (fab_2_border_strips * bor_strip_width), 
		fab_3_unit_c_squares	= Math.ceil(num_c / 2), 
		fab_3_unit_c_strips	= calc_strips(c_size, fab_3_unit_c_squares), 
		fab_3_border_strips = calc_strips(bor_length, 1, bor_strip_width, false), 
		fab_3_corner_strips = calc_strips((bor_width + 0.5), 4), 
		fab_3	= (c_size * fab_3_unit_c_strips) + (fab_3_border_strips * bor_strip_width) + (fab_3_corner_strips * bor_corner) + (2 * bind_strips);
	//	fabric 1
	//	format_cut_instr(width, strips, piece, num, height)
	fab_1	= calc_yds_str(fab_1) + format_cut_instr(a_size, fab_1_unit_a_strips, '<b>Unit A</b> square', num_a) + format_cut_instr(b_size, fab_1_unit_b_strips, '<b>Unit B</b> square', fab_1_unit_b_squares) + format_cut_instr(bor_strip_width, fab_1_border_strips) + ' (<b>Border</b>)';
	document.getElementById('mgq_fab1').innerHTML	= fab_1;
	//	fabric 2
	fab_2	= calc_yds_str(fab_2) + format_cut_instr(b_size, fab_2_unit_bc_strips, '<b>Unit B</b> square', fab_2_unit_bc_squares) + format_cut_instr(bor_strip_width, fab_2_border_strips) + ' (<b>Border</b>)';
	document.getElementById('mgq_fab2').innerHTML	= fab_2;
	//	fabric 3
	fab_3	= calc_yds_str(fab_3) + format_cut_instr(c_size, fab_3_unit_c_strips, '<b>Unit C</b> square', fab_3_unit_c_squares) + format_cut_instr(bor_strip_width, fab_3_border_strips) + '(<b>Border</b>)' + format_cut_instr(bor_corner, fab_3_corner_strips, '<b>Cornerstone</b> square', 4) + format_cut_instr(2, bind_strips) + '(<b>Binding</b>)';
	document.getElementById('mgq_fab3').innerHTML	= fab_3;
	document.getElementById('mgq_unit2').innerHTML	= num_b;
	document.getElementById('mgq_unit3').innerHTML	= num_c;
	document.getElementById('mgq_hst_size').innerHTML	= format_fraction(b_size, 8);
	//document.write('<textarea cols="50" rows="20">' + fab_1 + '</textarea>');
	//document.write('<textarea cols="50" rows="20">' + fab_2 + '</textarea>');
	//document.write('<textarea cols="50" rows="20">' + fab_3 + '</textarea>');
}

//	calc_quilt(size, cols, rows, redraw)
function calc_quilt(size, cols, rows, redraw) 
{
	var num			= cols * rows, 
		bor_width	= size / 2, 
		width		= (size * cols) + (bor_width * 2), 
		height		= (size * rows) + (bor_width * 2);
	document.getElementById('mgq_size').innerHTML	= ' ' + format_fraction(width, 8) + '" by ' + format_fraction(height, 8) + '"';
	document.getElementById('mgq_border_width').innerHTML	= format_fraction(bor_width, 8) + '"';
	document.getElementById('mgq_num_blocks').innerHTML	= num;
	document.getElementById('mgq_blocks').innerHTML	= num;
	if (redraw) 
	{
		set_preview(cols, rows);
	}
	calc_units(cols, rows, size, bor_width);
	document.getElementById('mgq_horiz_width').innerHTML	= format_fraction(((cols * size) + 0.5));
	document.getElementById('mgq_vert_width').innerHTML	= format_fraction(((rows * size) + 0.5));
	document.getElementById('mgq_backing').innerHTML	= Math.floor(width + 8) + '" by ' + Math.floor(height + 8) + '"';
	return 'Finished size: ' + format_fraction(width, 4) + '" X ' + format_fraction(height, 4) + '"';
}

//	calc_block(width, num);
function calc_block(width, num) 
{
	var num_units		= num * 4, 
		sq_width		= (width / 3) + .5, 
		hst_sq_width	= (width / 3) + .875, 
		fab1a_strips	= calc_strips(sq_width, num), 
		fab1b_strips	= calc_strips(hst_sq_width, (num_units / 2)), 
		fab1_inches		= (fab1a_strips + sq_width) + (fab1b_strips * hst_sq_width), 
		fab2_strips		= calc_strips(hst_sq_width, num_units), 
		fab3_strips		= calc_strips(hst_sq_width, (num_units / 2));
	document.getElementById('mgq_unit2').innerHTML	= num_units;
	document.getElementById('mgq_unit3').innerHTML	= num_units;
	document.getElementById('mgq_hst_size').innerHTML	= format_fraction(hst_sq_width, 8);
	document.getElementById('mgq_fab1').innerHTML	= calc_yds_str(fab1_inches) + '<br /><b>Unit A</b> Cut ' + fab1a_strips + ' ' + ((fab1a_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(sq_width, 4) + ' inches wide. Subcut into ' + num + ' ' + ((num > 1) ? 'squares' : 'square') + '.<br />Cut ' + fab1b_strips + ' ' + ((fab1b_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(hst_sq_width, 8) + ' inches wide. Subcut into ' + (num_units / 2) + ' squares.';
	document.getElementById('mgq_fab2').innerHTML	= calc_yds_str(hst_sq_width, fab2_strips) + '<br />Cut ' + fab2_strips + ' ' + ((fab2_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(hst_sq_width, 8) + ' inches wide. Subcut into ' + num_units + ' squares.';
	document.getElementById('mgq_fab3').innerHTML	= calc_yds_str(hst_sq_width, fab3_strips) + '<br />Cut ' + fab3_strips + ' ' + ((fab3_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(hst_sq_width, 8) + ' inches wide. Subcut into ' + (num_units / 2) + ' squares.';
}

//	update_page();
function update_page() 
{
	var size	= Number(document.mgq_form.block_size.value), 
		num, 
		cols, 
		rows, 
		fab_str, 
		block_str, 
		info_str, 
		redraw;
	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);
		if ((cols * rows) != Number(document.getElementById('mgq_num_blocks').innerHTML)) 
		{
			redraw	= true;
		}
		else 
		{
			redraw	= false;
		}
		info_str	= calc_quilt(size, cols, rows, redraw);
		fab_str	= (fab_width == 20) ? 'fat quarters' : (fab_width+2) + ' inch fabric bolt width';
		document.getElementById('mgq_info').innerHTML	= info_str + ' using ' + fab_str;
	}
}

//	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	= '';
	}
}
