AttachEvent(window, "load", selectall);
AttachEvent(window, "load", init);
AttachEvent(document, "keydown", keycheck);

var items = new Array();
var currentitem;

/*
AttachEvent(window, "load", initToggle);
function initToggle() 
{
	var elem = document.getElementsByTagName("li");
	elem[17].onclick = foldup2;
}

function foldup2(evt)
{
	
	evt = (evt) ? evt : ((window.event) ? event : null);
	switch (evt.type)
	{
		case "click":
			if (this.fold)
			{
				this.fold = false;
				this.style.borderWidth = "";
				this.style.borderStyle = "";
				this.style.borderColor = "";
			}
			else 
			{
				this.fold = true;
				this.style.borderWidth = "1px";
				this.style.borderStyle = "dotted";
				this.style.borderColor = "#6BBEEF";
			}
	}
}
*/
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 getStyle(el,styleProp)
{
	var x = el;
	if (window.getComputedStyle)
		var y = window.getComputedStyle(x,null).getPropertyValue(styleProp);
	else if (x.currentStyle)
		var y = eval('x.currentStyle.' + styleProp);
	return y;
}

function init()
{
	var tags = (document.all) ? document.all : document.getElementsByTagName("*");
	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, "...");
				//tags[i].onmouseover = hover;
				//tags[i].onmouseout = hover;
			}
			// foldup & hover
			else if (tags[i].className.indexOf("hide") > -1)
			{
				tags[i].onclick = foldup;
				// 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")
			{
				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 == "printpage")
			{
				tags[i].onclick = function() { window.print(); }
			}
			// expand all
			else if (tags[i].className == "expandall")
			{
				tags[i].onclick = expandall;
			}
			// image resize
			else if (tags[i].className == "image")
			{
				//image(tags[i]);
			}
		}
	}
	// display first item
	if (items.length > 0)
	{
		items[0].style.display = "block";
		currentitem = 0;
	}
	
	var Form = document.forms[0]["_ctl0:_ctl0"];
	if (Form)
	{
		Form.onclick = validate;
	}
	
	
}

function validate(evt) 
{
	selection = this.form["items"];
	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 showTip(oEvent) 
{
	var oDiv = document.getElementById("divTip1");
	oDiv.style.visibility = "visible";
	oDiv.style.left = oEvent.clientX + 5;
	oDiv.style.top = oEvent.clientY + 5;
}
function hideTip(oEvent) 
{
	var oDiv = document.getElementById("divTip1");
	oDiv.style.visibility = "hidden";
}



// debug util for releasing an items height-width
function image(img)
{	
	img.onclick = function()
	{
		if (this._width>0&&this._height>0)
		{
			this.setAttribute("width", this._width);
			this.setAttribute("height", this._height);
			this._width = 0;
			this._height = 0;
		}
		else
		{
			this._width = this.getAttribute("width");
			this._height =  this.getAttribute("height");
			
			this.setAttribute("width", this._width/72*96);
			this.setAttribute("height", this._height/72*96);
			//this.removeAttribute("width");
			//this.removeAttribute("height");
				
			//alert("Width: " + this.getAttribute("width") + " Height: " + this.getAttribute("height"));
		}
	}
}

function itemnavigate()
{
	if (this.className == "previous")
	{
		movePrev();
	}
	else if (this.className == "next")
	{
		moveNext();
	}
}

function movePrev()
{
	//alert("movePrev triggered");
	if (currentitem > 0)
	{
		items[currentitem].style.display = "";
		currentitem--;
		items[currentitem].style.display = "block";
	}
	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() 
{
	//alert("moveNext triggered");
	if (currentitem < items.length-1) 
	{
		items[currentitem].style.display = "";
		currentitem++;
		items[currentitem].style.display = "block";
	}
	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 hover(evt)
{
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (this.className.indexOf("hide") > -1) 
	{
		if (evt.type == "mouseout") 
		{
			this.className = this.className.replace("hover","");
		}
		else
		{
			this.className += "hover";
		}
	}
}
*/

function selectall() {
	var selectgroup = getElementsByClassName("selectgroup");
	for (i=selectgroup.length-1;i>=0;i--)
	{
		var input = selectgroup[i].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(e) 
{
	var key = null;
	if (document.all) {
		e = window.event;
		key = e.keyCode;
	} else {
		key = e.charCode | e.keyCode;
	}
	//alert(key);
	if (key == 37 || key == 80) // left
	{ 
		movePrev();
	}
	else if (key == 39 || key == 9 || key == 78) // right
	{ 
		moveNext();
	} 
	else if (key == 69) // expand all
	{
		expandall();
	}	
}

function expandall() 
{
	var elem = items[currentitem].getElementsByTagName("*");
	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 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>(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";
		}
	}
	
}

