var uscc = uscc || {};
uscc.sng = uscc.sng || {};
uscc.sng.socnet = uscc.sng.socnet || {};

/**
 * @class SimpleAuthentication
 *
 * SocialNetworkSettings : Allows a user to authenticate and deactivate their account through SNG
 *
 *      var simpleAuth = new uscc.sng.socnet.SimpleAuthentication(containerSelector, options);
 *
 *      options:
 *          {...
 *
 *          containerSelector: <selector to div element containing form element>,
 *          options : {...
 *
 *                  ...
 *          ...}
 *
 *
 * @events none
 * @depends JQYHtmlContainer
 * @extends JQYHtmlContainer
 * @events onAuthenticated.SimpleAuthentication
*/

uscc.sng.socnet.SimpleAuthentication = (function(containerSelector, options){
     var _options = $.extend({}, {}, options),
         _buttons = {},
         _containerSelector = containerSelector,
         _htmlContainer = new newbay.util.JQYHtmlContainer ("#SimpleSNAuth", {});

    _buttons["Login"] = function() {
        _authenticateUser();
    };

    _buttons["Cancel"] = function() {
        _self.close ();
    };

    // init parent dialog instance
    var _super = new newbay.widget.Dialog ("SimpleSNAuth", _htmlContainer, {
    		buttons: _buttons,
    		title: "Authenticate Network",
    		width:400,
            height: 280,
            hide: null,
            position: "center"
        }
    );

    // extend dialog instance
    var _self = $.extend({}, _super, {});

    var _authenticateUser = function(){

        var $containerSelector = $(_containerSelector);
        
        var userAuthInfo = {
            username    :   $containerSelector.find("#snusername").val(),
            password    :   $containerSelector.find("#snpassword").val()
        }

        SngAPI.authenticateNetwork(
                        userAuthInfo,
                        {
                            "sns.uid" : options.id
                        },
                        {
                            onSuccess: function(){
                                _self.close();
                                _self.trigger("onAuthenticated.SimpleAuthentication", {response : "success"});
                            },
                            onFailure : function(){
                                _self.close();
                                _self.trigger("onAuthenticated.SimpleAuthentication", {response : "failure"});
                            }

                        },
                        {});
    };

    var _disableAccount = function(id){
        SngAPI.deactivateAccount({},{"sns.uid" : id},{},{})
    };
    
    //laod the template and render
    var _init = function () {

        _self.addSupportedEvents (["onAuthenticated.SimpleAuthentication"]);
        
        _htmlContainer.setRenderOptions ({
            renderFromTemplate: {templateId : "tmpl_SimpleSNAuth"},
            renderOnce: true
        });
    };

    _init ();

    return _self;
});
