
// Generate the query string part that can be appended
// to a movie play URL.
//
// Options in "opts" can include:
//
//     assignment_id -> maps to param "assignment_id"
//     recommend_id  -> maps to param "recommend_id"
//     series_type   -> maps to param "type"
//     series_id     -> maps to param "sid"
//
function movieplayerPopupLinkParams (opts)
{
    var params = [];

    if (typeof opts == 'undefined') return '';

    if (opts.assignment_id) {
        params.push('assignment_id=' + escape(opts.assignment_id));
    }
    if (opts.recommend_id) {
        params.push('recommend_id=' + escape(opts.recommend_id));
    }
    if (opts.series_type) {
        params.push('type=' + escape(opts.series_type));
    }
    if (opts.series_id) {
        params.push('sid=' + escape(opts.series_id));
    }
    if (opts.partner) {
        params.push('partner=' + escape(opts.partner));
    }

    if (params.length) {
        return '?' + params.join('&');
    }
    else {
        return '';
    }
}

// Options in "opts" can include:
//
//     assignment_id
//     recommend_id
//     series_type -> maps to param "type"
//     series_id   -> maps to param "sid"
//
function movieplayerPopupLink (title, movie_id, opts)
{
    var urlOpts = {};
    var fbOpts  = {};

    if (typeof opts != 'undefined') {
        for (var item in opts) {
            if (   item == 'assignment_id'
                || item == 'recommend_id'
                || item == 'series_type'
                || item == 'series_id') {
                urlOpts[item] = opts[item];
            }
            else {
                fbOpts[item] = opts[item];
            }
        }
    }

    urlOpts = Ext.util.JSON.encode(urlOpts);
    urlOpts = urlOpts.replace(/'/g, "\\'");
    urlOpts = urlOpts.replace(/"/g, "'");

    fbOpts = Ext.util.JSON.encode(fbOpts);
    fbOpts = fbOpts.replace(/'/g, "\\'");
    fbOpts = fbOpts.replace(/"/g, "'");

    return '<a href=""'
         + ' onclick="al_movie_popup(' + movie_id + ','
         +                               urlOpts  + ','
         +                               fbOpts   + '); return false"'
         + ' oncontextmenu="return false">' + title + '</a>';
}

function getMoviePlayerFloatboxOptions (opts)
{
    // If you are *not* playing a movie, you should be
    // calling "getGenericFloatboxOptions" instead!

    if (typeof opts == 'undefined') {
        opts = {};
    }

    if (typeof opts.width == 'undefined') {
        opts.width = 954;
    }
    if (typeof opts.height == 'undefined') {
        opts.height = 664;
    }

    if (typeof opts.showCaption == 'undefined') {
        opts.showCaption = true;
    }
    if (typeof opts.caption == 'undefined' && opts.showCaption == true) {
        var now = new Date();
        var year = now.getFullYear();
        opts.caption = '`This tutorial &copy; Copyright ' + year
                     + ' Atomic&nbsp;Learning, Inc. &nbsp; All rights reserved.`';
    }

    return getGenericFloatboxOptions(opts);
}

function movieplayerLoadMovie (movie_id, opts)
{
    var spinner = ALBase.make_static_uri('/js/floatbox/graphics/loader_white.gif');

    movieplayerLoadFloatbox('<div id="movieplayer_message">'
                          + '<img src="' + spinner + '" alt="Loading" />'
                          + '<br />'
                          + 'Loading...'
                          + '</div>');

    var url = ALBase.make_uri('/movie/' + movie_id + '/play_div');

    url += movieplayerPopupLinkParams(opts);

    var errMsg = '<div id="movieplayer_message">'
               + '<p class="error_message">'
               + 'There was a problem connecting to the server - please'
               + ' <a href="javascript:movieplayerLoadMovie(' + movie_id + ',' + $(opts).toJSON + ')">'
               + 'click here</a> to try again.'
               + '</p>'
               + '<p class="error_message">'
               + 'If this problem persists, please contact Atomic Learning'
               + ' Customer Service<br />'
               + ' at 1-320-631-5900 ext 8 or'
               + ' <a href="mailto:cs@atomiclearning.com">'
               + 'cs@atomiclearning.com</a>.'
               + '</p>'
               + '</div>';

    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function (transport) {
            movieplayerLoadFloatbox(transport.responseText);
        },
        onFailure: function (transport) {
            movieplayerLoadFloatbox(errMsg);
        }
    });
}

function movieplayerLoadFloatbox (html)
{
    $('movie_container').update(html);
    return;
}

//----------------------------------------------------------

// New onclick method for playing movies in a popup window:
//
function al_movie_popup (movie_id, urlOpts, fbOpts, noPlaylist)
{
    var url_action = noPlaylist ? '/popup' : '/play';

    var url  = ALBase.make_uri('/movie/' + movie_id + url_action);
    url += movieplayerPopupLinkParams(urlOpts);

    var fbOptsString = getMoviePlayerFloatboxOptions(fbOpts);
    fbOptsString = decode_html_entities(fbOptsString);    // for caption

    fb.start({href:url, rev:fbOptsString});

    return false;   // for onclick
}

// OLD onclick method for playing movies in a popup window.
//
// It is here for Eastwood just so that all the existing
// 10% pages don't need to be edited.  Going forward,
// all movie popup links should use:
//
//     al_movie_popup(movie_id, urlOpts)
//
// instead.
//

function al_play_movie (site_not_used, movie_id)
{
    return al_movie_popup(movie_id, {}, {}, 'noPlaylist');
}

//----------------------------------------------------------

// This is called from the player window:

function playercontrols (toggleDiv, isQuicktime)
{
    var el;
    if (toggleDiv != 'movie_playlist') {
        el = $('movie_playlist');
        if (el) el.hide();
    }
    if (toggleDiv != 'movie_share') {
        el = $('movie_share');
        if (el) el.hide();
    }
    if (toggleDiv != 'movie_downloads') {
        el = $('movie_downloads');
        if (el) el.hide();
    }

    el = $(toggleDiv);
    if (el) {
        if (el.visible()) {
            el.hide();
            if (isQuicktime) startQuicktime();
        }
        else {
            if (isQuicktime) stopQuicktime();
            el.show();
        }
    }
    else {
        if (isQuicktime) startQuicktime();
    }
}

function stopQuicktime () {
    if (document.my_quicktime_movie) document.my_quicktime_movie.Stop();
    var qt_el = $('qt');
    if (qt_el) qt_el.setStyle({visibility: 'hidden'});
}

function startQuicktime () {
    var qt_el = $('qt');
    if (qt_el) qt_el.setStyle({visibility: 'visible'});
    if (document.my_quicktime_movie) document.my_quicktime_movie.Play();
}


