/**
 * @author sameerhalai
 */

var page = new Page(); //the page object containing all elements on the page
var nodes; //contains all the questions as a JSON object
var activeNodes; //contains all the nodes that are required by the dynamic consistency
var currentSubQuestion = null; //always holds the currentSubQuestion
var triggered = new TORSCStack(); //contains all the triggered node names
var ignored = new TORSCStack(); //contains all the currently ignored subquestion node names
triggered.push("nature_of_work");
//triggered.push("tech_readiness");

/* ---------------------------------------------------
 * Initialization functions
 * ---------------------------------------------------
 */
function globalInit() {
	_memoryInit();
	page.tree.init();
	_handleClickOnSubQuestion(nodes.questions[0].subQuestions[0]);
}

function _memoryInit() {
	nodes = myJSONObject;
/*	activeNodes = new cloneObject(nodes);
	for (var i=0; i<=activeNodes.questions.length; i++) {
		activeNodes.questions.pop();
	}
	activeNodes.questions.push(nodes.questions[0]);*/
}

/* ---------------------------------------------------
 * Handle clicks on tree
 * ---------------------------------------------------
 */
function _handleClickOnTree(question) {
	if ('subQuestions' in question) {
		_handleClickOnQuestion(question);
	} else {
		_handleClickOnSubQuestion(question);
	}
}

function _handleClickOnQuestion(question) {
	page.pane.setOutlineView();
}

function _handleClickOnSubQuestion(subQuestion) {
	if (subQuestion == "Report") { //in the special case that all questions have been answered - just go to generate report
		page.pane.report.show();
		return true;
	}
	//collapse all nodes
	/*for (var i=0; i<nodes.questions.length; i++) {
		var q = nodes.questions[i];
		if (q.node != null) {
			q.node.collapse();
		}
	}*/
	//Make sure the parent of the question is expanded
	//This method can be invoked by a user click or by proceed()
	if (subQuestion.node != null) {
		subQuestion.node.getAncestor().expand();
	}
	page.pane.setQuestionView();
	if (subQuestion == currentSubQuestion || subQuestion.state == "void") {
		//user has clicked on the same subquestion twice. do nothing for now.
		//or the question has been voided by other answers. Ignore all clicks on this.
	} else {
		if(currentSubQuestion!=null) {
			if (currentSubQuestion.response!=null) {
				if(currentSubQuestion.response.value == null) {	
					page.tree.setInactive(currentSubQuestion);
				} else {
					page.tree.setComplete(currentSubQuestion);
				}
			}
		}
		page.tree.setActive(subQuestion);
		page.pane.refresh(subQuestion);
		currentSubQuestion = subQuestion;
	}
}

/* ---------------------------------------------------
 * Flow logic
 * ---------------------------------------------------
 */
function proceed() {
	page.tree.refresh();
	_handleClickOnSubQuestion(page.tree.getNextInactive());
}

function report() {
	
}

/* --------------------------------------------------
 * Utility Methods
 * --------------------------------------------------
 */
function cloneObject(what) {
    for (i in what) {
        this[i] = what[i];
    }
}
