// JavaScript Document
var category_who_slider = null;
var category_what_slider = null;
var rootDir = "";

//////////////////////////////
//		CATEGORY CONTROL	//
//////////////////////////////
function TableSlider(type) {
	this.categoryGroupArray = new Array();
	this.tableSliderId = "tbl_slider_" + type;
	this.maskId = "curr_faq_categories_" + type;
	this.left = -1.1;
	this.type = type;
	
	this.addCategoryGroup = function(categoryGroupObj, redraw) {
		if (redraw == null) redraw = true;
		this.categoryGroupArray.push(categoryGroupObj);
		if (redraw == true) {
			this.redrawTable();
			this.scrollTable(1);
		}	
	}
	
	this.removeCategories = function(steps) {
		var i;
		var tempAr = new Array();
		var backindex;
		
		//update the question count
		backindex = this.categoryGroupArray.length - 1 - steps;
		AJAX_updateCount(this.type, this.categoryGroupArray[backindex].getParentId());
		if (this.type == 'who') {
			last_who_id = this.categoryGroupArray[backindex].getParentId();
		} else if (this.type == 'what') {
			last_what_id = this.categoryGroupArray[backindex].getParentId();
		}
		
		for (i = 0; i < steps; i++) {
			tempAr.push(this.categoryGroupArray.pop());
		}
		
		this.scrollTable(-1*steps);
		
		return tempAr;
	}
	
	this.redrawTable = function(){ //also redraws bread crumbs
		var i;
		var cellCount = this.categoryGroupArray.length;
		var tableWidth = cellCount*30;
		//var backId;
		var str;
		var bcValue;
		
		//redraw breadcrumbs
		str = "<table class='faq_breadcrumb'><tr><td>";
		for (i = 0; i < this.categoryGroupArray.length; i++) {
			
			//get the breadcrumb value
			bcValue = this.categoryGroupArray[i].getTitle();
			if (i == 0) {
				bcValue = "Top";
			}
			
			if (i > 0) str += " > ";
			
			//add a link if this is not the last breadcrumb
			if (this.categoryGroupArray.length - 1 > i) {
				str += "<a href=\"javascript: choppath('" + this.type + "', " + (this.categoryGroupArray.length - i - 1) + ", " + this.categoryGroupArray[i].getParentId() + ");\">" + bcValue + "</a>";
			} else {
				str += bcValue;
			}	
			
		}
		str += "</td></tr></table>";
		document.getElementById("faq_breadcrumb_" + this.type).innerHTML = str;
		
		//create the headings
		str = "<table id='" + this.tableSliderId + "' class='slidertable' cellpadding='0' cellspacing='0' style='width: " + tableWidth + "em; left: " + this.left + "em;'><tr>";
		for (i = 0; i < this.categoryGroupArray.length; i++) {
			str += "<th><div>" + this.categoryGroupArray[i].getTitle() + "</div></th>";
		}
		str += "</tr>";

		//create the body
		str += "<tr>";
		for (i = 0; i < this.categoryGroupArray.length; i++) {
			str += "<td>";
			if (i > 0) {
				str += "<a class=\"backCategory\" href=\"javascript: choppath('" + this.type + "', 1);\">Back To: ";
				if (i == 1) {
					str += "Top</a>";	
				} else {
					str += this.categoryGroupArray[i-1].getTitle() + "</a>";
				}
			}
			str += this.categoryGroupArray[i].getContent() + "</td>";
		}
		str += "</tr></table>";
		
		document.getElementById(this.maskId).innerHTML = str;
	
		return str;
	}
	
	this.scrollTable = function(steps) {
		var i;
		var prevPos = this.left;
		var nextPos = (-30.1 * steps) + parseFloat(prevPos); //30 is the width of a table cell
		var increments = 20; //how many loops should be performed?
		var duration = 300; //how long in ms should this take
		var stepDuration = duration/increments;
		var step = (nextPos-prevPos)/increments;
		
		//set the iterations to cause animation
		if (steps != 0) {
			for (i = 0; i < increments; i++) {
				window.setTimeout("setScrollTable('" + this.tableSliderId + "', " + step*(i+1) + ", " + prevPos + ");",i*stepDuration); //usually 15 is the multiplier, not 100
			}
		}
		
		//set the new left
		this.left = nextPos;
		
		//if step < 0 then the table has already been clipped through the function "choppath" but it need to be redrawn to reflect that
		if (steps < 0) {
			window.setTimeout("category_" + this.type + ".redrawTable();", duration);
		}
	}
	
	this.getCurrentId = function() {
		var arrLen = this.categoryGroupArray.length;
		var val = this.categoryGroupArray[arrLen - 1].getParentId();
		return val;	
	}
}

function CategorySliderController(treeRoot) {
	this.root = treeRoot;
	this.rootId = treeRoot.getId();
	this.type = treeRoot.getType();
	this.maskId = treeRoot.getType() + "_nav_container_slider";
	this.columns = new Array();
	this.nodeArray = null;
	this.currNode = null;
	this.breadcrumbOffset = 0;
	this.flashNavBack = true;
	
	this.addColumn = function(id) {
		var node = this.findNode(id);
		var count = -1;
		this.currNode = node;
		this.columns.push(this.createColumn(node));
		
		if (id != this.rootId) {
			count = AJAX_updateCount();
			Effect.Fade(this.type + "_nav_container_body", {duration: .12});
			window.setTimeout("category_" + this.type + "_slider.updateColumn();", 120);
			window.setTimeout("Effect.Appear(\"" + this.type + "_nav_container_body\", {duration: .12});", 130);
		} else {
			this.updateColumn();
			document.getElementById(this.type + "_nav_container_body").style.display = "block";
		}
		this.updateBack(true);
		this.updateTop();
	}
	
	this.stepback = function(steps) {
		var i;
		var node = this.currNode;
		
		for (i = 0; i < steps; i++) {
			if (node.getParent() != null) {
				node = node.getParent();
				this.columns.pop();
			}
		}
		this.currNode = node;
		
		Effect.Fade(this.type + "_nav_container_body", {duration: .12});
		window.setTimeout("category_" + this.type + "_slider.updateColumn();", 120);
		window.setTimeout("Effect.Appear(\"" + this.type + "_nav_container_body\", {duration: .12});", 130);
		this.updateBack(false);
		this.updateTop();
		AJAX_updateCount();
	}
	
	this.removeColumns = function(columnToRemove) {
		Effect.Fade(this.type + "_nav_container_body", {duration: 5});
	}
	
	this.createColumn = function(parentNode) {
		var node;
		var str = "";
		str += "<div class=\"nav_container_column\" id=\"" + this.type + "_nav_container_column\" style=\"width: 100%;\">";
		str += "<div class=\"nav_container_title\" id=\"" + this.type + "_nav_container_title_" + parentNode.getId() + "\">" + parentNode.getTitle() + "</div>";
		str += "<div class=\"nav_container_body\" id=\"" + this.type + "_nav_container_body\" style='display: none;'>";
		for (i = 0; i < parentNode.getChildCount(); i++) {
			node = parentNode.getChild(i);
			str += "<a href=\"javascript: selectCategory('" + this.type + "', " + node.getId() + ");\">" + node.getTitle() + "</a>";
		}
		if (parentNode.getChildCount() == 0) {
			str += "<div class='end_of_the_line' style='margin-top: 2em;'>";
			if (this.type == "who") str += "<p>Get Information For:</p>";
			if (this.type == "what") str += "<p>Get Information About:</p>";
			str += "<p><b>" + parentNode.getLongTitle() + "</b></p><div>";
		}
		str += "</div>";
		str += "</div>";
		
		return str;
	}
	
	this.updateColumn = function() {
		document.getElementById(this.maskId).innerHTML = this.columns[this.columns.length-1];
	}
	
	this.updateBack = function(animation) {
		var i;
		
		if (this.columns.length > 1) {
			
			
			if (animation == true) {
				for (i = 0; i <= 13; i++) {
						window.setTimeout("document.getElementById(\"" + this.type + "_category_back_nav\").style.textIndent = \"" + (13 - i) + "em\";", i*10);
				}
				/*if (this.flashNavBack == true) {
					window.setTimeout("category_" + this.type + "_slider.blinkBack(6);", 250);
					this.flashNavBack = false;
				}*/
			}
			
			document.getElementById(this.type + "_category_back_nav").innerHTML = "<a href='javascript: category_" + this.type + "_slider.stepback(1);'><img src='" + rootDir + "images/go_back.gif' class='go_back_arrow' alt='Go Back'></a>";
		} else {
			document.getElementById(this.type + "_category_back_nav").innerHTML = "Narrow Search (Above)";
		}
	}
	
	/*this.blinkBack = function(loops) {
		var i;
		var originalHTML = "<a href=\"javascript: category_" + this.type + "_slider.stepback(1);\">< Go Back</a>";
		for (i = 0; i < loops; i++) {
			window.setTimeout("document.getElementById(\"" + this.type + "_category_back_nav\").innerHTML = '" + originalHTML + "&nbsp;&nbsp;<&nbsp;&nbsp;<&nbsp;&nbsp;<&nbsp;&nbsp;';", 300*i);
			window.setTimeout("document.getElementById(\"" + this.type + "_category_back_nav\").innerHTML = '" + originalHTML + "&nbsp;<&nbsp;&nbsp;<&nbsp;&nbsp;<&nbsp;&nbsp;';", 100 + 300*i);
			window.setTimeout("document.getElementById(\"" + this.type + "_category_back_nav\").innerHTML = '" + originalHTML + "&nbsp;&nbsp;&nbsp;<&nbsp;&nbsp;<&nbsp;&nbsp;';", 200 + 300*i);
		}
		window.setTimeout("document.getElementById(\"" + this.type + "_category_back_nav\").innerHTML = '" + originalHTML + "';", loops*300);
	}*/
	
	this.updateTop = function() {
		var node = this.currNode;
		var str = "";
		var ar = new Array();
		var i;
		var fakeNav = "";
		
		while (node != null) {
			ar.push(node.getTitle());
			node = node.getParent();
		}
		ar = ar.reverse();
		
		ar[0] = "Top";
		for (i = 0; i < ar.length; i++) {
			if (i < ar.length - 1) str += "<a href='javascript: category_" + this.type + "_slider.stepback(" + (ar.length - i - 1) + ");'>";
			if (i == ar.length - 1) str += "<span style='font-weight: bold;'>";
			str += ar[i];
			if (i == ar.length - 1) str += "</span>";
			if (i < ar.length - 1) str += "</a> > ";
		}
		
		//store the new value with a white character at the end (white char helps determine if scroll is needed)
		str = "<div id='" + this.type + "_breadcrumb_slider'>" + str + "</div>";
		document.getElementById(this.type + "_breadcrumb_container").innerHTML = str;
		
	}
	
	this.createNodeArray = function(node) {
		var ar = node.getChildren();
		var i;
		
		for (i = 0; i < ar.length; i++) {
			ar = ar.concat(this.createNodeArray(ar[i]));	
		}
		
		return ar;
	}
		
	this.findNode = function(id) {
		var node = null;
		
		if (this.nodeArray == null) this.nodeArray = this.createNodeArray(this.root);
		
		if (id == this.rootId) {
			node = this.root;	
		} else {
			for (i = 0; i < this.nodeArray.length; i++) {
				if (this.nodeArray[i].getId() == id) {
					node = this.nodeArray[i];
				}
			}
		}
		return node;
	}
	
	this.getCurrentNode = function() {
		return this.currNode;	
	}
	
	this.getRootId = function() {
		return this.rootId;	
	}

}

function CategoryTreeNode(type, id, title, longTitle, parent) {
	this.type = type;
	this.id = id;
	this.title = title;
	this.longTitle = longTitle;
	this.parent = parent;
	this.children = new Array();
	
	this.getType = function() {
		return this.type;	
	}
	
	this.getId = function() {
		return this.id;
	}
	
	this.getTitle = function() {
		return this.title;
	}
	
	this.getLongTitle = function() {
		return this.longTitle;
	}
	
	this.getParent = function() {
		return this.parent;
	}
	
	this.addChild = function(catTreeNode) {
		this.children.push(catTreeNode);	
	}
	
	this.getChild = function(index) {
		return this.children[index];
	}
	
	this.getChildren = function() {
		return this.children;
	}
	
	this.getChildCount = function() {
		return this.children.length;	
	}
}

function createCategoryTree(parentNode, dataStr) {
	var node;
	var lines = dataStr.split("<entry>");
	var ar;
	var i;
	var _type = 0;
	var _id = 1;
	var _parentId = 2;
	var _title = 3;
	var _rootid = 4; 
	var _longTitle = 5;
	
	for (i = 0; i < lines.length; i++) {
		ar = lines[i].split("<|>");
		if (parseInt(ar[_parentId]) == parentNode.getId()) {
			node = new CategoryTreeNode(parentNode.getType(), ar[_id], ar[_title], ar[_longTitle], parentNode);
			createCategoryTree(node, dataStr);
			parentNode.addChild(node);
		}
	}
	
	return parentNode;
}

function selectCategory(type, id) {
	if (type == 'who') {
		category_who_slider.addColumn(id);
		document.fastanswerform.fastanswerwho.value = id;
	}
	if (type == 'what') {
		category_what_slider.addColumn(id);
		document.fastanswerform.fastanswerwhat.value = id;
	}
}

function flashBorder(id) {
	if (id == 'category_results_info') {
		document.getElementById("category_results_info").style.border = ".4em solid #8495A9";
		document.getElementById("category_results_info").style.marginLeft = "0px";
		document.getElementById("category_results_info").style.marginRight = "0px";
		document.getElementById("category_results_info").style.marginTop = "1.7em";
		
		window.setTimeout('document.getElementById("category_results_info").style.border = ".1em solid #33496D"; document.getElementById("category_results_info").style.marginLeft = ".3em"; document.getElementById("category_results_info").style.marginRight = ".3em"; document.getElementById("category_results_info").style.marginTop = "2em";', 200);
		
		//border: .1em solid #33496D; margin-left: .3em; margin-right: .3em; margin-top: 2em;
	} else {
		document.getElementById(id).style.borderColor = '#8495A9';
		window.setTimeout("document.getElementById('" + id + "').style.borderColor = '#FFFFFF';", 200);
		//window.setTimeout("document.getElementById('" + id + "').style.borderColor = '#8495A9';", 400);
//		window.setTimeout("document.getElementById('" + id + "').style.borderColor = '#FFFFFF';", 600);
	}
}

function AJAX_loadQuestions() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)	{
			try	{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e)	{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	document.getElementById('fastanswersloading').style.display = 'block';
	document.getElementById('category_results_info').innerHTML = 
				"<p style='font-size: 150%; margin-top: 4.5em;'>Getting Results. Please Wait...</p>";

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var response = xmlHttp.responseText;
			document.getElementById('fastanswersloading').style.display = 'none';
			document.getElementById('youasked').innerHTML = "";
			document.getElementById('fastanswer_result_answers').style.marginTop = "1.5em";
			document.getElementById('fastanswer_result_answers').innerHTML = response;
			document.getElementById('category_results_info').innerHTML = 
				"<p><b>Your results have loaded.</b><br /><br />To get different results, use the boxes to the left to change your search.</p>" +
				"<a href=\"javascript: alert('Your results have loaded. See below.');\" class='disabled' style='background-color: #D3D3D3; margin-top: 2.3em;'>Get Results</a>";
			resultsLoaded = true; //this variable comes from fastanswers.js
			document.fastanswerform.fastanswerquestionboxhidden.value = "";
		}
	}
	
	var whoid = category_who_slider.getCurrentNode().getId();
	var whatid = category_what_slider.getCurrentNode().getId();

	xmlHttp.open("GET", rootDir + "fastanswer_components/categories.php?whoid=" + whoid + "&whatid=" + whatid + "&mode=load",true);
	xmlHttp.send(null);
}

function AJAX_continueLoad(FAQString) {
	var xmlHttp;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
		
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var response = xmlHttp.responseText;
			document.getElementById("getMoreResultsDiv").innerHTML = "";
			document.getElementById("getMoreResultsDiv").style.display = "none";
			document.getElementById("fastanswer_result_answers").innerHTML += response;
		}
	}
	
	//alert("whoid=" + document.fastanswerform.fastanswerwho.value + "\nwhatid=" + document.fastanswerform.fastanswerwhat.value);
	
	params = "FAQString=" + FAQString;
	
	xmlHttp.open("POST", rootDir + "fastanswer_components/categories.php?mode=loadRemainingFAQ",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function AJAX_updateCount() {
	var xmlHttp;
	var whoid = category_who_slider.getCurrentNode().getId();
	var whatid = category_what_slider.getCurrentNode().getId();
	
	if ((whoid == category_who_slider.getRootId()) && (whatid == category_what_slider.getRootId())) {
		document.getElementById('category_results_info').innerHTML = "<p style=\"margin-top: 4em;\">Please use the navigation to the left to narrow down your results</p>";
	} else {
		
		try {
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		} catch (e) {
			// Internet Explorer
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e)	{
				try	{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e)	{
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
		document.getElementById('category_results_info').innerHTML = "<p>Please use the navigation to the left to narrow down your results</p><p class='processing'>Processing...</p>";
	
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				var response = xmlHttp.responseText;
				if (response > 0) {
					if (document.fastanswerform.fastanswertype.value == "category") {
						document.getElementById('category_results_info').innerHTML = "<p style='font-size: 150%;'>Currently Existing FAQ Results</p><p class='FAQCount'>" + response + "</p><a href='javascript: AJAX_loadQuestions();'>Get Results</a>";
					} else if (document.fastanswerform.fastanswertype.value = "advanced") {
						document.getElementById('category_results_info').innerHTML = "<p style='font-size: 150%;'>Currently Existing FAQ Results</p><p class='FAQCount'>" + response + "</p><a href='javascript:askQuestionBoxFocus();'><div style='font-size: 70%; font-weight: normal;'>Type a Question to Get Results</div></a>";
					}
				} else {
					document.getElementById('category_results_info').innerHTML = "<p style='font-size: 150%;'>Currently Existing FAQ Results</p><p class='FAQCount'>" + response + "</p><a href=\"javascript: alert('There are no results for your specified criteria.');\" class='disabled'>Get Results</a>";
				}
			}
		}
	
		xmlHttp.open("GET", rootDir + "fastanswer_components/categories.php?whoid=" + whoid + "&whatid=" + whatid + "&mode=count",true);
		xmlHttp.send(null);
	}
}

/*function AJAX_updateCount(type, id) {
	if (last_who_id == null) last_who_id = 0;
	if (last_what_id == null) last_what_id = 0;
	
	if ((whoLvl == 0) && (whatLvl == 0)) {
		document.getElementById('curr_faq_results_message').innerHTML = 
			"<p>Please use the navigation to the left to narrow down your results</p>" +
			"<a href=\"javascript: alert('Please narrow down your results before clicking \'Get Results\'.');\" class=\"disabled\">Get Results</a>";
		document.getElementById('curr_faq_results_message_adv').innerHTML = "<p><br /><br /><br /><br /><br />Please use the navigation to the left to narrow down your results</p>";
	} else {
		var xmlHttp;
		try {
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		} catch (e) {
			// Internet Explorer
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e)	{
				try	{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e)	{
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
		
		document.getElementById('curr_faq_results_message').innerHTML = "<p style='font-size: 200%;'>Processing...</p>";
		document.getElementById('curr_faq_results_message_adv').innerHTML = "<p style='font-size: 200%;'>Processing...</p>";
	
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				var response = xmlHttp.responseText;
				return response;
				if (response != "Load questions") {
					if (response == 0) {
						document.getElementById('curr_faq_results_message').innerHTML = 
							"<p><span style='font-size: 140%; margin-bottom: 0px;'>Currently existing FAQ Results</span><br><b class='resultno'>" + response + "</b></p>" + 
							"<a href=\"javascript: alert('There are no results. Please backup your search using the boxes to the left.');\" class='disabled'>Get Results</a>";
						document.getElementById('curr_faq_results_message_adv').innerHTML = 
							"<p style='font-size: 140%; margin-bottom: 0px;'>Questions in Search Pool</p><p class='resultno'>" + response + "</p>";
					} else {
						if (response > 50) {
							document.getElementById('curr_faq_results_message').innerHTML = 
								"<p><span style='font-size: 140%; margin-bottom: 0px;'>Currently existing FAQ Results</span><br><b class='resultno'>" + response + "</b></p>" + 
								"<a href=\"javascript: alert('Please narrow down your results before clicking \'Get Results\'.');\" class='disabled'>Get Results</a>";
						} else {
							document.getElementById('curr_faq_results_message').innerHTML = 
								"<p><span style='font-size: 140%; margin-bottom: 0px;'>Currently existing FAQ Results</span><br><b class='resultno'>" + response + "</b></p>" + 
								"<a href=\"javascript: AJAX_loadQuestions();\" class='enabled'>Get Results</a>";
						}
						document.getElementById('curr_faq_results_message_adv').innerHTML = 
							"<p><span style='font-size: 140%; margin-bottom: 0px;'>Questions in Search Pool</span><br /><b class='resultno'>" + response + "</b></p>";
					}
					
				} else {
					AJAX_loadQuestions();
				}
			}
		}
		
		var whoid = category_who.getCurrentId();
		var whatid = category_what.getCurrentId();
		if (type == 'who') {
			whoid = id;
		} else if (type == 'what') {
			whatid = id;
		}
	
		xmlHttp.open("GET","fastanswer_components/categories.php?whoid=" + whoid + "&whatid=" + whatid + "&mode=count",true);
		xmlHttp.send(null);
		
	}

}*/
