/* -----------------------------------------------------------------------------
	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'];

//	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'), 
		unit1	= document.getElementById('mgq_unit1'), 
		unit2	= document.getElementById('mgq_unit2'), 
		num_units	= num * 8, 
		strip_width	= (width / 4) + .875, 
		fab1_strips	= calc_strips(strip_width, num_units), 
		fab2_3_strips	= calc_strips(strip_width, (num_units / 2));
	unit1.innerHTML	= num_units;
	unit2.innerHTML	= num_units;
	fab1.innerHTML	= calc_yds_str(strip_width, fab1_strips) + '<br />Cut ' + fab1_strips + ' ' + ((fab1_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab1_strips > 1) ? 'strips' : 'strip') + ' into ' + num_units + ' squares.';
	fab2.innerHTML	= calc_yds_str(strip_width, fab2_3_strips) + '<br />Cut ' + fab2_3_strips + ' ' + ((fab2_3_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab2_3_strips > 1) ? 'strips' : 'strip') + ' into ' + (num_units / 2) + ' squares.';
	fab3.innerHTML	= calc_yds_str(strip_width, fab2_3_strips) + '<br />Cut ' + fab2_3_strips + ' ' + ((fab2_3_strips > 1) ? 'strips' : 'strip') + ' ' + format_fraction(strip_width, 8) + ' inches wide. Subcut ' + ((fab2_3_strips > 1) ? 'strips' : 'strip') + ' into ' + (num_units / 2) + ' squares.';
}

//	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/balkan_puzzle_12.jpg', 
		up_left	= 'images/balkan_puzzle_12_up_left.jpg', 
		up_right	= 'images/balkan_puzzle_12_up_right.jpg', 
		lo_left	= 'images/balkan_puzzle_12_lo_left.jpg', 
		lo_right	= 'images/balkan_puzzle_12_lo_right.jpg', 
		top		= 'images/balkan_puzzle_12_top.jpg', 
		left	= 'images/balkan_puzzle_12_left.jpg', 
		right	= 'images/balkan_puzzle_12_right.jpg', 
		bottom	= 'images/balkan_puzzle_12_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_fabric(size, cols, rows, num, bor_width, width, height);
function calc_fabric(size, cols, rows, num, bor_width, width, height) 
{
	var num_units			= num * 8, 
		unit_size			= size / 4, 
		unit_strip_width	= unit_size + 0.875, 
		unit_squares		= Math.ceil(num_units / 2), 
		unit_strips			= calc_strips(unit_strip_width, unit_squares), 
		bor_strip_width		= (bor_width / 3) + 0.5, 
		bor_strips			= calc_strips(((width + height + 2) * 2), 1, bor_strip_width, false),
		bind_strip_width	= 2, 
		bind_length			= (((width + height) * 2) + 10), 
		bind_strips			= calc_strips(bind_length, 1, bind_strip_width, true), 
		fab1_unit_ab_strips	= calc_strips(unit_strip_width, num_units), 
		fab1				= (fab1_unit_ab_strips * unit_strip_width) + (bor_strips * bor_strip_width), 
		fab2				= (unit_strips * unit_strip_width) + (bor_strips * bor_strip_width), 
		fab3				= (unit_strips * unit_strip_width) + (bor_strips * bor_strip_width) + (bind_strips * bind_strip_width);
	fab1	= calc_yds_str(fab1) + format_cut_instr(unit_strip_width, fab1_unit_ab_strips, '<b>Unit A &amp; B</b> square', num_units) + format_cut_instr(bor_strip_width, bor_strips) + ' (<b>Border</b>)';
	fab2	= calc_yds_str(fab2) + format_cut_instr(unit_strip_width, unit_strips, '<b>Unit A</b> square', unit_squares) + format_cut_instr(bor_strip_width, bor_strips) + ' (<b>Border</b>)';
	fab3	= calc_yds_str(fab3) + format_cut_instr(unit_strip_width, unit_strips, '<b>Unit B</b> square', unit_squares) + format_cut_instr(bor_strip_width, bor_strips) + ' (<b>Border</b>)' + format_cut_instr(bind_strip_width, bind_strips) + ' (<b>Binding</b>)';
	document.getElementById('mgq_fab1').innerHTML	= fab1;
	document.getElementById('mgq_fab2').innerHTML	= fab2;
	document.getElementById('mgq_fab3').innerHTML	= fab3;
	//document.write('<textarea cols="50" rows="20">' + fab1 + '</textarea>');
	//document.write('<textarea cols="50" rows="20">' + fab2 + '</textarea>');
	//document.write('<textarea cols="50" rows="20">' + fab3 + '</textarea>');
}

//	calc_quilt(size, cols, rows, redraw);
function calc_quilt(size, cols, rows, redraw) 
{
	var num			= cols * rows, 
		bor_width	= (size / 8) * 3, 
		width		= (cols * size) + (2 * bor_width), 
		height		= (rows * size) + (2 * bor_width);
	document.getElementById('mgq_size').innerHTML	= format_fraction(width, 4) + '" by ' + format_fraction(height, 4) + '"';
	document.getElementById('mgq_border_width').innerHTML	= format_fraction(bor_width, 8) + '"';;
	document.getElementById('mgq_num_blocks').innerHTML	= num;
	document.getElementById('mgq_blocks').innerHTML	= num;
	document.getElementById('mgq_unit1').innerHTML	= num * 8;
	document.getElementById('mgq_unit2').innerHTML	= num * 8;
	document.getElementById('mgq_horiz_width').innerHTML	= format_fraction(width + 0.5, 8);
	document.getElementById('mgq_vert_width').innerHTML	= format_fraction(height + 0.5, 8);
	document.getElementById('mgq_backing').innerHTML	= Math.floor(width + 8) + '" by ' + Math.floor(height + 8) + '"';
	if (redraw) 
	{
		set_preview(cols, rows);
	}
	calc_fabric(size, cols, rows, num, bor_width, width, height);
	return 'Finished ' + format_fraction(width, 4) + '" X ' + format_fraction(height, 8) + '"';
}

//	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);
	//	block only
	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;
	}
	//	full quilt
	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) + '" 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	= '';
	}
}
