app.update = {
	
	attachEvents : function(){
		app.grdata_update.event('submit', function(e){ 
				app.update.tryUpdate(e);
		});
	},
	
	clickLecture : function(e){
		sb.events.stopAndPrevent(e);
		var targ = sb.events.target(e);		
		targ = (targ.nodeName == 'A')?targ:targ.parentNode;	
		var lid = (targ.parentNode)?targ.parentNode.value:0;
		
		if(lid){
			window.open('/app/cert.php?lid=' + lid, 'certificate_' + lid, 
					'height=800px,width=820px,toolbar=0,status=0,menubar=0,top=0,left=0resizable=1,scrollbars=1');
		}
	}, 
	
	clickNotes : function(e){
		var targ = sb.events.target(e);
		if((targ.nodeName == 'LI') && (targ.node)){
			targ.node.focus();					
		}
	},
	
	init : function(){	
		
			
 		if(app.grdata_update && app.credits_notes){
				this.grdata_update = new tc.forms(app.grdata_update);
				this.formnotes = new tc.widget.notes(app.credits_notes, {events : {click : this.clickNotes}});
				this.attachEvents();	
		}
		
		
		if(app.credits_shell){
			this.credits = new tc.widget.pageControl(app.credits_shell, this.clickLecture);
		}			
			
	
	},
	
	
	tryUpdate : function(e){ 
		var that = this;
		var ms =  this.grdata_update.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',
				fire : 0,
				data : this.grdata_update.serialize(),
				handler : function(data){
					
					that.formnotes.removeOne('join_message');
										
					if(sb.typeOf(data) == 'object'){
						that.formnotes.add({
							id : 'join_message',
							message : data.message
						});
					}
					
				}
			}).fetch();
			
			delete app.ajax;	
		}			
	}	
};