/**
 * @author Brad Petit
 */

Ext.namespace("Das.Newsletter");

Ext.apply(Das.Newsletter, {
	version: "1.0",
	actionFile: 'actions.System.php'	
});

Das.Newsletter.SignupForm = Ext.extend(Ext.form.FormPanel, {
	initComponent: function(){
		
		Ext.apply(this, {
			header: false,
			bodyStyle:'padding:5px 5px;',
			buttonAlign: 'right',
			border: false,
        	defaultType: 'textfield',
        	items: [{
				xtype: 'box',
				autoEl: 'div',
				id: 'newsletter-msg',
				width: '100%',
				style: 'padding-bottom:3px;text-align:center;color: #FF0000;'
			},{
                fieldLabel: 'Full Name',
				id:'newsletter-fullname',
                name: 'full_name',
				anchor: '90%',
                allowBlank:false
            },{
                fieldLabel: 'Email',
                name: 'email',
                vtype:'email',
				anchor: '90%',
				allowBlank: false
            }],
			buttons: [{
				text: 'Join Newsletter',
				handler: function(btn){
					var form = this.getForm();
					
					if (form.isValid()) {
						form.submit({
							url: 'lib/system/' + Das.Newsletter.actionFile,
							method: 'GET',
							params: {
								d: 'JoinNewsletter'
							},
							success: function(form, action){
								if(action.result.success){
									Ext.Msg.alert("Newsletter Signup", action.result.msg);
									this.ownerCt.close();
								}
							},
							failure: function(form, action){
								var email = this.find("name", "email");
									email = email[0];
									email.focus();
									email.markInvalid();
									email.selectText();
									
								Ext.get('newsletter-msg').update(action.result.msg);
								return false;
							},
							scope: this
						});
					}else{
						return true;
					}
				},
				scope: this
			},{
				text: 'Cancel',
				handler: function(btn){
					this.getForm().reset();
					this.ownerCt.close();
				},
				scope: this
				
			}]
		});
		
		Das.Newsletter.SignupForm.superclass.initComponent.apply(this, arguments);
	}
});

Das.Newsletter.Subscribe = function(){
	
	return {
		showWindow: function(link){
			
			var win = new Ext.Window({
				id:'newsletter-window',
				layout: 'fit',
				bodyStyle: 'background-color:#FFFFFF;',
				title: 'Subscribe to our Newsletter',
				plain: false,
				width: 350,
				height: 150,
				items: [new Das.Newsletter.SignupForm()],
				renderTo: Ext.getBody()
			});
			
			win.alignTo(link.id, 'bl-bl');
			win.show();
		}
	};
}();

Ext.onReady(function(){
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

	Ext.get('newsletter').on('click', function(e, el){
		e.preventDefault();
		
		this.Subscribe.showWindow(el);
	}, Das.Newsletter);
});
