//
//        Disease and DiseaseList constructors and methods


function Disease(name, pValue, id, system, gList, index, desc) {
	this.name = name.toTitleCase();
	this.pValue = pValue;
	this.hasRank = true;
	this.rank = 0;
	this.system = system;
	this.desc = this.name + ": " + desc;
	this.topic = "diseases";
	this.topicLabel = "disease";
	this.topicArray = diseases;
	this.fullGeneList = gList;
	this.genes = [];
	this.genesNotInDataset = [];
	this.species = [];
	this.pathways = [];
	this.processes = [];
	this.citations = [];
	this.id = id;
	this.index = index;
	this.graphics = {wheel:null, matrix:null},
	this.inContext = true;
	
//	for (var i=0; i<this.fullGeneList.length; i++) {
//		var gi = genes.indexById(this.fullGeneList[i]);
//		if(gi>=0) {
//			this.genes.push(gi);
//			genes.list[gi].diseases.push(this.index);
//		}	else {
//			this.genesNotInDataset.push(this.fullGeneList[i])
//		}		
//	}

	this.getRank = function() {
		this.rank = 1.0 - (this.pValue - diseases.rankRange.min)*diseases.rankRange.inv;
		return this.rank;
	};
	
	this.removeFromContext = function() {
		// remove this disease ... and its genes from the context
	}

	this.addToContext = function() {
		// add this disease ... and its genes from the context
	}
	this.select = function() {
		// select each gene in the matrix and wheel 
		// update the details with information on this disease
		
		// add all the genes to the selection for hilighting
		selectionMgr.set(this);
		//parent.view.details.set("disease", this);  // note this is done also in setSelection.... need to fix this
	}
	this.updateDescription = function(){
		updateDiseaseDescription(this.id);
	}
	this.buildGeneList = function () {
		// have to do this after disease is constructed because gene list hasn't been built yet
		for (var i=0; i<this.fullGeneList.length; i++ ) {
			var indx = genes.indexById(this.fullGeneList[i]);  // a string symbol name;
			if(indx >=0) {
				this.genes.push(indx);
				genes.list[indx].diseases.push(this.index);
			} else {
				this.genesNotInDataset.push(this.fullGeneList[i]);
			}
		}
	}

	this.getGeneList = function() {
		return this.genes;
	}
	this.getGenesInContext = function() {
		var lst = [];
		for(var i=0; i<this.genes.length; i++) {
			if(genes.list[this.genes[i]].inContext) lst.push(this.genes[i])
		}
		return lst;
	}
	
	this.isInContext = function() {
		var gl = this.getGenesInContext();
		this.inContext = (gl.length > 0)? true : false;
		return this.inContext;
	}
	
	this.getRankQuartile = function() {
		return quartile(this.pValue, diseases.rankRange);	// min -> 3,  max -> 0
	}
	
	this.getHierarchyObject = function() {
		var hier;
		var curNode = null;
		for (var i=0; i<hierarchies.length; i++) {  // shouldn't have to do this now.. should be done at initialization
			if(hierarchies[i].topic.indexOf("diseas") >=0) hier = hierarchies[i];
		}
		var hierObj = null;
		for (var i=0; i < hier.nodes.length; i++) {
			if(hier.nodes[i].id == this.id) {
				hierObj = hier.nodes[i];
			};
		}
		return hierObj;
	};
	
	this.getRimObject = function() {
		var hierObj = this.getHierarchyObject();
		var curNode = null;
		if(hierObj) {
			curNode = hierObj;
			while(!curNode.rimChip) {
				curNode = curNode.parentObj;
			}
			// curNode is the closest ancestor that has a place on the wheel rim
		}
		return curNode;
	};
	
	this.getTopic 		= function() { return this.topic};
	this.getTopicLabel 	= function() { return this.topicLabel};
	this.getTopicArray  = function() {return this.topicArray;};
	this.getName 		= function() { return this.name};
	this.getIndex 		= function() {return this.index};
	this.getDesc 		= function() {return this.desc};
	this.getId  		= function() {return this.id};
}


function initDiseaseList(list, dataLoadStatus){
  var result = diseasesData[reportId];
   var tmpList = new Array();
   var len = (gDataSize == 0) ? result.diseases.length : gDataSize;
   for(var k = 0; k < len; k++){     
     var disease = result.diseases[k];
     var name = disease.name;
     var pValue = disease.pvalue;
     var system = disease.system;
     var desc = "";
     var id = disease.did;
     var gList = disease.molIds;
        var d = new  Disease(name, pValue, id, system, gList, k, desc);
        tmpList.push(d);
   }  
  updateDiseaseList(tmpList, list, dataLoadStatus);
}

function updateDiseaseList(resultList, diseaseList, callback){
	diseaseList.list = resultList;
	
	for (var i=0; i<diseaseList.list.length; i++) {
		diseaseList.rankRange.min = Math.min(diseaseList.rankRange.min, diseaseList.list[i].pValue);
		diseaseList.rankRange.max = Math.max(diseaseList.rankRange.max, diseaseList.list[i].pValue);
  }
  
	if(diseaseList.rankRange.min == diseaseList.rankRange.max) {
		diseaseList.rankRange.inv = 0;
	} else {
		diseaseList.rankRange.inv = 1.0/(diseaseList.rankRange.max - diseaseList.rankRange.min);
	}
	callback();
  // dataLoadStatus.update("diseases");
}

function updateDiseaseDescription(did) {
  // $.getJSON('/bda/data/diseases/desc/' + did, function showDesc(map) { 
  //  parent.view.$("#ddDesc").html(map.desc);
  // });
}

function DiseaseList() {
	this.name = 'diseases';
	this.list = new Array();
	this.rankRange = {min:9999999, max:-99999999, inv:1};
	

	this.getName = function(){
		return this.name;
	};
	
	this.numberInContext = function () {
		var num = 0;
		for (var i=0; i< this.list.length; i++ ) {
			if(this.list[i].inContext) num++;
		}
		return num;
	};

	this.unSelect = function() {
		// remove selection if any
	};

	this.buildGeneLists = function() {
		for (var i=0; i<this.list.length; i++) {
			this.list[i].buildGeneList();
		}
	};

	this.listInContext = function() {
		var l = new Array();
		for (var i=0;i<this.list.length; i++) {
			if(this.list[i].inContext) l.push(i);
		}
		return l;
	};
	
	this.byIndex = function(i) {
		return this.list[i];
	};
	
	
	this.sortBy = function(opt, inContext) {
		var l = [];
		if(inContext) {
			l = this.listInContext();
		} else {
			for (var i=0; i<this.list.length; i++) {
				l[i] = i;
			}
		}
		l.sort(function(a,b) {
			return diseases.list[a].pValue - diseases.list[b].pValue;
		});

		return l;
	};
	
	this.getByID = function(id) {
		for (var i=0; i<this.list.length; i++) {
			if(this.list[i].id == id) return this.list[i];
		}
		return null;
	};
}







