var xml_quotes;
var xml_categories;
var docElement;
var urlArray = new Array("quotes.xml", "categories.xml");

window.onload = function loadXMLDoc()
{
	dw_Rotator.setup(rotator1);
	xml_quotes = null;
	xml_categories = null;
	if (window.XMLHttpRequest) {
		// code for Firefox, Opera, IE7, etc.
		xml_quotes = new XMLHttpRequest();
		xml_categories = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		// code for IE6, IE5
		xml_quotes = new ActiveXObject("Microsoft.XMLHTTP");
		xml_categories = new XMLHttpRequest();
	}
	
	if (xml_quotes != null) {
		xml_quotes.onreadystatechange = state_Change;
		xml_quotes.open("GET",urlArray[0],true);
		xml_quotes.send(null);
	} else {
		alert("Your browser does not support XMLHTTP.");
	}
	
	if (xml_categories != null) {
		xml_categories.onreadystatechange = state_Change;
		xml_categories.open("GET",urlArray[1],true);
		xml_categories.send(null);
	} else {
		alert("Your browser does not support XMLHTTP.");
	}
}

function refocus(element) {
	box1.style.opacity = 10/10;
	box2.style.opacity = 10/10;
	box3.style.opacity = 10/10;
}


function state_Change() {
	if(xml_quotes.readyState == 4) {
		docElement = xml_quotes.responseXML.documentElement;
		update();
	}
	if(xml_categories.readyState == 4) {
		var tableHTML = "<table id='categoryTable' class='2'><tr>";
		var doc = xml_categories.responseXML.documentElement;
		var categoryArray = doc.getElementsByTagName("category");
		for(var i = 0; i < categoryArray.length; i++) {
			tableHTML += "<td class='categoryCell' id='category" + i + "' " + 
				"onmouseover='hover(this)'><span class='categoryTitle'>" + 
				categoryArray[i].getElementsByTagName("title")[0].childNodes[0].nodeValue + 
				"</span><br /><span class='categoryShort'>" + 
				categoryArray[i].getElementsByTagName("shortDesc")[0].childNodes[0].nodeValue + 
				"</span></td>";
		}
		tableHTML += "</tr></table>";
		document.getElementById("lowerContent").innerHTML = tableHTML;
	}
}

function hover(element) {
	var cells = document.getElementById("categoryTable").childNodes[0].childNodes[0].childNodes;
	var categoryNumber = 0;

	// determine the category number of the element that has hovered on
	for(var i = 0; i < cells.length; i++) {
		if(cells[i].id == element.id) {
			categoryNumber = i;
			break;
		}
	}
	// set the longDesc to the categoryOptions div
	var category = xml_categories.responseXML.documentElement.getElementsByTagName("category")[categoryNumber];
	document.getElementById("categoryTitle").textContent = category.getElementsByTagName("title")[0].childNodes[0].nodeValue;
	document.getElementById("longDesc").innerHTML = category.getElementsByTagName("longDesc")[0].getElementsByTagName("text")[0].childNodes[0].nodeValue;
	var linkArray = category.getElementsByTagName("link");
	var txt = "";
	for(var i = 0; i < linkArray.length; i++) {
		txt += "<a href='" + linkArray[i].getElementsByTagName("href")[0].childNodes[0].nodeValue + "'>" + linkArray[i].getElementsByTagName("title")[0].childNodes[0].nodeValue + "</a>";
		if(i < linkArray.length - 1) {
			txt += " | ";
		}
	}
	document.getElementById("spanLinks").innerHTML = txt;
	document.getElementById("spanLabel").innerHTML = category.getElementsByTagName("links_name")[0].childNodes[0].nodeValue;

	// ugly work-around for putting links into the long description (specific for category 1)
	if(categoryNumber == 1) {
		document.getElementById("descLinks").innerHTML = "<a href='http://episurveyor.org/m/'>Phone</a> | <a href='http://episurveyor.org/m/jar.html'>Computer</a>";
	} else {
		document.getElementById("descLinks").innerHTML = "";
	}
}

function update() {
	manageOpacity(0, true);
}

function manageOpacity(opacity, isAscending) {
	if(opacity == 0) {
		isAscending = true;
		var quoteArray = docElement.getElementsByTagName("quote");
		var randInt = Math.floor(Math.random() * quoteArray.length);
		document.getElementById('quoteDescription').textContent = 
			quoteArray[randInt].getElementsByTagName("description")[0].childNodes[0].nodeValue;
		document.getElementById('quoteAuthor').textContent = 
			"- " + quoteArray[randInt].getElementsByTagName("author")[0].childNodes[0].nodeValue;
	}
	if(opacity == 200) {
		isAscending = false;
	}
	setOpacity(document.getElementById('quoteBox'), opacity);
	opacity = isAscending ? opacity + 1 : opacity - 1;
	setTimeout("manageOpacity(" + opacity + ", " + isAscending + ")", 10);
}

function setOpacity (myElement, opacityValue) {  
	if (window.ActiveXObject) {
		myElement.style.filter = "alpha(opacity=" + opacityValue*100 + ")"; // IE  
	} else {
		myElement.style.opacity = opacityValue / 100; // Gecko/Opera  
	}  
}

