// (C) Two Corner Timing LLC
// $Id: two201007.js 3871 2010-07-20 05:36:29Z gritton $

var ie;

// preload
// Tasks to be run before the final page load

var perf_dates, perf_from, perf_to, perf_var, perf_varmix, perf_vs, perf_vsmix,
    perf_anyto;
var sa_mix, sa_mixcb, sa_mix_name, sa_mix_vars, sa_mix_blend, sa_mix_allin,
    sa_prefix;

function preload()
{
    // Because the worst browser has the best market share
    ie = /*@cc_on!@*/false;

    if (perf_dates = document.getElementById("perf_dates"))
    {
	// Get the necessary elements for the Performance page
	perf_from = document.getElementById("perf_from");
	perf_to = document.getElementById("perf_to");
	perf_var = document.getElementById("perf_var");
	perf_vs = document.getElementById("perf_vs");
	perf_varmix = document.getElementById("perf_varmix");
	perf_vsmix = document.getElementById("perf_vsmix");
	perf_anyto = document.getElementById("anyto").value;

	perf_from.onchange = perf_from.onkeypress = perf_to.onchange =
	    perf_to.onkeypress = perf_custom_dates;
	perf_dates.onchange = perf_canned_dates;
	perf_var.onchange = perf_select_var;
	perf_vs.onchange = perf_select_vs;
	perf_canned_dates();

	// Add the "mix" option to signal menus
	var lastopt = perf_var.lastChild;
	if (lastopt.value == "mix" ||
	    lastopt.previousSibling.value == "mix")
	    perf_select_var();
	else
	{
	    perf_var.appendChild(document.createElement("option"));
	    lastopt = document.createElement("option");
	    lastopt.value = "mix";
	    lastopt.appendChild(document.createTextNode("Mix..."));
	    perf_var.appendChild(lastopt);
	}
	lastopt = perf_vs.lastChild;
	if (lastopt.value == "mix" ||
	    lastopt.previousSibling.value == "mix")
	    perf_select_vs();
	else
	{
	    perf_vs.appendChild(document.createElement("option"));
	    lastopt = document.createElement("option");
	    lastopt.value = "mix";
	    lastopt.appendChild(document.createTextNode("Mix..."));
	    perf_vs.appendChild(lastopt);
	}
    }

    sa_mix = document.getElementById("sub_mix");
    if (!sa_mix)
	sa_mix = document.getElementById("edit_mix");
    if (sa_mix)
    {
	// Get necessary elements for the Subscribe and My Account pages page
	sa_prefix = sa_mix.id.match("[^_]*")[0];
	sa_mix_vars = new Array();
	var tbody = sa_mix.firstChild;
	var tr;
	for (tr = tbody.firstChild ? tbody.firstChild
	     : tbody.nextSibling.firstChild; tr; tr = tr.nextSibling)
	{
	    var td;
	    for (td = tr.firstChild; td; td = td.nextSibling)
	    {
		var lt;
		for (lt = td.firstChild; lt; lt = lt.nextSibling)
		{
		    if (lt.name == sa_prefix + "mixname")
		    {
			sa_mix_name = lt;
			lt.onkeypress = lt.onkeyup = lt.onchange =
			    sa_relay_mix_name;
			continue;
		    }
		    var el = lt.firstChild;
		    if (!el)
			continue;
		    if (el.nodeName != "INPUT")
			el = el.nextSibling;
		    if (el.name != sa_prefix + "mixA")
			sa_mix_vars.push(el);
		    else if (el.value)
			sa_mix_allin = el;
		    else
			sa_mix_blend = el;
		    if (ie)
			el.onpropertychange = sa_relay_mix;
		    else
			el.onchange = sa_relay_mix;
		}
	    }
	}
	// Add the "mix" checkbox to subscribe/account
	var br = document.createElement("br");
	br.className = "bigbr";
	var label = document.createElement("label");
	label.htmlFor = sa_prefix + "_mixcb";
	sa_mixcb = document.createElement("input");
	sa_mixcb.type = "checkbox";
	sa_mixcb.id = label.htmlFor;
	sa_mixcb.className = "checkbox";
	sa_mixcb.name = sa_prefix + "varmix";
	label.appendChild(sa_mixcb);
	var span = document.createElement("span");
	span.appendChild(document.createTextNode(" Mix..."));
	label.appendChild(span);
	sa_mix.parentNode.appendChild(br);
	sa_mix.parentNode.appendChild(label);

	for (; label; label = label.previousSibling)
	{
	    var el = label.firstChild;
	    if (!el)
		continue;
	    if (el.nodeName != "INPUT")
		el = el.nextSibling;
	    if (el.nodeName == "INPUT" && el.name.search('var..') >= 0)
	    {
		if (ie)
		    el.onpropertychange = sa_pop_mix;
		else
		    el.onchange = sa_pop_mix;
	    }
	}
    }

    var ce_height;
    var login = document.getElementById("login");
    if (login)
    {
	// Align the right edges of the login form.
	// Center the login form within the top bar.
	var login_name = document.getElementById("login_name");
	var login_password = document.getElementById("login_password");
	var login_button = document.getElementById("login_button");
	var right_delta = (login_name.offsetLeft + login_name.clientWidth) -
	    (login_button.offsetLeft + login_button.clientWidth);
	if (right_delta < 0)
	    login_name.style.width =
		(login_name.clientWidth - right_delta) + 'px';
	else if (right_delta > 0)
	    login_password.style.width =
		(login_password.clientWidth + right_delta) + 'px';
	login.style.marginTop = login.offsetHeight >= 150 ? 0
	    : (150 - login.offsetHeight) / 2 + 'px';
	ce_height = login.clientHeight;
    }
    else
    {
	// Center the quick signal box within the top bar.
	var signal_summaries = document.getElementById("signal_summaries");
	signal_summaries.style.marginTop =
	    signal_summaries.offsetHeight >= 150 ? 0
	    : (150 - signal_summaries.offsetHeight) / 2 + 'px';
	ce_height = signal_summaries.clientHeight;
    }

    // Push the sidebar down if necessary
    var sidebar = document.getElementById("sidebar");
    if (sidebar.offsetTop < ce_height)
	sidebar.style.top = (ce_height + 25) + 'px';
}


// perf_custom_dates, perf_canned_dates, perf_canned_vardate
// Gray out and auto-change the performance date fields whan a canned date
// range is chosen

function perf_custom_dates(e)
{
    if (ie || e.type != "keypress" || e.which >= 32 || e.which == 8 ||
	e.keyCode == 46)
    {
	perf_dates.value = "custom";
	perf_from.style.color = "black";
	perf_to.style.color = "black";
    }
}

function perf_canned_dates()
{
    if (perf_dates.value == "custom")
    {
	perf_from.style.color = "black";
	perf_to.style.color = "black";
    }
    else
    {
	var from = perf_dates.value == "live"
	    ? document.getElementById("live" + perf_var.value + "from")
	    : document.getElementById(perf_dates.value + "from");
	perf_from.value = from ? from.value : '';
	perf_from.style.color = "#689090";
	perf_to.value = perf_anyto;
	perf_to.style.color = "#689090";
    }
}

function perf_canned_vardate()
{
    if (perf_dates.value == "live")
    {
	var from = document.getElementById("live" + perf_var.value + "from");
	perf_from.value = from ? from.value : '';
    }
}


// perf_select_var, perf_select_vs
// Pop up or down the varmix/vsmix tables

function perf_select_var()
{
    perf_canned_vardate();
    perf_pop_mix(perf_var, perf_varmix);
}

function perf_select_vs()
{
    perf_pop_mix(perf_vs, perf_vsmix);
}

var vert_done;

function perf_pop_mix(v, mix)
{
    if (v.value != "mix")
	mix.style.visibility = "hidden";
    else
    {
	var td = mix.parentNode;
	if (!td.style.width)
	    td.style.width = mix.clientWidth + 'px';
	if (!vert_done)
	{
	    vert_done = 1;
	    var tbody = td.parentNode.parentNode;
	    var tr;
	    for (tr = tbody.firstChild; tr; tr = tr.nextSibling)
		if (tr.style)
		    tr.saved_height = tr.clientHeight;
	    tr = document.createElement("tr");
	    tr.appendChild(document.createElement("td"));
	    tbody.appendChild(tr);
	    tbody.style.height = mix.clientHeight + 'px';
	    for (tr = tbody.firstChild; tr; tr = tr.nextSibling)
		if (tr.saved_height)
		    tr.style.height = tr.saved_height + 'px';
	}
	mix.style.visibility = "visible";
    }
}


// sa_pop_mix
// Pop up or down the mix table and associate it with a checkbox

var sa_assoc_cb, sa_assoc_text, sa_defname, sa_in_callback;

function sa_pop_mix(e)
{
    if (ie)
	e = window.event;
    var cb = e.target ? e.target : e.srcElement;

    if (ie)
    {
	if (sa_in_callback)
	    return;
	sa_in_callback = true;
	if (!cb.tct_initial)
	{
	    cb.tct_initial = true;
	    sa_in_callback = false;
	    return;
	}
    }

    if (cb.checked || cb != sa_assoc_cb && cb != sa_mixcb)
    {
	if (sa_assoc_text)
	    sa_assoc_text.style.fontWeight = "normal";
	if (sa_assoc_cb == sa_mixcb)
	    sa_assoc_cb.checked = false;
	sa_assoc_text = cb.nextSibling;
	if (!sa_assoc_text.firstChild)
	    sa_assoc_text = sa_assoc_text.nextSibling;
	sa_assoc_text.style.fontWeight = "bold";
	sa_assoc_cb = cb;

	var vt = cb == sa_mixcb ? '' : cb.name.match('var.*')[0].substr(3);
	sa_defname = cb.value.search('^".*"$') < 0;
	sa_mix_name.value =
	    cb == sa_mixcb ? '' : cb.value.replace(/^"|"$/g, '');
	var i;
	for (i = 0; i < sa_mix_vars.length; i++)
	    sa_mix_vars[i].checked = 0 <=
		vt.search(sa_mix_vars[i].name.substr(sa_prefix.length + 3));
	if (vt.search('A') >= 0)
	    sa_mix_allin.checked = true;
	else
	    sa_mix_blend.checked = true;
	if (!vert_done)
	{
	    vert_done = 1;
	    if (sa_mix.parentNode.clientHeight < sa_mix.clientHeight)
		sa_mix.parentNode.style.height = sa_mix.clientHeight + 'px';
	}
	sa_mix.style.visibility = "visible";
	cb.checked = true;
    }
    else
    {
	sa_assoc_text.style.fontWeight = "normal";
	sa_assoc_cb = null;
	sa_assoc_text = null;
	sa_mix.style.visibility = "hidden";
    }
    if (ie)
	sa_in_callback = false;
}


// sa_relay_mix, sa_relay_mix_name
// Keep a mix checkbox in sync with the mix table

var sa_cbnum = 0;

function sa_relay_mix(e)
{
    if (ie)
	e = window.event;
    var cb = e.target ? e.target : e.srcElement;

    if (ie)
    {
	if (sa_in_callback)
	    return;
	sa_in_callback = true;
	if (!cb.tct_initial)
	{
	    cb.tct_initial = true;
	    sa_in_callback = false;
	    return;
	}
    }

    // Build a tag representing the mix table
    var vt = sa_mix_allin.checked ? 'A' : '';
    var i;
    for (i = 0; i < sa_mix_vars.length; i++)
	if (sa_mix_vars[i].checked && sa_mix_vars[i].value)
	    vt += sa_mix_vars[i].name.substr(sa_prefix.length + 3);

    // Update a default name
    var name;
    if (vt != "zwyph")
    {
	if (sa_mix_name.value == "Zero Five")
	    sa_defname = true;
    }
    else if (sa_defname)
    {
	sa_mix_name.value = "Zero Five";
	sa_defname = false;
    }
    if (sa_defname)
    {
	var sigs = vt.length - sa_mix_allin.checked;
	name = sigs + '-Signal ' + (sa_mix_allin.checked ? 'Combo' : 'Blend');
	sa_mix_name.value = sigs > 1 ? name : '';
    }
    else
	name = '"' + sa_mix_name.value + '"';

    // If there's no associated checkbox, create a new one
    if (sa_assoc_cb == sa_mixcb && vt.search('[a-z]{2}$') >= 0)
    {
	var label = document.createElement("label");
	label.htmlFor = sa_prefix + "_cb" + ++sa_cbnum;
	sa_assoc_cb = document.createElement("input");
	sa_assoc_cb.type = "checkbox";
	sa_assoc_cb.id = label.htmlFor;
	sa_assoc_cb.className = "checkbox";
	sa_assoc_cb.checked = true;
	if (ie)
	{
	    sa_assoc_cb.tct_initial = true;
	    sa_assoc_cb.onpropertychange = sa_pop_mix
	}
	else
	    sa_assoc_cb.onchange = sa_pop_mix;
	label.appendChild(sa_assoc_cb);
	sa_assoc_text.style.fontWeight = "normal";
	sa_assoc_text = document.createElement("span");
	sa_assoc_text.style.fontWeight = "bold";
	sa_assoc_text.appendChild(document.createTextNode(''));
	label.appendChild(sa_assoc_text);
	sa_mixcb.parentNode.parentNode.insertBefore(
					document.createElement("br"),
					sa_mixcb.parentNode.previousSibling);
	sa_mixcb.parentNode.parentNode.insertBefore(label,
					sa_mixcb.parentNode.previousSibling);
	sa_mixcb.checked = false;
    }

    // Update the associated checkbox
    if (sa_assoc_cb != sa_mixcb)
    {
	sa_assoc_cb.name = sa_prefix + "var" + vt;
	sa_assoc_cb.value = name;
	sa_assoc_text.firstChild.nodeValue = ' ' + name;
    }
    if (ie)
	sa_in_callback = false;
}

function sa_relay_mix_name(e)
{
    if (ie)
    {
	e = window.event;
	if (!e.srcElement.tct_initial)
	{
	    e.srcElement.tct_initial = true;
	    return;
	}
    }

    if (ie || e.type != "keypress" || e.which >= 32 || e.which == 8 ||
	e.keyCode == 46)
    {
	if (e.type != "keypress" && e.type != "keyup" ||
	    sa_mix_name.value != '')
	    sa_defname = sa_mix_name.value == '';
	if (sa_assoc_cb != sa_mixcb)
	    sa_relay_mix(e);
    }
}
