dojo.declare("com.mgh.SubCategories",null, {
	constructor : function(configParams) {
		this.subCategories;
		this.previousCat;
		
		/********************************** Default Variables *****************************************/
		// this class has an empty constructor because, although the values are initialized, no action is needed
		// until a category is clicked
		//accept a json object with all necessary constructor values
		this.queryParams = configParams;
		this.query = '';		
		
		// linksToDisplay: is how many categories are displayed before the "view all" link and overlay are displayed
		this.linksToDisplay = this.queryParams.links;
		try {
			fadeOutNode('subCategoriesColumn');
		} catch (e){
			console.log(e);
		}
	},
	
	/********************************** xml methods *****************************************/  
	updateQuery : function()
	{
		var m = this.queryParams;
		this.query = m.href + '?az=' + m.az + '&age=' + m.age + '&cat=' + m.cat +  '&subcat=' + m.subcat + '&type=' + m.type + '&query=' + m.query;
		console.log('subCategories ' + this.query);
	},	
	
	getXmlString : function (){
		this.updateQuery();
		dojo.xhrGet({
		url: this.query,
			handleAs: "text",
			load: function(response, ioArgs){
				subCategoryDataReady(response);
				return response;
			},
			error: function(response, ioArgs){
			  alert("An error occurred, with response: " + response);
			  return response;
			}
		});
	},

	/********************************** SubCategories Functions *****************************************/	
	getSubCategories : function (id){
		this.queryParams.cat = id;
		this.updateQuery();
		this.getXmlString();	
	},
	
	buildSubCategories : function (){
		var counter = 0;
		var output = '';
		var regEx = /\(.*\)/;
		var output = '<h3>Medical Subcategory:</h3>';
        output += '<ul>';		
		var myCategories = this.subCategories.getElementsByTagName("NAME");
		var myCatIds = this.subCategories.getElementsByTagName("ID");
		/*
		if (this.linksToDisplay > myCategories.length){
			this.linksToDisplay = myCategories.length;
		}*/
		var trimmed = '';
        for (var i=0; i< this.linksToDisplay; i++){
        	try {
	        	trimmed = myCategories[i].childNodes[0].nodeValue.toString().replace(regEx,'');
		       	output += '<li><a href="#" onclick="filterByCategory(\''+
	        	myCatIds[i].childNodes[0].nodeValue + '\'); updateSubCategories(\'' + 
	        	myCatIds[i].childNodes[0].nodeValue +'\',\'' + 
	        	trimmed + '\')">' + 
	        	trimmed + '</a></li>';
	        	counter += 1;
        	} catch (e) {
        		break;
        	}
        }
		if (myCategories.length > this.linksToDisplay){
    		output += '<a href="#" class="goBack" onclick="buildHiddenPanel(\'categoriesContent\',\'subCategories\'); dijit.byId(\'categoriesDialog\').show()">Display All</a>';
		}
        output += '</ul>';
        if (counter == 0){
        	output = "No subcategories available";
        }
        dojo.byId('subCategories1').innerHTML = output;
        dojo.byId('categories1').style.width = '180px';
        //dojo.narrow('categories1',180);
        dojo.byId('subCategoriesColumn').style.display = 'block';
	},
	
	buildHiddenPanel : function (dialogId,catName){
		var i;
		var myCategories = this.subCategories.getElementsByTagName("NAME");;
		var myCatIds = this.subCategories.getElementsByTagName("ID");;
		var output = '<ul>';
		var trimmed = '';
		
		for (i=0; i < myCategories.length; i++){
			trimmed = myCategories[i].childNodes[0].nodeValue.toString().replace('(Category)','');
			if (catName == "categories"){		        	
	        	output += '<li><a href="#" onclick="filterByCategory(\''+
	        	myCatIds[i].childNodes[0].nodeValue + '\'); updateCategories(\'' + 
	        	myCatIds[i].childNodes[0].nodeValue +'\',\'' + 
	        	trimmed + '\'); dijit.byId(\'categoriesDialog\').hide()">' + 
	        	trimmed + '</a></li>';
			} else {			
		       	output += '<li><a href="#" onclick="filterByCategory(\''+
	        	myCatIds[i].childNodes[0].nodeValue + '\'); updateSubCategories(\'' + 
	        	myCatIds[i].childNodes[0].nodeValue +'\',\'' + 
	        	trimmed + '\'); dijit.byId(\'categoriesDialog\').hide()">' + 
	        	trimmed + '</a></li>';		
			}		
		}
		output += '</ul>';
		dojo.byId('categoriesContent').innerHTML = output	
	},

	updateSubCategories : function (id,nodeName){
		var output = '<h3>Medical Subcategory:</h3>';
        output += '<ul>';
		var nodes = dojo.query('li', id);
		output += '<li id="' + nodeName + '" class="active"><strong>' + nodeName + '</strong></li>';
		output += '<li><a href="#" class="goBack" onclick="removeFilter(\'subCategories1\')">Show all medical subcategories</a></li>';
		output += '</ul>';
		dojo.byId('subCategories1').innerHTML = output;
	}
});