////////////////////////////////////////////////////////////////////////////////
// CONTRA DANCE DESIGNER - graphical display of contra dances, version 2.4
// 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.4 ONLY SUPPORTS MS/IE4.0+ FOR WINDOWS, MS/IE5.0+ FOR APPLES
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////

function screen_add()
{
	var opts = null;
	var label = null;

	stop_all();
	if ((g_contra_selected < 0) || (g_contra_selected >= g_contra_count))
	{
		trace("ERROR: invalid contra selected");
		return;
	}
	if ((g_move_selected < 0) || (g_move_selected >= g_move_count))
	{
		trace("ERROR: invalid move selected");
		return;
	}
	label = "(" + g_contras[g_contra_selected].c_move_count + "," + g_move_selected + ") " + g_moves[g_move_selected].m_name;
	contra_addmovebyindex(g_contra_selected,g_move_selected);
	opts = document.MAIN.CURRENT.options;
	opts[opts.length] = new Option(label,label,false,false);
	contra_info();
}

////////////////////////////////////////////////////////////////////////////////

function screen_contras()
{
	var i = 0;
	var j = 0;
	var opts = null;
	var label = null;

	stop_all();
	opts = document.MAIN.CURRENT.options;
	opts.length = 0;

	g_contra_selected = (document.MAIN.CONTRAS.selectedIndex - 1);
	trace("screen_contras ix = " + g_contra_selected);
	contra_info();

	if ((g_contra_selected < 0) || (g_contra_selected >= g_contra_count))
	{
		trace("ERROR: invalid contra ix");
		return;
	}
	for (i=0; i<g_contras[g_contra_selected].c_move_count; i+=1)
	{
		j = g_contras[g_contra_selected].c_moves[i];
		label = "(" + i + "," + j + ") " + g_moves[j].m_name;
		opts[opts.length] = new Option(label,label,false,false);
	}
}

////////////////////////////////////////////////////////////////////////////////

function screen_current()
{
	var ix = 0;

	ix = document.MAIN.CURRENT.selectedIndex;
	stop_all();

	if ((ix < 0) || (ix >= g_move_count))
	{
		trace("ERROR: invalid move ix");
		return;
	}
	if ((g_contra_selected < 0) || (g_contra_selected >= g_move_count))
	{
		trace("ERROR: invalid contra ix");
		return;
	}
	if (ix >= g_contras[g_contra_selected].c_move_count)
	{
		trace("ERROR: invalid move ix");
		return;
	}
	g_current_move = g_contras[g_contra_selected].c_moves[ix];
	g_move_selected = g_current_move;
	move_info();
	document.MAIN.CURRENT.selectedIndex = ix;
}

////////////////////////////////////////////////////////////////////////////////

function screen_fast()
{
	stop_all();

	g_move_delay -= g_move_increment;
	if ((g_move_delay < 50) || (g_move_delay > 3000))
	{
		g_move_delay = 500;
		trace("ERROR: invalid move delay");
	}
	info(4,"move speed = " + g_move_delay);
}

////////////////////////////////////////////////////////////////////////////////

function screen_help()
{
	show_help();
}

////////////////////////////////////////////////////////////////////////////////

function screen_loop()
{
	var mix1 = -1;
	var mix2 = -1;
	var mix3 = -1;

	if ((g_contra_selected < 0) || (g_contra_selected >= g_contra_count))
	{
		trace("ERROR: invalid contra selected");
		return;
	}

	if (g_contra_loop == -1)
	{
		stop_all();
		g_contra_loop = g_move_lineup;
		document.MAIN.CURRENT.selectedIndex = g_contra_loop;
		info(4,"current beats = 0");
		move_run(g_contra_loop);
		g_contra_loop += 1;
	}
	else if ((g_contra_loop > 0) && (g_contra_loop < g_contras[g_contra_selected].c_move_count))
	{
		document.MAIN.CURRENT.selectedIndex = g_contra_loop;
		move_run(g_contras[g_contra_selected].c_moves[g_contra_loop]);
		g_contra_loop += 1;
	}
	else if (g_contra_loop == g_contras[g_contra_selected].c_move_count)
	{
		mix1 = ((g_contras[g_contra_selected].c_move_count > 1) ? g_contras[g_contra_selected].c_moves[1] : -1);
		mix2 = ((g_contras[g_contra_selected].c_move_count > 2) ? g_contras[g_contra_selected].c_moves[2] : -1);
		mix3 = ((g_contras[g_contra_selected].c_move_count > 3) ? g_contras[g_contra_selected].c_moves[3] : -1);
		if ((mix1 != -1) && (mix1 != g_move_activesxo) && (mix1 != g_move_becketleft) && (mix1 != g_move_becketright))
		{
			g_contra_loop = 1;
			document.MAIN.CURRENT.selectedIndex = g_contra_loop;
			g_contra_beats = 0;
			info(4,"current beats = 0");
			move_run(g_contras[g_contra_selected].c_moves[g_contra_loop]);
			g_contra_loop += 1;
		}
		else if ((mix2 != -1) && (mix2 != g_move_activesxo) && (mix2 != g_move_becketleft) && (mix2 != g_move_becketright))
		{
			g_contra_loop = 2;
			document.MAIN.CURRENT.selectedIndex = g_contra_loop;
			g_contra_beats = 0;
			info(4,"current beats = 0");
			move_run(g_contras[g_contra_selected].c_moves[g_contra_loop]);
			g_contra_loop += 1;
		}
		else if (mix3 != -1)
		{
			g_contra_loop = 3;
			document.MAIN.CURRENT.selectedIndex = g_contra_loop;
			g_contra_beats = 0;
			info(4,"current beats = 0");
			move_run(g_contras[g_contra_selected].c_moves[g_contra_loop]);
			g_contra_loop += 1;
		}
		else
		{
			g_contra_loop = -1;
			document.MAIN.CURRENT.selectedIndex = -1;
			stop_all();
			move_randomize();
		}
	}
	else
	{
		stop_all();
	}
}

////////////////////////////////////////////////////////////////////////////////

function screen_moves()
{
	var ix = 0;

	ix = (document.MAIN.MOVES.selectedIndex + 1);
	trace("screen_moves ix = " + ix);

	stop_all();

	g_move_selected = ix;
	if ((g_move_selected < 0) || (g_move_selected >= g_move_count))
	{
		trace("ERROR: invalid move ix");
		return;
	}
	document.MAIN.MOVES.selectedIndex = (g_move_selected - 1);
	move_info();
}

////////////////////////////////////////////////////////////////////////////////

function screen_next()
{
	var mix1 = -1;
	var mix2 = -1;
	var mix3 = -1;

	if ((g_contra_selected < 0) || (g_contra_selected >= g_contra_count))
	{
		trace("ERROR: invalid contra selected");
		return;
	}

	if (g_contra_next == -1)
	{
		stop_all();
		g_contra_next = g_move_lineup;
		document.MAIN.CURRENT.selectedIndex = g_contra_next;
		info(4,"current beats = 0");
		move_run(g_contra_next);
		g_contra_next += 1;
	}
	else if ((g_contra_next > 0) && (g_contra_next < g_contras[g_contra_selected].c_move_count))
	{
		document.MAIN.CURRENT.selectedIndex = g_contra_next;
		move_run(g_contras[g_contra_selected].c_moves[g_contra_next]);
		g_contra_next += 1;
	}
	else if (g_contra_next == g_contras[g_contra_selected].c_move_count)
	{
		mix1 = ((g_contras[g_contra_selected].c_move_count > 1) ? g_contras[g_contra_selected].c_moves[1] : -1);
		mix2 = ((g_contras[g_contra_selected].c_move_count > 2) ? g_contras[g_contra_selected].c_moves[2] : -1);
		mix3 = ((g_contras[g_contra_selected].c_move_count > 3) ? g_contras[g_contra_selected].c_moves[3] : -1);
		if ((mix1 != -1) && (mix1 != g_move_activesxo) && (mix1 != g_move_becketleft) && (mix1 != g_move_becketright))
		{
			g_contra_next = 1;
			document.MAIN.CURRENT.selectedIndex = g_contra_next;
			g_contra_beats = 0;
			info(4,"current beats = 0");
			move_run(g_contras[g_contra_selected].c_moves[g_contra_next]);
			g_contra_next += 1;
		}
		else if ((mix2 != -1) && (mix2 != g_move_activesxo) && (mix2 != g_move_becketleft) && (mix2 != g_move_becketright))
		{
			g_contra_next = 2;
			document.MAIN.CURRENT.selectedIndex = g_contra_next;
			g_contra_beats = 0;
			info(4,"current beats = 0");
			move_run(g_contras[g_contra_selected].c_moves[g_contra_next]);
			g_contra_next += 1;
		}
		else if (mix3 != -1)
		{
			g_contra_next = 3;
			document.MAIN.CURRENT.selectedIndex = g_contra_next;
			g_contra_beats = 0;
			info(4,"current beats = 0");
			move_run(g_contras[g_contra_selected].c_moves[g_contra_next]);
			g_contra_next += 1;
		}
		else
		{
			g_contra_next = -1;
			document.MAIN.CURRENT.selectedIndex = -1;
			stop_all();
			move_randomize();
		}
	}
	else
	{
		stop_all();
	}
}

////////////////////////////////////////////////////////////////////////////////

function screen_slow()
{
	stop_all();

	g_move_delay += g_move_increment;
	if ((g_move_delay < 50) || (g_move_delay > 3000))
	{
		g_move_delay = 500;
		trace("ERROR: invalid move delay");
	}
	info(4,"move speed = " + g_move_delay);
}

////////////////////////////////////////////////////////////////////////////////

function screen_stop()
{
	stop_all();
}

////////////////////////////////////////////////////////////////////////////////

function screen_undo()
{
	var i = 0;
	var opts = null;

	stop_all();
	if ((g_contra_selected < 0) || (g_contra_selected >= g_contra_count))
	{
		trace("ERROR: invalid contra selected");
		return;
	}
	opts = document.MAIN.CURRENT.options;
	if (opts.length <= 1)
	{
		trace("ERROR: can't undo move '" + g_moves[g_move_lineup].m_name + "'");
		return;
	}
	contra_undoadd(g_contra_selected);
	opts.length -= 1;
	contra_info();
}

////////////////////////////////////////////////////////////////////////////////

function screen_initialize()
{
	trace("screen_initialize begin");

	var i = 0;
	var opts = null;
	var label = null;

	opts = document.MAIN.INFO.options;
	opts.length = 0;
	document.MAIN.CURRENT.selectedIndex = -1;
	label = "CDD version 2.4";
	opts[opts.length] = new Option(label,label,false,false);
	opts[opts.length] = new Option("","",false,false);
	opts[opts.length] = new Option("","",false,false);
	opts[opts.length] = new Option("","",false,false);

	opts = document.MAIN.MOVES.options;
	opts.length = 0;
	document.MAIN.MOVES.selectedIndex = -1;
	for (i=1; i<g_move_count; i+=1)
	{
		label = "(" + i + ") " + g_moves[i].m_name
		opts[opts.length] = new Option(label,label,false,false);
	}

	opts = document.MAIN.CONTRAS.options;
	opts.length = 0;
	label = "---- select contra ----";
	opts[opts.length] = new Option(label,label,false,false);
	document.MAIN.CONTRAS.selectedIndex = 0;
	for (i=0; i<g_contra_count; i+=1)
	{
		label = "(" + i + ") " + g_contras[i].c_name;
		opts[opts.length] = new Option(label,label,false,false);
	}

	opts = document.MAIN.CURRENT.options;
	opts.length = 0;
	document.MAIN.CURRENT.selectedIndex = -1;

	trace("screen_initialize end");
}

////////////////////////////////////////////////////////////////////////////////
// CONTRA DANCE DESIGNER - graphical display of contra dances, version 2.4
// 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.4 ONLY SUPPORTS MS/IE4.0+ FOR WINDOWS, MS/IE5.0+ FOR APPLES
////////////////////////////////////////////////////////////////////////////////
