dojo.declare("com.mgh.CentersView", null, {
	constructor : function(){
		this.renderLayer = '#centerDepartment div';		
		this.listLayout = '<ul>${0}</ul>';
		this.listElement = '<li>${0}</li>';
		this.model;	
		this.preSelectedCenter = 0;
		this.locationSelected = false;
		this.centerSelected = false;
	},
	
	EvaluateView : function(id,name,type){
		if(this.centerSelected == false && this.locationSelected == true){
			//center not selected and location selected, get centers by location
			dojo.publish('getCentersByLocation',[id,name,type]);
		}
		
		if(this.centerSelected == false && this.locationSelected == false){
			//center not selected and location deselected get all centers
			dojo.publish('requestCentersData',['51879',name,type]);		
		}
		
		if(this.centerSelected == true && this.locationSelected == true){
			//center selected and location selected, do nothing
		}
		
		if(this.centerSelected == true && this.locationSelected == true){
			//center selected and location de selected, do nothing
		}		
	},
	
	ToggleLocationSelected : function (id,name,type){
		this.locationSelected = this.locationSelected == false ? true : false;
	},	
		
	ToggleCenterSelected : function (id,name,type){
		this.centerSelected = this.centerSelected == false ? true : false;
	},	
		
	RenderInitialView : function(model){
		this.model = model;
		this.RenderListView('','','');
	},
		
	RenderPreSelectedView : function(id,model,type){
		this.centerSelected = true;
		this.model = model;
		for (var i=0; i < this.model.length; i++){
			if(this.model[i].id == id){
				selected = this.model[i].PreSelectedFormat();
				break;
			}
		}
		this.preSelectedCenter = id;
		jQuery(this.renderLayer).html(selected);
	},	
	
	RenderListView : function(id,name,type){
		if(type == 'Center' || 'Department'){
			this.centerSelected = false;		
		}
		
		if(type == 'Location'){
			this.locationSelected = false;
		}
		
		try {
			var output = "";
			for (var i = 0; i < this.model.length; i++){				
				output += dojo.string.substitute(this.listElement, [this.model[i].Format()]);
			}			
			output = dojo.string.substitute(this.listLayout,[output]);
			jQuery(this.renderLayer).html(output);
		} catch (e){
			console.log('com.mgh.CentersView error: ' + e);
		}
	},
	
	RenderSelectedView : function(id,name,type){
		try {
			if (this.preSelectedCenter == 0){
				for (var i=0; i < this.model.length; i++){
					if(this.model[i].id == id){			
						selected = this.model[i].SelectedFormat();	
							
						if(type == 'Center' || 'Department'){
							this.centerSelected = true;						
						}	
						
						if(type=="Location"){
							this.locationSelected = true;						
						}
						break;
					}
				}
				jQuery(this.renderLayer).html(selected);
			}
		} catch(e){
			alert('error');
		}
	}
});