/**
 * @author sameerhalai
 */

/* ---------------------------------------------------
 * UI display control methods
 * ---------------------------------------------------
 * 
 * Page
 * |--QuestionPane
 *    |--Question
 *    |--Prompt
 *    |--AssistanceBar
 *    |--NavigationBar
 *    |--Report
 * 
 */

/* The parent object for every element on the page */
function Page() {
	this.tree = new Tree();
	this.pane = new QuestionPane();
}

function QuestionPane() {
	this.question = new Question();
	this.prompt = new Prompt();
	this.assistanceBar = new AssistanceBar();
	this.navigationBar = new NavigationBar();
	this.report = new Report();
	this.refresh = function (subQuestion) {
		this.question.refresh(subQuestion);
		this.prompt.refresh(subQuestion);
		this.assistanceBar.refresh(subQuestion);
		this.navigationBar.refresh(subQuestion);
	}
	this.setQuestionView = function () {
		this.question.show();
		this.prompt.show();
		this.navigationBar.show();
		//this.report.hide();
	}
	this.setOutlineView = function () {
		this.question.hide();
		this.prompt.hide();
		this.navigationBar.hide();
		//this.report.hide();
	}
}

/* Encapsulates the question */
function Question() {
	this.el = new YAHOO.util.Element('question');
	this.text = "Question";
	this.show = function() {this.el.setStyle('display','block');}
	this.hide = function() {this.el.setStyle('display','none');}
	this.refresh = function (subQuestion) {
		this.el.set('innerHTML',subQuestion.question);
	}
}

/* Encapsulates the dynamic prompt that is generated for each question */
function Prompt() {
	this.el = new YAHOO.util.Element('prompt');
	this.show = function() {this.el.setStyle('display','block');}
	this.hide = function() {this.el.setStyle('display','none');}
	this.refresh = function (subQuestion) {
		if(subQuestion.response.type=="customRadio") {
			var text="";
			var prompts=subQuestion.response.prompt;
			for(x=0;x<prompts.length;x++) {
				text += "<div class='option'>";
				text += "<input onclick=\"page.pane.prompt.process('"+prompts[x].text+"');\"";
				text += " id=\"rad"+x+"\" name=\"answer\" type=\"radio\"";
				if(subQuestion.response.value!=null) {
					if (subQuestion.response.value==prompts[x].text) {
						text += " checked";
					}
				}
				text += " value=\""+prompts[x].text+"\")'/> <label for=\"rad"+x+"\">"+prompts[x].text+"</label></div>";
			}
		}
		document.getElementById("prompt").innerHTML=text;
	}
	this.process = function(promptValue) {
		//Make a note of elements that have been triggered,ignored,disignored by this action
		currentSubQuestion.response.value=promptValue;
		prompts=currentSubQuestion.response.prompt;
		for(x=0;x<prompts.length;x++) {
			if (prompts[x].text == promptValue) {
				//add the triggered nodes into the triggered array
				if (prompts[x].action!=null) {
					triggered.push(prompts[x].action);
				}
				if (prompts[x].remedy!=null) {
					currentSubQuestion.response.remedy = prompts[x].remedy;
				}
				if (prompts[x].redFlag!=null) {
					currentSubQuestion.response.redFlag = prompts[x].redFlag;
				}
				//add the ignored nodes into the ignored array
				if (prompts[x].ignore!=null) {
					for (ignoreNode in prompts[x].ignore) {
						ignored.push(prompts[x].ignore[ignoreNode]);	
					}
				}
			} else {
				if (prompts[x].ignore!=null) {
					for (ignoreNode in prompts[x].ignore) {
						//ignored.remove(prompts[x].ignore[ignoreNode]); //remove nodes from the ignore array
					}
				}
			}
		}
	}
	/*Different parameter (accepts entire subQuestin) for the this.process() method. Used mainly by the tree.update() method*/
	this.processSubQuestion = function(subQuestion) {
		var temp = currentSubQuestion;
		currentSubQuestion = subQuestion;
		if(currentSubQuestion.response.value!=null) {
			this.process(currentSubQuestion.response.value);
		}
		currentSubQuestion = temp;
	}
}

/* Encapsulates the assistance bar which changes in context with the question */
function AssistanceBar() {
	this.el = new YAHOO.util.Element('assistanceBar');
	this.show = function() {this.el.setStyle('display','block');}
	this.hide = function() {this.el.setStyle('display','none');}
	this.refresh = function(subQuestion) { } 
}

/* Encapsulates the navigation bar which allows user to proceed to next state */
function NavigationBar() {
	this.el = new YAHOO.util.Element('navigationBar');
	this.show = function() {this.el.setStyle('display','block');}
	this.hide = function() {this.el.setStyle('display','none');}
	this.refresh = function(subQuestion) { } 
}

/*Encapsulates the report area which holds all the flags etc*/
function Report() {
	this.el = new YAHOO.util.Element('report');
	this.show = function() {
		this.generate();
		$D = YAHOO.util.Dom;
		this.el.setStyle('display','block');
		//this.el.setStyle('height',$D.getViewportHeight-100+'px');
		var attributes = {  opacity: { to: 1}};
		var anim = new YAHOO.util.Anim('report', attributes, 1.0, YAHOO.util.Easing.easeOut);
		//var bgAttributes = {backgroundColor: { to: '#000000' }};
		//var anim2 = new YAHOO.util.Anim('doc', bgAttributes);
		//anim2.animate();
		anim.animate();
		var overlay = new YAHOO.util.Element('overlay');
		overlay.setStyle('width',$D.getViewportWidth()+'px');
		overlay.setStyle('height',$D.getDocumentHeight()+'px');
		overlay.setStyle('display','block');
	}
	this.hide = function() {
		//var attributes = {  width: { to: 0 }  }; 
		//var anim = new YAHOO.util.Anim('report', attributes);
		//anim.animate();
		this.el.setStyle('display','none');
		var overlay = new YAHOO.util.Element('overlay');
		overlay.setStyle('display','none');
	}
	this.generate = function() {
		var content = "<h1>Report</h1>\n";
		for (var i=0; i<nodes.questions.length; i++) {
			var q = nodes.questions[i];
			if(triggered.contains(q.id)) {
				content = content + "<h2>"+q.label+"</h2>";
				for (var j=0; j<q.subQuestions.length; j++) {
					var s = q.subQuestions[j];
					if (s.response.value!= null && !ignored.contains(s.id)) {
						content = content + "<h3>"+s.question+" <b>"+s.response.value+"</b></h3>";
						if (s.response.remedy!= null) {
							content = content + "<h4 class='remedy'><b>Remedy</b>:"+s.response.remedy+"</h4>";
						}
						if (s.response.redFlag!=null) {
							content = content + "<h4 class='redFlag'><b>Red Flag</b>:"+s.response.redFlag+"</h4>";
						}
					}
				}
			}
		}
		document.getElementById('report').innerHTML=content;
	}
}

function Tree() {
	this.tree; //contains the YUI TreevView element
	this.root; //contains the root of the YUI TreeView element
	this.questions; //contains a local copy of the questions loaded at start of session
	this.init = function()  {
		this.tree = new YAHOO.widget.TreeView("questionsTree");
		this.root = this.tree.getRoot();
		this.tree.subscribe("labelClick",function(node) {
			_handleClickOnTree(node.data);
		})
		this.populate();
		this.update();
	}	
	this.populate = function() {
		this.questions = nodes.questions;
	}
	/*repopulate the tree based on the values in the triggered array. Also, repopulate the ignored array*/
	this.update = function () {
		var _question; var _father; var _subQuestion;
		ignored = new TORSCStack();
		for (var i=0; i<this.questions.length; i++) {
			_question = this.questions[i];
			if(triggered.contains(_question.id)) {
				//attach the UI element 'node' to the logical element 'question'
				if(_question.node==null)
					_question.node = new YAHOO.widget.TextNode(_question, this.root, false);
				for (var j=0; j<_question.subQuestions.length;j++) {
					_subQuestion = _question.subQuestions[j];
					if (_subQuestion.state == null) _subQuestion.state = 'inactive';
					if(_subQuestion.node==null) _subQuestion.node = new YAHOO.widget.TextNode(_subQuestion, _question.node, false);
					//eval("subnode"+subQuestion.id+"=new YAHOO.widget.TextNode(subQuestion, father, false);");
					page.pane.prompt.processSubQuestion(_subQuestion);
				}	
			}
		}
		this.tree.draw();
	}
	/* Go through all the valid nodes in the tree and match their appearance to their logical state */
	this.refresh = function () {
		this.update();
		for (var i=0; i<this.questions.length; i++) {
			_question = this.questions[i];
			if(triggered.contains(_question.id)) { //proceed only if the question has been triggered to be available for display
				for (var j=0; j<_question.subQuestions.length;j++) {
					_subQuestion = _question.subQuestions[j];
					if (ignored.contains(_subQuestion.id)) { //if the subquestion is in the ignored array, render it as void before proceeding
						_subQuestion.state = "void";
					} else if (_subQuestion.state == "void") { //if it is no longer in array but still marked as void, then update it's state to reflect the change
						if (_subQuestion.response.value != null) {
							_subQuestion.state = "complete";
						} else {
							_subQuestion.state = "inactive";
						}
					}
					if (document.getElementById(_subQuestion.node.labelElId)!=null) { //has the node been rendered?
						if (_subQuestion.state == "inactive")
							document.getElementById(_subQuestion.node.labelElId).style.color="#000000";
						if (_subQuestion.state == "active")
							document.getElementById(_subQuestion.node.labelElId).style.color="#448888";
						if (_subQuestion.state == "complete") 
							document.getElementById(_subQuestion.node.labelElId).style.color="#88AA88";
						if (_subQuestion.state == "void")
							document.getElementById(_subQuestion.node.labelElId).style.color="#DDDDDD";						
					}
				}
			}
		}
	}
	
	this.getNextInactive = function (subQuestion) {
		for (var i=0; i<this.questions.length; i++) {
			_question = this.questions[i];
			if (triggered.contains(_question.id)) {
				for (var j=0; j<_question.subQuestions.length;j++) {
					_subQuestion = _question.subQuestions[j];
					if (_subQuestion.state == "inactive") {
						return _subQuestion;
					}
				}
			}
		}
		return "Report";
	}

	this.setActive = function (subQuestion) {
		subQuestion.state = "active";
		this.refresh();
	}
	this.setInactive = function (subQuestion) {
		subQuestion.state = "inactive";
		this.refresh();
	}
	this.setComplete = function (subQuestion) {
		subQuestion.state = "complete";
		this.refresh();
	}
	this.setVoid = function (subQuestion) {
		subQuestion.state = "void";
		this.refresh();
	}
}

function TORSCStack () {
	this.values = new Array();
	this.push = function (value) {
		this.values.push(value);
	}
	this.remove = function (value) {
		for(i=0; i<this.values.length;i++){
    		if(value==this.values[i]) this.values.splice(i, 1);
  		}
	}
	this.contains = function (value) {
		for (var x=0;x<this.values.length;x++) {
			if (this.values[x]==value) return true;
		}
		return false;
	}
}
