// $Id: action.js 1446 2010-05-26 14:24:28Z rcf8 $
/**
 * @description Action module for dealing with logic of adding/removing studies and resultsets
 * @author <a href="mailto:rcfree@gmail.com">Rob Free</a>
 * @version 3.0
 */

var resultsets;
var studies;
var phenotypeCount;
function Action(resultsets, studies, phenotypeCount) {
	this.resultsets = resultsets;
	this.studies = studies;
	this.phenotypeCount = phenotypeCount;
}

Action.prototype.addResultsets=function(resultsets) {
	var finalList = Action.combineArrays(resultsets, this.resultsets);
	this.resultsets = finalList;
}

Action.prototype.removeResultsets=function(resultsets) {
	var finalList = Action.extractArrays(this.resultsets, resultsets);
	this.resultsets = finalList;
}

Action.prototype.addStudy = function(study) {
	var studiesList = this.studies;
	var studies = new Array(study);
	var finalList = Action.combineArrays(studies, studiesList);
	this.studies = finalList;
}

Action.prototype.addStudies = function(studies) {
	var studiesList = this.studies;
	var finalList = Action.combineArrays(studies, studiesList);
	this.studies = finalList;
}

Action.prototype.removeStudy = function(study) {
	var studiesList = this.studies;
	var studies = new Array(study);
	var finalList = Action.extractArrays(studiesList, studies);
	this.studies = finalList;
}

Action.prototype.incPhenotypeCount = function() {
	this.phenotypeCount = this.phenotypeCount + 1;
}

Action.prototype.decPhenotypeCount = function() {
	this.phenotypeCount = this.phenotypeCount - 1;
}

Action.combineArrays = function(array1, array2) {
	var map = {};
	var finalList = new Array();

	if (array1 && array1.length>0) {
		for(var i = 0;i<array1.length;i++) {
			map[array1[i]]=1;
		}
	}

	if (array2 && array2.length>0) {
		for(var i = 0;i<array2.length;i++) {
			map[array2[i]]=1;
		}
	}	
	
	var mapSize = Action.size(map)
	
	for(var item in map) {
		finalList.push(item);
	}
	
	return finalList;
}

Action.size = function (hashObject) {
	var size = 0;
	for(var item in hashObject) {
		size++;
	}
	return size;
}
 
Action.extractArrays =function(arrayBase, arrayToRemove) {
	var mapToRemove = new Array();
	var finalList = new Array();
	
	for(var rsNo = 0;rsNo<arrayToRemove.length;rsNo++) {
		mapToRemove[arrayToRemove[rsNo]]=1;
	}
	
	for(var rsNo = 0;rsNo<arrayBase.length;rsNo++) {
		var toRemove = arrayBase[rsNo];
		
		
		if (!mapToRemove[toRemove]) {
			finalList.push(arrayBase[rsNo]);
		}
	}

	return finalList;
}

Action.presentInArray = function(arrayBase, arrayPresent) {
	var mapToCheck = new Array();
	var finalList = new Array();
	
	if (arrayBase == null) {
		return finalList;
	}
	
	for(var rsNo = 0;rsNo<arrayPresent.length;rsNo++) {
		mapToCheck[arrayPresent[rsNo]]=1;
	}
	
	for(var rsNo = 0;rsNo<arrayBase.length;rsNo++) {
		var toCheck = arrayBase[rsNo];
		
		if (mapToCheck[toCheck]) {
			finalList.push(arrayBase[rsNo]);
		}
	}

	return finalList;
}

Action.calcLabel = function(array, singular, plural) {
	var count = array.length;
	if (count==null) {
		count = array;
	}
	if (count==0) return { number: 'No', label:plural};
	if (count==1) return { number: count, label: singular};
	return {number:count, label: plural};
}

