function hide_rst2pdf() {
    hideElement('rst2pdf');
    removeElementClass('trigger_rst2pdf_container', 'button-active');
}
function show_rst2pdf() {
    showElement('rst2pdf');
    addElementClass('trigger_rst2pdf_container', 'button-active');
}

function hide_rst2html() {
    hideElement('rst2html');
    removeElementClass('trigger_rst2html_container', 'button-active');
}
function show_rst2html() {
    showElement('rst2html');
    addElementClass('trigger_rst2html_container', 'button-active');
}

function hide_legend() {
    hideElement('legend');
    var trigger_legend = getElement('trigger_legend');
    if(trigger_legend){
        trigger_legend.src = '/static/images/buttons/legend-open.gif';
    }
}
function show_legend() {
    showElement('legend');
    var trigger_legend = getElement('trigger_legend');
    if(trigger_legend){
        trigger_legend.src = '/static/images/buttons/legend-closed.gif';
    }
}

function reveal_none() {
    hide_rst2pdf();
    hide_rst2html();
    hide_legend();
    legend_open = false;
}
function reveal_rst2pdf() {
    show_rst2pdf();
    hide_rst2html();
    hide_legend();
    legend_toggle_method = reveal_rst2pdf;
    legend_open = false;
}
function reveal_rst2html() {
    hide_rst2pdf();
    show_rst2html();
    hide_legend();
    legend_toggle_method = reveal_rst2html;
    legend_open = false;
}
function reveal_legend() {
    hide_rst2pdf();
    hide_rst2html();
    show_legend();
    legend_open = true;
}

var legend_toggle_method = reveal_none;
var legend_open = true;
function toggle_legend() {
    if(legend_open) legend_toggle_method();
    else reveal_legend();
}

function set_legend() {
    var legend = getElement('select_legend');
    legend = legend.options[legend.selectedIndex].value;
    map(hideElement, getElementsByTagAndClassName(null, 'legend-pane', 'legend'));
    showElement('legend-pane-' + legend);
}

function set_html_sample() {
    var style = getElement('select_html_style');
    var style = style.options[style.selectedIndex].value;
    html_style.set_style(html_styles[style]);
}

function set_pdf_sample() {
    var klass = getElement('select_pdf_class');
    var klass = klass.options[klass.selectedIndex].value;

    var style = getElement('select_pdf_style');
    var style = style.options[style.selectedIndex].value;

    pdf_style.set_style(pdf_shapes[klass + '-' + style]);
}

function get_token() {
    if (getElement('token')) {
        return getElement('token').value;
    }

    var query = {
        rst: getElement('rst').value
    };
    query = queryString(query);

    var d = doXHR('/1.0/rst2/token', {
        method: 'POST',
        sendContent: query
    });
    d.addCallback(
        function(result) {
            return result.responseText;
        } 
    );
    return d;
}

function download_html() {
    var d = maybeDeferred(get_token);
    d.addCallback(
        function(token) {
            var style = getElement('select_html_style').value;
            var f = getElement('altform');
            f.action = '/1.0/rst2/html?token=' + token + '&style=' + style;
            f.target = '_blank';
            f.submit();
        }
    );
    return d;
}

function download_pdf() {
    var d = maybeDeferred(get_token);
    d.addCallback(
        function(token) {
            var klass = getElement('select_pdf_class').value;
            var style = getElement('select_pdf_style').value;
            var f = getElement('altform');
            f.action = '/1.0/rst2/pdf?token=' + token + '&class=' + klass + '&style=' + style;
            if(browser == 'Internet Explorer') {
                /* arg, can't get IE to submit the form unless target is blank */
                f.target = '_blank';
            } else {
                f.target = '';
            }
            f.submit();
        }
    );
    return d;
}

function onLoad() {
    connect('trigger_rst2pdf', 'onclick', reveal_rst2pdf);
    connect('trigger_rst2html', 'onclick', reveal_rst2html);

    connect('select_html_style', 'onchange', set_html_sample);

    connect('select_pdf_class', 'onchange', set_pdf_sample);
    connect('select_pdf_style', 'onchange', set_pdf_sample);

    connect('download_html', 'onclick', download_html);
    connect('download_pdf', 'onclick', download_pdf);

    if (getElement('trigger_legend')) {
        connect('trigger_legend', 'onclick', toggle_legend);
        connect('select_legend', 'onchange', set_legend);
        set_legend();
    }
}
connect(window, 'onload', onLoad);
