var questions = new Array();
var items = new Array();
var currentitem;

function question(obj)
{
	this.obj = obj;
	this.foldups = new Array();
}

function scoreURL(key)
{
	document.forms[0].action = "Questions.aspx?appName=scorerTraining&i=" + key;
	document.forms[0].submit();		
}

function pageInit()
{
	AttachEvent(document, "keydown", keycheck);

	// Deprecated
	//var tags = (document.all) ? document.all : document.getElementsByTagName("*");
	
	var hideTags = "optionhide|studentresponsehide|explanationhide|scoringhide|"
	var searchTags = "magnify|passagehide|hide|cschide|sbihide|statshide|item|next|previous|printAll|printpage|expandall|collapseall|selectgroup|viewall|viewselected|loading"
	var itemContainer = document.getElementById("itemrelease")
	var tags = getElementsByClassName(hideTags + searchTags, "*", itemContainer);
	
	assign(tags);
	
	// hides loader 
	var loader = document.getElementById("loading");
	if (loader)
	{
		loader.style.display = "none";
	}

	// display first item
	if (items.length > 0)
	{
		//items[0].style.display = "block";
		items[0].className = "itemShow";
		
		currentitem = 0;
	}
}

function viewall()
{
	var msg = "Depending on your Internet connection, loading all questions may take several minutes. If you want to continue, please click \"Ok\"; if not, please click \"Cancel\". Thank you."
	if (!confirm(msg)) return false;

	var questions = document.forms[0].i;
	for (i=0;i<questions.length;i++)
	{
		questions[i].checked = true;
	}
}

function assign(tags) 
{
	var length = tags.length;
	for (i=0;i<length;i++)
	{
		if (tags[i] != null )
		{			
			// magnify
			if (tags[i].className == "magnify")
			{
				tags[i].onclick = magnify;
			}
			// passage
			else if (tags[i].className == "passagehide")
			{
				charTrunc(tags[i], 2, "...");
				questions[questions.length-1].foldups.push(tags[i]);
			}
			// foldup & hover
			else if (tags[i].className.indexOf("hide") > -1)
			{
				tags[i].onclick = foldup;
				questions[questions.length-1].foldups.push(tags[i]);

				// hover ignores question boxes
				if (tags[i].className != "cschide" && tags[i].className != "sbihide" && tags[i].className != "statshide")
				{
					tags[i].onmouseover = hover;
					tags[i].onmouseout = hover;
				}
			}								
			// item array 
			else if (tags[i].className == "item")
			{
				var q = new question(tags[i]);
				questions.push(q);
				items.push(tags[i]);
			}
			// move next
			else if (tags[i].className == "next")
			{
				tags[i].onclick = itemnavigate;
			}
			// move previous
			else if (tags[i].className == "previous")
			{
				tags[i].onclick = itemnavigate;
			}
			// print page
//			else if (tags[i].className == "printAll")
//			{
//				tags[i].onclick = function() { window.print(); }
//			}
			// print specific item 
			else if (tags[i].className == "printpage")
			{
				tags[i].onclick = function() { window.print(); }
			}
			// score item 
			else if (tags[i].className == "score")
			{
				tags[i].onclick = function() { 
					alert('TODO: I am not sure how to pass an item id into scorer training without losing my selected items.'); 
				}
			}
			// expand all
			else if (tags[i].className == "expandall")
			{
				tags[i].onclick = expandall;
			}
			// collapse all
			else if (tags[i].className == "collapseall")
			{
				tags[i].onclick = collapseall;
			}
			else if (tags[i].className == "selectgroup")
			{
				selectgroup(tags[i]);
			}
			else if (tags[i].className == "viewall")
			{
				tags[i].onclick = viewall;
			}
			else if (tags[i].className == "viewselected")
			{
				tags[i].onclick = viewselected;
			}
		}
	}
}

function hover(evt)
{
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (this.className.indexOf("hide") > -1) 
	{
		if (evt.type == "mouseover") 
		{
			this.style.borderWidth = "1px";
			this.style.borderStyle = "dashed";
			this.style.borderColor = "#6BBEEF";
			this.style.backgroundColor = "#F7F7F7";
		}
		else if (evt.type == "mouseout")
		{
			this.style.border = "";
			this.style.borderWidth = "";
			this.style.borderStyle = "";
			this.style.borderColor = "";
			this.style.backgroundColor = "";
		}
	}
}

function viewselected(evt) 
{
	selection = this.form["i"];
	for (i=selection.length-1;i>=0;i--)
	{
		if (selection[i].checked) return true;
	}
	alert ("You must make at least one selection");
	return false;
}

function foldup(evt)
{
	this.style.borderWidth = "";
	this.style.borderStyle = "";
	this.style.borderColor = "";
	this.style.backgroundColor = "";
	if (this.className.indexOf("hide") > -1) 
	{
		this.className = this.className.replace("hide","");
	}
	else
	{
		this.className += "hide";
	}
}

function itemnavigate()
{
	if (this.className == "previous")
	{
		movePrev();
	}
	else if (this.className == "next")
	{
		moveNext();
	}
}

function printItem()
{
	alert("print item");
}

/**
 * This is used to go to a specific item by it's order position.
 * @param (Number) position
 */
function moveSpecific(position)
{
		//items[currentitem].style.display = "";
		items[currentitem].className = "item";
		currentitem = position - 1;
		//items[currentitem].style.display = "block";
		items[currentitem].className = "itemShow";
}

function movePrev()
{
	if (currentitem > 0)
	{
		//items[currentitem].style.display = "";
		items[currentitem].className = "item";
		currentitem--;
		//items[currentitem].style.display = "block";
		items[currentitem].className = "itemShow";
	}
	else
	{
		alert("You've reached the start of your selection. You can make a new selection or go forward with your current one.");
	}
}

function moveNext()
{
	if (currentitem < items.length-1) 
	{
		//items[currentitem].style.display = "";
		items[currentitem].className = "item";
		currentitem++;
		//items[currentitem].style.display = "block";
		items[currentitem].className = "itemShow";
	}
	else
	{
		alert("You've reached the end of your selection. You can make a new selection or go back to your current one.");
	}
}

function magnify()
{
	var doc = document.getElementsByTagName("body");
	if (doc[0].style.fontSize == "1em")
	{
		doc[0].style.fontSize = ".75em";
		this.childNodes[0].className = "increase";
		this.childNodes[0].childNodes[0].nodeValue = "Magnify"
	}
	else
	{
		doc[0].style.fontSize = "1em";
		this.childNodes[0].className = "reduce";
		this.childNodes[0].childNodes[0].nodeValue = "Reduce";
	}
}

function selectgroup(elem) {
	var input = elem.getElementsByTagName("input");
	var items = new Array();
	if (input) 
	{
		for (j=input.length-1;j>=0;j--)
		{
			switch (input[j].type) 
			{
				case "checkbox":
					items.push(input[j]);
					break;
				case "button":						
					input[j].items = items;
					input[j].onclick = function() 						
					{
						for (k=this.items.length-1;k>=0;k--)
						{
							this.items[k].checked = true;
						}
					}
					break;
			}
		}
	}
}

function keycheck(evt) 
{
	evt = (evt) ? evt : ((window.event) ? event : null);
	var key = (evt.keyCode) ? evt.keyCode : evt.charCode;
	
	if (key == 37 || key == 80) // left
	{ 
		movePrev();
	}
	else if (key == 39 || key == 9 || key == 78) // right
	{ 
		moveNext();
	} 
	else if (key == 69) // e
	{
		expandall();
	}
	else if (key == 83) // s
	{
		saveas();
	}
}

function saveas() 
{
	//document.execCommand('SaveAs', false, 'anotherfilename.mht');
	//document.location.href="savehtml.aspx";
	var link = items[currentitem].getElementsByTagName("a");
	
	a = document.body.createTextRange()
	a.moveToElementText(link[3])
	a.execCommand("Copy")
}

function expandall() 
{
	var elem = questions[currentitem].foldups;
	for (i=elem.length-1;i>=0;i--)
	{
		if (elem[i].className == "passagehide")
		{
			//alert(elem[i].onclick);
			elem[i].onclick();
		}
		elem[i].className = elem[i].className.replace(/hide/g, "");
	}
}

function collapseall() 
{
	var elem = questions[currentitem].foldups;
	for (i=elem.length-1;i>=0;i--)
	{
		if (elem[i].className == "passage")
		{
			elem[i].onclick();
		}
		if (elem[i].className.indexOf("hide") == -1) 
		{
			elem[i].className += "hide";
		}
	}
}


function charTrunc(passage, num, endstring)
{
	var parent = passage;
	passage = passage.childNodes[2];
	var html = passage.innerHTML.toLowerCase();
	passage.oldHTML = passage.innerHTML;
	var idx = 0;
	
	/*
	TODO:
	Get all paragraphs into a collection, for each one that has text
	insert into the current passage (ignore images to).
	*/	
	
	/* count how many paragraphs that have text */
	p = passage.getElementsByTagName("p");
	var passcnt = 0;
	var textreader = (document.getElementsByTagName("body")[0].innerText);
	for (j=0;j<p.length;j++)
	{
		//str = p[j].innerText.replace(/\s/g,"");
		str = (textreader) ? p[j].innerText.replace(/\s/g,"") : p[j].textContent.replace(/\s/g,"");
		if (str.length>0)
		{
			passcnt++;
			if (passcnt==num)
			{
				num = j+1;
				break;
			}
		}
	}
	
	/* gets innerHTML string index of last paragraph with text */
	while (num-- > 0)
	{
		idx = html.indexOf("</p>", idx) + 4;
	}
	
	/* if index found then replace the current innerHTML with the shortened version */
	if (idx > 0)
	{
		passage.innerHTML = passage.innerHTML.substring(0,idx - 4) + "</p><p>&nbsp</p><p>(Click to read...)</p>";
	}
	else
	{
		passage.innerHTML = "";
	}

	/* This sets onclick for adding/removing the paragraph */
	parent.onclick = function() 
	{
		tmp = this.childNodes[2].innerHTML;
		this.childNodes[2].innerHTML = this.childNodes[2].oldHTML;
		this.childNodes[2].oldHTML = tmp;

		this.style.borderWidth = "";
		this.style.borderStyle = "";
		this.style.borderColor = "";
		this.style.backgroundColor = "";
		if (this.className.indexOf("hide") > -1) 
		{
			this.className = this.className.replace("hide","");
		}
		else 
		{
			this.className += "hide";
		}
	}
	
}
