/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

(function($) {
    'use strict';

    // If there are problems initialising the browserID javascript
    // don't try to setup the watchers
    try{
        navigator.id.watch;
    }
    catch(error)
    {
        console.debug('navigator.id not available. Login with Persona not possible.');
        return;
    }
    
    $(function() {
        var requestOptions = [
            'siteName',
            'siteLogo',
            'oncancel',
            'privacyPolicy',
            'returnTo',
            'termsOfService'
        ];

        $(document).delegate('.browserid-login, #browserid', 'click', function(e) {
            e.preventDefault();

            // Arguments to navigator.id.request can be specified by data-attributes
            // on the BrowserID link: <a href="#" data-site-name="Site Name">
            var options = {};
            var $link = $(e.target);
            for (var k = 0; k < requestOptions.length; k++) {
                var name = requestOptions[k];
                var value = $link.data(name);
                if (value !== undefined) {
                    options[name] = value;
                }
            }

            navigator.id.request(options); // Triggers BrowserID login dialog.
        });

        $('.browserid-logout').bind('click', function(e) {
            // Clears User Agent BrowserID state.
            navigator.id.logout();
        });

        navigator.id.watch({
            
            onlogin: function(assertion) {
                if (assertion) {
                    var $e = $('#id_assertion');
                    $e.val(assertion.toString());
                    $e.parent().submit();
                }
            },

            onlogout: function() {
                // No functionality here
                //console.debug('navigator.id.watch onLogout'); 
            }
        });
    });
})(jQuery);
