/**
 * Email Module
 * @author Kevin McBriarty
 * Set and display email preferences *
 */
function EmailModule() {

    /** name of the module */
    this._name=EmailModule.NAME;

    this._cont = null;
    this._dlg  = null;
    this._emailDialog = null;
    this._prefCont = null;

    this._prefs =[];

    this._PREF_IMAGE_YES = "/static/images/icon_check.jpg";
    this._PREF_IMAGE_NO = "/static/images/icon_x.jpg";

    this.initialize = function()
    {
        this._cont = $("#emailMod");
        this._dlg = this._cont.find("#emDialog");
        this._emailDialog = new DialogControl("#emDialog");

        this._prefs["emNewsletter"] = {cont :$("#emNewsletter"), pref:false};
        this._prefs["emFriends"] = {cont :$("#emFriends"), pref:false};
        this._prefs["emInvites"]= {cont :$("#emInvites"), pref:false};
        this._prefs["emHsAlerts"]= {cont :$("#emHsAlerts"), pref:false};
        this._prefs["emComments"] = {cont :$("#emComments"), pref:false};
        this._prefs["emEmail"] = {cont :$("#emEmail"), email:null};


        this._cont.find(".formEdit a").bind("click",this,function(e){            
          if ($(this).hasClass("emailLink")){
              return
          }
          e.preventDefault();
          var mod = e.data;
          mod._prefCont = $(e.target).closest(".formElement");
          mod._showDialog();
        });

        this._dlg.find("#emDlgYesButton").bind("click", this, function(e) {
            e.preventDefault();
            var mod = e.data;
            mod._updatePref(true, mod._prefCont.attr("id"));
        });
        this._dlg.find("#emDlgNoButton").bind("click", this, function(e) {
            e.preventDefault();
            var mod = e.data;
            mod._updatePref(false,mod._prefCont.attr("id"));
        });

		if ( LoginModule.getSessionFacebookIsProxyEmail() == 'true' ) {
			$("#emEmail").hide();
		}
	};

	/** list of initial AJAX request that needs to be made when a module loads */
    this.getInitRequests = function() {
        return [
            { "methodName" : "profileService.getPrivateProfile" }
        ];
    };

    this._showDialog = function(){
           this._dlg.find("#emDialogText").html(this._prefCont.find(".formLabel .emLabel").text());
           this._dlg.find(".dialogue_top p.h1").html(this._prefCont.find(".formLabel strong").text());
           this._emailDialog.showDialog();
    }



    this._makePrefReq= function (){
         this._page.makeModuleAjaxCall(this,
            [{"methodName" : "profileService.setEmailPreferences",
                "emailNewsletter" :  this._prefs["emNewsletter"].pref,
                "emailFriendsRequests" : this._prefs["emFriends"].pref,
                "emailGameInvitations" :this._prefs["emInvites"].pref,
                "emailHighScoreAlerts" : this._prefs["emHsAlerts"].pref,
                "emailCommentAlerts" :   this._prefs["emComments"].pref},
             { "methodName" : "profileService.getPrivateProfile" }
            ]);
    }

    this._handleGetPrivateProfile= function(res){
        this._prefs["emNewsletter"].pref = res.subscribedToNewsletter;
        this._prefs["emFriends"].pref = res.emailFriendsRequests;
        this._prefs["emInvites"].pref = res.emailGameInvitations;
        this._prefs["emHsAlerts"].pref = res.emailHighScoreAlerts;
        this._prefs["emComments"].pref = res.emailCommentAlerts;
        this._prefs["emEmail"].email = res.email;

        this._prefs["emEmail"].cont.find(".emName").html(res.email);

        for (var i in this._prefs){
            if (this._prefs[i].pref){
              this._prefs[i].cont.find(".formIcon img").attr("src",this._PREF_IMAGE_YES);
           }else{
              this._prefs[i].cont.find(".formIcon img").attr("src",this._PREF_IMAGE_NO);
           }
        }
        $("#profileWait").closest(".account_main").hide();
        this._cont.show();
    }

    this._updatePref = function(val, id){
        this._prefs[id].pref = val;
        this._emailDialog.hideDialog();
        this._makePrefReq();
    }

    /** handle array of response object - this is to be defined by child class
     * @name name of parameter
     * @value list of response objects
     */
    this.handleResObjects = function(resObjects) {
        var gameIndex = 0;

        for (var i = 0; i < resObjects.length; i++) {
            var res = resObjects[i];
            if (res.methodName == "profileService.getPrivateProfile") {
                this._handleGetPrivateProfile(res.response);
            }
        }
    };


}

EmailModule.prototype = new Module();

EmailModule.NAME = "EmailModule";

