////////////////////////////////////////////////////////////////////////////////
// CONTRA DANCE DESIGNER - graphical display of contra dances, version 2.3
// COPYRIGHT (C) 2001  GCH & FRR Productions
// COPYRIGHT (C) 2001  GCH & AC Productions
////////////////////////////////////////////////////////////////////////////////
// this reference includes the most current GNU GENERAL PUBLIC LICENSE
// available online at www.gnu.org
////////////////////////////////////////////////////////////////////////////////
// VERSION 2.3 ONLY SUPPORTS MS/IE4.0+ FOR WINDOWS, MS/IE5.0+ FOR APPLES
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////

function d_constructor()
{
	this.d_couple = 0;
	this.d_lead = null;
	this.d_active = null;
	this.d_in = null;
	this.d_style = null;
	this.d_tlx = 0;
	this.d_tly = 0;
	this.d_move_ix = -1;
	this.d_step_ix = -1;
	this.d_step_count = -1;
	this.d_step_code = -1;
	this.d_condition = new Array();
	this.d_setfalse();
}

////////////////////////////////////////////////////////////////////////////////

function d_getlws()
{
	var zerone = 0;
	var top = true;
	var left = true;

	zerone = (((this.d_tlx - g_floor_x_top) / (g_dancer_size * g_floor_square)) % 2);
	if (zerone == 0)
	{
		top = ((g_contra_skip == true) ? true : false);
	}
	else if (zerone == 1)
	{
		top = ((g_contra_skip == true) ? false : true);
	}
	else
	{
		top = true;
		trace("ERROR: getlws unknown tlx");
	}
	if (this.d_tly == g_floor_y_leftedge)
	{
		left = true;
	}
	else if (this.d_tly == (g_floor_y_rightedge - g_dancer_size))
	{
		left = false;
	}
	else
	{
		left = true;
		trace("ERROR: getlws unknown tly");
	}
	switch(this.d_step_code)
	{
	case g_stepcode_lwstopleft:
	{
		if ((top == true) && (left == true))
		{
			return true;
		}
		break;
	}
	case g_stepcode_lwstopright:
	{
		if ((top == true) && (left == false))
		{
			return true;
		}
		break;
	}
	case g_stepcode_lwsbottomleft:
	{
		if ((top == false) && (left == true))
		{
			return true;
		}
		break;
	}
	case g_stepcode_lwsbottomright:
	{
		if ((top == false) && (left == false))
		{
			return true;
		}
		break;
	}
	default:
	{
		trace("ERROR: getlws unknown stepcode");
		break;
	}
	}

	return false;
}

////////////////////////////////////////////////////////////////////////////////

function d_getsql()
{
	var xtop = 0;
	var xbottom = 0;
	var sqval = (g_dancer_size * g_floor_square);

	xtop = (g_floor_x_topedge + sqval) + ((g_contra_skip == true) ? sqval : 0);
	xbottom = (g_floor_x_bottomedge - sqval - g_dancer_size) - ((g_contra_skip == true) ? sqval : 0);

	switch(this.d_step_code)
	{
	case g_stepcode_sqltop:
	{
		if (this.d_tlx <= xtop)
		{
			return true;
		}
		break;
	}
	case g_stepcode_sqlmiddle:
	{
		if ((this.d_tlx > xtop) && (this.d_tlx < xbottom))
		{
			return true;
		}
		break;
	}
	case g_stepcode_sqlbottom:
	{
		if (this.d_tlx >= xbottom)
		{
			return true;
		}
		break;
	}
	default:
	{
		trace("ERROR: getsql unknown stepcode");
		break;
	}
	}

	return false;
}

////////////////////////////////////////////////////////////////////////////////

function d_goto(nx,ny)
{
	if ((nx >= g_floor_x_top) && (nx <= g_floor_x_bottom))
	{
		this.d_tlx = nx;
		this.d_style.top = nx;
	}
	if ((ny >= g_floor_y_left) && (ny <= g_floor_y_right))
	{
		this.d_tly = ny;
		this.d_style.left = ny;
	}
}

////////////////////////////////////////////////////////////////////////////////

function dancer_initialize()
{
	trace("dancer_initialize begin");

	g_dancers = new Array();
	g_dancer_count = 0;

	d_constructor.prototype.d_getlws = d_getlws;
	d_constructor.prototype.d_getsql = d_getsql;
	d_constructor.prototype.d_goto = d_goto;
	d_constructor.prototype.d_moveup = d_moveup;
	d_constructor.prototype.d_moveupright = d_moveupright;
	d_constructor.prototype.d_moveright = d_moveright;
	d_constructor.prototype.d_movedownright = d_movedownright;
	d_constructor.prototype.d_movedown = d_movedown;
	d_constructor.prototype.d_movedownleft = d_movedownleft;
	d_constructor.prototype.d_moveleft = d_moveleft;
	d_constructor.prototype.d_moveupleft = d_moveupleft;
	d_constructor.prototype.d_run = d_run;
	d_constructor.prototype.d_scanstep = d_scanstep;
	d_constructor.prototype.d_setfalse = d_setfalse;

	g_dancers[g_dancer_count] = new d_constructor();
	g_dancers[g_dancer_count].d_couple = 1
	g_dancers[g_dancer_count].d_lead = true;
	g_dancers[g_dancer_count].d_style = document.getElementById("LEAD_GREEN").style;
	g_dancers[g_dancer_count].d_goto(g_floor_x_top,g_floor_y_right);
	g_dancer_count += 1;

	g_dancers[g_dancer_count] = new d_constructor();
	g_dancers[g_dancer_count].d_couple = 1
	g_dancers[g_dancer_count].d_lead = false;
	g_dancers[g_dancer_count].d_style = document.getElementById("FOLLOW_GREEN").style;
	g_dancers[g_dancer_count].d_goto(g_floor_x_top,g_floor_y_right);
	g_dancer_count += 1;

	g_dancers[g_dancer_count] = new d_constructor();
	g_dancers[g_dancer_count].d_couple = 2
	g_dancers[g_dancer_count].d_lead = true;
	g_dancers[g_dancer_count].d_style = document.getElementById("LEAD_GRAY").style;
	g_dancers[g_dancer_count].d_goto(g_floor_x_top,g_floor_y_right);
	g_dancer_count += 1;

	g_dancers[g_dancer_count] = new d_constructor();
	g_dancers[g_dancer_count].d_couple = 2
	g_dancers[g_dancer_count].d_lead = false;
	g_dancers[g_dancer_count].d_style = document.getElementById("FOLLOW_GRAY").style;
	g_dancers[g_dancer_count].d_goto(g_floor_x_top,g_floor_y_right);
	g_dancer_count += 1;

	g_dancers[g_dancer_count] = new d_constructor();
	g_dancers[g_dancer_count].d_couple = 3
	g_dancers[g_dancer_count].d_lead = true;
	g_dancers[g_dancer_count].d_style = document.getElementById("LEAD_BLUE").style;
	g_dancers[g_dancer_count].d_goto(g_floor_x_top,g_floor_y_right);
	g_dancer_count += 1;

	g_dancers[g_dancer_count] = new d_constructor();
	g_dancers[g_dancer_count].d_couple = 3
	g_dancers[g_dancer_count].d_lead = false;
	g_dancers[g_dancer_count].d_style = document.getElementById("FOLLOW_BLUE").style;
	g_dancers[g_dancer_count].d_goto(g_floor_x_top,g_floor_y_right);
	g_dancer_count += 1;

	g_dancers[g_dancer_count] = new d_constructor();
	g_dancers[g_dancer_count].d_couple = 4
	g_dancers[g_dancer_count].d_lead = true;
	g_dancers[g_dancer_count].d_style = document.getElementById("LEAD_YELLOW").style;
	g_dancers[g_dancer_count].d_goto(g_floor_x_top,g_floor_y_right);
	g_dancer_count += 1;

	g_dancers[g_dancer_count] = new d_constructor();
	g_dancers[g_dancer_count].d_couple = 4
	g_dancers[g_dancer_count].d_lead = false;
	g_dancers[g_dancer_count].d_style = document.getElementById("FOLLOW_YELLOW").style;
	g_dancers[g_dancer_count].d_goto(g_floor_x_top,g_floor_y_right);
	g_dancer_count += 1;

	g_dancers[g_dancer_count] = new d_constructor();
	g_dancers[g_dancer_count].d_couple = 5
	g_dancers[g_dancer_count].d_lead = true;
	g_dancers[g_dancer_count].d_style = document.getElementById("LEAD_VIOLET").style;
	g_dancers[g_dancer_count].d_goto(g_floor_x_top,g_floor_y_right);
	g_dancer_count += 1;

	g_dancers[g_dancer_count] = new d_constructor();
	g_dancers[g_dancer_count].d_couple = 5
	g_dancers[g_dancer_count].d_lead = false;
	g_dancers[g_dancer_count].d_style = document.getElementById("FOLLOW_VIOLET").style;
	g_dancers[g_dancer_count].d_goto(g_floor_x_top,g_floor_y_right);
	g_dancer_count += 1;

	g_dancers[g_dancer_count] = new d_constructor();
	g_dancers[g_dancer_count].d_couple = 6
	g_dancers[g_dancer_count].d_lead = true;
	g_dancers[g_dancer_count].d_style = document.getElementById("LEAD_RED").style;
	g_dancers[g_dancer_count].d_goto(g_floor_x_top,g_floor_y_right);
	g_dancer_count += 1;

	g_dancers[g_dancer_count] = new d_constructor();
	g_dancers[g_dancer_count].d_couple = 6
	g_dancers[g_dancer_count].d_lead = false;
	g_dancers[g_dancer_count].d_style = document.getElementById("FOLLOW_RED").style;
	g_dancers[g_dancer_count].d_goto(g_floor_x_top,g_floor_y_right);
	g_dancer_count += 1;

	trace("dancer_initialize end");
}

////////////////////////////////////////////////////////////////////////////////

function d_movedown()
{
	var nx = this.d_tlx + g_dancer_move;

	this.d_goto(nx,-1);
}

function d_movedownleft()
{
	var nx = this.d_tlx + g_dancer_move;
	var ny = this.d_tly - g_dancer_move;

	this.d_goto(nx,ny);
}

function d_movedownright()
{
	var nx = this.d_tlx + g_dancer_move;
	var ny = this.d_tly + g_dancer_move;

	this.d_goto(nx,ny);
}

function d_moveleft()
{
	var ny = this.d_tly - g_dancer_move;

	this.d_goto(-1,ny);
}

function d_moveright()
{
	var ny = this.d_tly + g_dancer_move;

	this.d_goto(-1,ny);
}

function d_moveup()
{
	var nx = this.d_tlx - g_dancer_move;

	this.d_goto(nx,-1);
}

function d_moveupleft()
{
	var nx = this.d_tlx - g_dancer_move;
	var ny = this.d_tly - g_dancer_move;

	this.d_goto(nx,ny);
}

function d_moveupright()
{
	var nx = this.d_tlx - g_dancer_move;
	var ny = this.d_tly + g_dancer_move;

	this.d_goto(nx,ny);
}

////////////////////////////////////////////////////////////////////////////////

function d_run(pix)
{
	trace("d_run begin " + pix + " " + this.d_move_ix + " " + this.d_step_ix);

	var again = false;
	var square = 0;

	if ((pix < 0) || (pix >= g_dancer_count))
	{
		g_move_continue = false;
		trace("ERROR: invalid d_run ix");
		return;
	}
	if ((this.d_move_ix < 0) || (this.d_move_ix >= g_move_count))
	{
		g_move_continue = false;
		trace("ERROR: invalid move ix");
		return;
	}

	this.d_step_count = g_moves[this.d_move_ix].m_step_count;

	if ((this.d_step_ix < 0) || (this.d_step_ix >= this.d_step_count))
	{
		g_move_continue = false;
		trace("ERROR: invalid step ix");
		return;
	}

	this.d_step_code = g_moves[this.d_move_ix].m_steps[this.d_step_ix];
	this.d_step_ix += 1;

	trace("d_run step code = " + this.d_step_code);
	switch(this.d_step_code)
	{
	case g_stepcode_wait:
	{
		break;
	}
	case g_stepcode_exit:
	{
		again = false;
		g_move_continue = false;
		break;
	}
	case g_stepcode_moveup:
	{
		this.d_moveup();
		break;
	}
	case g_stepcode_moveupright:
	{
		this.d_moveupright();
		break;
	}
	case g_stepcode_moveright:
	{
		this.d_moveright();
		break;
	}
	case g_stepcode_movedownright:
	{
		this.d_movedownright();
		break;
	}
	case g_stepcode_movedown:
	{
		this.d_movedown();
		break;
	}
	case g_stepcode_movedownleft:
	{
		this.d_movedownleft();
		break;
	}
	case g_stepcode_moveleft:
	{
		this.d_moveleft();
		break;
	}
	case g_stepcode_moveupleft:
	{
		this.d_moveupleft();
		break;
	}
	case g_stepcode_lwstopleft:
	case g_stepcode_lwstopright:
	case g_stepcode_lwsbottomleft:
	case g_stepcode_lwsbottomright:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_getlws() == true)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_sqltop:
	case g_stepcode_sqlmiddle:
	case g_stepcode_sqlbottom:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_getsql() == true)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_dfztophalf:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_tlx < g_floor_x_middle)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_dfzbottomhalf:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_tlx >= g_floor_x_middle)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_dfztopedge:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_tlx < g_floor_x_topedge)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_dfzbottomedge:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_tlx >= g_floor_x_bottomedge)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_dfzlefthalf:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_tly < g_floor_y_center)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_dfzrighthalf:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_tly >= g_floor_y_center)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_dfzleftedge:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_tly < g_floor_y_leftedge)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_dfzrightedge:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_tly >= g_floor_y_rightedge)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_dfzmiddle:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if ((this.d_tlx >= g_floor_x_topedge) && (this.d_tlx < g_floor_x_bottomedge) &&
			(this.d_tly >= g_floor_y_leftedge) && (this.d_tly < g_floor_y_rightedge))
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_lead:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_lead == true)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_follow:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_lead != true)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_active:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_active == true)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_inactive:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_active != true)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_in:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_in == true)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	case g_stepcode_out:
	{
		again = true;
		if (this.d_condition[this.d_step_code] == true)
		{
			this.d_condition[this.d_step_code] = false;
		}
		else if (this.d_in != true)
		{
			this.d_condition[this.d_step_code] = true;
		}
		else
		{
			this.d_scanstep();
		}
		break;
	}
	default:
	{
		g_move_continue = false;
		again = false;
		trace("ERROR: unknown step = " + this.d_step_code);
		break;
	}
	}
	if ((this.d_step_ix < 0) || (this.d_step_ix >= this.d_step_count))
	{
		g_move_continue = false;
		again = false;
	}
	if (again == true)
	{
		this.d_run(pix);
	}

	trace("d_run end " + pix + " " + this.d_move_ix);
}

////////////////////////////////////////////////////////////////////////////////

function d_scanstep()
{
	while ((this.d_step_ix < this.d_step_count) &&
		(this.d_step_code != g_moves[this.d_move_ix].m_steps[this.d_step_ix]))
	{
		this.d_step_ix += 1;
	}
	this.d_step_ix += 1;
}

////////////////////////////////////////////////////////////////////////////////

function d_setfalse()
{
	this.d_condition[g_stepcode_lwstopleft] = false;
	this.d_condition[g_stepcode_lwstopright] = false;
	this.d_condition[g_stepcode_lwsbottomleft] = false;
	this.d_condition[g_stepcode_lwsbottomright] = false;
	this.d_condition[g_stepcode_sqltop] = false;
	this.d_condition[g_stepcode_sqlmiddle] = false;
	this.d_condition[g_stepcode_sqlbottom] = false;
	this.d_condition[g_stepcode_dfztophalf] = false;
	this.d_condition[g_stepcode_dfzbottomhalf] = false;
	this.d_condition[g_stepcode_dfzlefthalf] = false;
	this.d_condition[g_stepcode_dfzrighthalf] = false;
	this.d_condition[g_stepcode_dfztopedge] = false;
	this.d_condition[g_stepcode_dfzbottomedge] = false;
	this.d_condition[g_stepcode_dfzleftedge] = false;
	this.d_condition[g_stepcode_dfzrightedge] = false;
	this.d_condition[g_stepcode_dfzmiddle] = false;
	this.d_condition[g_stepcode_lead] = false;
	this.d_condition[g_stepcode_follow] = false;
	this.d_condition[g_stepcode_active] = false;
	this.d_condition[g_stepcode_inactive] = false;
	this.d_condition[g_stepcode_in] = false;
	this.d_condition[g_stepcode_out] = false;
}

////////////////////////////////////////////////////////////////////////////////

function dancer_show(pix)
{
	if ((pix < 0) || (pix >= g_dancer_count))
	{
		trace("ERROR: invalid dancer ix");
		return;
	}
	trace("");
	trace("show dancer ix = " + pix);
	trace("  couple = " + g_dancers[pix].d_couple);
	trace("  lead = " + g_dancers[pix].d_lead);
	trace("  active = " + g_dancers[pix].d_active);
	trace("  in = " + g_dancers[pix].d_in);
	trace("  tlx = " + g_dancers[pix].d_tlx);
	trace("  tly = " + g_dancers[pix].d_tly);
	trace("  move_ix = " + g_dancers[pix].d_move_ix);
	trace("  step_ix = " + g_dancers[pix].d_step_ix);
	trace("  step_count = " + g_dancers[pix].d_step_count);
	trace("  step_code = " + g_dancers[pix].d_step_code);
}

////////////////////////////////////////////////////////////////////////////////
// CONTRA DANCE DESIGNER - graphical display of contra dances, version 2.3
// COPYRIGHT (C) 2001  GCH & FRR Productions
// COPYRIGHT (C) 2001  GCH & AC Productions
////////////////////////////////////////////////////////////////////////////////
// this reference includes the most current GNU GENERAL PUBLIC LICENSE
// available online at www.gnu.org
////////////////////////////////////////////////////////////////////////////////
// VERSION 2.3 ONLY SUPPORTS MS/IE4.0+ FOR WINDOWS, MS/IE5.0+ FOR APPLES
////////////////////////////////////////////////////////////////////////////////

