
function dlink_check_section_movie_ids (el)
{
    var value = (el.checked == true) ? true : false;

    var css_locator = '#dlink_form #movies_for_' + el.id
                    + ' .movie_checkbox';

    $$(css_locator).each( function (el) {
            el.checked = value;
        }
    );
}

function dlink_check_all ()
{
    dlink_set_value_for_all(true);
}

function dlink_uncheck_all ()
{
    dlink_set_value_for_all(false);
}

function dlink_set_value_for_all (value)
{
    var css_locator = '#dlink_form .movie_checkbox';

    $$(css_locator).each( function (el) {
            el.checked = value;
        }
    );

    css_locator = '#dlink_form .section_checkbox';

    $$(css_locator).each( function (el) {
            el.checked = value;
        }
    );
}

function dlink_preview (el)
{
    var series_title = dlink_data_series_title;
    var series_url   = dlink_data_series_url;

    var series_link = '<a href="' + series_url + '" target="_blank">' + series_title + '</a>';

    var dlink_html = '<style type="text/css">\r\n'
                   + '   div.AL_direct_links ul {\r\n'
                   + '      list-style: none !important;\r\n'
                   + '   }\r\n'
                   + '   div.AL_direct_links ul.AL_movies {\r\n'
                   + '      list-style: disc !important;\r\n'
                   + '   }\r\n'
                   + '</style>\r\n'
                   + '<div class="AL_direct_links">\r\n'
                   + '   <ul class="AL_series">\r\n'
                   + '      <li class="AL_series_name">' + series_link + '</li>\r\n';

    var any_checked = false;

    var css_section_locator = '#dlink_form .section_checkbox';

    $$(css_section_locator).each( function (section) {

            var section_id = section.id;
            var css_movie_locator = '#dlink_form'
                                  + ' #movies_for_' + section_id
                                  + ' .movie_checkbox';

            section_id = section_id.replace(/^section_/, '');

            var section_name = dlink_data_section_names[section_id];

            var section_html = '      <li>\r\n'
                             + '         <ul class="AL_section">\r\n'
                             + '            <li class="AL_section_name">' + section_name + '</li>\r\n'
                             + '            <li>\r\n'
                             + '               <ul class="AL_movies">\r\n';

            var any_checked_for_section = false;

            $$(css_movie_locator).each( function (movie) {
                    if (movie.checked) {
                        any_checked_for_section = true;

                        var movie_id = movie.id;
                        movie_id = movie_id.replace(/^movie_/, '');

                        var movie_title = dlink_data_movie_titles[movie_id];

                        var movie_url = dlink_data_movie_url_prefix
                                      + '?key=' + movie_id;

                        var movie_link = '<a href="' + movie_url + '" target="_blank">' + movie_title + '</a>';

                        section_html += '                  <li class="AL_movie">' + movie_link + '</li>\r\n';
                    }
                }
            );

            section_html += '               </ul>\r\n'
                          + '            </li>\r\n'
                          + '         </ul>\r\n'
                          + '      </li>\r\n';

            if (any_checked_for_section) {
                any_checked = true;
                dlink_html += section_html;
            }
        }
    );

    dlink_html += '   </ul>\r\n'
                + '</div>\r\n';

    if (any_checked) {
        $('dlink_final_html').value = dlink_html;    // this is a textarea

        var dlink_preview_html = '<style type="text/css">\r\n'
                               + ' div.AL_direct_links * {\r\n'
                               + '  font-family: serif !important;\r\n'
                               + '  font-size: 14px !important;\r\n'
                               + '  line-height: 1.5 !important;\r\n'
                               + '  color: #000000 !important;\r\n'
                               + ' }\r\n'
                               + ' div.AL_direct_links ul {\r\n'
                               + '  margin: 1em !important;\r\n'
                               + '  margin-left: 2em !important;\r\n'
                               + ' }\r\n'
                               + ' div.AL_direct_links li li li {\r\n'
                               + '  list-style: disc outside !important;\r\n'
                               + ' }\r\n'
                               + ' div.AL_direct_links a {\r\n'
                               + '  color: #0000ff !important;\r\n'
                               + '  text-decoration: underline !important;\r\n'
                               + '  font-weight: normal !important;\r\n'
                               + ' }\r\n'
                               + ' div.AL_direct_links a * {\r\n'
                               + '  color: #0000ff !important;\r\n'
                               + ' }\r\n'
                               + '</style>\r\n'
                               + dlink_html;
        $('dlink_preview_html').innerHTML = dlink_preview_html;

        $('direct_linking_start').hide();
        $('direct_linking_preview').show();
        $('direct_linking_final').hide();
    }
    else {
        alert('No tutorials are checked.');
    }
}

function dlink_show_initial ()
{
        $('direct_linking_start').show();
        $('direct_linking_preview').hide();
        $('direct_linking_final').hide();
}

function dlink_show_preview ()
{
        $('direct_linking_start').hide();
        $('direct_linking_preview').show();
        $('direct_linking_final').hide();
}

function dlink_generate ()
{
        $('direct_linking_start').hide();
        $('direct_linking_preview').hide();
        $('direct_linking_final').show();

        document.dlink_final_form.dlink_final_html.focus();
        document.dlink_final_form.dlink_final_html.select();
}

