// ucid-login.js
// Plugin for Echo Auth application that changes the login/logout buttons into UCID (Capture) buttons
// Requires capture.js to be on the page; will throw a JS error otherwise.

(function($) {

    var plugin = Echo.createPlugin({
        "name": "UCIDLogin",
        "applications": ["Auth"],
        "init": function(plugin, application) {
            plugin.extendRenderer("Auth", "logout", plugin.logoutRenderer);
            plugin.extendRenderer("Auth", "login", plugin.loginRenderer);
        }
    });

    plugin.loginRenderer = function(element, dom) {
        var self = this;
        element.click(function() {
            var ucidBar = $('#ucid_bar');
            if (ucidBar.length > 0 && ucidBar.css('display') !== 'none') {
                $(document.body).trigger('ucidActivate'); // flash the UCID bar
            }
            else {
                $({}).enableCapture('signinLink').click(); // bring up "old" login flow
            }
        });
    };

    plugin.logoutRenderer = function(element, dom) {
		var self = this;
		$().ready(function(){
			element.click(function() {
				element.empty().append(self.label("loggingOut"));
			}).enableCapture('signoutLink', { 'returnUrl': capture_redirectURL });
		});
    };

})(jQuery);
