/* hash.js
 * Controls the history of the browser and provides a central point for storing:
 *   -Current RadlexID
 *   -Current BaseID
 *   -Current RelationType
 *
 * Previously this data was stored using window.location.hash, but has been modified to merely store
 * this values in global variables.  This change allows browsers such as Safari and Konquer to use
 * RadLex
 * 
 * Created by: John Paulett (9/1/06)
 */
var historyStore = new Array();
var historyPosition = -1;

function addCurrentStateToHistory(){
	addHistory(getBaseID(),getRadlexID(), getRelationType());
}
function addHistory(radlexID, baseID, relationType){
	//in the case of already being back in history, and you go a new direction, you should remove all 'future' moves
	if (historyPosition < historyStore.length -1 && historyPosition >= 0){
		historyStore = historyStore.slice(0,historyPosition); //delete the future
	}
	
	//add new history at the end of the array
	historyPosition = historyPosition + 1; //make the new history spot
	historyStore[historyPosition] = new Array();
	historyStore[historyPosition]['baseID'] = baseID;
	historyStore[historyPosition]['radlexID'] = radlexID;
	historyStore[historyPosition]['relationType'] = relationType;	
}

function goBack(){
	if ( historyPosition > 0){
		historyPosition = historyPosition - 1;
		updateHash(historyStore[historyPosition]['radlexID'],historyStore[historyPosition]['baseID'],historyStore[historyPosition]['relationType']);
		loadTree(parseTreeAndTermForHistoryRestore);
	}
	
}
function goForward(){
	if (historyPosition < historyStore.length - 1  && historyPosition >= 0){
		historyPosition = historyPosition + 1;
		updateHash(historyStore[historyPosition]['radlexID'],historyStore[historyPosition]['baseID'],historyStore[historyPosition]['relationType']);
		loadTree(parseTreeAndTermForHistoryRestore);
	}
}


/* global variable for storing state */
var gHash = new Array();

/* Sets the values for the hash */
function updateHash(radlexID,baseID,relationType){
	gHash['radlexID'] = radlexID;
	gHash['baseID'] = baseID;
	gHash['relationType'] = relationType;
}

/*  Returns an array of the state (a copy of, not a reference to gHash) */
function getHash(){
	var hashArr = new Array();
	hashArr['radlexID'] = gHash['radlexID'];
	hashArr['baseID'] = gHash['baseID'];
	hashArr['relationType'] = gHash['relationType'];
	
	return hashArr;
}

/* Returns the current RadlexID */
function getRadlexID(){
	var hash = getHash();
	return hash['radlexID'];
}

/* Returns the current BasedID */
function getBaseID(){
	var hash = getHash();
	return hash['baseID'];
}

/* Returns the current RelationType */
function getRelationType(){
	var hash = getHash();
	return hash['relationType'];
}

function setRadlexID(radlexID){
	var hash = getHash();
	updateHash(radlexID,hash['baseID'],hash['relationType']);
	loadTree(parseTreeAndTerm);
}

function setRelationType(relationType,baseID){
	var hash = getHash();
	if (baseID == null){
		baseID = hash['radlexID'];
	}
	updateHash(hash['radlexID'],baseID,relationType);
	loadTree(parseTree);
	highlightTerm(hash['radlexID']);
}
function setBaseID(baseID){
	var hash = getHash();
	updateHash(hash['radlexID'],baseID,hash['relationType']);
	loadTree(parseTree);
}
function removeBaseID(){
	updateHash(getRadlexID(),'RID1','preferred');
	loadTree(parseTree);
}

