dojo.declare("com.mgh.AgeGroupView", null, {
	constructor : function(){
		this.renderLayer = '#ageGroup';		
		this.listLayout = '<ul>${0}</ul>';
		this.listElement = '<li>${0}</li>';
		this.model;
	},	

	RenderInitialView : function(){		
		this.model = new Array();
		this.model[0] = new com.mgh.AgeGroup('0','Adult');
		this.model[1] = new com.mgh.AgeGroup('1','Pediatric');
		var state = applicationState.getApplicationComponentState('age');
		var id;
		
		/* jlp8 09/01/201 view object type was changed to Age, class is still AgeGroup, needs to be refactored */
		if (state){
			/* id is a number but "smart logic" takes a string, name */		
			id = applicationState.model.age == 'Adult' ? '0' : '1';	
			this.RenderSelectedView(id, 'Age', 'Age');
		} else {
			this.RenderListView('','Age','Age');
		}
	},
	
	RenderListView : function(id,name,type){
		this.RenderListViewFromExistingModel();
	},
		
	RenderListViewFromExistingModel : function(){
			var output = "";
			try {
			for (var i = 0; i < this.model.length; i++){
				output += dojo.string.substitute(this.listElement, [this.model[i].Format()]);				
			}
			output = "<ul>" + output + "</ul>";
		} catch(e) {
			output = "<ul><li><a href=\"#\" onclick=\"dojo.publish('ageGroupClicked',['0','Adult','Age'])\">Adult</a></li><li><a href=\"#\" onclick=\"dojo.publish('ageGroupClicked',['1','Pediatric','Age'])\">Pediatric</a></li></ul>";			
		}
		
		jQuery(this.renderLayer).html(output);
	},
	
	RenderSelectedView : function(id,name,type){
		try {
			for (var i=0; i < this.model.length; i++){
				if(this.model[i].id == id){			
					selected = this.model[i].SelectedFormat();	
					break;
				}
			}
		} catch(e) {
			if(id == '0') {
			selected = "<p class=\"title\">Adult</p><a href=\"#\" class=\"goBack\" id=\"0\" onclick=\"dojo.publish('showAllAgeGroups',['','','Age'])\">Show all</a>";
			} 
			else {
				selected = "<p class=\"title\">Pediatric</p><a href=\"#\" class=\"goBack\" id=\"1\" onclick=\"dojo.publish('showAllAgeGroups',['','','Age'])\">Show all</a>";
			}
		}
		jQuery(this.renderLayer).html(selected);
	}
});
