app.signup = {
	
	attachEvents : function(){				
		app.grdata_signup.event('submit', function(e){ 
				app.signup.trySignup(e);
		});
	},
	
	
	clickNotes : function(e){
		var targ = sb.events.target(e);
		if((targ.nodeName == 'LI') && (targ.node)){
			targ.node.focus();					
		}
	},
	

	init : function(){	
		
	var grdata_signup = $('#grdata_signup');
	var formnotes = $('#formnotes');
		
		if(grdata_signup && formnotes){
				this.grdata_signup = new tc.forms(grdata_signup);
				this.formnotes = new tc.widget.notes(formnotes, {events : {click : this.clickNotes}});
				this.attachEvents();	
		}
		
		if(app.signup_cancel){
			app.signup_cancel.event('click', function(e){
				app.warn('You will now be logged out'); 	
				app.navi.visit('logout');
			})
		}
	},

	
	trySignup : function(e){ 
		var that = this;
		var ms =  this.grdata_signup.noEmpties();
		
		//kill bubble
		sb.events.stopAndPrevent(e);
				
		//remove errors
		ms.remove.forEach(function(v){ 
			that.formnotes.removeOne(v);
		});
		
		//add errors
		if(ms.hasEmpties){			
			ms.add.forEach(function(v){ 
				that.formnotes.add(v);
			});
		}else{			
			//success!			
						
			app.ajax = new sb.ajax({
				format : 'json',
				method : 'get',
				data : this.grdata_signup.serialize(),
				handler : function(data){	
					
					that.formnotes.removeOne('join_message');
					
					if(sb.typeOf(data) == 'object'){
						if(data.success == 0){
							that.formnotes.add({
								id : 'join_error',
								message : data.message
							});
						}else{
							app.navi.visit("thanks");
						}
					}
										
				}
			}).fetch();
			
			delete app.ajax;
		}			
	}	
	
};