dojo.provide("UI.Widgets.User.Activation");


(function() {
	var EVT_ACTIVATION_SUCCESS = "activationSuccess";
	
	dojo.declare("UI.Widgets.User.Activation", UI.Base, {
		constructor: function(callback) {
			UI.Dialog.Generic.create(
				{template: {template: "activation"}},
				dojo.hitch(this, function() {
					this._init();
					
					if ( typeof callback == "function" ) {
						callback(this);
					}
				})
			);
		},
		
		_init: function() {
			this.container = dojo.byId("dlg-activation");
			this.stage     = new UI.Stage(this.container);
			this.inpEmail  = dojo.byId("wdg-activation-inp-email");
			this.inpCode   = dojo.byId("wdg-activation-inp-activation-code");
			this.fields    = dojo.query("#dlg-activation .field");
			this.btnSubmit = dojo.byId("wdg-activation-btn-submit");
			
			dojo.addClass(this.container, PPP.data.user.isChild ? "coppa" : "noncoppa");
			
			if ( !PPP.data.user.isChild ) {
				this.stage.set(2);
			}
	
			this.dialog = UI.Dialog.Generic(this.container)
				.useModal()
				.addEventListener("open", dojo.hitch(this, function() {
					if ( this.isChild ) {
						this.inpEmail.focus();
					} else if ( this.stage.get() == 2 ) {
						this.sendCode();
						this.inpCode.focus();
					}
				}), true);
			
			dojo.map(['keyup', 'change'], dojo.hitch(this, function(event) {
				dojo.connect(this.inpEmail, event, this, function(event) {
					if ( UI.Util.Validate.emailPattern(this.inpEmail.value) ) {
						this.btnSubmit.disabled = false;
						dojo.removeClass(this.btnSubmit, "disabled");
						
						if ( event.keyCode == 13 ) {
							this.sendCode();
						}
					} else {
						this.btnSubmit.disabled = true;
						dojo.addClass(this.btnSubmit, "disabled");
					}
				});
			}));
			
			dojo.map(['keyup', 'change'], dojo.hitch(this, function(event) {
				dojo.connect(this.inpCode, event, this, function(event) {
					if ( this.inpCode.value.length >= 4 ) {
						this.btnSubmit.disabled = false;
						dojo.removeClass(this.btnSubmit, "disabled");
						
						if ( event.keyCode == 13 ) {
							this.activate();
						}
					} else {
						this.btnSubmit.disabled = true;
						dojo.addClass(this.btnSubmit, "disabled");
					}
				});
			}));
			
			dojo.connect(this.btnSubmit, "click", this, function() {
				if ( this.stage.get() == 1 ) {
					this.sendCode();
				} else {
					this.activate();
				}
			});			
		},
		
		sendCode: function() {
			dojo.addClass(this.container, "pending");
	
			dojo.xhrGet({
				preventCache: true,
				handleAs:     "json",
				url:          "/account/activate/send",
				content: {
					parentEmail: this.inpEmail.value
				},
				load: dojo.hitch(this, function(response) {
					dojo.removeClass(this.container, "pending");

					if ( response.status ) {
						this.btnSubmit.disabled = true;
						dojo.addClass(this.btnSubmit, "disabled");
						dojo.byId("wdg-activation-lbl-email-domain").innerHTML = response.emailDomain;
						
						this.stage.set(3);
						this.inpCode.focus();
					} else if ( response.field ) {
						dojo.addClass(this.container, "error");
					} else {
						UI.Dialog.Generic.create(
							{template: {template: "activation-failed"}},
							dojo.hitch(this, function(dialog) {
								dialog.useModal().open();
								
								if ( !PPP.data.user.isChild ) {
									this.dialog.close();
								}
							})
						);
					}
				})
			});
		},
		
		activate: function() {
			dojo.addClass(this.container, "pending");
	
			dojo.xhrGet({
				preventCache: true,
				handleAs:     "json",
				url:          "/account/activate",
				content: {
					code:       this.inpCode.value,
					noRedirect: 1
				},
				load: dojo.hitch(this, function(response) {
					dojo.removeClass(this.container, "pending");
					
					if ( response.status ) {
						this.stage.set(4);
						this.triggerEvent(EVT_ACTIVATION_SUCCESS);
					} else {
						dojo.addClass(this.container, "error");
					}
				})
			});
		},
		
		activationComplete: function() {
			this.stage.set(4);
			this.dialog.open();
		}
	});
}());
